Example #1
0
        private List <ExplorerItem> InternalGetSchemaAndBuildAssembly(IConnectionInfo cxInfo, AssemblyName assemblyToBuild, ref string @namespace, ref string typeName)
        {
            var connectionProperties = new ConnectionProperties(cxInfo);

#if NETCORE
            Log("Generating code");
            string code;
            var    properties = new List <PropertyInfo>();
            using (var generator = new LiteDBPad.CodeGenerator(connectionProperties, @namespace, typeName))
            {
                //properties.AddRange(generator.CapitalizedCollectionNames.Select(_ => new PropertyInfo(_, typeof(LiteCollection<DumpableBsonDocument>))));
                properties.AddRange(generator.CapitalizedCollectionNames.Select(_ => new PropertyInfo("All" + _, typeof(DumpableBsonDocumentCollection))));
                code = generator.TransformText();
            }

            var assembliesToReference = GetCoreFxReferenceAssemblies().ToList();
            assembliesToReference.Add(typeof(DynamicDataContextDriver).Assembly.Location);
            assembliesToReference.Add(typeof(LiteDB.LiteDatabase).Assembly.Location);

            //Log($"Assemblies: {string.Join(Environment.NewLine, assembliesToReference)}");

            var compileResult = CompileSource(new CompilationInput
            {
                FilePathsToReference = assembliesToReference.ToArray(),
                OutputPath           = assemblyToBuild.CodeBase,
                SourceCode           = new[] { code }
            });

            if (compileResult.Errors.Length > 0)
            {
#if DEBUG
                Log("Error compiling generated code");
                Log(string.Join(Environment.NewLine, compileResult.Errors));
#endif
                throw new Exception($"Cannot compile typed context: {string.Join(Environment.NewLine, compileResult.Errors)}");
            }

            var items = GetSchema(cxInfo, properties.ToArray());
#else
            string code;
            //using (var generator = new LiteDBPad.CodeGenerator(connectionProperties, @namespace, typeName))
            //    code = generator.TransformText();
            var properties = new List <PropertyInfo>();
            using (var generator = new LiteDBPad.CodeGenerator(connectionProperties, @namespace, typeName))
            {
                //properties.AddRange(generator.CapitalizedCollectionNames.Select(_ => new PropertyInfo(_, typeof(LiteCollection<DumpableBsonDocument>))));
                properties.AddRange(generator.CapitalizedCollectionNames.Select(_ => new PropertyInfo("All" + _, typeof(DumpableBsonDocumentCollection))));
                code = generator.TransformText();
            }

            // Use the CSharpCodeProvider to compile the generated code:
            CompilerResults results;
            using (var codeProvider = new CSharpCodeProvider(new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            }))
            {
                var options = new CompilerParameters(
                    "System.dll System.Core.dll System.Xml.dll System.Data.Services.Client.dll".Split(),
                    assemblyToBuild.CodeBase,
                    true);

                options.ReferencedAssemblies.Add(typeof(LiteDB.LiteDatabase).Assembly.Location);
                options.ReferencedAssemblies.Add(typeof(LiteDBPad.DumpableBsonDocument).Assembly.Location);
                options.ReferencedAssemblies.Add(typeof(LINQPad.ICustomMemberProvider).Assembly.Location);

                results = codeProvider.CompileAssemblyFromSource(options, code);
            }
            if (results.Errors.Count > 0)
            {
                throw new Exception
                          ("Cannot compile typed context: " + results.Errors[0].ErrorText + " (line " + results.Errors[0].Line + ")");
            }

            //var customType = results.CompiledAssembly.GetType(string.Concat(@namespace, ".", typeName));

            //if (customType == null)
            //    throw new InvalidOperationException();

            var items = GetSchema(cxInfo, properties.ToArray());
#endif

#if DEBUG
            Log("Found {0} items", items.Count);
#endif

            return(items);
        }