Пример #1
0
        private void OnError(int?contextId, string error)
        {
            var exception = new InvalidOperationException(error);

            if (contextId == null || contextId == -1)
            {
                _projectContexts.TrySetException(exception);
                _shutdown.RequestShutdown();
            }
            else
            {
                _compileResponses.AddOrUpdate(contextId.Value,
                                              _ =>
                {
                    var tcs = new TaskCompletionSource <CompileResponse>();
                    tcs.SetException(exception);
                    return(tcs);
                },
                                              (_, existing) =>
                {
                    if (!existing.TrySetException(exception))
                    {
                        var tcs = new TaskCompletionSource <CompileResponse>();
                        tcs.TrySetException(exception);
                        return(tcs);
                    }

                    return(existing);
                });
            }
        }
 public bool StopServer()
 {
     Task.Run(() => {
         Thread.Sleep(200);
         _applicationShutdown.RequestShutdown();
     });
     return(true);
 }
 public async Task Invoke(HttpContext httpContext)
 {
     if (httpContext.Request.Path.HasValue)
     {
         var endpoint = httpContext.Request.Path.Value;
         if (endpoint == OmnisharpEndpoints.StopServer)
         {
             await Task.Run(() =>
             {
                 Thread.Sleep(200);
                 _applicationShutdown.RequestShutdown();
             });
         }
     }
 }
Пример #4
0
        public DesignTimeHostCompiler(IApplicationShutdown shutdown, Stream stream)
        {
            _queue = new ProcessingQueue(stream);
            _queue.ProjectCompiled     += OnProjectCompiled;
            _queue.ProjectsInitialized += ProjectContextsInitialized;
            _queue.ProjectChanged      += _ => shutdown.RequestShutdown();
            _queue.Closed += OnClosed;
            _queue.Start();

            _queue.Send(new DesignTimeMessage
            {
                HostId      = "Application",
                MessageType = "EnumerateProjectContexts"
            });
        }
Пример #5
0
        public DesignTimeHostCompiler(IApplicationShutdown shutdown, IFileWatcher watcher, Stream stream)
        {
            _queue = new ProcessingQueue(stream);
            _queue.ProjectCompiled     += OnProjectCompiled;
            _queue.ProjectsInitialized += ProjectContextsInitialized;
            _queue.ProjectChanged      += _ => shutdown.RequestShutdown();
            _queue.ProjectSources      += files =>
            {
                foreach (var file in files)
                {
                    watcher.WatchFile(file);
                }
            };

            _queue.Closed += OnClosed;
            _queue.Start();

            _queue.Send(new DesignTimeMessage
            {
                HostId      = "Application",
                MessageType = "EnumerateProjectContexts"
            });
        }
Пример #6
0
        public DesignTimeHostCompiler(IApplicationShutdown shutdown, IFileWatcher watcher, Stream stream)
        {
            _queue = new ProcessingQueue(stream);
            _queue.ProjectCompiled += OnProjectCompiled;
            _queue.ProjectsInitialized += ProjectContextsInitialized;
            _queue.ProjectChanged += _ => shutdown.RequestShutdown();
            _queue.ProjectSources += files =>
            {
                foreach (var file in files)
                {
                    watcher.WatchFile(file);
                }
            };

            _queue.Closed += OnClosed;
            _queue.Start();

            _queue.Send(new DesignTimeMessage
            {
                HostId = "Application",
                MessageType = "EnumerateProjectContexts"
            });
        }
