-
Notifications
You must be signed in to change notification settings - Fork 3
/
ILLink.cs
197 lines (165 loc) · 6.51 KB
/
ILLink.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
namespace illinkrepro
{
internal class ILLink
{
public abstract record Argument();
public record UnknownArgument(string Value) : Argument
{
public override string ToString() => Value;
}
private record DotnetPathSection(string Path) : Argument
{
public override string ToString() => Path;
}
private record ILLinkPathSection(string Path) : Argument
{
public override string ToString() => Path;
}
public record Root(string AssemblyPath, string? Mode) : Argument
{
public override string ToString() => $"-a {AssemblyPath} {Mode}";
}
public record Reference(string AssemblyPath) : Argument
{
public override string ToString() => $"-reference {AssemblyPath}";
}
public record Out(string Path) : Argument
{
public override string ToString() => $"-out {Path}";
}
public record Descriptor(string Path) : Argument
{
public override string ToString() => $"-x {Path}";
}
public record LinkAttributes(string Path) : Argument
{
public override string ToString() => $"--link-attributes {Path}";
}
public record SearchDirectory(string Path) : Argument
{
public override string ToString() => $"-d {Path}";
}
readonly string _workingPath;
public string DotnetPath { get; private set; }
public string ILLinkPath { get; private set; }
public Argument[] Arguments { get; }
public ILLink(string workingPath, string commandLine)
{
_workingPath = workingPath;
Arguments = Parse(commandLine).ToArray();
if (DotnetPath == null || ILLinkPath == null)
throw new ApplicationException("Invalid ILLink command line");
}
IEnumerable<Argument> Parse(string commandLine)
{
var lines = commandLine.Split(new[] { '\n', '\r' }).Select(l => l.Trim(new[] { '\n', '\r' })).ToList();
var firstLine = lines.FirstOrDefault();
if (firstLine == null)
throw new ApplicationException("Invalid ILLink command line detected");
int firstQuote = firstLine.IndexOf('"');
DotnetPath = firstLine[..firstQuote].Trim(' ');
var firstLineSplit = SplitLine(firstLine[firstQuote..]).ToArray();
if (firstLineSplit.Length < 1)
throw new ApplicationException("Invalid ILLink command line detected");
ILLinkPath = ToPath(firstLineSplit.First());
lines[0] = string.Join(" ", firstLineSplit.Skip(1));
List<string[]> splitLines = new List<string[]>();
foreach (var line in lines)
{
var lineSplit = SplitLine(line).ToArray();
if (lineSplit.Length == 0)
continue;
for (int partIndex = 0; partIndex < lineSplit.Length; partIndex++)
{
if (lineSplit[partIndex].StartsWith("-"))
{
List<string> subparts = new List<string>();
subparts.Add(lineSplit[partIndex]);
int subpartIndex;
for (subpartIndex = partIndex + 1; subpartIndex < lineSplit.Length; subpartIndex++)
{
if (lineSplit[subpartIndex].StartsWith("-"))
{
break;
}
subparts.Add(lineSplit[subpartIndex]);
}
partIndex = subpartIndex - 1;
splitLines.Add(subparts.ToArray());
}
else
{
splitLines.Add(new string[] { lineSplit[partIndex] });
}
}
}
foreach (var lineParts in splitLines)
{
switch (lineParts[0])
{
case "-a":
yield return CreateRoot(lineParts[1], lineParts.Length > 2 ? lineParts[2] : null); break;
case "-reference":
yield return new Reference(ToPath(lineParts[1])); break;
case "-out":
yield return new Out(ToPath(lineParts[1])); break;
case "-x":
yield return new Descriptor(ToPath(lineParts[1])); break;
case "--link-attributes":
yield return new LinkAttributes(ToPath(lineParts[1])); break;
case "-d":
yield return new SearchDirectory(ToPath(lineParts[1])); break;
default:
yield return new UnknownArgument(string.Join(' ', lineParts)); break;
}
}
}
static IEnumerable<string> SplitLine(string line)
{
bool quoted = false;
int start = 0;
for (int i = 0; i < line.Length; i++)
{
switch (line[i])
{
case '"':
quoted = !quoted;
break;
case ' ':
if (!quoted)
{
if (start < i)
yield return line[start..i];
start = i + 1;
}
break;
default:
break;
}
}
if (start < line.Length)
yield return line[start..];
}
Root CreateRoot(string assembly, string? mode)
{
assembly = assembly.Trim('"');
if (File.Exists(assembly))
{
return new Root(ToPath(assembly), mode);
}
string assemblyPath = Path.Combine(_workingPath, assembly);
if (File.Exists(assemblyPath))
{
return new Root(ToPath(assemblyPath), mode);
}
return new Root(assembly, mode);
}
string ToPath(string v)
{
v = v.Trim('"');
if (Path.IsPathRooted(v))
return Path.GetFullPath(v);
return Path.GetFullPath(Path.Combine(_workingPath, v));
}
}
}