Skip to content
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

[StaticWebAssets] Make all the properties in StaticWebAssetEndpoint lazy #47833

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
be892b5
Make all the properties in StaticWebAssetEndpoint lazy
javiercn Mar 24, 2025
d5bfdfa
tmp
javiercn Mar 24, 2025
689a4b7
Fix initialization
javiercn Mar 24, 2025
9c143d5
Make StaticWebAssets properties lazy
javiercn Mar 24, 2025
369603e
Validate assets only at manifest generation time
javiercn Mar 25, 2025
0f53579
Fix copy
javiercn Mar 25, 2025
dce017a
Custom ITaskItem2 implementation
javiercn Mar 25, 2025
0135f57
Fix array covariance
javiercn Mar 25, 2025
d134313
Remove unnecessary using
javiercn Mar 25, 2025
2e34673
Return FullPath as metadata
javiercn Mar 26, 2025
d38ec0e
Add missing implementations
javiercn Mar 26, 2025
fcc26f2
Fix implementation
javiercn Mar 26, 2025
fbc7349
Another fix
javiercn Mar 26, 2025
8a4c5ca
Path.GetFullPath
javiercn Mar 26, 2025
d3d10bc
Fix path
javiercn Mar 26, 2025
8521bd3
Revert "Fix path"
javiercn Mar 27, 2025
f13a3f6
Revert "Path.GetFullPath"
javiercn Mar 27, 2025
998da42
Fix tests
javiercn Mar 27, 2025
a9980b5
Revert "Fix tests"
javiercn Mar 27, 2025
a1f21dc
Fix tests
javiercn Mar 27, 2025
97ae53a
Fix remaining tests
javiercn Mar 27, 2025
59044d4
Implement ITaskItem2 on StaticWebAssetEndpoint
javiercn Mar 28, 2025
23383df
Small cleanup
javiercn Mar 28, 2025
aa8500b
Remove some allocations for GenerateStaticWebAssetsManifest
javiercn Mar 28, 2025
ba6cee6
Remove additional allocations and right size dictionary
javiercn Mar 31, 2025
4ca4eba
Remove linq call
javiercn Mar 31, 2025
67c4992
Fix the build
javiercn Mar 31, 2025
75e4aec
Fix nullref
javiercn Apr 1, 2025
c2e24f6
Sort on write
javiercn Apr 1, 2025
1c342b9
Address feedback
javiercn Apr 3, 2025
01070bb
Fix build
javiercn Apr 3, 2025
151ec89
Fix failing test
javiercn Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ApplyCompressionNegotiation : Task

