Datei: NDOEnhancer/NDOEnhancer/ConsoleProcess.cs
Last Commit (7d77e26)
| 1 | // |
| 2 | // Copyright (c) 2002-2022 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Text; |
| 25 | using System.Globalization; |
| 26 | using System.IO; |
| 27 | using System.Threading; |
| 28 | using System.Diagnostics; |
| 29 | using System.Threading.Tasks; |
| 30 | |
| 31 | namespace NDOEnhancer |
| 32 | { |
| 33 | ····/// <summary> |
| 34 | ····/// Zusammenfassung für ConsoleProcess. |
| 35 | ····/// </summary> |
| 36 | ····public class ConsoleProcess |
| 37 | ····{ |
| 38 | ········string stdout = string.Empty; |
| 39 | ········bool verboseMode; |
| 40 | |
| 41 | ········public string Stdout |
| 42 | ········{ |
| 43 | ············get { return stdout; } |
| 44 | ········} |
| 45 | |
| 46 | ········string stderr = string.Empty;········ |
| 47 | ········public string Stderr |
| 48 | ········{ |
| 49 | ············get { return stderr; } |
| 50 | ········} |
| 51 | |
| 52 | ········public ConsoleProcess(bool verboseMode) |
| 53 | ········{ |
| 54 | ············this.verboseMode = verboseMode; |
| 55 | ········} |
| 56 | |
| 57 | ········public int Execute(string exeFileName, string parameters) |
| 58 | ········{ |
| 59 | ············return Execute(exeFileName, parameters, null); |
| 60 | ········} |
| 61 | |
| 62 | ········public int Execute(string exeFileName, string parameters, string workingDirectory) |
| 63 | ········{ |
| 64 | ············if (this.verboseMode) |
| 65 | ················Console.WriteLine("Execute: " + "\"" + exeFileName + "\" " + parameters); |
| 66 | |
| 67 | ············ProcessStartInfo psi = new ProcessStartInfo(exeFileName, parameters); |
| 68 | |
| 69 | ············psi.CreateNoWindow·········· = true; |
| 70 | ············psi.UseShellExecute·········· = false; |
| 71 | ············if (workingDirectory != null) |
| 72 | ················psi.WorkingDirectory······ = workingDirectory; |
| 73 | ············psi.RedirectStandardOutput = true; |
| 74 | ············psi.RedirectStandardError··= true; |
| 75 | |
| 76 | ············//var codePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage; |
| 77 | ············//if (codePage == 1)··// Windows Bug, der 65001 in 1 ersetzt |
| 78 | ············//····codePage = 437; |
| 79 | ············//psi.StandardErrorEncoding = Encoding.GetEncoding( codePage ); |
| 80 | ············//psi.StandardOutputEncoding = Encoding.GetEncoding( codePage ); |
| 81 | |
| 82 | ············Process proc = Process.Start( psi ); |
| 83 | |
| 84 | ············var tasks = new Task[3]; |
| 85 | |
| 86 | ············tasks[0] = proc.StandardError.ReadToEndAsync(); |
| 87 | ············tasks[1] = proc.StandardOutput.ReadToEndAsync(); |
| 88 | ············tasks[2] = proc.WaitForExitAsync(); |
| 89 | |
| 90 | ············Task.WaitAll( tasks ); |
| 91 | ············return proc.ExitCode; |
| 92 | ········} |
| 93 | |
| 94 | ····} |
| 95 | } |
| 96 |
New Commit (4bf6f30)
| 1 | // |
| 2 | // Copyright (c) 2002-2022 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Text; |
| 25 | using System.Globalization; |
| 26 | using System.IO; |
| 27 | using System.Threading; |
| 28 | using System.Diagnostics; |
| 29 | using System.Threading.Tasks; |
| 30 | |
| 31 | namespace NDOEnhancer |
| 32 | { |
| 33 | ····/// <summary> |
| 34 | ····/// Zusammenfassung für ConsoleProcess. |
| 35 | ····/// </summary> |
| 36 | ····public class ConsoleProcess |
| 37 | ····{ |
| 38 | ········string stdout = string.Empty; |
| 39 | ········bool verboseMode; |
| 40 | |
| 41 | ········public string Stdout |
| 42 | ········{ |
| 43 | ············get { return stdout; } |
| 44 | ········} |
| 45 | |
| 46 | ········string stderr = string.Empty;········ |
| 47 | ········public string Stderr |
| 48 | ········{ |
| 49 | ············get { return stderr; } |
| 50 | ········} |
| 51 | |
| 52 | ········public ConsoleProcess(bool verboseMode) |
| 53 | ········{ |
| 54 | ············this.verboseMode = verboseMode; |
| 55 | ········} |
| 56 | |
| 57 | ········/// <summary> |
| 58 | ········/// NuGet doesn't always preserve the Unix file mode while extracting packages. |
| 59 | ········/// Make sure, the native tools (ilasm, ildasm) can be executed. |
| 60 | ········/// </summary> |
| 61 | ········public static void EnsureExecutable( string fileName ) |
| 62 | ········{ |
| 63 | ············if (OperatingSystem.IsWindows() || !File.Exists( fileName )) |
| 64 | ················return; |
| 65 | ············try |
| 66 | ············{ |
| 67 | ················var mode = File.GetUnixFileMode( fileName ); |
| 68 | ················const UnixFileMode exec = UnixFileMode.UserExecute | UnixFileMode.GroupExecute | UnixFileMode.OtherExecute; |
| 69 | ················if ((mode & UnixFileMode.UserExecute) == 0) |
| 70 | ····················File.SetUnixFileMode( fileName, mode | exec ); |
| 71 | ············} |
| 72 | ············catch (Exception ex) |
| 73 | ············{ |
| 74 | ················Console.WriteLine( $"Warning: Can't set the executable flag of {fileName}: {ex.Message}" ); |
| 75 | ············} |
| 76 | ········} |
| 77 | |
| 78 | ········public int Execute(string exeFileName, string parameters) |
| 79 | ········{ |
| 80 | ············return Execute(exeFileName, parameters, null); |
| 81 | ········} |
| 82 | |
| 83 | ········public int Execute(string exeFileName, string parameters, string workingDirectory) |
| 84 | ········{ |
| 85 | ············if (this.verboseMode) |
| 86 | ················Console.WriteLine("Execute: " + "\"" + exeFileName + "\" " + parameters); |
| 87 | |
| 88 | ············ProcessStartInfo psi = new ProcessStartInfo(exeFileName, parameters); |
| 89 | |
| 90 | ············psi.CreateNoWindow·········· = true; |
| 91 | ············psi.UseShellExecute·········· = false; |
| 92 | ············if (workingDirectory != null) |
| 93 | ················psi.WorkingDirectory······ = workingDirectory; |
| 94 | ············psi.RedirectStandardOutput = true; |
| 95 | ············psi.RedirectStandardError··= true; |
| 96 | |
| 97 | ············//var codePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage; |
| 98 | ············//if (codePage == 1)··// Windows Bug, der 65001 in 1 ersetzt |
| 99 | ············//····codePage = 437; |
| 100 | ············//psi.StandardErrorEncoding = Encoding.GetEncoding( codePage ); |
| 101 | ············//psi.StandardOutputEncoding = Encoding.GetEncoding( codePage ); |
| 102 | |
| 103 | ············Process proc = Process.Start( psi ); |
| 104 | |
| 105 | ············var tasks = new Task[3]; |
| 106 | |
| 107 | ············tasks[0] = proc.StandardError.ReadToEndAsync(); |
| 108 | ············tasks[1] = proc.StandardOutput.ReadToEndAsync(); |
| 109 | ············tasks[2] = proc.WaitForExitAsync(); |
| 110 | |
| 111 | ············Task.WaitAll( tasks ); |
| 112 | ············return proc.ExitCode; |
| 113 | ········} |
| 114 | |
| 115 | ····} |
| 116 | } |
| 117 |