示例#1
0
        public static async Task <ExecuteResponse> ExecuteAsync(ExecuteRequest request)
        {
            Process process               = null;
            string  shellScriptFile       = null;
            string  shellScriptOutputFile = null;

            try
            {
                // create script file

                shellScriptFile       = Path.Combine(request.WorkingDirectory, $"{Path.GetFileNameWithoutExtension(request.FilePath)}-{Guid.NewGuid().ToString().Split('-').First()}.bat");
                shellScriptOutputFile = $"{shellScriptFile}.output";
                File.WriteAllText(shellScriptFile, $@"""{request.FilePath}"" {request.Arguments} > ""{shellScriptOutputFile}""");

                // run script file

                var commandTask = Cli
                                  .Wrap(shellScriptFile)
                                  .WithValidation(CommandResultValidation.None)
                                  .WithWorkingDirectory(request.WorkingDirectory)
                                  .ExecuteBufferedAsync();

                // enlist process

                try
                {
                    process = Process.GetProcessById(commandTask.ProcessId);
                    if (process != null)
                    {
                        Processes.Add(process);
                    }
                }
                catch
                {
                    // ignore
                }

                // run command

                var result   = await commandTask;
                var exitCode = result.ExitCode;

                // get script output

                var outputList = new List <string>();

                var directOutput = string.Join(Environment.NewLine, new[]
                {
                    result.StandardOutput,
                    Environment.NewLine,
                    result.StandardError
                }
                                               .Where(x => !string.IsNullOrWhiteSpace(x)))
                                   .Trim('\r', '\n')
                                   .Trim();

                if (!string.IsNullOrWhiteSpace(directOutput))
                {
                    outputList.Add(directOutput);
                }

                if (File.Exists(shellScriptOutputFile))
                {
                    var redirectOutput = File.ReadAllText(shellScriptOutputFile)
                                         .Trim('\r', '\n')
                                         .Trim();

                    if (!string.IsNullOrWhiteSpace(redirectOutput))
                    {
                        outputList.Add(redirectOutput);
                    }
                }
                else
                {
                    // There is a problem if the shellScriptOutputFile is not produced
                    exitCode = FAILED_TO_PRODUCE_OUTPUT_FILE_CODE;
                }

                var output = string.Join(Environment.NewLine, outputList)
                             .Trim('\r', '\n')
                             .Trim();

                // return

                return(new ExecuteResponse
                {
                    ExitCode = exitCode,
                    ExitTime = result.ExitTime,
                    RunTime = result.RunTime,
                    StartTime = result.StartTime,
                    Output = output
                });
            }
            finally
            {
                try
                {
                    File.Delete(shellScriptFile);
                }
                catch
                {
                    // ignore
                }

                try
                {
                    File.Delete(shellScriptOutputFile);
                }
                catch
                {
                    // ignore
                }

                try
                {
                    process?.Kill();
                }
                catch
                {
                    // ignore
                }

                try
                {
                    process?.Dispose();
                }
                catch
                {
                    // ignore
                }

                try
                {
                    Processes.Remove(process);
                }
                catch
                {
                    // ignore
                }
            }
        }
        public static async Task <ExecuteResponse> ExecuteAsync(ExecuteRequest request)
        {
            string shellScriptFile       = null;
            string shellScriptOutputFile = null;

            // create script file

            shellScriptFile       = Path.Combine(request.WorkingDirectory, $"{Guid.NewGuid().ToString().Split('-').First()}.bat");
            shellScriptOutputFile = $"{shellScriptFile}.output";
            File.WriteAllText(shellScriptFile, $@"""{request.FilePath}"" {request.Arguments} > ""{shellScriptOutputFile}""");

            // run script file

            var commandTask = Cli
                              .Wrap(shellScriptFile)
                              .WithValidation(CommandResultValidation.None)
                              .WithWorkingDirectory(request.WorkingDirectory)
                              .ExecuteBufferedAsync(CancellationToken);

            BufferedCommandResult result          = null;    // result is null when cancelled
            ExecuteResponse       executeResponse = null;

            try
            {
                result = await commandTask;
            }
            catch (OperationCanceledException)
            {
            }

            if (result != null)
            {
                var exitCode = result.ExitCode;

                // get script output

                var outputList = new List <string>();

                var directOutput = string.Join(Environment.NewLine, new[]
                {
                    result.StandardOutput,
                    Environment.NewLine,
                    result.StandardError
                }
                                               .Where(x => !string.IsNullOrWhiteSpace(x)))
                                   .Trim('\r', '\n')
                                   .Trim();

                if (!string.IsNullOrWhiteSpace(directOutput))
                {
                    outputList.Add(directOutput);
                }

                if (File.Exists(shellScriptOutputFile))
                {
                    var redirectOutput = File.ReadAllText(shellScriptOutputFile)
                                         .Trim('\r', '\n')
                                         .Trim();

                    if (!string.IsNullOrWhiteSpace(redirectOutput))
                    {
                        outputList.Add(redirectOutput);
                    }
                }
                else
                {
                    // There is a problem if the shellScriptOutputFile is not produced
                    exitCode = FAILED_TO_PRODUCE_OUTPUT_FILE_CODE;
                }

                var output = string.Join(Environment.NewLine, outputList)
                             .Trim('\r', '\n')
                             .Trim();

                executeResponse = new ExecuteResponse
                {
                    ExitCode  = exitCode,
                    ExitTime  = result.ExitTime,
                    RunTime   = result.RunTime,
                    StartTime = result.StartTime,
                    Output    = output
                };
            }

            FileSystemInfoDeleteExtensions.TryDelete(shellScriptFile);
            FileSystemInfoDeleteExtensions.TryDelete(shellScriptOutputFile);

            return(executeResponse);
        }