-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add AdditionalStaticWebAssetsBasePath property for external content roots #52020
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-msbuild-asset-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−0
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
733c1db
Initial plan
Copilot 6966c36
Add AdditionalStaticWebAssetsBasePath property for external content r…
Copilot 7900320
Address code review comments for cross-framework compatibility
Copilot 76775fb
Replace custom task with MSBuild ItemGroup transforms
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/StaticWebAssetsSdk/Tasks/ParseAdditionalStaticWebAssetsBasePaths.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #nullable disable | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| namespace Microsoft.AspNetCore.StaticWebAssets.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// Parses the AdditionalStaticWebAssetsBasePath property which is a semicolon-separated list of | ||
| /// content-root,base-path pairs and discovers the files in each content root. | ||
| /// </summary> | ||
| public class ParseAdditionalStaticWebAssetsBasePaths : Task | ||
| { | ||
| /// <summary> | ||
| /// The semicolon-separated list of content-root,base-path pairs. | ||
| /// Format: content-root-1,base-path-1;content-root-2,base-path-2 | ||
| /// </summary> | ||
| [Required] | ||
| public string AdditionalStaticWebAssetsBasePaths { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The parsed content root and base path pairs as items with ContentRoot and BasePath metadata. | ||
| /// </summary> | ||
| [Output] | ||
| public ITaskItem[] ParsedBasePaths { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The discovered files from all content roots with appropriate metadata for DefineStaticWebAssets. | ||
| /// </summary> | ||
| [Output] | ||
| public ITaskItem[] DiscoveredFiles { get; set; } | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| var parsedPaths = new List<ITaskItem>(); | ||
| var discoveredFiles = new List<ITaskItem>(); | ||
|
|
||
| if (string.IsNullOrEmpty(AdditionalStaticWebAssetsBasePaths)) | ||
| { | ||
| ParsedBasePaths = []; | ||
| DiscoveredFiles = []; | ||
| return true; | ||
| } | ||
|
|
||
| var pairs = AdditionalStaticWebAssetsBasePaths.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
| foreach (var pair in pairs) | ||
| { | ||
| var parts = pair.Split(new[] { ',' }, 2, StringSplitOptions.None); | ||
|
|
||
| if (parts.Length < 2) | ||
| { | ||
| Log.LogError( | ||
| "Invalid format for AdditionalStaticWebAssetsBasePath entry '{0}'. Expected format: 'content-root,base-path'.", | ||
| pair); | ||
| continue; | ||
| } | ||
|
|
||
| var contentRoot = parts[0].Trim(); | ||
| var basePath = parts[1].Trim(); | ||
|
|
||
| if (string.IsNullOrEmpty(contentRoot)) | ||
| { | ||
| Log.LogError( | ||
| "Content root is empty in AdditionalStaticWebAssetsBasePath entry '{0}'.", | ||
| pair); | ||
| continue; | ||
| } | ||
|
|
||
| // Normalize content root path to end with directory separator | ||
| if (!contentRoot.EndsWith(Path.DirectorySeparatorChar.ToString()) && | ||
| !contentRoot.EndsWith(Path.AltDirectorySeparatorChar.ToString())) | ||
| { | ||
| contentRoot += Path.DirectorySeparatorChar; | ||
| } | ||
|
|
||
| // Make content root path absolute if it's relative | ||
| if (!Path.IsPathRooted(contentRoot)) | ||
| { | ||
| contentRoot = Path.GetFullPath(contentRoot); | ||
| } | ||
|
|
||
| var pathItem = new TaskItem(parsedPaths.Count.ToString(System.Globalization.CultureInfo.InvariantCulture)); | ||
| pathItem.SetMetadata("ContentRoot", contentRoot); | ||
| pathItem.SetMetadata("BasePath", basePath); | ||
|
|
||
| Log.LogMessage(MessageImportance.Low, | ||
| "Parsed additional static web asset base path: ContentRoot='{0}', BasePath='{1}'", | ||
| contentRoot, | ||
| basePath); | ||
|
|
||
| parsedPaths.Add(pathItem); | ||
|
|
||
| // Discover files from this content root | ||
| if (Directory.Exists(contentRoot)) | ||
| { | ||
| var files = Directory.GetFiles(contentRoot, "*", SearchOption.AllDirectories); | ||
| foreach (var file in files) | ||
| { | ||
| var fullPath = Path.GetFullPath(file); | ||
| var relativePath = fullPath.Substring(contentRoot.Length); | ||
|
|
||
| // Normalize path separators | ||
| relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); | ||
|
|
||
| var fileItem = new TaskItem(fullPath); | ||
| fileItem.SetMetadata("ContentRoot", contentRoot); | ||
| fileItem.SetMetadata("BasePath", basePath); | ||
| fileItem.SetMetadata("RelativePath", relativePath); | ||
|
|
||
| Log.LogMessage(MessageImportance.Low, | ||
| "Discovered file '{0}' with relative path '{1}' in content root '{2}'", | ||
| fullPath, | ||
| relativePath, | ||
| contentRoot); | ||
|
|
||
| discoveredFiles.Add(fileItem); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Log.LogWarning( | ||
| "Content root directory '{0}' does not exist.", | ||
| contentRoot); | ||
| } | ||
| } | ||
|
|
||
| ParsedBasePaths = [.. parsedPaths]; | ||
| DiscoveredFiles = [.. discoveredFiles]; | ||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot don't do this with a custom task.
Use an itemgroup with
Includethen from that item group, create a separate item group with a transform.Something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the custom task with MSBuild ItemGroup transforms in 76775fb. The implementation now uses property functions to split the semicolon-separated list into items and extract ContentRoot/BasePath using
Split(','), followed by a glob pattern to discover files.