-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ProjectInfo.cs
576 lines (506 loc) · 23 KB
/
ProjectInfo.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// 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.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// A class that represents all the arguments necessary to create a new project instance.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
public sealed class ProjectInfo
{
internal ProjectAttributes Attributes { get; }
/// <summary>
/// The unique Id of the project.
/// </summary>
public ProjectId Id => Attributes.Id;
/// <summary>
/// The version of the project.
/// </summary>
public VersionStamp Version => Attributes.Version;
/// <summary>
/// The name of the project. This may differ from the project's filename.
/// </summary>
public string Name => Attributes.Name;
/// <summary>
/// The name of the assembly that this project will create, without file extension.
/// </summary>,
public string AssemblyName => Attributes.AssemblyName;
/// <summary>
/// The language of the project.
/// </summary>
public string Language => Attributes.Language;
/// <summary>
/// The path to the project file or null if there is no project file.
/// </summary>
public string FilePath => Attributes.FilePath;
/// <summary>
/// The path to the output file (module or assembly).
/// </summary>
public string OutputFilePath => Attributes.OutputFilePath;
/// <summary>
/// The path to the reference assembly output file.
/// </summary>
public string OutputRefFilePath => Attributes.OutputRefFilePath;
/// <summary>
/// The default namespace of the project ("" if not defined, which means global namespace),
/// or null if it is unknown or not applicable.
/// </summary>
/// <remarks>
/// Right now VB doesn't have the concept of "default namespace", but we conjure one in workspace
/// by assigning the value of the project's root namespace to it. So various features can choose to
/// use it for their own purpose.
/// In the future, we might consider officially exposing "default namespace" for VB project
/// (e.g. through a "defaultnamespace" msbuild property)
/// </remarks>
internal string DefaultNamespace => Attributes.DefaultNamespace;
/// <summary>
/// True if this is a submission project for interactive sessions.
/// </summary>
public bool IsSubmission => Attributes.IsSubmission;
/// <summary>
/// True if project information is complete. In some workspace hosts, it is possible
/// a project only has partial information. In such cases, a project might not have all
/// information on its files or references.
/// </summary>
internal bool HasAllInformation => Attributes.HasAllInformation;
/// <summary>
/// The initial compilation options for the project, or null if the default options should be used.
/// </summary>
public CompilationOptions CompilationOptions { get; }
/// <summary>
/// The initial parse options for the source code documents in this project, or null if the default options should be used.
/// </summary>
public ParseOptions ParseOptions { get; }
/// <summary>
/// The list of source documents initially associated with the project.
/// </summary>
public IReadOnlyList<DocumentInfo> Documents { get; }
/// <summary>
/// The project references initially defined for the project.
/// </summary>
public IReadOnlyList<ProjectReference> ProjectReferences { get; }
/// <summary>
/// The metadata references initially defined for the project.
/// </summary>
public IReadOnlyList<MetadataReference> MetadataReferences { get; }
/// <summary>
/// The analyzers initially associated with this project.
/// </summary>
public IReadOnlyList<AnalyzerReference> AnalyzerReferences { get; }
/// <summary>
/// The list of non-source documents associated with this project.
/// </summary>
public IReadOnlyList<DocumentInfo> AdditionalDocuments { get; }
/// <summary>
/// Type of the host object.
/// </summary>
public Type HostObjectType { get; }
private ProjectInfo(
ProjectAttributes attributes,
CompilationOptions compilationOptions,
ParseOptions parseOptions,
IEnumerable<DocumentInfo> documents,
IEnumerable<ProjectReference> projectReferences,
IEnumerable<MetadataReference> metadataReferences,
IEnumerable<AnalyzerReference> analyzerReferences,
IEnumerable<DocumentInfo> additionalDocuments,
Type hostObjectType)
{
Attributes = attributes;
CompilationOptions = compilationOptions;
ParseOptions = parseOptions;
Documents = documents.ToImmutableReadOnlyListOrEmpty();
ProjectReferences = projectReferences.ToImmutableReadOnlyListOrEmpty();
MetadataReferences = metadataReferences.ToImmutableReadOnlyListOrEmpty();
AnalyzerReferences = analyzerReferences.ToImmutableReadOnlyListOrEmpty();
AdditionalDocuments = additionalDocuments.ToImmutableReadOnlyListOrEmpty();
HostObjectType = hostObjectType;
}
/// <summary>
/// Create a new instance of a ProjectInfo.
/// </summary>
internal static ProjectInfo Create(
ProjectId id,
VersionStamp version,
string name,
string assemblyName,
string language,
string filePath,
string outputFilePath,
string outputRefFilePath,
string defaultNamespace,
CompilationOptions compilationOptions,
ParseOptions parseOptions,
IEnumerable<DocumentInfo> documents,
IEnumerable<ProjectReference> projectReferences,
IEnumerable<MetadataReference> metadataReferences,
IEnumerable<AnalyzerReference> analyzerReferences,
IEnumerable<DocumentInfo> additionalDocuments,
bool isSubmission,
Type hostObjectType,
bool hasAllInformation)
{
return new ProjectInfo(
new ProjectAttributes(
id,
version,
name,
assemblyName,
language,
filePath,
outputFilePath,
outputRefFilePath,
defaultNamespace,
isSubmission,
hasAllInformation),
compilationOptions,
parseOptions,
documents,
projectReferences,
metadataReferences,
analyzerReferences,
additionalDocuments,
hostObjectType);
}
// 2.7.0 BACKCOMPAT OVERLOAD -- DO NOT TOUCH
/// <summary>
/// Create a new instance of a ProjectInfo.
/// </summary>
public static ProjectInfo Create(
ProjectId id,
VersionStamp version,
string name,
string assemblyName,
string language,
string filePath,
string outputFilePath,
CompilationOptions compilationOptions,
ParseOptions parseOptions,
IEnumerable<DocumentInfo> documents,
IEnumerable<ProjectReference> projectReferences,
IEnumerable<MetadataReference> metadataReferences,
IEnumerable<AnalyzerReference> analyzerReferences,
IEnumerable<DocumentInfo> additionalDocuments,
bool isSubmission,
Type hostObjectType)
{
return Create(
id, version, name, assemblyName, language,
filePath, outputFilePath, outputRefFilePath: null, defaultNamespace: null, compilationOptions, parseOptions,
documents, projectReferences, metadataReferences, analyzerReferences, additionalDocuments,
isSubmission, hostObjectType, hasAllInformation: true);
}
/// <summary>
/// Create a new instance of a ProjectInfo.
/// </summary>
public static ProjectInfo Create(
ProjectId id,
VersionStamp version,
string name,
string assemblyName,
string language,
string filePath = null,
string outputFilePath = null,
CompilationOptions compilationOptions = null,
ParseOptions parseOptions = null,
IEnumerable<DocumentInfo> documents = null,
IEnumerable<ProjectReference> projectReferences = null,
IEnumerable<MetadataReference> metadataReferences = null,
IEnumerable<AnalyzerReference> analyzerReferences = null,
IEnumerable<DocumentInfo> additionalDocuments = null,
bool isSubmission = false,
Type hostObjectType = null,
string outputRefFilePath = null)
{
return Create(
id, version, name, assemblyName, language,
filePath, outputFilePath, outputRefFilePath, defaultNamespace: null, compilationOptions, parseOptions,
documents, projectReferences, metadataReferences, analyzerReferences, additionalDocuments,
isSubmission, hostObjectType, hasAllInformation: true);
}
private ProjectInfo With(
ProjectAttributes attributes = null,
CompilationOptions compilationOptions = null,
ParseOptions parseOptions = null,
IEnumerable<DocumentInfo> documents = null,
IEnumerable<ProjectReference> projectReferences = null,
IEnumerable<MetadataReference> metadataReferences = null,
IEnumerable<AnalyzerReference> analyzerReferences = null,
IEnumerable<DocumentInfo> additionalDocuments = null,
Optional<Type> hostObjectType = default)
{
var newAttributes = attributes ?? Attributes;
var newCompilationOptions = compilationOptions ?? CompilationOptions;
var newParseOptions = parseOptions ?? ParseOptions;
var newDocuments = documents ?? Documents;
var newProjectReferences = projectReferences ?? ProjectReferences;
var newMetadataReferences = metadataReferences ?? MetadataReferences;
var newAnalyzerReferences = analyzerReferences ?? AnalyzerReferences;
var newAdditionalDocuments = additionalDocuments ?? AdditionalDocuments;
var newHostObjectType = hostObjectType.HasValue ? hostObjectType.Value : HostObjectType;
if (newAttributes == Attributes &&
newCompilationOptions == CompilationOptions &&
newParseOptions == ParseOptions &&
newDocuments == Documents &&
newProjectReferences == ProjectReferences &&
newMetadataReferences == MetadataReferences &&
newAnalyzerReferences == AnalyzerReferences &&
newAdditionalDocuments == AdditionalDocuments &&
newHostObjectType == HostObjectType)
{
return this;
}
return new ProjectInfo(
newAttributes,
newCompilationOptions,
newParseOptions,
newDocuments,
newProjectReferences,
newMetadataReferences,
newAnalyzerReferences,
newAdditionalDocuments,
newHostObjectType);
}
public ProjectInfo WithDocuments(IEnumerable<DocumentInfo> documents)
{
return With(documents: documents.ToImmutableReadOnlyListOrEmpty());
}
public ProjectInfo WithAdditionalDocuments(IEnumerable<DocumentInfo> additionalDocuments)
{
return With(additionalDocuments: additionalDocuments.ToImmutableReadOnlyListOrEmpty());
}
public ProjectInfo WithVersion(VersionStamp version)
{
return With(attributes: Attributes.With(version: version));
}
public ProjectInfo WithName(string name)
{
return With(attributes: Attributes.With(name: name));
}
public ProjectInfo WithFilePath(string filePath)
{
return With(attributes: Attributes.With(filePath: filePath));
}
public ProjectInfo WithAssemblyName(string assemblyName)
{
return With(attributes: Attributes.With(assemblyName: assemblyName));
}
public ProjectInfo WithOutputFilePath(string outputFilePath)
{
return With(attributes: Attributes.With(outputPath: outputFilePath));
}
public ProjectInfo WithOutputRefFilePath(string outputRefFilePath)
{
return With(attributes: Attributes.With(outputRefPath: outputRefFilePath));
}
internal ProjectInfo WithDefaultNamespace(string defaultNamespace)
{
return With(attributes: Attributes.With(defaultNamespace: defaultNamespace));
}
public ProjectInfo WithCompilationOptions(CompilationOptions compilationOptions)
{
return With(compilationOptions: compilationOptions);
}
public ProjectInfo WithParseOptions(ParseOptions parseOptions)
{
return With(parseOptions: parseOptions);
}
public ProjectInfo WithProjectReferences(IEnumerable<ProjectReference> projectReferences)
{
return With(projectReferences: projectReferences.ToImmutableReadOnlyListOrEmpty());
}
public ProjectInfo WithMetadataReferences(IEnumerable<MetadataReference> metadataReferences)
{
return With(metadataReferences: metadataReferences.ToImmutableReadOnlyListOrEmpty());
}
public ProjectInfo WithAnalyzerReferences(IEnumerable<AnalyzerReference> analyzerReferences)
{
return With(analyzerReferences: analyzerReferences.ToImmutableReadOnlyListOrEmpty());
}
internal ProjectInfo WithHasAllInformation(bool hasAllInformation)
{
return With(attributes: Attributes.With(hasAllInformation: hasAllInformation));
}
internal string GetDebuggerDisplay()
{
return nameof(ProjectInfo) + " " + Name + (!string.IsNullOrWhiteSpace(FilePath) ? " " + FilePath : "");
}
/// <summary>
/// type that contains information regarding this project itself but
/// no tree information such as document info
/// </summary>
internal class ProjectAttributes : IChecksummedObject, IObjectWritable
{
/// <summary>
/// The unique Id of the project.
/// </summary>
public ProjectId Id { get; }
/// <summary>
/// The version of the project.
/// </summary>
public VersionStamp Version { get; }
/// <summary>
/// The name of the project. This may differ from the project's filename.
/// </summary>
public string Name { get; }
/// <summary>
/// The name of the assembly that this project will create, without file extension.
/// </summary>,
public string AssemblyName { get; }
/// <summary>
/// The language of the project.
/// </summary>
public string Language { get; }
/// <summary>
/// The path to the project file or null if there is no project file.
/// </summary>
public string FilePath { get; }
/// <summary>
/// The path to the output file (module or assembly).
/// </summary>
public string OutputFilePath { get; }
/// <summary>
/// The path to the reference assembly output file.
/// </summary>
public string OutputRefFilePath { get; }
/// <summary>
/// The default namespace of the project.
/// </summary>
public string DefaultNamespace { get; }
/// <summary>
/// True if this is a submission project for interactive sessions.
/// </summary>
public bool IsSubmission { get; }
/// <summary>
/// True if project information is complete. In some workspace hosts, it is possible
/// a project only has partial information. In such cases, a project might not have all
/// information on its files or references.
/// </summary>
public bool HasAllInformation { get; }
public ProjectAttributes(
ProjectId id,
VersionStamp version,
string name,
string assemblyName,
string language,
string filePath,
string outputFilePath,
string outputRefFilePath,
string defaultNamespace,
bool isSubmission,
bool hasAllInformation)
{
Id = id ?? throw new ArgumentNullException(nameof(id));
Name = name ?? throw new ArgumentNullException(nameof(name));
Language = language ?? throw new ArgumentNullException(nameof(language));
AssemblyName = assemblyName ?? throw new ArgumentNullException(nameof(assemblyName));
Version = version;
FilePath = filePath;
OutputFilePath = outputFilePath;
OutputRefFilePath = outputRefFilePath;
DefaultNamespace = defaultNamespace;
IsSubmission = isSubmission;
HasAllInformation = hasAllInformation;
}
public ProjectAttributes With(
VersionStamp? version = default,
string name = null,
string assemblyName = null,
string language = null,
Optional<string> filePath = default,
Optional<string> outputPath = default,
Optional<string> outputRefPath = default,
Optional<string> defaultNamespace = default,
Optional<bool> isSubmission = default,
Optional<bool> hasAllInformation = default)
{
var newVersion = version.HasValue ? version.Value : Version;
var newName = name ?? Name;
var newAssemblyName = assemblyName ?? AssemblyName;
var newLanguage = language ?? Language;
var newFilepath = filePath.HasValue ? filePath.Value : FilePath;
var newOutputPath = outputPath.HasValue ? outputPath.Value : OutputFilePath;
var newOutputRefPath = outputRefPath.HasValue ? outputRefPath.Value : OutputRefFilePath;
var newDefaultNamespace = defaultNamespace.HasValue ? defaultNamespace.Value : DefaultNamespace;
var newIsSubmission = isSubmission.HasValue ? isSubmission.Value : IsSubmission;
var newHasAllInformation = hasAllInformation.HasValue ? hasAllInformation.Value : HasAllInformation;
if (newVersion == Version &&
newName == Name &&
newAssemblyName == AssemblyName &&
newLanguage == Language &&
newFilepath == FilePath &&
newOutputPath == OutputFilePath &&
newOutputRefPath == OutputRefFilePath &&
newDefaultNamespace == DefaultNamespace &&
newIsSubmission == IsSubmission &&
newHasAllInformation == HasAllInformation)
{
return this;
}
return new ProjectAttributes(
Id,
newVersion,
newName,
newAssemblyName,
newLanguage,
newFilepath,
newOutputPath,
newOutputRefPath,
newDefaultNamespace,
newIsSubmission,
newHasAllInformation);
}
bool IObjectWritable.ShouldReuseInSerialization => true;
public void WriteTo(ObjectWriter writer)
{
Id.WriteTo(writer);
// TODO: figure out a way to send version info over as well
// info.Version.WriteTo(writer);
writer.WriteString(Name);
writer.WriteString(AssemblyName);
writer.WriteString(Language);
writer.WriteString(FilePath);
writer.WriteString(OutputFilePath);
writer.WriteString(OutputRefFilePath);
writer.WriteString(DefaultNamespace);
writer.WriteBoolean(IsSubmission);
writer.WriteBoolean(HasAllInformation);
// TODO: once CompilationOptions, ParseOptions, ProjectReference, MetadataReference, AnalyzerReference supports
// serialization, we should include those here as well.
}
public static ProjectAttributes ReadFrom(ObjectReader reader)
{
var projectId = ProjectId.ReadFrom(reader);
// var version = VersionStamp.ReadFrom(reader);
var name = reader.ReadString();
var assemblyName = reader.ReadString();
var language = reader.ReadString();
var filePath = reader.ReadString();
var outputFilePath = reader.ReadString();
var outputRefFilePath = reader.ReadString();
var defaultNamespace = reader.ReadString();
var isSubmission = reader.ReadBoolean();
var hasAllInformation = reader.ReadBoolean();
return new ProjectAttributes(projectId, VersionStamp.Create(), name, assemblyName, language, filePath, outputFilePath, outputRefFilePath, defaultNamespace, isSubmission, hasAllInformation);
}
private Checksum _lazyChecksum;
Checksum IChecksummedObject.Checksum
{
get
{
if (_lazyChecksum == null)
{
_lazyChecksum = Checksum.Create(WellKnownSynchronizationKind.ProjectAttributes, this);
}
return _lazyChecksum;
}
}
}
}
}