Esempio n. 1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Current.MainWindow = new MainWindow();

            var notify = Services.Get <INotify>();

            notify.ShowMessage("Metatool starting...");

            var logger = Services.Get <ILogger <App> >();

            var scaffolder = new Scaffolder(logger);

            scaffolder.AddToPath(EnvironmentVariableTarget.User);
            scaffolder.AddToPath(EnvironmentVariableTarget.Machine);
            scaffolder.SetupEnvVar();

            ConfigNotify(notify);
            logger.LogInformation($"Registered MetatoolDir: {Environment.GetEnvironmentVariable("MetatoolPath")}");
            logger.LogInformation("Metatool started!");
        }
Esempio n. 2
0
        public int ArgumentsProcess()
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                Name             = "metatool",
                Description      = "tools for Windows",
                ExtendedHelpText = "===Metaseed Metatool==="
            };

            app.HelpOption(inherited: true);

            app.OnExecute(() =>
            {
                // without command
                var application   = new App();
                var pluginManager = Services.GetOrCreate <PluginManager>();
                pluginManager.InitPlugins();
                application.RunApp();
            });

            app.Command("new", configCmd =>
            {
                configCmd.OnExecute(() =>
                {
                    Console.WriteLine("Please specify a subcommand");
                    configCmd.ShowHelp();
                    return(1);
                });

                configCmd.Command("script", c =>
                {
                    c.Description =
                        "Creates a sample script tool along with the files needed to launch and debug the script.";

                    var fileName = c.Argument("name",
                                              "The name of the tool script to be created.").IsRequired(errorMessage: "please set the tool name \nusage: metatool new script <name>");
                    var cwd = c.Option("-dir |--directory <dir>",
                                       "The directory to initialize the tool scripts. Defaults to current directory.",
                                       CommandOptionType.SingleValue).Accepts(v => v.ExistingDirectory());
                    c.HelpOption(HelpOptionTemplate);
                    c.OnExecute(() =>
                    {
                        var scaffolder = new Scaffolder(_logger);
                        scaffolder.InitTemplate(fileName.Value, cwd.Value());
                    });
                });

                configCmd.Command("lib", c =>
                {
                    c.Description =
                        "Creates a sample lib(dll) tool along with the files needed to launch and debug the csharp project.";

                    var fileName = c.Argument("name",
                                              "The name of the tool to be created.");
                    var cwd = c.Option("-dir |--directory <dir>",
                                       "The directory to initialize the tool. Defaults to current directory.",
                                       CommandOptionType.SingleValue).Accepts(v => v.ExistingDirectory());
                    c.HelpOption(HelpOptionTemplate);
                    c.OnExecute(() =>
                    {
                        var scaffolder = new Scaffolder(_logger);
                        scaffolder.InitTemplate(fileName.Value, cwd.Value(), true);
                    });
                });
            });

            app.Command("run", c =>
            {
                c.Description = "run the script or lib with metatool ";

                var fileName = c.Argument("path",
                                          "The name of the tool script to be created.");
                fileName.Validators.Add(new FileNameValidator());

                c.HelpOption(HelpOptionTemplate);
                c.OnExecute(() =>
                {
                    var application = new App();
                    var fullPath    = fileName.Value;
                    if (!File.Exists(fullPath))
                    {
                        fullPath = Path.Combine(Context.CurrentDirectory, fullPath);
                    }

                    if (fullPath.EndsWith(".dll"))
                    {
                        try
                        {
                            var pluginManager = Services.GetOrCreate <PluginManager>();
                            pluginManager.LoadDll(fullPath);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex,
                                             $"Error while loading tool {fullPath}! No tools loaded! Please fix it then restart!");
                        }
                    }
                    else if (fullPath.EndsWith(".csx"))
                    {
                        var assemblyName = Path.GetFileName(Path.GetDirectoryName(fullPath));
                        _logger.LogInformation($"Compile&Run: {fullPath}, {assemblyName}");
                        var pluginManager = Services.GetOrCreate <PluginManager>();
                        pluginManager.BuildReload(fullPath, assemblyName, false);
                    }
                    application.RunApp();
                });
            });

            // windows only
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // on windows we have command to register .csx files to be executed by dotnet-script
                app.Command("register", c =>
                {
                    c.Description = "Register .csx file handler to enable running scripts directly";
                    c.HelpOption(HelpOptionTemplate);
                    c.OnExecute(() =>
                    {
                        var scaffolder = new Scaffolder(_logger);
                        scaffolder.RegisterFileHandler();
                    });
                });
            }


            return(app.Execute(_args));
        }