forked from nikhilk/scriptsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCompilerExecTask.cs
More file actions
327 lines (268 loc) · 10 KB
/
Copy pathScriptCompilerExecTask.cs
File metadata and controls
327 lines (268 loc) · 10 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
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
// ScriptCompilerExecTask.cs
// Script#/Core/Build
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using Microsoft.Build;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace ScriptSharp.Tasks {
/// <summary>
/// The Script# MSBuild task corresponding exactly to functionality exposed
/// by the command-line tool.
/// </summary>
public sealed class ScriptCompilerExecTask : Task, IErrorHandler, IStreamSourceResolver {
private string _projectPath;
private ITaskItem[] _references;
private ITaskItem[] _sources;
private ITaskItem[] _resources;
private bool _minimize;
private bool _tests;
private string _defines;
private string _locale;
private string _outputPath;
private ITaskItem _script;
private bool _hasErrors;
public string Defines {
get {
if (_defines == null) {
return String.Empty;
}
return _defines;
}
set {
_defines = value;
}
}
public bool IncludeTests {
get {
return _tests;
}
set {
_tests = value;
}
}
public string Locale {
get {
if (_locale == null) {
return String.Empty;
}
return _locale;
}
set {
_locale = value;
}
}
public bool Minimize {
get {
return _minimize;
}
set {
_minimize = value;
}
}
[Required]
public string OutputPath {
get {
if (_outputPath == null) {
return String.Empty;
}
return _outputPath;
}
set {
_outputPath = value;
}
}
public string ProjectPath {
get {
return _projectPath;
}
set {
_projectPath = value;
}
}
[Required]
public ITaskItem[] References {
get {
return _references;
}
set {
_references = value;
}
}
public ITaskItem[] Resources {
get {
return _resources;
}
set {
_resources = value;
}
}
[Output]
public ITaskItem Script {
get {
return _script;
}
}
[Required]
public ITaskItem[] Sources {
get {
return _sources;
}
set {
_sources = value;
}
}
private bool Compile() {
CompilerOptions options = new CompilerOptions();
options.IncludeTests = IncludeTests;
options.Minimize = Minimize;
options.Defines = GetDefines();
options.References = GetReferences();
options.Sources = GetSources(_sources);
options.Resources = GetResources(_resources);
options.IncludeResolver = this;
ITaskItem scriptTaskItem = new TaskItem(OutputPath);
options.ScriptFile = new TaskItemOutputStreamSource(scriptTaskItem);
string errorMessage = String.Empty;
if (options.Validate(out errorMessage) == false) {
Log.LogError(errorMessage);
return false;
}
ScriptCompiler compiler = new ScriptCompiler(this);
compiler.Compile(options);
if (_hasErrors == false) {
_script = scriptTaskItem;
string projectName = (_projectPath != null) ? Path.GetFileNameWithoutExtension(_projectPath) : String.Empty;
string scriptFileName = Path.GetFileName(scriptTaskItem.ItemSpec);
string scriptPath = Path.GetFullPath(scriptTaskItem.ItemSpec);
Log.LogMessage(MessageImportance.High, "{0} -> {1} ({2})", projectName, scriptFileName, scriptPath);
}
else {
return false;
}
return true;
}
public override bool Execute() {
bool success = false;
try {
success = Compile();
}
catch {
}
return success;
}
private ICollection<string> GetDefines() {
if (Defines.Length == 0) {
return new string[0];
}
return Defines.Split(';');
}
private ICollection<string> GetReferences() {
if (_references == null) {
return new string[0];
}
List<string> references = new List<string>(_references.Length);
foreach (ITaskItem reference in _references) {
// TODO: This is a hack... something in the .net 4 build system causes
// System.Core.dll to get included [sometimes].
// That shouldn't happen... so filter it out here.
if (reference.ItemSpec.EndsWith("System.Core.dll", StringComparison.OrdinalIgnoreCase)) {
continue;
}
references.Add(reference.ItemSpec);
}
return references;
}
private ICollection<IStreamSource> GetResources(IEnumerable<ITaskItem> allResources) {
if (allResources == null) {
return new TaskItemInputStreamSource[0];
}
string locale = Locale;
List<IStreamSource> resources = new List<IStreamSource>();
foreach (ITaskItem resource in allResources) {
string itemLocale = ResourceFile.GetLocale(resource.ItemSpec);
if (String.IsNullOrEmpty(locale) && String.IsNullOrEmpty(itemLocale)) {
resources.Add(new TaskItemInputStreamSource(resource));
}
else if ((String.Compare(locale, itemLocale, StringComparison.OrdinalIgnoreCase) == 0) ||
locale.StartsWith(itemLocale, StringComparison.OrdinalIgnoreCase)) {
// Either the item locale matches, or the item locale is a prefix
// of the locale (eg. we want to include "fr" if the locale specified
// is fr-FR)
resources.Add(new TaskItemInputStreamSource(resource));
}
}
return resources;
}
private ICollection<IStreamSource> GetSources(IEnumerable<ITaskItem> sourceItems) {
if (sourceItems == null) {
return new TaskItemInputStreamSource[0];
}
List<IStreamSource> sources = new List<IStreamSource>();
foreach (ITaskItem sourceItem in sourceItems) {
// TODO: This is a hack... something in the .net 4 build system causes
// generation of an AssemblyAttributes.cs file with fully-qualified
// type names, that we can't handle (this comes from multitargeting),
// and there doesn't seem like a way to turn it off.
if (sourceItem.ItemSpec.EndsWith(".AssemblyAttributes.cs", StringComparison.OrdinalIgnoreCase)) {
continue;
}
sources.Add(new TaskItemInputStreamSource(sourceItem));
}
return sources;
}
#region Implementation of IErrorHandler
void IErrorHandler.ReportError(string errorMessage, string location) {
_hasErrors = true;
int line = 0;
int column = 0;
if (String.IsNullOrEmpty(location) == false) {
if (location.EndsWith(")", StringComparison.Ordinal)) {
int index = location.LastIndexOf("(", StringComparison.Ordinal);
Debug.Assert(index > 0);
string position = location.Substring(index + 1, location.Length - index - 2);
string[] positionParts = position.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
Debug.Assert(positionParts.Length == 2);
Int32.TryParse(positionParts[0], out line);
Int32.TryParse(positionParts[1], out column);
location = location.Substring(0, index);
}
}
Log.LogError(String.Empty, String.Empty, String.Empty, location, line, column, 0, 0, errorMessage);
}
#endregion
#region Implementation of IStreamSourceResolver
IStreamSource IStreamSourceResolver.Resolve(string name) {
if (_projectPath != null) {
string path = Path.Combine(Path.GetDirectoryName(_projectPath), name);
if (File.Exists(path)) {
return new FileInputStreamSource(path, name);
}
}
return null;
}
#endregion
private sealed class TaskItemInputStreamSource : FileInputStreamSource {
public TaskItemInputStreamSource(ITaskItem taskItem)
: base(taskItem.ItemSpec) {
}
public TaskItemInputStreamSource(ITaskItem taskItem, string name)
: base(taskItem.ItemSpec, name) {
}
}
private sealed class TaskItemOutputStreamSource : FileOutputStreamSource {
public TaskItemOutputStreamSource(ITaskItem taskItem)
: base(taskItem.ItemSpec) {
}
public TaskItemOutputStreamSource(ITaskItem taskItem, string name)
: base(taskItem.ItemSpec, name) {
}
}
}
}