|
4 | 4 | using System;
|
5 | 5 | using System.Diagnostics;
|
6 | 6 | using System.IO;
|
| 7 | +using Microsoft.DotNet.Cli.Utils; |
7 | 8 |
|
8 |
| -class Program |
| 9 | + |
| 10 | +// Get the path to the current executable |
| 11 | +string? exePath = Environment.ProcessPath; |
| 12 | +if (exePath == null) |
9 | 13 | {
|
10 |
| - static int Main(string[] args) |
11 |
| - { |
12 |
| - // Get the path to the current executable |
13 |
| - string? exePath = Process.GetCurrentProcess().MainModule?.FileName; |
14 |
| - if (exePath == null) |
15 |
| - { |
16 |
| - Console.Error.WriteLine("Could not determine the path to the current executable."); |
17 |
| - return 1; |
18 |
| - } |
19 |
| - string? exeDir = Path.GetDirectoryName(exePath); |
20 |
| - if (exeDir == null) |
21 |
| - { |
22 |
| - Console.Error.WriteLine("Could not determine the directory of the current executable."); |
23 |
| - return 1; |
24 |
| - } |
25 |
| - // Path to dotnet in the same directory |
26 |
| - string dotnetPath = Path.Combine(exeDir, "dotnet"); |
| 14 | + Console.Error.WriteLine("Could not determine the path to the current executable."); |
| 15 | + return 1; |
| 16 | +} |
| 17 | +string? exeDir = Path.GetDirectoryName(exePath); |
| 18 | +if (exeDir == null) |
| 19 | +{ |
| 20 | + Console.Error.WriteLine("Could not determine the directory of the current executable."); |
| 21 | + return 1; |
| 22 | +} |
| 23 | +// Path to dotnet in the same directory |
| 24 | +string dotnetPath = Path.Combine(exeDir, "dotnet"); |
27 | 25 | #if WINDOWS
|
28 |
| - dotnetPath += ".exe"; |
| 26 | +dotnetPath += ".exe"; |
29 | 27 | #endif
|
30 |
| - // Build argument list: "dnx" + all args |
31 |
| - string arguments = "dnx"; |
32 |
| - if (args.Length > 0) |
33 |
| - { |
34 |
| - arguments += " " + string.Join(" ", args.Select(a => $"\"{a.Replace("\"", "\\\"")}")); |
35 |
| - } |
36 |
| - var psi = new ProcessStartInfo |
37 |
| - { |
38 |
| - FileName = dotnetPath, |
39 |
| - Arguments = arguments, |
40 |
| - UseShellExecute = false |
41 |
| - }; |
42 |
| - try |
43 |
| - { |
44 |
| - using var process = Process.Start(psi); |
45 |
| - if (process == null) |
46 |
| - { |
47 |
| - Console.Error.WriteLine($"Failed to start process: {dotnetPath}"); |
48 |
| - return 1; |
49 |
| - } |
50 |
| - process.WaitForExit(); |
51 |
| - return process.ExitCode; |
52 |
| - } |
53 |
| - catch (Exception ex) |
54 |
| - { |
55 |
| - Console.Error.WriteLine($"Error launching process: {ex.Message}"); |
56 |
| - return 1; |
57 |
| - } |
| 28 | + |
| 29 | +string argumentsToForward = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(["dnx", ..args]); |
| 30 | + |
| 31 | +var psi = new ProcessStartInfo |
| 32 | +{ |
| 33 | + FileName = dotnetPath, |
| 34 | + Arguments = argumentsToForward, |
| 35 | + UseShellExecute = false |
| 36 | +}; |
| 37 | +try |
| 38 | +{ |
| 39 | + using var process = Process.Start(psi); |
| 40 | + if (process == null) |
| 41 | + { |
| 42 | + Console.Error.WriteLine($"Failed to start process: {dotnetPath}"); |
| 43 | + return 1; |
58 | 44 | }
|
| 45 | + process.WaitForExit(); |
| 46 | + return process.ExitCode; |
| 47 | +} |
| 48 | +catch (Exception ex) |
| 49 | +{ |
| 50 | + Console.Error.WriteLine($"Error launching process: {ex.ToString()}"); |
| 51 | + return 1; |
59 | 52 | }
|
0 commit comments