Пример #7
0
        public virtual int Main([NotNull] string[] args)
        {
            Check.NotNull(args, nameof(args));

            Console.CancelKeyPress += (_, __) => _applicationShutdown.RequestShutdown();

            var app = new CommandLineApplication
            {
                Name     = "dnx ef",
                FullName = "Entity Framework Commands"
            };

            app.VersionOption(
                "--version",
                ProductInfo.GetVersion());
            app.HelpOption("-?|-h|--help");
            app.OnExecute(
                () =>
            {
                ShowLogo();
                app.ShowHelp();
            });
            app.Command(
                "database",
                database =>
            {
                database.Description = "Commands to manage your database";
                database.HelpOption("-?|-h|--help");
                database.OnExecute(() => database.ShowHelp());
                database.Command(
                    "update",
                    update =>
                {
                    update.Description = "Updates the database to a specified migration";
                    var migrationName  = update.Argument(
                        "[migration]",
                        "the target migration. If '0', all migrations will be reverted. If omitted, all pending migrations will be applied");
                    var context = update.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    update.HelpOption("-?|-h|--help");
                    update.OnExecute(() => UpdateDatabase(migrationName.Value, context.Value()));
                });
            });
            app.Command(
                "dbcontext",
                dbcontext =>
            {
                dbcontext.Description = "Commands to manage your DbContext types";
                dbcontext.HelpOption("-?|-h|--help");
                dbcontext.OnExecute(() => dbcontext.ShowHelp());
                dbcontext.Command(
                    "list",
                    list =>
                {
                    list.Description = "List your DbContext types";
                    list.HelpOption("-?|-h|--help");
                    list.OnExecute(() => ListContexts());
                });
                dbcontext.Command(
                    "scaffold",
                    scaffold =>
                {
                    scaffold.Description = "Scaffolds a DbContext and entity type classes for a specified database";
                    var connection       = scaffold.Argument(
                        "[connection]",
                        "The connection string of the database");
                    var provider = scaffold.Argument(
                        "[provider]",
                        "The provider to use. For example, EntityFramework.SqlServer");
                    var dbContextClassName = scaffold.Option(
                        "-c|--context-class-name <name>",
                        "Name of the generated DbContext class.");
                    var outputPath = scaffold.Option(
                        "-o|--output-path <path>",
                        "Directory of the project where the classes should be output. If omitted, the top-level project directory is used.");
                    var tableFilters = scaffold.Option(
                        "-t|--tables <filter>",
                        "Selects for which tables to generate classes. "
                        + "<filter> is a comma-separated list of schema:table entries where either schema or table can be * to indicate 'any'.");
                    var useFluentApiOnly = scaffold.Option(
                        "-u|--fluent-api",
                        "Exclusively use fluent API to configure the model. If omitted, the output code will use attributes, where possible, instead.");
                    scaffold.HelpOption("-?|-h|--help");
                    scaffold.OnExecute(
                        async() =>
                    {
                        if (string.IsNullOrEmpty(connection.Value))
                        {
                            _logger.Value.LogError("Missing required argument '{0}'", connection.Name);

                            scaffold.ShowHelp();

                            return(1);
                        }
                        if (string.IsNullOrEmpty(provider.Value))
                        {
                            _logger.Value.LogError("Missing required argument '{0}'", provider.Name);

                            scaffold.ShowHelp();

                            return(1);
                        }

                        await ReverseEngineerAsync(
                            connection.Value,
                            provider.Value,
                            outputPath.Value(),
                            dbContextClassName.Value(),
                            tableFilters.Value(),
                            useFluentApiOnly.HasValue(),
                            _applicationShutdown.ShutdownRequested);

                        return(0);
                    });
                });
            });
            app.Command(
                "migrations",
                migration =>
            {
                migration.Description = "Commands to manage your migrations";
                migration.HelpOption("-?|-h|--help");
                migration.OnExecute(() => migration.ShowHelp());
                migration.Command(
                    "add",
                    add =>
                {
                    add.Description = "Add a new migration";
                    var name        = add.Argument(
                        "[name]",
                        "The name of the migration");
                    var context = add.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    add.HelpOption("-?|-h|--help");
                    add.OnExecute(
                        () =>
                    {
                        if (string.IsNullOrEmpty(name.Value))
                        {
                            _logger.Value.LogError("Missing required argument '{0}'", name.Name);

                            add.ShowHelp();

                            return(1);
                        }

                        AddMigration(name.Value, context.Value());

                        return(0);
                    });
                });
                migration.Command(
                    "list",
                    list =>
                {
                    list.Description = "List the migrations";
                    var context      = list.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    list.HelpOption("-?|-h|--help");
                    list.OnExecute(() => ListMigrations(context.Value()));
                });
                migration.Command(
                    "remove",
                    remove =>
                {
                    remove.Description = "Remove the last migration";
                    var context        = remove.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    remove.HelpOption("-?|-h|--help");
                    remove.OnExecute(() => RemoveMigration(context.Value()));
                });
                migration.Command(
                    "script",
                    script =>
                {
                    script.Description = "Generate a SQL script from migrations";
                    var from           = script.Argument(
                        "[from]",
                        "The starting migration. If omitted, '0' (the initial database) is used");
                    var to = script.Argument(
                        "[to]",
                        "The ending migration. If omitted, the last migration is used");
                    var output = script.Option(
                        "-o|--output <file>",
                        "The file to write the script to instead of stdout");
                    var idempotent = script.Option(
                        "-i|--idempotent",
                        "Generates an idempotent script that can used on a database at any migration");
                    var context = script.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    script.HelpOption("-?|-h|--help");
                    script.OnExecute(
                        () =>
                    {
                        if (!string.IsNullOrEmpty(to.Value) && string.IsNullOrEmpty(from.Value))
                        {
                            _logger.Value.LogError("Missing required argument '{0}'", from.Name);

                            return(1);
                        }

                        ScriptMigration(
                            from.Value,
                            to.Value,
                            output.Value(),
                            idempotent.HasValue(),
                            context.Value());

                        return(0);
                    });
                });
            });

            try
            {
                return(app.Execute(args));
            }
            catch (Exception ex)
            {
                LogException(ex);

                return(1);
            }
        }
