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}"); } }
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)); }