public void Execute(GeneratorExecutionContext context) { context.CheckDebugger("ThisAssemblyProject"); if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ThisAssemblyProject", out var properties)) { return; } var metadata = properties.Split('|') .Select(prop => new KeyValuePair <string, string>(prop, context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property." + prop, out var value) ? value : null)) .Where(pair => pair.Value != null) .Distinct(new KeyValueComparer()) .ToDictionary(x => x.Key, x => x.Value); var model = new Model(metadata); var language = context.ParseOptions.Language; var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); var output = template.Render(model, member => member.Name); context.ApplyDesignTimeFix(output, "ThisAssembly.Project", language); context.AddSource("ThisAssembly.Project", SourceText.From(output, Encoding.UTF8)); }
public void Execute(GeneratorExecutionContext context) { context.CheckDebugger("ThisAssemblyConstants"); var constantFiles = context.AdditionalFiles .Where(f => context.AnalyzerConfigOptions .GetOptions(f) .TryGetValue("build_metadata.AdditionalFiles.SourceItemType", out var itemType) && itemType == "Constant"); if (!constantFiles.Any()) { return; } var pairs = constantFiles.Select(f => { context.AnalyzerConfigOptions.GetOptions(f).TryGetValue("build_metadata.Constant.Value", out var value); context.AnalyzerConfigOptions.GetOptions(f).TryGetValue("build_metadata.Constant.Comment", out var comment); return(new Constant(Path.GetFileName(f.Path), value, string.IsNullOrWhiteSpace(comment) ? null : comment)); }).Where(x => x.Value != null).ToList(); var root = Area.Load(pairs); var language = context.ParseOptions.Language; var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); var output = template.Render(new Model(root), member => member.Name); // Apply formatting since indenting isn't that nice in Scriban when rendering nested // structures via functions. if (language == LanguageNames.CSharp) { output = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit(output) .NormalizeWhitespace() .GetText() .ToString(); } //else if (language == LanguageNames.VisualBasic) //{ // output = Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseCompilationUnit(output) // .NormalizeWhitespace() // .GetText() // .ToString(); //} context.ApplyDesignTimeFix(output, "ThisAssembly.Constants", language); context.AddSource("ThisAssembly.Constants", SourceText.From(output, Encoding.UTF8)); }
public void Execute(GeneratorExecutionContext context) { context.CheckDebugger("ThisAssemblyAssemblyInfo"); var metadata = context.Compilation.Assembly.GetAttributes() .Where(x => attributes.Contains(x.AttributeClass?.Name)) .Select(x => new KeyValuePair <string, string>(x.AttributeClass.Name.Substring(8).Replace("Attribute", ""), (string)x.ConstructorArguments[0].Value)) .ToDictionary(x => x.Key, x => x.Value); var model = new Model(metadata); var language = context.ParseOptions.Language; var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); var output = template.Render(model, member => member.Name); context.ApplyDesignTimeFix(output, "ThisAssembly.Info", language); context.AddSource("ThisAssembly.Info", SourceText.From(output, Encoding.UTF8)); }
public void Execute(GeneratorExecutionContext context) { context.CheckDebugger("ThisAssemblyMetadata"); var metadata = context.Compilation.Assembly.GetAttributes() .Where(x => x.AttributeClass?.Name == nameof(System.Reflection.AssemblyMetadataAttribute) && Microsoft.CodeAnalysis.CSharp.SyntaxFacts.IsValidIdentifier((string)x.ConstructorArguments[0].Value)) .Select(x => new KeyValuePair <string, string>((string)x.ConstructorArguments[0].Value, (string)x.ConstructorArguments[1].Value)) .Distinct(new KeyValueComparer()) .ToDictionary(x => x.Key, x => x.Value); var model = new Model(metadata); var language = context.ParseOptions.Language; var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); var output = template.Render(model, member => member.Name); context.ApplyDesignTimeFix(output, "ThisAssembly.Metadata", language); context.AddSource("ThisAssembly.Metadata", SourceText.From(output, Encoding.UTF8)); }
public void Execute(GeneratorExecutionContext context) { context.CheckDebugger("ThisAssemblyStrings"); var resourceFiles = context.AdditionalFiles .Where(f => context.AnalyzerConfigOptions .GetOptions(f) .TryGetValue("build_metadata.AdditionalFiles.SourceItemType", out var itemType) && itemType == "EmbeddedResource"); if (!resourceFiles.Any()) { return; } var language = context.ParseOptions.Language; var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); foreach (var resourceFile in resourceFiles) { var options = context.AnalyzerConfigOptions.GetOptions(resourceFile); if (!options.TryGetValue("build_metadata.AdditionalFiles.ManifestResourceName", out var resourceName) || string.IsNullOrEmpty(resourceName)) { continue; } var rootArea = ResourceFile.Load(resourceFile.Path, "Strings"); var model = new Model(rootArea, resourceName); model = model with { ResourceName = resourceName }; var output = template.Render(model, member => member.Name); // Apply formatting since indenting isn't that nice in Scriban when rendering nested // structures via functions. if (language == LanguageNames.CSharp) { output = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit(output) .NormalizeWhitespace() .GetText() .ToString(); } //else if (language == LanguageNames.VisualBasic) //{ // output = Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseCompilationUnit(output) // .NormalizeWhitespace() // .GetText() // .ToString(); //} context.ApplyDesignTimeFix(output, resourceName, language); context.AddSource(resourceName, SourceText.From(output, Encoding.UTF8)); } var extension = language == LanguageNames.CSharp ? "cs" : language == LanguageNames.VisualBasic ? "vb" : "fs"; var strings = EmbeddedResource.GetContent("ThisAssembly.Strings." + extension); context.ApplyDesignTimeFix(strings, "ThisAssembly.Strings", language); context.AddSource("ThisAssembly.Strings", SourceText.From(strings, Encoding.UTF8)); } }