Exemplo n.º 1
0
        private void GenerateImplementation(StandaloneGeneratorExecutionContext context, INamedTypeSymbol intf)
        {
            var ns      = intf.ContainingNamespace;
            var nsName  = ns.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Substring(8);
            var clsName = intf.Name.Substring(1);
            var cls     = ns.GetMembers(clsName).OfType <INamedTypeSymbol>().Where(nts => nts.TypeKind == TypeKind.Class).FirstOrDefault();


            holder.addElement(intf, context);
            holder.addProperty(intf, context);
            holder.addImplementations(intf, context);

            var element = holder.getElementByName(intf.Name);

            var source =
                $@"
using System.Collections.Generic;
using ModelGenerator;
   namespace {nsName} {{
    public partial class {clsName} : {intf.Name} {{
        {element.getProperties()}
        {element.getImplementations()}
    }}
}}";

            context.AddSource(clsName + ".cs", source);
            //getSource(context, intf, firstLine);
        }
Exemplo n.º 2
0
        public void addElement(INamedTypeSymbol intf, StandaloneGeneratorExecutionContext context)
        {
            if (getElementByName(intf.Name) == null)
            {
                InterfaceElement e = new InterfaceElement(intf.Name);

                elements.Add(e);
            }
        }
Exemplo n.º 3
0
 public void Execute(StandaloneGeneratorExecutionContext context)
 {
     foreach (var symbol in GeneratorUtils.GetNamedTypeSymbols(context.Compilation))
     {
         if (symbol.DeclaringSyntaxReferences.Length > 0)
         {
             if (GeneratorUtils.IsModelObject(symbol) || GeneratorUtils.HasOppositeAttribute(symbol))
             {
                 GenerateImplementation(context, symbol);
             }
         }
     }
 }
Exemplo n.º 4
0
        public void addProperty(INamedTypeSymbol intf, StandaloneGeneratorExecutionContext context)
        {
            foreach (var e in elements)
            {
                if (e.Name == intf.Name)
                {
                    var arr = context.Compilation.SyntaxTrees.ToArray();
                    List <customProperty> funcProperties = getFuncProperties(intf.Name, arr[0].GetRoot().DescendantNodes().ToArray());

                    foreach (customProperty fp in funcProperties)
                    {
                        e.addProperty(fp);
                    }
                }
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            if (Directory.Exists(GeneratedFilesPath))
            {
                Directory.Delete(GeneratedFilesPath, true);
            }
            Directory.CreateDirectory(GeneratedFilesPath);

            var syntaxTrees    = SourceFiles.Select(fileName => CSharpSyntaxTree.ParseText(File.ReadAllText(Path.Combine(SourceFilesPath, fileName))));
            var options        = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            var preCompilation = CSharpCompilation.Create("GenModel", options: options).AddSyntaxTrees(syntaxTrees).AddReferences(SystemRuntimeAssemblies()).AddReferences(ReferencedAssemblies());
            var formatter      = new DiagnosticFormatter();

            Console.WriteLine("Pre-generation diagnostics:");
            foreach (var diag in preCompilation.GetDiagnostics())
            {
                if (diag.Severity != DiagnosticSeverity.Hidden)
                {
                    Console.WriteLine("  " + formatter.Format(diag));
                }
            }
            Console.WriteLine("===============================");

            var generator = new StandaloneModelGenerator();
            var context   = StandaloneGeneratorExecutionContext.FromParams(preCompilation);

            generator.Execute(context);
            foreach (var hs in context.GeneratedSources)
            {
                var genFilePath = Path.Combine(GeneratedFilesPath, hs.HintName);
                Console.WriteLine("Generated file: " + genFilePath);
                File.WriteAllText(genFilePath, hs.Source);
            }

            Console.WriteLine("===============================");
            var postCompilation = preCompilation.AddSyntaxTrees(context.GeneratedSources.Select(hs => CSharpSyntaxTree.ParseText(hs.Source, path: hs.HintName)));

            Console.WriteLine("Post-generation diagnostics:");
            foreach (var diag in postCompilation.GetDiagnostics())
            {
                if (diag.Severity != DiagnosticSeverity.Hidden)
                {
                    Console.WriteLine("  " + formatter.Format(diag));
                }
            }
        }
Exemplo n.º 6
0
        public void addImplementations(INamedTypeSymbol intf, StandaloneGeneratorExecutionContext context)
        {
            foreach (var e in elements)
            {
                if (e.Name == intf.Name)
                {
                    var           arr        = context.Compilation.SyntaxTrees.ToArray();
                    List <string> funcNames  = getFuncNames(intf.Name, arr[0].GetRoot().DescendantNodes().ToArray());
                    List <string> funcBodies = getFuncBody(funcNames, arr[1].GetRoot().DescendantNodes().ToArray());

                    foreach (string fb in funcBodies)
                    {
                        e.addImplementation(fb);
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void Execute(GeneratorExecutionContext context)
        {
            var generator = new StandaloneModelGenerator();

            generator.Execute(StandaloneGeneratorExecutionContext.FromCSharp(context));
        }