예제 #1
0
        public static GeneratedAssembly StartAssembly(this ICodeFileCollection generator, GenerationRules rules)
        {
            if (generator.ChildNamespace.IsEmpty())
            {
                throw new InvalidOperationException($"Missing {nameof(ICodeFileCollection.ChildNamespace)} for {generator}");
            }

            var @namespace = $"{rules.ApplicationNamespace}.{generator.ChildNamespace}";

            return(new GeneratedAssembly(rules, @namespace));
        }
예제 #2
0
        public static GeneratedAssembly AssembleTypes(this ICodeFileCollection generator, GenerationRules rules)
        {
            var generatedAssembly = generator.StartAssembly(rules);

            foreach (var file in generator.BuildFiles())
            {
                file.AssembleTypes(generatedAssembly);
            }

            return(generatedAssembly);
        }
예제 #3
0
 public static void WriteCodeFile(this ICodeFile file, ICodeFileCollection parent, GenerationRules rules, string code)
 {
     try
     {
         var directory = parent.ToExportDirectory(rules.GeneratedCodeOutputPath);
         var fileName  = Path.Combine(directory, file.FileName.Replace(" ", "_") + ".cs");
         File.WriteAllText(fileName, code);
         Console.WriteLine("Generated code to " + fileName.ToFullPath());
     }
     catch (Exception e)
     {
         Console.WriteLine("Unable to write code file");
         Console.WriteLine(e.ToString());
     }
 }
예제 #4
0
        private string generateCode(ICodeFileCollection collection)
        {
            if (collection.ChildNamespace.IsEmpty())
            {
                throw new InvalidOperationException($"Missing {nameof(ICodeFileCollection.ChildNamespace)} for {collection}");
            }

            var @namespace = collection.ToNamespace(collection.Rules);

            var generatedAssembly = new GeneratedAssembly(collection.Rules, @namespace);
            var files             = collection.BuildFiles();

            foreach (var file in files)
            {
                file.AssembleTypes(generatedAssembly);
            }

            return(generatedAssembly.GenerateCode(ServiceVariableSource));
        }
예제 #5
0
        public static string ToExportDirectory(this ICodeFileCollection generator, string exportDirectory)
        {
            if (generator.ChildNamespace.IsEmpty())
            {
                throw new InvalidOperationException($"Missing {nameof(ICodeFileCollection.ChildNamespace)} for {generator}");
            }

            var generatorDirectory = exportDirectory;
            var parts = generator.ChildNamespace.Split('.');

            foreach (var part in parts)
            {
                generatorDirectory = Path.Combine(generatorDirectory, part);
            }

            new FileSystem().CreateDirectory(generatorDirectory);

            return(generatorDirectory);
        }
예제 #6
0
 public GeneratorCompilationFailureException(ICodeFileCollection generator, Exception innerException) : base($"Failure when trying to generate code for {generator.GetType().GetFullName()}", innerException)
 {
 }
예제 #7
0
        public static void InitializeSynchronously(this ICodeFile file, GenerationRules rules, ICodeFileCollection parent, IServiceProvider?services)
        {
            var @namespace = parent.ToNamespace(rules);

            if (rules.TypeLoadMode == TypeLoadMode.Dynamic)
            {
                Console.WriteLine($"Generated code for {parent.ChildNamespace}.{file.FileName}");

                var generatedAssembly = parent.StartAssembly(rules);
                file.AssembleTypes(generatedAssembly);
                var serviceVariables = services?.GetService(typeof(IServiceVariableSource)) as IServiceVariableSource;

                var compiler = new AssemblyGenerator();
                compiler.Compile(generatedAssembly, serviceVariables);
                file.AttachTypesSynchronously(rules, generatedAssembly.Assembly, services, @namespace);

                return;
            }

            var found = file.AttachTypesSynchronously(rules, rules.ApplicationAssembly, services, @namespace);

            if (found)
            {
                Console.WriteLine($"Types from code file {parent.ChildNamespace}.{file.FileName} were loaded from assembly {rules.ApplicationAssembly.GetName()}");
            }

            if (!found)
            {
                if (rules.TypeLoadMode == TypeLoadMode.Static)
                {
                    throw new ExpectedTypeMissingException(
                              $"Could not load expected pre-built types for code file {file.FileName} ({file})");
                }

                var generatedAssembly = parent.StartAssembly(rules);
                file.AssembleTypes(generatedAssembly);
                var serviceVariables = services?.GetService(typeof(IServiceVariableSource)) as IServiceVariableSource;


                var compiler = new AssemblyGenerator();
                compiler.Compile(generatedAssembly, serviceVariables);

                file.AttachTypesSynchronously(rules, generatedAssembly.Assembly, services, @namespace);

                if (rules.SourceCodeWritingEnabled)
                {
                    var code = compiler.Code;
                    file.WriteCodeFile(parent, rules, code);
                }

                Console.WriteLine($"Generated and compiled code in memory for {parent.ChildNamespace}.{file.FileName}");
            }
        }
 public ScriptContext(dynamic inputParameter, IModelRepository models, ICodeFileCollection codeFileCollection)
 {
     Code   = codeFileCollection;
     In     = inputParameter ?? new ExpandoObject();
     Models = models;
 }
예제 #9
0
 public static string ToNamespace(this ICodeFileCollection codeFileCollection, GenerationRules rules)
 {
     return($"{rules.ApplicationNamespace}.{codeFileCollection.ChildNamespace}");
 }