コード例 #1
0
 public static void ThrowOnFailure(this CommandLineResult result, string message = null)
 {
     if (result.ExitCode != 0)
     {
         throw new CommandLineInvocationException(result, $"{message ?? string.Empty}{Environment.NewLine}{string.Join(Environment.NewLine, result.Error.Concat(result.Output))}");
     }
 }
コード例 #2
0
ファイル: CommandLine.cs プロジェクト: yamachu/try
        public static async Task <CommandLineResult> Execute(
            string command,
            string args,
            DirectoryInfo workingDir = null,
            Budget budget            = null)
        {
            args   = args ?? "";
            budget = budget ?? new Budget();

            var stdOut = new StringBuilder();
            var stdErr = new StringBuilder();

            using (var operation = CheckBudgetAndStartConfirmationLogger(command, args, budget))
                using (var process = StartProcess(
                           command,
                           args,
                           workingDir,
                           output: data =>
                {
                    stdOut.AppendLine(data);
                },
                           error: data =>
                {
                    stdErr.AppendLine(data);
                }))
                {
                    var exitCode = await process.Complete(budget);

                    var output = stdOut.Replace("\r\n", "\n").ToString().Split('\n');

                    var error = stdErr.Replace("\r\n", "\n").ToString().Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    if (error.All(string.IsNullOrWhiteSpace))
                    {
                        error = null;
                    }

                    var result = new CommandLineResult(
                        exitCode: exitCode,
                        output: output,
                        error: error);

                    if (exitCode == 0)
                    {
                        operation.Succeed(
                            "{command} {args} exited with {code}",
                            process.StartInfo.FileName,
                            process.StartInfo.Arguments,
                            process.ExitCode);
                    }
                    else
                    {
                        var ex = new BudgetExceededException(budget);
                        operation.Fail(ex);
                    }

                    return(result);
                }
        }
コード例 #3
0
 public CommandLineInvocationException(CommandLineResult result, string message = null) : base(
         $"{message}{Environment.NewLine}Exit code {result.ExitCode}: {string.Join("\n", result.Error)}".Trim())
 {
 }