Exemplo n.º 1
0
 protected override void WriteBeforeLiteralBody(SourceCodeWriter tw)
 {
     if (!string.IsNullOrWhiteSpace(tw.Context.Namespace))
     {
         tw.WriteLine("Namespace global.[{0}]", tw.Context.Namespace);
         tw.Shift();
     }
     tw.WriteLine("Module ThisAssembly");
     tw.Shift();
 }
Exemplo n.º 2
0
        protected override void WriteAfterLiteralBody(SourceCodeWriter tw)
        {
            tw.UnShift();
            tw.WriteLine("End Module");

            if (!string.IsNullOrWhiteSpace(tw.Context.Namespace))
            {
                tw.UnShift();
                tw.WriteLine("End Namespace");
            }
        }
Exemplo n.º 3
0
        protected override void WriteAfterBody(SourceCodeWriter tw)
        {
            var required = IsRequiredSelfHostingMetadataAttribute(tw.Context);

            if (!required)
            {
                tw.WriteLine("#if NET10 || NET11 || NET20 || NET30 || NET35 || NET40");
                tw.WriteLine();
            }

            tw.WriteLine("Namespace global.System.Reflection");
            tw.Shift();
            tw.WriteLine("<AttributeUsage(AttributeTargets.Assembly, AllowMultiple := True, Inherited := False)>");
            tw.WriteLine("Friend NotInheritable Class AssemblyMetadataAttribute");
            tw.Shift();
            tw.WriteLine("Inherits Attribute");
            tw.WriteLine("Public Sub New(key As String, value As String)");
            tw.Shift();
            tw.WriteLine("Me.Key = key");
            tw.WriteLine("Me.Value = value");
            tw.UnShift();
            tw.WriteLine("End Sub");
            tw.WriteLine("Public ReadOnly Property Key As String");
            tw.WriteLine("Public ReadOnly Property Value As String");
            tw.UnShift();
            tw.WriteLine("End Class");
            tw.UnShift();
            tw.WriteLine("End Namespace");

            if (!required)
            {
                tw.WriteLine();
                tw.WriteLine("#endif");
            }

            tw.WriteLine();
        }
Exemplo n.º 4
0
 protected override void WriteAfterNestedLiteralBody(SourceCodeWriter tw)
 {
     tw.UnShift();
     tw.WriteLine("End Class");
 }
Exemplo n.º 5
0
 protected override void WriteLiteral(SourceCodeWriter tw, string name, string value) =>
 tw.WriteLine("Public Const [{0}] As String = {1}", name, value);
Exemplo n.º 6
0
 protected override void WriteBeforeNestedLiteralBody(SourceCodeWriter tw, string name)
 {
     tw.WriteLine("Public NotInheritable Class {0}", name);
     tw.Shift();
 }
Exemplo n.º 7
0
 protected override void WriteAttribute(SourceCodeWriter tw, string name, string args) =>
 tw.WriteLine("<Assembly: {0}({1})>", name, args);
Exemplo n.º 8
0
 protected override void WriteImport(SourceCodeWriter tw, string namespaceName) =>
 tw.WriteLine("Imports {0}", namespaceName);
Exemplo n.º 9
0
 protected override void WriteComment(SourceCodeWriter tw, string format, params object[] args) =>
 tw.WriteLine("' " + format, args);
Exemplo n.º 10
0
        public void Write(
            ProcessorContext context,
            Dictionary <string, object> keyValues,
            DateTimeOffset generated,
            IEnumerable <Rule> ruleSet,
            IEnumerable <string> importSet)
        {
            Debug.Assert(string.IsNullOrWhiteSpace(context.OutputPath) == false);
            Debug.Assert(keyValues != null);
            Debug.Assert(ruleSet != null);
            Debug.Assert(importSet != null);

            var targetFolder = Path.GetDirectoryName(context.OutputPath);

            if (!string.IsNullOrWhiteSpace(targetFolder) && !Directory.Exists(targetFolder))
            {
                try
                {
                    // Construct sub folders (ex: obj\Debug).
                    // May fail if parallel-building on MSBuild, ignoring exceptions.
                    Directory.CreateDirectory(targetFolder);
                }
                catch
                {
                }
            }

            using (var ftw = File.CreateText(context.OutputPath))
            {
                var tw = new SourceCodeWriter(ftw, context);

                this.WriteComment(tw,
                                  $"This is auto-generated version information attributes by RelaxVersioner.{this.GetType().Assembly.GetName().Version}, Do not edit.");
                this.WriteComment(tw,
                                  $"Generated date: {generated:R}");
                tw.WriteLine();

                this.WriteBeforeBody(tw);

                foreach (var importNamespace in importSet)
                {
                    this.WriteImport(tw, importNamespace);
                }
                tw.WriteLine();

                foreach (var rule in ruleSet)
                {
                    var formattedValue = Named.Format(rule.Format, keyValues, key => string.Empty);
                    if (!string.IsNullOrWhiteSpace(rule.Key))
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, rule.Key, formattedValue);
                    }
                    else
                    {
                        this.WriteAttributeWithArguments(tw, rule.Name, formattedValue);
                    }
                }
                tw.WriteLine();

                if (context.GenerateStatic)
                {
                    this.WriteBeforeLiteralBody(tw);

                    foreach (var g in ruleSet.GroupBy(rule => rule.Name))
                    {
                        var rules = g.ToArray();

                        if (rules.Length >= 2)
                        {
                            this.WriteBeforeNestedLiteralBody(tw, rules[0].Name);
                        }

                        foreach (var rule in rules)
                        {
                            var formattedValue = Named.Format(rule.Format, keyValues, key => string.Empty);
                            if (!string.IsNullOrWhiteSpace(rule.Key))
                            {
                                this.WriteLiteralWithArgument(tw, rule.Key, formattedValue);
                            }
                            else
                            {
                                this.WriteLiteralWithArgument(tw, rule.Name, formattedValue);
                            }
                        }

                        if (rules.Length >= 2)
                        {
                            this.WriteAfterNestedLiteralBody(tw);
                        }
                    }

                    this.WriteAfterLiteralBody(tw);
                    tw.WriteLine();
                }

                this.WriteAfterBody(tw);

                tw.Flush();
            }
        }