static Maybe <Version> GetPython3Version()
        {
            var executable = PythonBootstrapper.FindPythonExecutable();
            var command    = new CommandLine(executable).Argument("--version");
            var capture    = new CaptureCommandOutput();
            var runner     = new CommandLineRunner(
                new SplitCommandOutput(
                    new ConsoleCommandOutput(),
                    new ServiceMessageCommandOutput(new VariableDictionary()),
                    capture));
            var result = runner.Execute(command.Build());

            if (result.ExitCode != 0)
            {
                return(Maybe <Version> .None);
            }

            var allCapturedMessages = capture.AllMessages.Aggregate((a, b) => $"{a}, {b}");
            var pythonVersionMatch  = PythonVersionFinder.Match(allCapturedMessages);

            if (!pythonVersionMatch.Success)
            {
                return(Maybe <Version> .None);
            }

            var major = pythonVersionMatch.Groups[1].Value;
            var minor = pythonVersionMatch.Groups[2].Value;

            return(new Version(int.Parse(major), int.Parse(minor)).AsSome());
        }
示例#2
0
        private static CalamariResult ExecuteScript(IScriptEngine psse, string scriptName, CalamariVariableDictionary variables)
        {
            var capture = new CaptureCommandOutput();
            var runner  = new CommandLineRunner(capture);
            var result  = psse.Execute(new Script(scriptName), variables, runner);

            return(new CalamariResult(result.ExitCode, capture));
        }
        public void ScriptShouldFailIfExecutableDoesNotExist()
        {
            const string executable = "TestingCalamariThisExecutableShouldNeverExist";
            var          output     = new CaptureCommandOutput();
            var          subject    = new CommandLineRunner(output);
            var          result     = subject.Execute(new CommandLineInvocation(executable: executable, arguments: "--version"));

            result.HasErrors.Should().BeTrue();
            output.Errors.Should().Contain(CommandLineRunner.ConstructWin32ExceptionMessage(executable));
        }
        private CalamariResult ExecuteScript(IScriptWrapper wrapper, string scriptName, CalamariVariableDictionary variables)
        {
            var capture = new CaptureCommandOutput();
            var runner  = new CommandLineRunner(capture);

            wrapper.NextWrapper = new TerminalScriptWrapper(new PowerShellScriptEngine());
            var result = wrapper.ExecuteScript(new Script(scriptName), ScriptSyntax.PowerShell, variables, runner, new StringDictionary());

            //var result = psse.Execute(new Script(scriptName), variables, runner);
            return(new CalamariResult(result.ExitCode, capture));
        }
            IEnumerable <string> ExecuteCommandAndReturnOutput(string exe, params string[] arguments)
            {
                var captureCommandOutput = new CaptureCommandOutput();
                var invocation           = new CommandLineInvocation(exe, arguments)
                {
                    EnvironmentVars  = environmentVars,
                    WorkingDirectory = workingDirectory,
                    OutputAsVerbose  = false,
                    OutputToLog      = false,
                    AdditionalInvocationOutputSink = captureCommandOutput
                };

                var result = commandLineRunner.Execute(invocation);

                return(result.ExitCode == 0
                    ? captureCommandOutput.Messages.Where(m => m.Level == Level.Info).Select(m => m.Text).ToArray()
                    : Enumerable.Empty <string>());
            }
            CommandResult ExecuteCommand(CommandLineInvocation invocation)
            {
                invocation.EnvironmentVars  = environmentVars;
                invocation.WorkingDirectory = workingDirectory;
                invocation.OutputAsVerbose  = false;
                invocation.OutputToLog      = false;

                var captureCommandOutput = new CaptureCommandOutput();

                invocation.AdditionalInvocationOutputSink = captureCommandOutput;

                var commandString = invocation.ToString();

                commandString = redactMap.Aggregate(commandString, (current, pair) => current.Replace(pair.Key, pair.Value));
                log.Verbose(commandString);

                var result = commandLineRunner.Execute(invocation);

                foreach (var message in captureCommandOutput.Messages)
                {
                    if (result.ExitCode == 0)
                    {
                        log.Verbose(message.Text);
                        continue;
                    }

                    switch (message.Level)
                    {
                    case Level.Info:
                        log.Verbose(message.Text);
                        break;

                    case Level.Error:
                        log.Error(message.Text);
                        break;
                    }
                }

                return(result);
            }
            /// <summary>
            /// This is a special case for when the invocation results in an error
            /// 1) but is to be expected as a valid scenario; and
            /// 2) we don't want to inform this at an error level when this happens.
            /// </summary>
            /// <param name="invocation"></param>
            /// <returns></returns>
            CommandResult ExecuteCommandWithVerboseLoggingOnly(CommandLineInvocation invocation)
            {
                invocation.EnvironmentVars  = environmentVars;
                invocation.WorkingDirectory = workingDirectory;
                invocation.OutputAsVerbose  = true;
                invocation.OutputToLog      = false;

                var captureCommandOutput = new CaptureCommandOutput();

                invocation.AdditionalInvocationOutputSink = captureCommandOutput;

                var commandString = invocation.ToString();

                commandString = redactMap.Aggregate(commandString, (current, pair) => current.Replace(pair.Key, pair.Value));
                log.Verbose(commandString);

                var result = commandLineRunner.Execute(invocation);

                captureCommandOutput.Messages.ForEach(message => log.Verbose(message.Text));

                return(result);
            }