Пример #1
0
 private static void RemoveQuotes(EpewOptions options)
 {
     options.Argument         = TrimQuotes(options.Argument);
     options.Program          = TrimQuotes(options.Program);
     options.Workingdirectory = TrimQuotes(options.Workingdirectory);
     options.LogFile          = TrimQuotes(options.LogFile);
     options.ExitCodeFile     = TrimQuotes(options.ExitCodeFile);
     options.ProcessIdFile    = TrimQuotes(options.ProcessIdFile);
     options.StdOutFile       = TrimQuotes(options.StdOutFile);
     options.StdErrFile       = TrimQuotes(options.StdErrFile);
     options.Title            = TrimQuotes(options.Title);
     options.LogNamespace     = TrimQuotes(options.LogNamespace);
 }
Пример #2
0
 private int ProgramExecutionResultHandler(ExternalProgramExecutor externalProgramExecutor, EpewOptions options, Guid executionId, string commandLineExecutionAsString)
 {
     try
     {
         int result;
         if (externalProgramExecutor.ProcessWasAbortedDueToTimeout)
         {
             result = ExitCodeTimeout;
         }
         else
         {
             result = externalProgramExecutor.ExitCode;
         }
         WriteToFile(options.StdOutFile, externalProgramExecutor.AllStdOutLines);
         WriteToFile(options.StdErrFile, externalProgramExecutor.AllStdErrLines);
         WriteNumberToFile(options.Verbosity, executionId, _Title, commandLineExecutionAsString, result, "exit-code", options.ExitCodeFile);
         return(result);
     }
     finally
     {
         externalProgramExecutor.Dispose();
     }
 }
Пример #3
0
        private int HandleSuccessfullyParsedArguments(EpewOptions options)
        {
            Guid executionId = Guid.NewGuid();
            int  result      = ExitCodeNoProgramExecuted;

            try
            {
                RemoveQuotes(options);
                string argumentForExecution;
                if (options.ArgumentIsBase64Encoded)
                {
                    argumentForExecution = new UTF8Encoding(false).GetString(Convert.FromBase64String(options.Argument));
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(options.Argument))
                    {
                        argumentForExecution = string.Empty;
                    }
                    else
                    {
                        argumentForExecution = options.Argument;
                    }
                }
                string workingDirectory;
                if (string.IsNullOrWhiteSpace(options.Workingdirectory))
                {
                    workingDirectory = Directory.GetCurrentDirectory();
                }
                else
                {
                    if (Directory.Exists(options.Workingdirectory))
                    {
                        workingDirectory = options.Workingdirectory;
                    }
                    else
                    {
                        throw new ArgumentException($"The specified workingdirectory '{options.Workingdirectory}' does not exist.");
                    }
                }
                if (string.IsNullOrWhiteSpace(options.Program))
                {
                    throw new ArgumentException($"No program to execute specified.");
                }

                string commandLineExecutionAsString = $"'{workingDirectory}>{options.Program} {argumentForExecution}'";
                if (string.IsNullOrWhiteSpace(options.Title))
                {
                    _Title = $"{ProgramName}: {commandLineExecutionAsString}";
                }
                else
                {
                    _Title = options.Title;
                }
                if (!string.IsNullOrWhiteSpace(options.LogFile))
                {
                    _Log.Configuration.GetLogTarget <LogFile>().Enabled = true;
                    _Log.Configuration.GetLogTarget <LogFile>().File    = options.LogFile;
                }
                foreach (GRYLogTarget target in _Log.Configuration.LogTargets)
                {
                    target.Format = options.AddLogOverhead ? GRYLogLogFormat.GRYLogFormat : GRYLogLogFormat.OnlyMessage;
                }
                string commandLineArguments = Utilities.GetCommandLineArguments();

                _ExternalProgramExecutor = new ExternalProgramExecutor(new ExternalProgramExecutorConfiguration()
                {
                    Program          = options.Program,
                    Argument         = argumentForExecution,
                    WorkingDirectory = workingDirectory,
                    Verbosity        = options.Verbosity,
                    User             = options.User,
                    Password         = options.Password,
                });

                _ExternalProgramExecutor.Run();

                WriteNumberToFile(options.Verbosity, executionId, _Title, commandLineExecutionAsString, _ExternalProgramExecutor.ProcessId, "process-id", options.ProcessIdFile);
                if (options.NotSynchronous)
                {
                    return(2147393804);
                }
                else
                {
                    new Task(() =>
                    {
                        _ExternalProgramExecutor.WaitUntilTerminated();
                        ProgramExecutionResultHandler(_ExternalProgramExecutor, options, executionId, commandLineExecutionAsString);
                    }).Start();
                    result = _ExternalProgramExecutor.ExitCode;
                }
            }
            catch (Exception exception)
            {
                _Log.Log($"Error in {ProgramName}.", exception);
            }
            return(result);
        }