Inheritance: LINQPad.Extensibility.DataContext.DynamicDataContextDriver
Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            ConsoleColor old = Console.ForegroundColor;
            try{
                var props = new ConnectionProperties();
                using (var frm = new ConnectionDialog(props, true, Assembly.LoadFrom))
                {
                    var result = frm.ShowDialog();
                    if (result != System.Windows.Forms.DialogResult.OK)
                        return;
                }

                var cxi = new FakeConnection();
                new ConnectionPropertiesSerializer().Serialize(cxi.DriverData, props);

                var driver = new MongoDynamicDataContextDriver();

                List<Assembly> assemblies = props.AssemblyLocations.Select(Assembly.LoadFrom).ToList();
                var code = driver.GetStaticCodeFiles()
                    .Concat(new string[] {driver.GenerateDynamicCode(props, assemblies, "", "driver")});
                if(props.InitializationQuery != null)
                    code = code.Concat(new string[]{driver.GenerateCustomInitQuery(props.InitializationQuery, "driver")});

                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.WriteLine("------------------------------------------------");
                foreach (string s in code)
                {
                    Console.WriteLine(s);
                    Console.WriteLine("------------------------------------------------");
                }
                Console.ForegroundColor = old;

                using (var frm = new SaveFileDialog())
                {
                    var result = frm.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        using (StreamWriter writer = new StreamWriter(frm.OpenFile()))
                        {
                            foreach (string s in code)
                            {
                                writer.WriteLine(s);
                                writer.WriteLine("---------------------------------------------------");
                            }

                            writer.Flush();
                        }

                    }
                }

            }catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex);
            }
            finally{
                Console.ForegroundColor = old;
            }
        }
        private string CreateShellQuery(ConnectionProperties props)
        {
            var driver = new MongoDynamicDataContextDriver();

            //build the query XDocument
            var doc   = new XDocument();
            var query = new XElement("Query");

            doc.Add(query);
            query.SetAttributeValue("Kind", "Program");
            query.SetElementValue("Reference", Path.Combine(driver.GetDriverFolder(), "LinqPadMongoDriver.dll"));
            foreach (string loc in props.AssemblyLocations)
            {
                var el = new XElement("Reference");
                el.SetValue(loc);
                query.Add(el);
            }
            foreach (string ns in MongoDynamicDataContextDriver.GetNamespacesToAdd(props)
                     .Concat(new[]
            {
                "System",
                "GDSX.Externals.LinqPad.Driver"
            }))
            {
                var el = new XElement("Namespace");
                el.SetValue(ns);
                query.Add(el);
            }

            StringBuilder sb = new StringBuilder();

            using (var writer = XmlWriter.Create(sb, new XmlWriterSettings {
                OmitXmlDeclaration = true
            }))
                doc.Save(writer);

            sb.AppendLine();


            var ass = Assembly.GetExecutingAssembly();

            using (var stream = ass.GetManifestResourceStream("GDSX.Externals.LinqPad.Driver.ShellInitQuery.linq"))
            {
                if (stream == null)
                {
                    throw new Exception("Could not find static code files");
                }
                using (var reader = new StreamReader(stream))
                    sb.Append(reader.ReadToEnd());
            }

            return(sb.ToString());
        }
        private List <string> ValidateLinqQuery(LinqPadQuery query, ConnectionProperties props)
        {
            List <string> errors = new List <string>();

            StringBuilder sb = new StringBuilder();

            foreach (var ns in query.Namespaces)
            {
                sb.Append("using ").Append(ns).AppendLine(";");
            }
            sb.AppendFormat(@"
public class TestQuery
{{
    public TestQuery()
    {{

    }}

    {0}
}}", query.Query);

            var             driver = new MongoDynamicDataContextDriver();
            CompilerResults results;

            using (var codeProvider = new CSharpCodeProvider(new Dictionary <string, string>()
            {
                { "CompilerVersion", "v4.0" }
            }))
            {
                var assemblyNames = new HashSet <string>(query.References, new AssemblyPathEqualityComparer());

                //add additional assemblies which may or may not have been overridden
                assemblyNames.AddRange("System.dll System.Core.dll".Split());
                assemblyNames.Add(Path.Combine(driver.GetDriverFolder(), "MongoDB.Driver.dll"));
                assemblyNames.Add(Path.Combine(driver.GetDriverFolder(), "MongoDB.Bson.dll"));
                assemblyNames.Add(Path.Combine(driver.GetDriverFolder(), "LinqPadMongoDriver.dll"));

                var options = new CompilerParameters(assemblyNames.ToArray());
                options.GenerateInMemory = true;

                results = codeProvider.CompileAssemblyFromSource(options, sb.ToString());
            }
            if (results.Errors.Count > 0)
            {
                errors.AddRange(results.Errors.Cast <CompilerError>().Select(x => x.ToString()));

                return(errors);
            }

            Type       compiledType = results.CompiledAssembly.GetType("TestQuery");
            object     instance     = Activator.CreateInstance(compiledType);
            MethodInfo initMethod   = compiledType.GetMethod("Initialize", new[] { typeof(ConnectionProperties) });

            if (initMethod == null)
            {
                errors.Add(string.Format("The query must contain a method called Initialize that takes one parameter of type {0}", typeof(ConnectionProperties)));
                return(errors);
            }


            return(errors);
        }