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 static GeneratedAssembly AssembleTypes(this ICodeFileCollection generator, GenerationRules rules) { var generatedAssembly = generator.StartAssembly(rules); foreach (var file in generator.BuildFiles()) { file.AssembleTypes(generatedAssembly); } return(generatedAssembly); }