Пример #8
0
        public virtual int Main([NotNull] string[] args)
        {
            Check.NotNull(args, nameof(args));

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                _applicationShutdown.RequestShutdown();
            };

            // TODO: Enable subcommands in help
            _app = new CommandLineApplication {
                Name = "ef"
            };
            _app.VersionOption(
                "-v|--version",
                typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                .InformationalVersion);
            _app.HelpOption("-h|--help");
            _app.Command(
                "context",
                context =>
            {
                context.Description = "Commands to manage your DbContext";
                context.HelpOption("-h|--help");
                context.Command(
                    "list",
                    list =>
                {
                    list.Description = "List the contexts";
                    list.HelpOption("-h|--help");
                    list.OnExecute(() => ListContexts());
                });
                context.OnExecute(
                    () =>
                {
                    _app.ShowHelp(context.Name);

                    return(0);
                });
            });
            _app.Command(
                "migration",
                migration =>
            {
                migration.Description = "Commands to manage your Code First Migrations";
                migration.HelpOption("-h|--help");
                migration.Command(
                    "add",
                    add =>
                {
                    add.Description = "Add a new migration";
                    var name        = add.Argument("[name]", "The name of the migration");
                    var context     = add.Option(
                        "-c|--context <context>",
                        "The context class to use",
                        CommandOptionType.SingleValue);
                    var startupProject = add.Option(
                        "-s|--startupProject <startupProject>",
                        "The start-up project to use",
                        CommandOptionType.SingleValue);
                    add.HelpOption("-h|--help");
                    add.OnExecute(
                        () =>
                    {
                        if (name.Value == null)
                        {
                            _logger.LogError("Missing required argument '{0}'.", name.Name);

                            migration.ShowHelp(add.Name);

                            return(1);
                        }

                        return(AddMigration(name.Value, context.Value(), startupProject.Value()));
                    });
                });
                migration.Command(
                    "apply",
                    apply =>
                {
                    apply.Description = "Apply migrations to the database";
                    var migrationName = apply.Argument(
                        "[migration]",
                        "The migration to apply. Use '0' to unapply all migrations");
                    var context = apply.Option(
                        "-c|--context <context>",
                        "The context class to use",
                        CommandOptionType.SingleValue);
                    var startupProject = apply.Option(
                        "-s|--startupProject <startupProject>",
                        "The start-up project to use",
                        CommandOptionType.SingleValue);
                    apply.HelpOption("-h|--help");
                    apply.OnExecute(() => ApplyMigration(migrationName.Value, context.Value(), startupProject.Value()));
                });
                migration.Command(
                    "list",
                    list =>
                {
                    list.Description = "List the migrations";
                    var context      = list.Option(
                        "-c|--context <context>",
                        "The context class to use",
                        CommandOptionType.SingleValue);
                    var startupProject = list.Option(
                        "-s|--startupProject <startupProject>",
                        "The start-up project to use",
                        CommandOptionType.SingleValue);
                    list.HelpOption("-h|--help");
                    list.OnExecute(() => ListMigrations(context.Value(), startupProject.Value()));
                });
                migration.Command(
                    "script",
                    script =>
                {
                    script.Description = "Generate a SQL script from migrations";
                    var from           = script.Argument("[from]", "The starting migration");
                    var to             = script.Argument("[to]", "The ending migration");
                    var output         = script.Option(
                        "-o|--output <file>",
                        "The file to write the script to instead of stdout",
                        CommandOptionType.SingleValue);
                    var idempotent = script.Option(
                        "-i|--idempotent",
                        "Generate an idempotent script",
                        CommandOptionType.NoValue);
                    var context = script.Option(
                        "-c|--context <context>",
                        "The context class to use",
                        CommandOptionType.SingleValue);
                    var startupProject = script.Option(
                        "-s|--startupProject <startupProject>",
                        "The start-up project to use",
                        CommandOptionType.SingleValue);
                    script.HelpOption("-h|--help");
                    script.OnExecute(() => ScriptMigration(from.Value, to.Value, output.Value(), idempotent.HasValue(), context.Value(), startupProject.Value()));
                });
                migration.Command(
                    "remove",
                    remove =>
                {
                    remove.Description = "Remove the last migration";
                    var context        = remove.Option(
                        "-c|--context <context>",
                        "The context class to use",
                        CommandOptionType.SingleValue);
                    var startupProject = remove.Option(
                        "-s|--startupProject <startupProject>",
                        "The start-up project to use",
                        CommandOptionType.SingleValue);
                    remove.HelpOption("-h|--help");
                    remove.OnExecute(() => RemoveMigration(context.Value(), startupProject.Value()));
                });
                migration.OnExecute(
                    () =>
                {
                    _app.ShowHelp(migration.Name);

                    return(0);
                });
            });
            _app.Command(
                "revEng",
                revEng =>
            {
                revEng.Description = "Command to reverse engineer code from a database";
                revEng.HelpOption("-h|--help");
                var connectionString = revEng.Argument(
                    "[connectionString]",
                    "The connection string of the database");
                var providerAssemblyName = revEng.Argument(
                    "[provider]",
                    "The assembly name of the provider");

                revEng.OnExecute(() => ReverseEngineerAsync(
                                     connectionString.Value,
                                     providerAssemblyName.Value,
                                     _applicationShutdown.ShutdownRequested));

                revEng.Command(
                    "customize",
                    customize =>
                {
                    customize.Description = "Outputs the default reverse engineer templates allowing the user to customize them";
                    var providerAssemblyNameForTemplates = customize.Argument(
                        "[provider]",
                        "The assembly name of the provider");
                    customize.HelpOption("-h|--help");

                    customize.OnExecute(() => CustomizeReverseEngineer(
                                            providerAssemblyNameForTemplates.Value));
                });
            });
            _app.Command(
                "help",
                help =>
            {
                help.Description = "Show help information";
                var command      = help.Argument("[command]", "Command that help information explains");
                help.OnExecute(
                    () =>
                {
                    if (command.Value != null)
                    {
                        _app.ShowHelp(command.Value);

                        return(0);
                    }

                    return(ShowHelp());
                });
            });
            _app.OnExecute(() => ShowHelp());

            return(_app.Execute(args));
        }
