Skip to content

Commit 4edf4ff

Browse files
committed
Copilot: Write dnx shim code
Copilot prompt: Add code to #file:'dnx.cs' to launch a new process. It should launch the dotnet executable in the same directory as the current program. It should pass "dnx" as the first argument, and then add all the other arguments that were passed to the current program.
1 parent a96142c commit 4edf4ff

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/Layout/dnx/dnx.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
8+
class Program
9+
{
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");
27+
#if WINDOWS
28+
dotnetPath += ".exe";
29+
#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+
}
58+
}
59+
}

0 commit comments

Comments
 (0)