-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[watch] Static web asset abstraction #55188
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
4 changes: 3 additions & 1 deletion
4
...et.Watch/Watch/Build/ProjectInstanceId.cs → ...atch/HotReloadClient/ProjectInstanceId.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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.DotNet.Watch; | ||
| #nullable enable | ||
|
|
||
| namespace Microsoft.DotNet.HotReload; | ||
|
|
||
| internal readonly record struct ProjectInstanceId(string ProjectPath, string TargetFramework); |
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/Dotnet.Watch/HotReloadClient/Web/StaticWebAssetUpdateBuilder.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 enable | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.DotNet.HotReload; | ||
|
|
||
| internal abstract class StaticWebAssetUpdateBuilder | ||
| { | ||
| public readonly struct ProjectInstanceInfo | ||
| { | ||
| public required ProjectInstanceId Id { get; init; } | ||
| public required bool HasScopedCssTargets { get; init; } | ||
| public required string AssemblyName { get; init; } | ||
| } | ||
|
|
||
| private readonly Dictionary<ProjectInstanceId, Dictionary<string, StaticWebAsset>> _assets = []; | ||
| private readonly HashSet<ProjectInstanceId> _projectInstancesToRegenerate = []; | ||
|
|
||
| public IReadOnlyDictionary<ProjectInstanceId, Dictionary<string, StaticWebAsset>> Assets => _assets; | ||
|
|
||
| #if NET | ||
| public IReadOnlySet<ProjectInstanceId> ProjectInstancesToRegenerate => _projectInstancesToRegenerate; | ||
| #else | ||
| public IReadOnlyCollection<ProjectInstanceId> ProjectInstancesToRegenerate => _projectInstancesToRegenerate; | ||
| #endif | ||
|
|
||
| protected abstract bool TryGetManifest(ProjectInstanceId id, [NotNullWhen(true)] out StaticWebAssetsManifest? manifest); | ||
| protected abstract IEnumerable<ProjectInstanceInfo> GetProjectInstances(string projectPath); | ||
| protected abstract IEnumerable<(ProjectInstanceInfo info, ILogger logger)> GetApplicationProjectAncestors(ProjectInstanceId projectInstanceId); | ||
|
|
||
| public void AddAssets( | ||
| string filePath, | ||
| IEnumerable<string> containingProjectPaths, | ||
| string? staticWebAssetRelativeUrl) | ||
| { | ||
| var isScopedCss = StaticWebAsset.IsScopedCssFile(filePath); | ||
| if (!isScopedCss && staticWebAssetRelativeUrl is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var containingProjectPath in containingProjectPaths) | ||
| { | ||
| foreach (var containingProjectInstanceInfo in GetProjectInstances(containingProjectPath)) | ||
| { | ||
| if (isScopedCss) | ||
| { | ||
| if (!containingProjectInstanceInfo.HasScopedCssTargets) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| _projectInstancesToRegenerate.Add(containingProjectInstanceInfo.Id); | ||
| } | ||
|
|
||
| // Enumerate running application projects that transitively reference the project containing the changed asset. | ||
| // | ||
| // For regular asset files contained in Razor Class Libraries we could expect containingProjectPaths to have all the application projects that transitively reference the RCL | ||
| // based on the asset manifest (these assets have _content-prefixed URLs). However, scoped CSS files in the RCLs are bundled into a single asset file. | ||
| // They are not individually listed in the manifest so we can't map each individual asset's file path to its containing project based on the manifest. | ||
| // | ||
| // Instead, we only track files directly contained in their own project (Web App or RCL), i.e. the asset's containing project is only the RCL project, | ||
| // and then enumerate the web application projects that transitively reference the containing project here. | ||
| foreach (var (applicationProjectInstanceInfo, applicationProjectLogger) in GetApplicationProjectAncestors(containingProjectInstanceInfo.Id)) | ||
| { | ||
| string relativeUrl; | ||
|
|
||
| if (isScopedCss) | ||
| { | ||
| // Razor class library may be referenced by application that does not have static assets: | ||
| if (!applicationProjectInstanceInfo.HasScopedCssTargets) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| _projectInstancesToRegenerate.Add(applicationProjectInstanceInfo.Id); | ||
|
|
||
| var bundleFileName = StaticWebAsset.GetScopedCssBundleFileName( | ||
| applicationProjectFilePath: applicationProjectInstanceInfo.Id.ProjectPath, | ||
| containingProjectFilePath: containingProjectInstanceInfo.Id.ProjectPath); | ||
|
|
||
| if (!TryGetManifest(applicationProjectInstanceInfo.Id, out var manifest)) | ||
| { | ||
| // Shouldn't happen. | ||
| applicationProjectLogger.Log(LogEvents.StaticWebAssetManifestNotFound); | ||
| continue; | ||
| } | ||
|
|
||
| if (!manifest.TryGetBundleFilePath(bundleFileName, out var bundleFilePath)) | ||
| { | ||
| // Shouldn't happen. | ||
| applicationProjectLogger.Log(LogEvents.ScopedCssBundleFileNotFound, bundleFileName); | ||
| continue; | ||
| } | ||
|
|
||
| filePath = bundleFilePath; | ||
| relativeUrl = bundleFileName; | ||
| } | ||
| else if (TryGetManifest(applicationProjectInstanceInfo.Id, out var _)) | ||
| { | ||
| Debug.Assert(staticWebAssetRelativeUrl != null); | ||
| relativeUrl = staticWebAssetRelativeUrl; | ||
| } | ||
| else | ||
| { | ||
| // only refresh static web assets for web application projects (e.g. not for Aspire host app that references a web project) | ||
| continue; | ||
| } | ||
|
|
||
| if (!_assets.TryGetValue(applicationProjectInstanceInfo.Id, out var applicationAssets)) | ||
| { | ||
| applicationAssets = []; | ||
| _assets.Add(applicationProjectInstanceInfo.Id, applicationAssets); | ||
| } | ||
| else if (applicationAssets.ContainsKey(filePath)) | ||
| { | ||
| // asset already being updated in this application project: | ||
| continue; | ||
| } | ||
|
|
||
| applicationAssets.Add(filePath, new StaticWebAsset( | ||
| filePath, | ||
| StaticWebAsset.WebRoot + "/" + relativeUrl, | ||
| containingProjectInstanceInfo.AssemblyName, | ||
| isApplicationProject: containingProjectInstanceInfo.Id == applicationProjectInstanceInfo.Id)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.