Esempio n. 1
0
        private static void InitCli(string[] args)
        {
            var packageResolver = new PackageResolver();
            var registery       = new SetupRegistry();
            var cliService      = new CommandLineService();
            CommandLineApplication commandLineApplication = new CommandLineApplication(true)
            {
                Name     = "Rayvarz Setup",
                FullName = "Rayvarz Setup Installer"
            };

            commandLineApplication.Command("install-package", command =>
            {
                command.Description                   = "example: install-package --package BPMS_9603.0.2.0 --jsonConfigFile ./config.json [--jsonConfigBase64 base64Value]";
                CommandOption packageIdCommand        = command.Option("--package", "Setup Package Id", CommandOptionType.SingleValue);
                CommandOption jsonConfigFileCommand   = command.Option("--jsonConfigFile", "Full Path to json config file", CommandOptionType.SingleValue);
                CommandOption jsonConfigBase64Command = command.Option("--jsonConfigBase64", "Base64 encoded content of json config file", CommandOptionType.SingleValue);
                command.OnExecute(() =>
                {
                    if (!packageIdCommand.HasValue() && (!jsonConfigFileCommand.HasValue() && !jsonConfigBase64Command.HasValue()))
                    {
                        command.ShowHelp(null);
                        return(-1);
                    }
                    var manifest = packageResolver.GetPackage();
                    if (manifest == null)
                    {
                        Console.Error.WriteLine(string.Format("{0} does not exists.", (object)packageIdCommand.Value()));
                        return(-1);
                    }

                    string jsonConfig = string.Empty;
                    if (jsonConfigFileCommand.HasValue())
                    {
                        jsonConfig = File.ReadAllText(jsonConfigFileCommand.Value());
                    }
                    else if (jsonConfigBase64Command.HasValue())
                    {
                        jsonConfig = Encoding.UTF8.GetString(Convert.FromBase64String(jsonConfigBase64Command.Value()));
                    }
                    var installationResult = cliService.CommandLineInstall(jsonConfig).Result;
                    if (installationResult)
                    {
                        return(1);
                    }
                    return(-1);
                });
            }, true);
            commandLineApplication.Command("uninstall-package", (Action <CommandLineApplication>)(command =>
            {
                CommandOption packageIdCommand        = command.Option("--package", "Setup Package Id", CommandOptionType.SingleValue);
                CommandOption jsonConfigFileCommand   = command.Option("--jsonConfigFile", "Full Path to json config file", CommandOptionType.SingleValue);
                CommandOption jsonConfigBase64Command = command.Option("--jsonConfigBase64", "Base64 encoded content of json config file", CommandOptionType.SingleValue);
                command.OnExecute(() =>
                {
                    if (!packageIdCommand.HasValue() && (!jsonConfigFileCommand.HasValue() && !jsonConfigBase64Command.HasValue()))
                    {
                        command.ShowHelp((string)null);
                        return(-1);
                    }

                    string jsonConfig = string.Empty;
                    if (jsonConfigFileCommand.HasValue())
                    {
                        jsonConfig = File.ReadAllText(jsonConfigFileCommand.Value());
                    }
                    else if (jsonConfigBase64Command.HasValue())
                    {
                        jsonConfig = Encoding.UTF8.GetString(Convert.FromBase64String(jsonConfigBase64Command.Value()));
                    }
                    var installationResult = cliService.CommandLineDelete(jsonConfig).Result;
                    if (installationResult)
                    {
                        return(1);
                    }
                    return(-1);
                });
            }), true);
            commandLineApplication.Command("update-package", command =>
            {
                CommandOption packageIdCommand        = command.Option("--package", "Setup Package Id", CommandOptionType.SingleValue);
                CommandOption jsonConfigFileCommand   = command.Option("--jsonConfigFile", "Full Path to json config file", CommandOptionType.SingleValue);
                CommandOption jsonConfigBase64Command = command.Option("--jsonConfigBase64", "Base64 encoded content of json config file", CommandOptionType.SingleValue);
                command.OnExecute(() =>
                {
                    if (!packageIdCommand.HasValue() && (!jsonConfigFileCommand.HasValue() && !jsonConfigBase64Command.HasValue()))
                    {
                        command.ShowHelp(null);
                        return(-1);
                    }
                    //InstallPathInfo installPathInfo = registery.InstallPaths.SingleOrDefault<InstallPathInfo>((Func<InstallPathInfo, bool>)(ip => ip.Id == new Guid(installPathId.Value())));
                    //if (registery == null)
                    //{
                    //    Console.WriteLine(string.Format("\r\n'{0}' not found", (object)result));
                    //    return 1;
                    //}
                    //Manifest manifest = packageResolver.GetPackage();


                    string jsonConfig = string.Empty;
                    if (jsonConfigFileCommand.HasValue())
                    {
                        jsonConfig = File.ReadAllText(jsonConfigFileCommand.Value());
                    }
                    else if (jsonConfigBase64Command.HasValue())
                    {
                        jsonConfig = Encoding.UTF8.GetString(Convert.FromBase64String(jsonConfigBase64Command.Value()));
                    }
                    var installationResult = cliService.CommandLineUpdate(jsonConfig).Result;
                    if (installationResult)
                    {
                        return(1);
                    }
                    return(-1);
                });
            }, true);
            try
            {
                commandLineApplication.Execute(args);
            }
            catch (CommandParsingException ex)
            {
                commandLineApplication.ShowHelp(null);
            }
            finally
            {
                FreeConsole();
                //SendKeys.SendWait("{ENTER}");
            }
        }