예제 #1
0
 internal static int RunIlTool(string installPath, string toolFileName, string requiredPaths, string workingDirectory, string settingsName, string arguments, string toolLoggingCode, string verboseLoggingCode, IDllExportNotifier notifier, int timeout, Func <string, bool> suppressErrorOutputLine = null)
 {
     using (notifier.CreateContextName((object)null, toolFileName))
     {
         if (suppressErrorOutputLine != null)
         {
             Func <string, bool> suppressErrorOutputLineCore = suppressErrorOutputLine;
             suppressErrorOutputLine = (Func <string, bool>)(line => {
                 if (line != null)
                 {
                     return(suppressErrorOutputLineCore(line));
                 }
                 return(false);
             });
         }
         else
         {
             suppressErrorOutputLine = (Func <string, bool>)(l => false);
         }
         string fileName         = Path.GetFileName(toolFileName);
         string exePath          = IlParser.GetExePath(fileName, installPath, settingsName);
         string withoutExtension = Path.GetFileNameWithoutExtension(fileName);
         int    num;
         using (Process process = new Process())
         {
             notifier.Notify(-2, toolLoggingCode, Resources.calling_0_with_1_, (object)exePath, (object)arguments);
             ProcessStartInfo processStartInfo = new ProcessStartInfo(exePath, arguments)
             {
                 UseShellExecute        = false,
                 CreateNoWindow         = true,
                 RedirectStandardOutput = true,
                 RedirectStandardError  = true
             };
             if (!string.IsNullOrEmpty(workingDirectory))
             {
                 processStartInfo.WorkingDirectory = Path.GetFullPath(workingDirectory);
             }
             if (!string.IsNullOrEmpty(requiredPaths))
             {
                 processStartInfo.EnvironmentVariables["PATH"] = requiredPaths.Trim(';') + ";" + (processStartInfo.EnvironmentVariables.ContainsKey("PATH") ? processStartInfo.EnvironmentVariables["PATH"] : (string)null);
             }
             process.StartInfo = processStartInfo;
             process.Start();
             Stopwatch     stopwatch      = Stopwatch.StartNew();
             StringBuilder stringBuilder1 = new StringBuilder();
             StringBuilder stringBuilder2 = new StringBuilder();
             Action <IEnumerable <string>, StringBuilder> appendLines = (Action <IEnumerable <string>, StringBuilder>)((lines, sb) => lines.Aggregate <string, StringBuilder>(sb, (Func <StringBuilder, string, StringBuilder>)((r, line) => r.AppendLine(line))));
             Action <StreamReader, StringBuilder>         action1     = (Action <StreamReader, StringBuilder>)((sr, sb) => appendLines(IlParser.EnumerateStreamLines(sr), sb));
             Action <StreamReader, StringBuilder>         action2     = (Action <StreamReader, StringBuilder>)((sr, sb) => appendLines(IlParser.EnumerateStreamLines(sr).Where <string>((Func <string, bool>)(line => !suppressErrorOutputLine(line))), sb));
             while (stopwatch.ElapsedMilliseconds < (long)timeout && !process.HasExited)
             {
                 action1(process.StandardOutput, stringBuilder1);
                 action2(process.StandardError, stringBuilder2);
             }
             bool hasExited = process.HasExited;
             action1(process.StandardOutput, stringBuilder1);
             action2(process.StandardError, stringBuilder2);
             if (hasExited)
             {
                 notifier.Notify(-2, toolLoggingCode, Resources.R_0_1_returned_gracefully, (object)withoutExtension, (object)exePath);
                 int exitCode = process.ExitCode;
                 if (exitCode != 0 || stringBuilder2.Length > 0)
                 {
                     throw new InvalidOperationException((stringBuilder2.Length > 0 ? (object)stringBuilder2 : (object)stringBuilder1).ToString());
                 }
                 if (stringBuilder2.Length > 0)
                 {
                     notifier.Notify(-3, verboseLoggingCode, stringBuilder2.ToString());
                 }
                 if (stringBuilder1.Length > 0)
                 {
                     notifier.Notify(-3, verboseLoggingCode, stringBuilder1.ToString());
                 }
                 num = exitCode;
             }
             else
             {
                 bool      flag      = false;
                 Exception exception = (Exception)null;
                 try
                 {
                     process.Kill();
                     flag = true;
                 }
                 catch (Exception ex)
                 {
                     exception = ex;
                 }
                 if (flag)
                 {
                     throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, Resources.R_0_did_not_return_after_1_ms, new object[2]
                     {
                         (object)withoutExtension,
                         (object)timeout
                     }));
                 }
                 throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, Resources.R_0_did_not_return_after_1_ms_and_it_could_not_be_stopped, (object)withoutExtension, (object)timeout, (object)exception.Message));
             }
         }
         return(num);
     }
 }
예제 #2
0
 protected DllExportNotifierWrapper(IDllExportNotifier notifier)
 {
     Notifier = notifier;
 }