public override bool Execute()
{
var assetsById = CandidateAssets.Select(StaticWebAsset.FromTaskItem).ToDictionary(a => a.Identity);
var assetsById = StaticWebAsset.ToAssetDictionary(CandidateAssets);

var endpointsByAsset = CandidateEndpoints.Select(StaticWebAssetEndpoint.FromTaskItem)
.GroupBy(e => e.AssetFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override bool Execute()
var normalizedOutputPath = StaticWebAsset.NormalizeContentRootPath(Path.GetFullPath(OutputPath));
try
{
foreach (var asset in Assets.Select(StaticWebAsset.FromTaskItem))
foreach (var asset in StaticWebAsset.FromTaskItemGroup(Assets))
{
string fileOutputPath = null;
if (!(asset.IsDiscovered() || asset.IsComputed()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override bool Execute()
return true;
}

var candidates = CandidateAssets.Select(StaticWebAsset.FromTaskItem).ToArray();
var candidates = StaticWebAsset.FromTaskItemGroup(CandidateAssets);
var assetsToUpdate = new List<ITaskItem>();

var candidatesByIdentity = candidates.ToDictionary(asset => asset.Identity, OSPath.PathComparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public override bool Execute()
return true;
}

var candidates = CandidateAssets.Select(StaticWebAsset.FromTaskItem).ToArray();
var explicitAssets = ExplicitAssets?.Select(StaticWebAsset.FromTaskItem).ToArray() ?? [];
var candidates = StaticWebAsset.FromTaskItemGroup(CandidateAssets).ToArray();
var explicitAssets = ExplicitAssets == null ? [] : StaticWebAsset.FromTaskItemGroup(ExplicitAssets);
var existingCompressionFormatsByAssetItemSpec = CollectCompressedAssets(candidates);

var includePatterns = SplitPattern(IncludePatterns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ComputeEndpointsForReferenceStaticWebAssets : Task

public override bool Execute()
{
var assets = Assets.Select(StaticWebAsset.FromTaskItem).ToDictionary(a => a.Identity, a => a);
var assets = StaticWebAsset.ToAssetDictionary(Assets);
var candidateEndpoints = StaticWebAssetEndpoint.FromItemGroup(CandidateEndpoints);

var endpoints = new List<StaticWebAssetEndpoint>();
Expand Down
47 changes: 11 additions & 36 deletions src/StaticWebAssetsSdk/Tasks/ComputeReferenceStaticWebAssetItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,19 @@ public override bool Execute()
{
try
{
var existingAssets = Assets
.Where(asset => StaticWebAsset.HasSourceId(asset, Source))
.Select(StaticWebAsset.FromTaskItem)
.GroupBy(
a => a.ComputeTargetPath("", '/'),
(key, group) => (key, StaticWebAsset.ChooseNearestAssetKind(group, AssetKind)));

var resultAssets = new List<StaticWebAsset>();
foreach (var (key, group) in existingAssets)
var existingAssets = StaticWebAsset.AssetsByTargetPath(Assets, Source, AssetKind);

var resultAssets = new List<StaticWebAsset>(existingAssets.Count);
foreach (var kvp in existingAssets)
{
if (!TryGetUniqueAsset(group, out var selected))
var targetPath = kvp.Key;
var (selected, all) = kvp.Value;
if (all != null)
{
if (selected == null)
{
Log.LogMessage(MessageImportance.Low, "No compatible asset found for '{0}'", key);
continue;
}
else
{
Log.LogError("More than one compatible asset found for '{0}'.", selected.Identity);
return false;
}
Log.LogError("More than one compatible asset found for target path '{0}' -> {1}.",
targetPath,
Environment.NewLine + string.Join(Environment.NewLine, all.Select(a => $"({a.Identity},{a.AssetKind})")));
return false;
}

if (ShouldIncludeAssetAsReference(selected, out var reason))
Expand Down Expand Up @@ -106,22 +97,6 @@ public override bool Execute()
return !Log.HasLoggedErrors;
}

private static bool TryGetUniqueAsset(IEnumerable<StaticWebAsset> candidates, out StaticWebAsset selected)
{
selected = null;
foreach (var asset in candidates)
{
if (selected != null)
{
return false;
}

selected = asset;
}

return selected != null;
}

private bool ShouldIncludeAssetAsReference(StaticWebAsset candidate, out string reason)
{
if (!StaticWebAssetsManifest.ManifestModes.ShouldIncludeAssetAsReference(candidate, ProjectMode))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,19 @@ public override bool Execute()
{
try
{
var currentProjectAssets = Assets
.Where(asset => StaticWebAsset.HasSourceId(asset, Source))
.Select(StaticWebAsset.FromTaskItem)
.GroupBy(
a => a.ComputeTargetPath("", '/'),
(key, group) => (key, StaticWebAsset.ChooseNearestAssetKind(group, AssetKind)));
var currentProjectAssets = StaticWebAsset.AssetsByTargetPath(Assets, Source, AssetKind);

var resultAssets = new List<StaticWebAsset>();
foreach (var (key, group) in currentProjectAssets)
var resultAssets = new List<StaticWebAsset>(currentProjectAssets.Count);
foreach (var kvp in currentProjectAssets)
{
if (!TryGetUniqueAsset(group, out var selected))
var targetPath = kvp.Key;
var (selected, all) = kvp.Value;
if (all != null)
{
if (selected == null)
{
Log.LogMessage(MessageImportance.Low, "No compatible asset found for '{0}'", key);
continue;
}
else
{
Log.LogError("More than one compatible asset found for '{0}'.", selected.Identity);
return false;
}
Log.LogError("More than one compatible asset found for target path '{0}' -> {1}.",
targetPath,
Environment.NewLine + string.Join(Environment.NewLine, all.Select(a => $"({a.Identity},{a.AssetKind})")));
return false;
}

if (!selected.IsForReferencedProjectsOnly())
Expand All @@ -74,20 +65,4 @@ public override bool Execute()

return !Log.HasLoggedErrors;
}

private static bool TryGetUniqueAsset(IEnumerable<StaticWebAsset> candidates, out StaticWebAsset selected)
{
selected = null;
foreach (var asset in candidates)
{
if (selected != null)
{
return false;
}

selected = asset;
}

return selected != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;

Expand All @@ -27,7 +26,7 @@ public override bool Execute()
try
{
Log.LogMessage(MessageImportance.Low, "Using path prefix '{0}'", PathPrefix);
AssetsWithTargetPath = new TaskItem[Assets.Length];
AssetsWithTargetPath = new ITaskItem[Assets.Length];

for (var i = 0; i < Assets.Length; i++)
{
Expand Down
Loading