public static void Compile() { var baseType = typeof(IBuilderTest); var handlers = new ReadOnlyDictionary <int, ReadOnlyCollection <HandlerInformation> >( new Dictionary <int, ReadOnlyCollection <HandlerInformation> >()); var namespaces = new SortedSet <string> { baseType.Namespace ! }; var options = new RockOptions(); var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false); var trees = new[] { builder.Tree }; var compiler = new InMemoryCompiler(trees, options.Optimization, new List <Assembly> { baseType.Assembly }.AsReadOnly(), builder.IsUnsafe, options.AllowWarning); var assembly = compiler.Compile(); Assert.That(compiler.Optimization, Is.EqualTo(options.Optimization), nameof(compiler.Optimization)); Assert.That(compiler.Trees, Is.SameAs(trees), nameof(compiler.Trees)); Assert.That(assembly, Is.Not.Null, nameof(compiler.Compile)); Assert.That( (from type in assembly.GetTypes() where baseType.IsAssignableFrom(type) select type).Single(), Is.Not.Null); } }
public void Compile() { var baseType = typeof(IBuilderTest); var handlers = new ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>>( new Dictionary<int, ReadOnlyCollection<HandlerInformation>>()); var namespaces = new SortedSet<string> { baseType.Namespace }; var options = new RockOptions(); var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, false); builder.Build(); var trees = new[] { builder.Tree }; var compiler = new InMemoryCompiler(trees, options.Optimization, new List<Assembly> { baseType.Assembly }.AsReadOnly(), builder.IsUnsafe, options.AllowWarnings); compiler.Compile(); Assert.AreEqual(options.Optimization, compiler.Optimization, nameof(compiler.Optimization)); Assert.AreSame(trees, compiler.Trees, nameof(compiler.Trees)); Assert.IsNotNull(compiler.Result, nameof(compiler.Result)); Assert.IsNotNull( (from type in compiler.Result.GetTypes() where baseType.IsAssignableFrom(type) select type).Single()); }
public static Assembly BuildDbContextAssembly(string datasetName) { var dbContextIMC = new InMemoryCompiler(); dbContextIMC.AddFile($"../Models/Contexts/{datasetName}/Context.cs"); dbContextIMC.AddFile($"../Models/Contexts/{datasetName}/Master.cs"); dbContextIMC.AddFile($"../Models/Contexts/{datasetName}/Models.cs"); return(dbContextIMC.CompileAssembly()); }
internal InMemoryMaker(Type baseType, ReadOnlyDictionary<int, ReadOnlyCollection<HandlerInformation>> handlers, SortedSet<string> namespaces, RockOptions options, bool isMake) { var builder = new InMemoryBuilder(baseType, handlers, namespaces, options, isMake); builder.Build(); var compiler = new InMemoryCompiler(new List<SyntaxTree> { builder.Tree }, options.Optimization, new List<Assembly> { baseType.Assembly }.AsReadOnly(), builder.IsUnsafe, options.AllowWarnings); compiler.Compile(); this.Mock = compiler.Result.GetType($"{baseType.Namespace}.{builder.TypeName}"); }
public static bool IsCodeValid(string code) { try { var imc = new InMemoryCompiler(); imc.AddCodeBody(code); // Validate code imc.CompileAssembly(); return(true); } catch { return(false); } }
public static void Main(string[] args) { Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { bool generateModels = o.GenerateModels; bool loadData = o.LoadData; string frenchFile = o.French; bool loadFrench = !string.IsNullOrEmpty(frenchFile); string datasetName = o.Dataset.ToUpper(); string csvFilePath = o.File; var connectionString = o.ConnectionString; if (generateModels) { Console.Write("This process will guide you through the creation of the data tool. Press enter to continue or ^C to exit."); Console.ReadLine(); using (var sr = new StreamReader(csvFilePath)) using (var csv = new CsvReader(sr, new Configuration { Delimiter = ",", Encoding = Encoding.UTF8 })) { csv.Read(); csv.ReadHeader(); var directory = new DirectoryInfo($"../Models/Contexts/{datasetName}"); if (directory.Exists) { Console.WriteLine($"{directory.FullName} directory already exists. Manually delete it or use another name for the dataset"); Environment.Exit(0); } else { directory.Create(); } Console.Write("Generating master entity"); var outputMaster = GenerateMasterSource(datasetName, csv.Context.HeaderRecord); bool isValidMaster = IsCodeValid(outputMaster); if (!isValidMaster) { throw new Exception("Failed to generate valid master file"); } string masterPath = Path.Join(directory.FullName, "Master.cs"); File.WriteAllText(masterPath, outputMaster); var editedMasterValidated = false; while (!editedMasterValidated) { Console.Write($"Created {masterPath}. Press Enter when done editing."); Console.ReadLine(); outputMaster = File.ReadAllText(masterPath); editedMasterValidated = IsCodeValid(outputMaster); } var imc = new InMemoryCompiler(); imc.AddCodeBody(outputMaster); var masterType = imc.CompileAssembly().GetType($"Models.Contexts.{datasetName}.Master"); Console.Write("Generating model entities"); var outputModels = GenerateModelsForMaster(masterType); bool isValidModels = IsCodeValid(outputModels); if (!isValidModels) { throw new Exception("Failed to generate valid models file"); } string modelsPath = Path.Join(directory.FullName, "Models.cs"); File.WriteAllText(modelsPath, outputModels); var editedModelsValidated = false; while (!editedModelsValidated) { Console.Write($"Created {modelsPath}. Press Enter when done editing."); Console.ReadLine(); outputModels = File.ReadAllText(modelsPath); editedModelsValidated = IsCodeValid(outputModels); } imc = new InMemoryCompiler(); imc.AddCodeBody(outputModels); var names = imc.CompileAssembly().GetTypes().Where(t => t.Namespace == $"Models.Contexts.{datasetName}").Select(t => t.Name); var outputContext = GenerateContextSource(datasetName, names); string contextPath = Path.Join(directory.FullName, "Context.cs"); File.WriteAllText(contextPath, outputContext); } } Console.WriteLine("Done. Confirm results and press enter to begin database initialization."); //Console.ReadLine(); if (loadData) { SetupDatabase(datasetName, csvFilePath, connectionString, generateModels, frenchFile); } }); }