Repository navigation
Expand file tree
/
Copy pathScriptOptions.cs
More file actions
489 lines (440 loc) · 17.7 KB
/
Copy pathScriptOptions.cs
File metadata and controls
489 lines (440 loc) · 17.7 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// Options for creating and running scripts.
/// </summary>
public class ScriptOptions
{
private readonly ImmutableArray<MetadataReference> _references;
private readonly ImmutableArray<string> _namespaces;
private readonly AssemblyReferenceResolver _referenceResolver;
private readonly bool _isCollectible;
private readonly bool _isInteractive;
public ScriptOptions()
: this(ImmutableArray<MetadataReference>.Empty,
ImmutableArray<string>.Empty,
new AssemblyReferenceResolver(GacFileResolver.Default, MetadataFileReferenceProvider.Default),
isInteractive: true,
isCollectible: false)
{
}
public static readonly ScriptOptions Default;
static ScriptOptions()
{
var paths = ImmutableArray.Create(RuntimeEnvironment.GetRuntimeDirectory());
Default = new ScriptOptions()
.WithReferences(typeof(int).Assembly)
.WithNamespaces("System")
.WithSearchPaths(paths);
}
private ScriptOptions(
ImmutableArray<MetadataReference> references,
ImmutableArray<string> namespaces,
AssemblyReferenceResolver referenceResolver,
bool isInteractive,
bool isCollectible)
{
_references = references;
_namespaces = namespaces;
_referenceResolver = referenceResolver;
_isInteractive = isInteractive;
_isCollectible = isCollectible;
}
/// <summary>
/// The set of <see cref="MetadataReference"/>'s used by the script.
/// </summary>
public ImmutableArray<MetadataReference> References
{
get { return _references; }
}
/// <summary>
/// The namespaces automatically imported by the script.
/// </summary>
public ImmutableArray<string> Namespaces
{
get { return _namespaces; }
}
/// <summary>
/// The paths used when searching for references.
/// </summary>
public ImmutableArray<string> SearchPaths
{
get { return _referenceResolver.PathResolver.SearchPaths; }
}
/// <summary>
/// The base directory used when searching for references.
/// </summary>
public string BaseDirectory
{
get { return _referenceResolver.PathResolver.BaseDirectory; }
}
/// <summary>
/// The <see cref="MetadataFileReferenceProvider"/> scripts will use to translate assembly names into metadata file paths. (#r syntax)
/// </summary>
public MetadataReferenceResolver ReferenceResolver
{
get { return _referenceResolver; }
}
// TODO:
internal AssemblyReferenceResolver AssemblyResolver
{
get { return _referenceResolver; }
}
internal MetadataFileReferenceResolver FileReferenceResolver
{
get { return _referenceResolver.PathResolver; }
}
/// <summary>
/// True if the script is interactive.
/// Interactive scripts may contain a final expression whose value is returned when the script is run.
/// </summary>
public bool IsInteractive
{
get { return _isInteractive; }
}
internal bool IsCollectible
{
get { return _isCollectible; }
}
private ScriptOptions With(
Optional<ImmutableArray<MetadataReference>> references = default(Optional<ImmutableArray<MetadataReference>>),
Optional<ImmutableArray<string>> namespaces = default(Optional<ImmutableArray<string>>),
Optional<AssemblyReferenceResolver> resolver = default(Optional<AssemblyReferenceResolver>),
Optional<bool> isInteractive = default(Optional<bool>),
Optional<bool> isCollectible = default(Optional<bool>))
{
var newReferences = references.HasValue ? references.Value : _references;
var newNamespaces = namespaces.HasValue ? namespaces.Value : _namespaces;
var newResolver = resolver.HasValue ? resolver.Value : _referenceResolver;
var newIsInteractive = isInteractive.HasValue ? isInteractive.Value : _isInteractive;
var newIsCollectible = isCollectible.HasValue ? isCollectible.Value : _isCollectible;
if (newReferences == _references &&
newNamespaces == _namespaces &&
newResolver == _referenceResolver &&
newIsInteractive == _isInteractive &&
newIsCollectible == _isCollectible)
{
return this;
}
else
{
return new ScriptOptions(newReferences, newNamespaces, newResolver, newIsInteractive, newIsCollectible);
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(ImmutableArray<MetadataReference> references)
{
return With(references: references.IsDefault ? ImmutableArray<MetadataReference>.Empty : references);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(IEnumerable<MetadataReference> references)
{
return WithReferences(references != null ? references.ToImmutableArray() : ImmutableArray<MetadataReference>.Empty);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(params MetadataReference[] references)
{
return WithReferences((IEnumerable<MetadataReference>)references);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(IEnumerable<MetadataReference> references)
{
if (_references == null)
{
return this;
}
else
{
return this.WithReferences(this.References.AddRange(references.Where(r => r != null && !this.References.Contains(r))));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(params MetadataReference[] references)
{
return AddReferences((IEnumerable<MetadataReference>)references);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(IEnumerable<System.Reflection.Assembly> assemblies)
{
if (assemblies == null)
{
return WithReferences((IEnumerable<MetadataReference>)null);
}
else
{
return WithReferences(assemblies.Select(MetadataReference.CreateFromAssembly));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(params System.Reflection.Assembly[] assemblies)
{
return WithReferences((IEnumerable<System.Reflection.Assembly>)assemblies);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(IEnumerable<System.Reflection.Assembly> assemblies)
{
if (assemblies == null)
{
return this;
}
else
{
return AddReferences(assemblies.Select(MetadataReference.CreateFromAssembly));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(params System.Reflection.Assembly[] assemblies)
{
return AddReferences((IEnumerable<System.Reflection.Assembly>)assemblies);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(IEnumerable<string> references)
{
if (references == null)
{
return WithReferences(ImmutableArray<MetadataReference>.Empty);
}
else
{
return WithReferences(references.Where(name => name != null).Select(name => ResolveReference(name)));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the references changed.
/// </summary>
public ScriptOptions WithReferences(params string[] references)
{
return WithReferences((IEnumerable<string>)references);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(IEnumerable<string> references)
{
if (references == null)
{
return this;
}
else
{
return AddReferences(references.Where(name => name != null).Select(name => ResolveReference(name)));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with references added.
/// </summary>
public ScriptOptions AddReferences(params string[] references)
{
return AddReferences((IEnumerable<string>)references);
}
private MetadataReference ResolveReference(string assemblyDisplayNameOrPath)
{
// TODO:
string fullPath = _referenceResolver.PathResolver.ResolveReference(assemblyDisplayNameOrPath, baseFilePath: null);
if (fullPath == null)
{
throw new System.IO.FileNotFoundException(ScriptingResources.AssemblyNotFound, assemblyDisplayNameOrPath);
}
return _referenceResolver.Provider.GetReference(fullPath, MetadataReferenceProperties.Assembly);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the namespaces changed.
/// </summary>
public ScriptOptions WithNamespaces(ImmutableArray<string> namespaces)
{
return With(namespaces: namespaces.IsDefault ? ImmutableArray<string>.Empty : namespaces);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the namespaces changed.
/// </summary>
public ScriptOptions WithNamespaces(IEnumerable<string> namespaces)
{
return WithNamespaces(namespaces != null ? namespaces.ToImmutableArray() : ImmutableArray<string>.Empty);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the namespaces changed.
/// </summary>
public ScriptOptions WithNamespaces(params string[] namespaces)
{
return WithNamespaces((IEnumerable<string>)namespaces);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with namespaces added.
/// </summary>
public ScriptOptions AddNamespaces(IEnumerable<string> namespaces)
{
if (namespaces == null)
{
return this;
}
else
{
return this.WithNamespaces(this.Namespaces.AddRange(namespaces.Where(n => n != null && !this.Namespaces.Contains(n))));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with namespaces added.
/// </summary>
public ScriptOptions AddNamespaces(params string[] namespaces)
{
return AddNamespaces((IEnumerable<string>)namespaces);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the search paths changed.
/// </summary>
public ScriptOptions WithSearchPaths(IEnumerable<string> searchPaths)
{
if (this.SearchPaths.SequenceEqual(searchPaths))
{
return this;
}
else
{
// TODO:
var gacResolver = _referenceResolver.PathResolver as GacFileResolver;
if (gacResolver != null)
{
return With(resolver: new AssemblyReferenceResolver(
new GacFileResolver(
searchPaths,
gacResolver.BaseDirectory,
gacResolver.Architectures,
gacResolver.PreferredCulture),
_referenceResolver.Provider));
}
else
{
return With(resolver: new AssemblyReferenceResolver(
new MetadataFileReferenceResolver(
searchPaths,
_referenceResolver.PathResolver.BaseDirectory),
_referenceResolver.Provider));
}
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the search paths changed.
/// </summary>
public ScriptOptions WithSearchPaths(params string[] searchPaths)
{
return WithSearchPaths((IEnumerable<string>)searchPaths);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with search paths added.
/// </summary>
public ScriptOptions AddSearchPaths(params string[] searchPaths)
{
return AddSearchPaths((IEnumerable<string>)searchPaths);
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with search paths added.
/// </summary>
public ScriptOptions AddSearchPaths(IEnumerable<string> searchPaths)
{
if (searchPaths == null)
{
return this;
}
else
{
return WithSearchPaths(this.SearchPaths.AddRange(searchPaths.Where(s => s != null && !this.SearchPaths.Contains(s))));
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the base directory changed.
/// </summary>
public ScriptOptions WithBaseDirectory(string baseDirectory)
{
if (this.BaseDirectory == baseDirectory)
{
return this;
}
else
{
// TODO:
var gacResolver = _referenceResolver.PathResolver as GacFileResolver;
if (gacResolver != null)
{
return With(resolver: new AssemblyReferenceResolver(
new GacFileResolver(
_referenceResolver.PathResolver.SearchPaths,
baseDirectory,
gacResolver.Architectures,
gacResolver.PreferredCulture),
_referenceResolver.Provider));
}
else
{
return With(resolver: new AssemblyReferenceResolver(
new MetadataFileReferenceResolver(
_referenceResolver.PathResolver.SearchPaths,
baseDirectory),
_referenceResolver.Provider));
}
}
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the reference resolver specified.
/// </summary>
internal ScriptOptions WithReferenceResolver(MetadataFileReferenceResolver resolver)
{
if (resolver == _referenceResolver.PathResolver)
{
return this;
}
return With(resolver: new AssemblyReferenceResolver(resolver, _referenceResolver.Provider));
}
/// <summary>
/// Creates a new <see cref="ScriptOptions"/> with the reference provider specified.
/// </summary>
internal ScriptOptions WithReferenceProvider(MetadataFileReferenceProvider provider)
{
if (provider == _referenceResolver.Provider)
{
return this;
}
return With(resolver: new AssemblyReferenceResolver(_referenceResolver.PathResolver, provider));
}
/// <summary>
/// Create a new <see cref="ScriptOptions"/> with the interactive state specified.
/// Interactive scripts may contain a final expression whose value is returned when the script is run.
/// </summary>
public ScriptOptions WithIsInteractive(bool isInteractive)
{
return With(isInteractive: isInteractive);
}
internal ScriptOptions WithIsCollectible(bool isCollectible)
{
return With(isCollectible: isCollectible);
}
}
}