Пример #9
0
        public virtual int Main([NotNull] string[] args)
        {
            Check.NotNull(args, nameof(args));

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                _applicationShutdown.RequestShutdown();
            };

            var app = new CommandLineApplication
            {
                Name     = "ef",
                FullName = "Entity Framework Commands"
            };

            app.VersionOption(
                "--version",
                typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                .InformationalVersion);
            app.HelpOption("-?|-h|--help");
            app.OnExecute(
                () =>
            {
                ShowLogo();
                app.ShowHelp();
            });
            app.Command(
                "database",
                database =>
            {
                database.Description = "Commands to manage your database";
                database.HelpOption("-?|-h|--help");
                database.OnExecute(() => database.ShowHelp());
                database.Command(
                    "update",
                    update =>
                {
                    update.Description = "Updates the database to a specified migration";
                    var migrationName  = update.Argument(
                        "[migration]",
                        "the target migration. If '0', all migrations will be reverted. If omitted, all pending migrations will be applied");
                    var context = update.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    var startupProject = update.Option(
                        "-s|--startupProject <project>",
                        "The start-up project to use. If omitted, the current project is used");
                    update.HelpOption("-?|-h|--help");
                    update.OnExecute(() => ApplyMigration(migrationName.Value, context.Value(), startupProject.Value()));
                });
            });
            app.Command(
                "dbcontext",
                dbcontext =>
            {
                dbcontext.Description = "Commands to manage your DbContext types";
                dbcontext.HelpOption("-?|-h|--help");
                dbcontext.OnExecute(() => dbcontext.ShowHelp());
                dbcontext.Command(
                    "list",
                    list =>
                {
                    list.Description = "List your DbContext types";
                    list.HelpOption("-?|-h|--help");
                    list.OnExecute(() => ListContexts());
                });
                dbcontext.Command(
                    "scaffold",
                    scaffold =>
                {
                    scaffold.Description = "Scaffolds a DbContext and entity type classes for a specified database";
                    var connection       = scaffold.Argument(
                        "[connection]",
                        "The connection string of the database");
                    var provider = scaffold.Argument(
                        "[provider]",
                        "The provider to use. For example, EntityFramework.SqlServer");
                    scaffold.HelpOption("-?|-h|--help");
                    scaffold.OnExecute(
                        async() =>
                    {
                        if (string.IsNullOrEmpty(connection.Value))
                        {
                            _logger.LogError("Missing required argument '{0}'", connection.Name);

                            scaffold.ShowHelp();

                            return(1);
                        }
                        if (string.IsNullOrEmpty(provider.Value))
                        {
                            _logger.LogError("Missing required argument '{0}'", provider.Name);

                            return(1);
                        }

                        await ReverseEngineerAsync(
                            connection.Value,
                            provider.Value,
                            _applicationShutdown.ShutdownRequested);

                        return(0);
                    });
                });
                dbcontext.Command(
                    "scaffold-templates",
                    scaffoldTemplates =>
                {
                    scaffoldTemplates.Description = "Scaffolds customizable DbContext and entity type templates to use during 'ef dbcontext scaffold'";
                    var provider = scaffoldTemplates.Argument(
                        "[provider]",
                        "The provider to use. For example, EntityFramework.SqlServer");
                    scaffoldTemplates.HelpOption("-?|-h|--help");
                    scaffoldTemplates.OnExecute(
                        () =>
                    {
                        if (string.IsNullOrEmpty(provider.Value))
                        {
                            _logger.LogError("Missing required argument '{0}'", provider.Name);

                            scaffoldTemplates.ShowHelp();

                            return(1);
                        }

                        CustomizeReverseEngineer(provider.Value);

                        return(0);
                    });
                });
            });
            app.Command(
                "migrations",
                migration =>
            {
                migration.Description = "Commands to manage your migrations";
                migration.HelpOption("-?|-h|--help");
                migration.OnExecute(() => migration.ShowHelp());
                migration.Command(
                    "add",
                    add =>
                {
                    add.Description = "Add a new migration";
                    var name        = add.Argument(
                        "[name]",
                        "The name of the migration");
                    var context = add.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    var startupProject = add.Option(
                        "-s|--startupProject <project>",
                        "The start-up project to use. If omitted, the current project is used");
                    add.HelpOption("-?|-h|--help");
                    add.OnExecute(
                        () =>
                    {
                        if (string.IsNullOrEmpty(name.Value))
                        {
                            _logger.LogError("Missing required argument '{0}'", name.Name);

                            add.ShowHelp();

                            return(1);
                        }

                        AddMigration(name.Value, context.Value(), startupProject.Value());

                        return(0);
                    });
                });
                migration.Command(
                    "list",
                    list =>
                {
                    list.Description = "List the migrations";
                    var context      = list.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    var startupProject = list.Option(
                        "-s|--startupProject <project>",
                        "The start-up project to use. If omitted, the current project is used");
                    list.HelpOption("-?|-h|--help");
                    list.OnExecute(() => ListMigrations(context.Value(), startupProject.Value()));
                });
                migration.Command(
                    "remove",
                    remove =>
                {
                    remove.Description = "Remove the last migration";
                    var context        = remove.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    var startupProject = remove.Option(
                        "-s|--startupProject <project>",
                        "The start-up project to use. If omitted, the current project is used");
                    remove.HelpOption("-?|-h|--help");
                    remove.OnExecute(() => RemoveMigration(context.Value(), startupProject.Value()));
                });
                migration.Command(
                    "script",
                    script =>
                {
                    script.Description = "Generate a SQL script from migrations";
                    var from           = script.Argument(
                        "[from]",
                        "The starting migration. If omitted, '0' (the initial database) is used");
                    var to = script.Argument(
                        "[to]",
                        "The ending migration. If omitted, the last migration is used");
                    var output = script.Option(
                        "-o|--output <file>",
                        "The file to write the script to instead of stdout");
                    var idempotent = script.Option(
                        "-i|--idempotent",
                        "Generates an idempotent script that can used on a database at any migration");
                    var context = script.Option(
                        "-c|--context <context>",
                        "The DbContext to use. If omitted, the default DbContext is used");
                    var startupProject = script.Option(
                        "-s|--startupProject <project>",
                        "The start-up project to use. If omitted, the current project is used");
                    script.HelpOption("-?|-h|--help");
                    script.OnExecute(
                        () =>
                    {
                        if (!string.IsNullOrEmpty(to.Value) && string.IsNullOrEmpty(from.Value))
                        {
                            _logger.LogError("Missing required argument '{0}'", from.Name);

                            return(1);
                        }

                        ScriptMigration(
                            from.Value,
                            to.Value,
                            output.Value(),
                            idempotent.HasValue(),
                            context.Value(),
                            startupProject.Value());

                        return(0);
                    });
                });
            });

            return(app.Execute(args));
        }