public string RenderPartial(EventSourcePrototype eventSource, int index, string fileName) { var output = Template.Template_LOGGER_PARTIAL_CLASS_DELCARATION; output = output.Replace(Template.Variable_EVENTSOURCE_CLASS_NAME, eventSource.ClassName); output = output.Replace(Template.Variable_NAMESPACE_DECLARATION, eventSource.Namespace); output = output.Replace(Template.Variable_EVENTSOURCE_PARTIAL_FILE_NAME, fileName); output = output.Replace(Template.Variable_LOGGER_SOURCE_FILE_NAME, this.Name); var logger = new StringBuilder(); var next = index; var renderer = new EventRenderer(); foreach (var loggerEvent in this.Events) { if (ImplicitArguments != null && ImplicitArguments.Length > 0) { loggerEvent.InsertImplicitArguments(ImplicitArguments); } if (OverrideArguments != null && OverrideArguments.Length > 0) { loggerEvent.OverrideArguments(OverrideArguments); } loggerEvent.Keywords = new string[] { this.GetKeyword() }; logger.AppendLine(renderer.Render(loggerEvent, next, eventSource)); next += 1; } output = output.Replace(Template.Variable_LOGGER_EVENTS_DECLARATION, logger.ToString()); return(output); }
public IEnumerable <ProjectItem> Render(string projectBasePath) { var outputs = new List <ProjectItem>(); var output = Template_EVENTSOURCE_CLASS_DECLARATION; output = output.Replace(Variable_SOURCE_FILE_NAME, this.SourceFilePath); output = output.Replace(Variable_EVENTSOURCE_NAME, this.Name); output = output.Replace(Variable_EVENTSOURCE_CLASS_NAME, ClassName); output = output.Replace(Variable_NAMESPACE_DECLARATION, Namespace); var next = 1; var loggerStartId = 10000; var allKeywords = new List <string>(); if (this.Keywords != null) { allKeywords.AddRange(this.Keywords); } foreach (var logger in this.Loggers) { var loggerTemplate = this.AvailableLoggers.FirstOrDefault(l => l.Name.Equals(logger.Name, StringComparison.InvariantCultureIgnoreCase)); if (loggerTemplate == null) { throw new NotSupportedException($"Logger {logger.Name} was not found in the project. Declare an interface with a matching name in a file with the same name as the interface."); } logger.AddTemplate(loggerTemplate); next = logger.StartId ?? loggerStartId; var eventSourcePartialFileInclude = this.Include.Replace(this.ClassName, $"{ClassName}.{logger.Name}"); var eventSourcePartialFileName = System.IO.Path.Combine(projectBasePath, eventSourcePartialFileInclude); var loggerFileInclude = this.Include.Replace(this.ClassName, logger.Name.Substring(1)); var loggerFileName = System.IO.Path.Combine(projectBasePath, loggerFileInclude); var loggerKeyword = logger.GetKeyword(); logger.SourceFileName = logger.Name.Substring(1); if (!allKeywords.Contains(loggerKeyword)) { allKeywords.Add(loggerKeyword); } outputs.Add(new ProjectItem(ProjectItemType.EventSourceLoggerPartial, eventSourcePartialFileName, logger.RenderPartial(this, next, System.IO.Path.GetFileName(eventSourcePartialFileName))) { Include = eventSourcePartialFileInclude }); outputs.Add(new ProjectItem(ProjectItemType.LoggerImplementation, loggerFileName, logger.RenderImplementation(this, next)) { Include = loggerFileInclude }); loggerStartId += 1000; } next = 1; var events = new StringBuilder(); var renderer = new EventRenderer(); foreach (var eventSourceEvent in this?.Events ?? new EventModel[0]) { if (eventSourceEvent.Id != null) { next = eventSourceEvent.Id.Value; } events.AppendLine(renderer.Render(eventSourceEvent, next, this)); next += 1; } output = output.Replace(Variable_EVENTS_DECLARATION, events.ToString()); next = 1; var keywords = new StringBuilder(); foreach (var keyword in allKeywords.Select(k => new EventSourceKeyword() { Name = k })) { keywords.AppendLine(keyword.Render(next)); next *= 2; } output = output.Replace(Variable_KEYWORDS_DECLARATION, keywords.ToString()); if (this.Extensions != null && this.Extensions.Any()) { var extensions = new StringBuilder(); foreach (var extension in this.Extensions) { extensions.AppendLine(extension.Render()); } var extensionMethod = Template_EXTENSIONS_DECLARATION; extensionMethod = extensionMethod.Replace(Variable_EVENTSOURCE_CLASS_NAME, this.ClassName); extensionMethod = extensionMethod.Replace(Variable_EXTENSION_METHODS_DECLARATION, extensions.ToString()); output = output.Replace(Variable_EXTENSIONS_DECLARATION, extensionMethod); } else { output = output.Replace(Variable_EXTENSIONS_DECLARATION, ""); } outputs.Add(new ProjectItem(ProjectItemType.EventSource, this.Include, output) { Include = this.Include }); return(outputs); }