-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support variables in #:project directives
#51108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
bc17aea
7a28231
bfba2dc
b3dc5e4
2a0f51a
ffa9443
4194017
f829602
9fd4539
9f33382
dd92781
be97a18
8987eb1
5c7334a
422beed
fdd1d0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| using System.Collections.Immutable; | ||
| using System.Collections.ObjectModel; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Security; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
@@ -164,14 +165,26 @@ public VirtualProjectBuildingCommand( | |
| /// </summary> | ||
| public bool NoWriteBuildMarkers { get; init; } | ||
|
|
||
| private SourceFile EntryPointSourceFile | ||
| { | ||
| get | ||
| { | ||
| if (field == default) | ||
| { | ||
| field = SourceFile.Load(EntryPointFileFullPath); | ||
| } | ||
|
|
||
| return field; | ||
| } | ||
| } | ||
|
|
||
| public ImmutableArray<CSharpDirective> Directives | ||
| { | ||
| get | ||
| { | ||
| if (field.IsDefault) | ||
| { | ||
| var sourceFile = SourceFile.Load(EntryPointFileFullPath); | ||
| field = FindDirectives(sourceFile, reportAllErrors: false, DiagnosticBag.ThrowOnFirst()); | ||
| field = FindDirectives(EntryPointSourceFile, reportAllErrors: false, DiagnosticBag.ThrowOnFirst()); | ||
| Debug.Assert(!field.IsDefault); | ||
| } | ||
|
|
||
|
|
@@ -1047,6 +1060,23 @@ public ProjectInstance CreateProjectInstance(ProjectCollection projectCollection | |
| private ProjectInstance CreateProjectInstance( | ||
| ProjectCollection projectCollection, | ||
| Action<IDictionary<string, string>>? addGlobalProperties) | ||
| { | ||
| var project = CreateProjectInstance(projectCollection, Directives, addGlobalProperties); | ||
|
|
||
| var directives = EvaluateDirectives(project, Directives, EntryPointSourceFile, DiagnosticBag.ThrowOnFirst()); | ||
| if (directives != Directives) | ||
| { | ||
| Directives = directives; | ||
| project = CreateProjectInstance(projectCollection, directives, addGlobalProperties); | ||
| } | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return project; | ||
| } | ||
|
|
||
| private ProjectInstance CreateProjectInstance( | ||
| ProjectCollection projectCollection, | ||
| ImmutableArray<CSharpDirective> directives, | ||
| Action<IDictionary<string, string>>? addGlobalProperties) | ||
| { | ||
| var projectRoot = CreateProjectRootElement(projectCollection); | ||
|
|
||
|
|
@@ -1069,7 +1099,7 @@ ProjectRootElement CreateProjectRootElement(ProjectCollection projectCollection) | |
| var projectFileWriter = new StringWriter(); | ||
| WriteProjectFile( | ||
| projectFileWriter, | ||
| Directives, | ||
| directives, | ||
| isVirtualProject: true, | ||
| targetFilePath: EntryPointFileFullPath, | ||
| artifactsPath: ArtifactsPath, | ||
|
|
@@ -1589,6 +1619,28 @@ static bool Fill(ref WhiteSpaceInfo info, in SyntaxTriviaList triviaList, int in | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If there are any <c>#:project</c> <paramref name="directives"/>, expand <c>$()</c> in them and then resolve the project paths. | ||
| /// </summary> | ||
| public static ImmutableArray<CSharpDirective> EvaluateDirectives( | ||
| ProjectInstance? project, | ||
| ImmutableArray<CSharpDirective> directives, | ||
| SourceFile sourceFile, | ||
| DiagnosticBag diagnostics) | ||
| { | ||
| if (directives.OfType<CSharpDirective.Project>().Any()) | ||
| { | ||
| return directives | ||
| .Select(d => d is CSharpDirective.Project p | ||
| ? (project is null ? p : p.WithName(project.ExpandString(p.Name))) | ||
| .ResolveProjectPath(sourceFile, diagnostics) | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : d) | ||
| .ToImmutableArray(); | ||
| } | ||
|
|
||
| return directives; | ||
| } | ||
|
|
||
| public static SourceText? RemoveDirectivesFromFile(ImmutableArray<CSharpDirective> directives, SourceText text) | ||
| { | ||
| if (directives.Length == 0) | ||
|
|
@@ -1867,8 +1919,31 @@ public sealed class Package(in ParseInfo info) : Named(info) | |
| /// <summary> | ||
| /// <c>#:project</c> directive. | ||
| /// </summary> | ||
| public sealed class Project(in ParseInfo info) : Named(info) | ||
| public sealed class Project : Named | ||
| { | ||
| [SetsRequiredMembers] | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public Project(in ParseInfo info, string name) : base(info) | ||
| { | ||
| Name = name; | ||
| OriginalName = name; | ||
| UnresolvedName = name; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Preserved across <see cref="WithName"/> calls. | ||
| /// </summary> | ||
| public string OriginalName { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Preserved across <see cref="ResolveProjectPath"/> calls. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When MSBuild <c>$(..)</c> vars are expanded via <see cref="WithName"/>, | ||
| /// the <see cref="UnresolvedName"/> will be expanded, but not resolved | ||
| /// (i.e., can be pointing to project directory or file). | ||
| /// </remarks> | ||
| public string UnresolvedName { get; init; } | ||
|
||
|
|
||
| public static new Project? Parse(in ParseContext context) | ||
| { | ||
| var directiveText = context.DirectiveText; | ||
|
|
@@ -1878,11 +1953,32 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| return context.Diagnostics.AddError<Project?>(context.SourceFile, context.Info.Span, string.Format(CliCommandStrings.MissingDirectiveName, directiveKind)); | ||
| } | ||
|
|
||
| return new Project(context.Info, directiveText); | ||
| } | ||
|
|
||
| public Project WithName(string name, bool preserveUnresolvedName = false) | ||
| { | ||
| return name == Name | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ? this | ||
| : new Project(Info, name) | ||
| { | ||
| OriginalName = OriginalName, | ||
| UnresolvedName = preserveUnresolvedName ? UnresolvedName : name, | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If the directive points to a directory, returns a new directive pointing to the corresponding project file. | ||
| /// </summary> | ||
| public Project ResolveProjectPath(SourceFile sourceFile, DiagnosticBag diagnostics) | ||
| { | ||
| var directiveText = Name; | ||
|
|
||
| try | ||
| { | ||
| // If the path is a directory like '../lib', transform it to a project file path like '../lib/lib.csproj'. | ||
| // Also normalize blackslashes to forward slashes to ensure the directive works on all platforms. | ||
| var sourceDirectory = Path.GetDirectoryName(context.SourceFile.Path) ?? "."; | ||
| // Also normalize backslashes to forward slashes to ensure the directive works on all platforms. | ||
| var sourceDirectory = Path.GetDirectoryName(sourceFile.Path) ?? "."; | ||
| var resolvedProjectPath = Path.Combine(sourceDirectory, directiveText.Replace('\\', '/')); | ||
| if (Directory.Exists(resolvedProjectPath)) | ||
| { | ||
|
|
@@ -1900,18 +1996,10 @@ public sealed class Project(in ParseInfo info) : Named(info) | |
| } | ||
| catch (GracefulException e) | ||
| { | ||
| context.Diagnostics.AddError(context.SourceFile, context.Info.Span, string.Format(CliCommandStrings.InvalidProjectDirective, e.Message), e); | ||
| diagnostics.AddError(sourceFile, Info.Span, string.Format(CliCommandStrings.InvalidProjectDirective, e.Message), e); | ||
| } | ||
|
|
||
| return new Project(context.Info) | ||
| { | ||
| Name = directiveText, | ||
| }; | ||
| } | ||
|
|
||
| public Project WithName(string name) | ||
| { | ||
| return new Project(Info) { Name = name }; | ||
| return WithName(directiveText, preserveUnresolvedName: true); | ||
| } | ||
|
|
||
| public override string ToString() => $"#:project {Name}"; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.