-
Notifications
You must be signed in to change notification settings - Fork 54
/
Skc.cs
342 lines (311 loc) · 11.8 KB
/
Skc.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Tasks;
using System.IO;
using Microsoft.Build.Framework;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
//using SharpKit.Build.Tasks.CsbService.Client;
using System.Configuration;
using System.ServiceProcess;
//using System.ServiceModel.Configuration;
//using System.ServiceModel;
namespace SharpKit.Build.Tools
{
public class Skc : ManagedCompiler
{
public Skc()
{
}
protected override string GenerateFullPathToTool()
{
return Path.Combine(ToolPath, ToolExe);
}
private static bool IsLegalIdentifier(string identifier)
{
if (identifier.Length == 0)
{
return false;
}
if (!TokenChar.IsLetter(identifier[0]) && (identifier[0] != '_'))
{
return false;
}
for (int i = 1; i < identifier.Length; i++)
{
char c = identifier[i];
if (((!TokenChar.IsLetter(c) && !TokenChar.IsDecimalDigit(c)) && (!TokenChar.IsConnecting(c) && !TokenChar.IsCombining(c))) && !TokenChar.IsFormatting(c))
{
return false;
}
}
return true;
}
internal string GetDefineConstantsSwitch(string originalDefineConstants)
{
if (originalDefineConstants != null)
{
StringBuilder builder = new StringBuilder();
foreach (string str in originalDefineConstants.Split(new char[] { ',', ';', ' ' }))
{
if (IsLegalIdentifier(str))
{
if (builder.Length > 0)
{
builder.Append(";");
}
builder.Append(str);
}
else if (str.Length > 0)
{
base.Log.LogWarningWithCodeFromResources("Csc.InvalidParameterWarning", new object[] { "/define:", str });
}
}
if (builder.Length > 0)
{
return builder.ToString();
}
}
return null;
}
public ITaskItem[] NoneFiles { get; set; }
public ITaskItem[] ContentFiles { get; set; }
public ITaskItem[] SkcPlugins { get; set; }
protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
{
if (OutputGeneratedFile != null && !String.IsNullOrEmpty(OutputGeneratedFile.ItemSpec))
commandLine.AppendSwitchIfNotNull("/outputgeneratedfile:", OutputGeneratedFile);
commandLine.AppendSwitchUnquotedIfNotNull("/define:", this.GetDefineConstantsSwitch(base.DefineConstants));
this.AddReferencesToCommandLine(commandLine);
base.AddResponseFileCommands(commandLine);
if (ResponseFiles != null)
{
foreach (ITaskItem item in ResponseFiles)
{
commandLine.AppendSwitchIfNotNull("@", item.ItemSpec);
}
}
if (ContentFiles != null)
{
foreach (var file in ContentFiles)
{
commandLine.AppendSwitchIfNotNull("/contentfile:", file.ItemSpec);
}
}
if (NoneFiles != null)
{
foreach (var file in NoneFiles)
{
commandLine.AppendSwitchIfNotNull("/nonefile:", file.ItemSpec);
}
}
if (SkcPlugins != null)
{
foreach (var file in SkcPlugins)
{
commandLine.AppendSwitchIfNotNull("/plugin:", file.ItemSpec);
}
}
if (SkcRebuild)
commandLine.AppendSwitch("/rebuild");
if (UseBuildService)
{
Log.LogMessage("CurrentDirectory is: " + Directory.GetCurrentDirectory());
commandLine.AppendSwitchIfNotNull("/dir:", Directory.GetCurrentDirectory());
}
commandLine.AppendSwitchIfNotNull("/TargetFrameworkVersion:", TargetFrameworkVersion);
}
private void AddReferencesToCommandLine(CommandLineBuilderExtension commandLine)
{
if ((base.References != null) && (base.References.Length != 0))
{
foreach (ITaskItem item in base.References)
{
string metadata = item.GetMetadata("Aliases");
if ((metadata == null) || (metadata.Length == 0))
{
commandLine.AppendSwitchIfNotNull("/reference:", item.ItemSpec);
}
else
{
foreach (string str2 in metadata.Split(new char[] { ',' }))
{
string str3 = str2.Trim();
if (str2.Length != 0)
{
if (str3.IndexOfAny(new char[] { ',', ' ', ';', '"' }) != -1)
{
throw new ArgumentException("Csc.AssemblyAliasContainsIllegalCharacters" + item.ItemSpec + str3);
}
if (string.Compare("global", str3, StringComparison.OrdinalIgnoreCase) == 0)
{
commandLine.AppendSwitchIfNotNull("/reference:", item.ItemSpec);
}
else
{
throw new NotImplementedException("TODO: Implement");
//commandLine.AppendSwitchAliased("/reference:", str3, item.ItemSpec);
}
}
}
}
}
}
}
protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
base.LogEventsFromTextOutput(singleLine, messageImportance);
}
protected override MessageImportance StandardOutputLoggingImportance
{
get
{
return MessageImportance.High;
}
}
[Output]
public ITaskItem OutputGeneratedFile { get; set; }
private string GetTemporaryResponseFile(string responseFileCommands, out string responseFileSwitch)
{
string path = null;
responseFileSwitch = null;
if (!string.IsNullOrEmpty(responseFileCommands))
{
try
{
path = Path.GetTempFileName();
}
catch (IOException exception)
{
throw new IOException("Shared.FailedCreatingTempFile", exception);
}
using (StreamWriter writer = new StreamWriter(path, false, this.ResponseFileEncoding))
{
writer.Write(responseFileCommands);
}
responseFileSwitch = this.GetResponseFileSwitch(path);
}
return path;
}
public bool UseHostCompilerIfAvailable { get; set; }
public string TargetFrameworkVersion { get; set; }
public bool UseBuildService { get; set; }
public bool SkcRebuild { get; set; }
string OutputGeneratedDir;
string AssemblyFilenameWithoutExtension;
bool DetectBuildService()
{
try
{
var x = new CompilerServiceClient();
x.Test();
return true;
}
catch// (Exception e)
{
return false;
}
//var sc = new ServiceController("SharpKit");
//if (sc.Status == ServiceControllerStatus.Running)
// return true;
//return false;
//var service = Process.GetProcessesByName("skc5").Where(t => t.Id != Process.GetCurrentProcess().Id).FirstOrDefault();
//return service != null;
}
public override bool Execute()
{
UseBuildService = DetectBuildService();
var outputAssembly = OutputAssembly.ItemSpec;
OutputGeneratedDir = Path.GetDirectoryName(outputAssembly);
AssemblyFilenameWithoutExtension = Path.GetFileNameWithoutExtension(outputAssembly);
OutputAssembly.ItemSpec = Path.Combine(OutputGeneratedDir, Path.GetFileName(outputAssembly));
if (UseBuildService)
{
var ext = new CommandLineBuilderExtension();
//var args = new CompilerToolArgs();
AddResponseFileCommands(ext);
var res = new CompilerServiceClient().Compile(new CompileRequest { CommandLineArgs = ext.ToString() });
foreach (var s in res.Output)
{
LogEventsFromTextOutput(s, MessageImportance.High);
}
return res.ExitCode == 0;
}
else
{
var success = base.Execute();
return success;
}
}
protected override bool CallHostObjectToExecute()
{
return base.CallHostObjectToExecute();
}
protected override string ToolName
{
get
{
return "skc.exe";
}
}
}
internal static class TokenChar
{
// Methods
internal static bool IsCombining(char c)
{
UnicodeCategory unicodeCategory = char.GetUnicodeCategory(c);
if ((unicodeCategory != UnicodeCategory.NonSpacingMark) && (unicodeCategory != UnicodeCategory.SpacingCombiningMark))
{
return false;
}
return true;
}
internal static bool IsConnecting(char c)
{
return (char.GetUnicodeCategory(c) == UnicodeCategory.ConnectorPunctuation);
}
internal static bool IsDecimalDigit(char c)
{
return (char.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber);
}
internal static bool IsFormatting(char c)
{
return (char.GetUnicodeCategory(c) == UnicodeCategory.Format);
}
internal static bool IsHexDigit(char c)
{
if ((((c < '0') || (c > '9')) && ((c < 'A') || (c > 'F'))) && ((c < 'a') || (c > 'f')))
{
return false;
}
return true;
}
internal static bool IsLetter(char c)
{
UnicodeCategory unicodeCategory = char.GetUnicodeCategory(c);
if ((((unicodeCategory != UnicodeCategory.UppercaseLetter) && (unicodeCategory != UnicodeCategory.LowercaseLetter)) && ((unicodeCategory != UnicodeCategory.TitlecaseLetter) && (unicodeCategory != UnicodeCategory.ModifierLetter))) && ((unicodeCategory != UnicodeCategory.OtherLetter) && (unicodeCategory != UnicodeCategory.LetterNumber)))
{
return false;
}
return true;
}
internal static bool IsNewLine(char c)
{
if (((c != '\r') && (c != '\n')) && (c != '\u2028'))
{
return (c == '\u2029');
}
return true;
}
internal static bool IsOctalDigit(char c)
{
return ((c >= '0') && (c <= '7'));
}
}
}