-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCDPWrapper.cs
More file actions
107 lines (90 loc) · 2.95 KB
/
CodeCDPWrapper.cs
File metadata and controls
107 lines (90 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
class Program
{
static readonly string DefaultCodeRoot = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Programs",
"Microsoft VS Code"
);
static bool IsChildLaunch(string[] originalArgs)
{
return originalArgs.Any(arg => arg.StartsWith("--type=", StringComparison.OrdinalIgnoreCase));
}
static string QuoteArgument(string arg)
{
if (string.IsNullOrEmpty(arg))
{
return "\"\"";
}
if (!arg.Any(ch => char.IsWhiteSpace(ch) || ch == '"'))
{
return arg;
}
return "\"" + arg.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
}
static string TryGetSiblingRealCodePath(string wrapperPath)
{
string wrapperDirectory = Path.GetDirectoryName(wrapperPath);
if (string.IsNullOrWhiteSpace(wrapperDirectory))
{
return null;
}
string siblingRealPath = Path.Combine(wrapperDirectory, "Code.real.exe");
return File.Exists(siblingRealPath) ? siblingRealPath : null;
}
static string ResolveTargetExe(string wrapperPath)
{
string siblingRealPath = TryGetSiblingRealCodePath(wrapperPath);
if (!string.IsNullOrWhiteSpace(siblingRealPath))
{
return siblingRealPath;
}
string managedRealPath = Path.Combine(DefaultCodeRoot, "Code.real.exe");
if (File.Exists(managedRealPath))
{
return managedRealPath;
}
return Path.Combine(DefaultCodeRoot, "Code.exe");
}
static void Main(string[] args)
{
string wrapperPath = Process.GetCurrentProcess().MainModule.FileName;
string targetExe = ResolveTargetExe(wrapperPath);
if (
string.IsNullOrWhiteSpace(targetExe)
|| !File.Exists(targetExe)
|| string.Equals(
Path.GetFullPath(targetExe),
Path.GetFullPath(wrapperPath),
StringComparison.OrdinalIgnoreCase
)
)
{
return;
}
string[] originalArgs = args;
var launchArgs = new List<string>(originalArgs);
bool hasCdpFlag = originalArgs.Any(
a => a.IndexOf("remote-debugging-port", StringComparison.OrdinalIgnoreCase) >= 0
);
if (!IsChildLaunch(originalArgs) && !hasCdpFlag)
{
launchArgs.Insert(0, "--remote-debugging-port=9222");
}
string allArgs = string.Join(" ", launchArgs.Select(QuoteArgument));
try
{
var psi = new ProcessStartInfo(targetExe, allArgs)
{
UseShellExecute = false,
WorkingDirectory = Path.GetDirectoryName(targetExe) ?? DefaultCodeRoot,
};
Process.Start(psi);
}
catch { }
}
}