-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/9.0.1xx] [StaticWebAssets] Improve Up to date check logic (#…
…43156) Co-authored-by: Javier Calvarro Nelson <jacalvar@microsoft.com> Co-authored-by: campersau <buchholz.bastian@googlemail.com>
- Loading branch information
1 parent
65dbcdb
commit b8e84b1
Showing
6 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.Design.targets
This file contains 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,68 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<CollectUpToDateCheckInputDesignTimeDependsOn> | ||
$(CollectUpToDateCheckInputDesignTimeDependsOn); | ||
ResolveStaticWebAssetsConfiguration; | ||
ResolveProjectStaticWebAssets; | ||
CollectStaticWebAssetInputsDesignTime; | ||
</CollectUpToDateCheckInputDesignTimeDependsOn> | ||
<CollectUpToDateCheckOutputDesignTimeDependsOn> | ||
$(CollectUpToDateCheckOutputDesignTimeDependsOn); | ||
ResolveStaticWebAssetsConfiguration; | ||
CollectStaticWebAssetOutputsDesignTime; | ||
</CollectUpToDateCheckOutputDesignTimeDependsOn> | ||
</PropertyGroup> | ||
|
||
<Target Name="CollectStaticWebAssetInputsDesignTime"> | ||
|
||
<ReadLinesFromFile | ||
File="$(StaticWebAssetUpToDateCheckManifestPath)" | ||
Condition="Exists('$(StaticWebAssetUpToDateCheckManifestPath)')" | ||
> | ||
<Output TaskParameter="Lines" ItemName="_StaticWebAssetUpToDateCheckInput" /> | ||
</ReadLinesFromFile> | ||
|
||
<ReadLinesFromFile | ||
File="$(StaticWebAssetReferencesUpToDateCheckManifestPath)" | ||
Condition="Exists('$(StaticWebAssetReferencesUpToDateCheckManifestPath)')" | ||
> | ||
<Output TaskParameter="Lines" ItemName="_StaticWebAssetReferenceUpToDateCheckInput" /> | ||
</ReadLinesFromFile> | ||
|
||
<ItemGroup> | ||
<_UpToDateCheckStaticWebAssetResolved Include="@(StaticWebAsset)" Condition="'%(SourceType)' == 'Discovered'" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<_UpToDateCheckStaticWebAssetResolvedCandidate Include="@(_UpToDateCheckStaticWebAssetResolved->'%(OriginalItemSpec)')" /> | ||
<_StaticWebAssetUpToDateCheckInput Include="@(_UpToDateCheckStaticWebAssetResolvedCandidate->Distinct()->'%(FullPath)')" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<_ExistingStaticWebAssetUpToDateCheckInput Include="%(_StaticWebAssetUpToDateCheckInput.FullPath)" Condition="Exists('%(_StaticWebAssetUpToDateCheckInput.FullPath)')" /> | ||
<_NonExistingStaticWebAssetUpToDateCheckInput Include="%(_StaticWebAssetUpToDateCheckInput.FullPath)" Condition="!Exists('%(_StaticWebAssetUpToDateCheckInput.FullPath)')" /> | ||
</ItemGroup> | ||
|
||
<WriteLinesToFile | ||
File="$(StaticWebAssetUpToDateCheckRemovedManifestPath)" | ||
Lines="@(_NonExistingStaticWebAssetUpToDateCheckInput)" | ||
Overwrite="true" | ||
WriteOnlyWhenDifferent="true" /> | ||
|
||
<ItemGroup> | ||
<UpToDateCheckInput Condition="'@(_NonExistingStaticWebAssetUpToDateCheckInput)' != ''" Include="$(StaticWebAssetUpToDateCheckRemovedManifestPath)" Set="StaticWebAssets" /> | ||
<UpToDateCheckInput Include="@(_ExistingStaticWebAssetUpToDateCheckInput)" Set="StaticWebAssets" /> | ||
<UpToDateCheckInput Include="@(_StaticWebAssetReferenceUpToDateCheckInput)" Set="StaticWebAssets" /> | ||
</ItemGroup> | ||
|
||
</Target> | ||
|
||
<Target Name="CollectStaticWebAssetOutputsDesignTime"> | ||
|
||
<ItemGroup> | ||
<UpToDateCheckOutput Include="$(StaticWebAssetBuildManifestPath)" Set="StaticWebAssets" /> | ||
</ItemGroup> | ||
|
||
</Target> | ||
|
||
</Project> |
This file contains 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 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 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 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
161 changes: 161 additions & 0 deletions
161
test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsDesignTimeTest.cs
This file contains 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,161 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.Tests; | ||
|
||
public class StaticWebAssetsDesignTimeTest(ITestOutputHelper log) : AspNetSdkBaselineTest(log) | ||
{ | ||
#if DEBUG | ||
public const string Configuration = "Debug"; | ||
#else | ||
public const string Configuration = "Release"; | ||
#endif | ||
|
||
[Fact] | ||
public void CollectUpToDateCheckInputOutputsDesignTime_ReportsAddedFiles() | ||
{ | ||
// Arrange | ||
var testAsset = "RazorAppWithP2PReference"; | ||
ProjectDirectory = AddIntrospection(CreateAspNetSdkTestAsset(testAsset)); | ||
|
||
var build = CreateBuildCommand(ProjectDirectory, "ClassLibrary"); | ||
|
||
build.Execute("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:build.binlog").Should().Pass(); | ||
|
||
File.WriteAllText(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "file.js"), "New File"); | ||
|
||
var msbuild = CreateMSBuildCommand( | ||
ProjectDirectory, | ||
"ClassLibrary", | ||
"ResolveStaticWebAssetsConfiguration;ResolveProjectStaticWebAssets;CollectStaticWebAssetInputsDesignTime;CollectStaticWebAssetOutputsDesignTime"); | ||
|
||
msbuild.ExecuteWithoutRestore("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:design.binlog").Should().Pass(); | ||
|
||
// Check the contents of the input and output files | ||
var inputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCInput.txt"); | ||
new FileInfo(inputFilePath).Should().Exist(); | ||
var inputFiles = File.ReadAllLines(inputFilePath); | ||
inputFiles.Should().HaveCount(3); | ||
inputFiles.Should().Contain(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "file.js")); | ||
inputFiles.Should().Contain(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "project-transitive-dep.js")); | ||
inputFiles.Should().Contain(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "project-transitive-dep.v4.js")); | ||
|
||
var outputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCOutput.txt"); | ||
new FileInfo(outputFilePath).Should().Exist(); | ||
var outputFiles = File.ReadAllLines(outputFilePath); | ||
outputFiles.Should().ContainSingle(); | ||
Path.GetFileName(outputFiles[0]).Should().Be("staticwebassets.build.json"); | ||
} | ||
|
||
[Fact] | ||
public void CollectUpToDateCheckInputOutputsDesignTime_ReportsRemovedFiles_Once() | ||
{ | ||
// Arrange | ||
var testAsset = "RazorAppWithP2PReference"; | ||
ProjectDirectory = AddIntrospection(CreateAspNetSdkTestAsset(testAsset)); | ||
|
||
var build = CreateBuildCommand(ProjectDirectory, "ClassLibrary"); | ||
|
||
build.Execute("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:build.binlog").Should().Pass(); | ||
|
||
File.Delete(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "project-transitive-dep.js")); | ||
|
||
var msbuild = CreateMSBuildCommand( | ||
ProjectDirectory, | ||
"ClassLibrary", | ||
"ResolveStaticWebAssetsConfiguration;ResolveProjectStaticWebAssets;CollectStaticWebAssetInputsDesignTime;CollectStaticWebAssetOutputsDesignTime"); | ||
|
||
msbuild.ExecuteWithoutRestore("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:design.binlog").Should().Pass(); | ||
|
||
// Check the contents of the input and output files | ||
var inputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCInput.txt"); | ||
new FileInfo(inputFilePath).Should().Exist(); | ||
var inputFiles = File.ReadAllLines(inputFilePath); | ||
inputFiles.Should().HaveCount(2); | ||
inputFiles.Should().Contain(Path.Combine(build.GetIntermediateDirectory().FullName, "staticwebassets.removed.txt")); | ||
inputFiles.Should().Contain(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "wwwroot", "js", "project-transitive-dep.v4.js")); | ||
|
||
var outputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCOutput.txt"); | ||
new FileInfo(outputFilePath).Should().Exist(); | ||
var outputFiles = File.ReadAllLines(outputFilePath); | ||
outputFiles.Should().ContainSingle(); | ||
Path.GetFileName(outputFiles[0]).Should().Be("staticwebassets.build.json"); | ||
} | ||
|
||
[Fact] | ||
public void CollectUpToDateCheckInputOutputsDesignTime_IncludesReferencedProjectsManifests() | ||
{ | ||
// Arrange | ||
var testAsset = "RazorAppWithP2PReference"; | ||
ProjectDirectory = AddIntrospection(CreateAspNetSdkTestAsset(testAsset)); | ||
|
||
var build = CreateBuildCommand(ProjectDirectory, "AppWithP2PReference"); | ||
|
||
build.Execute("/bl:build.binlog").Should().Pass(); | ||
build.Execute("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:build.binlog").Should().Pass(); | ||
|
||
var msbuild = CreateMSBuildCommand( | ||
ProjectDirectory, | ||
"AppWithP2PReference", | ||
"ResolveStaticWebAssetsConfiguration;ResolveProjectStaticWebAssets;CollectStaticWebAssetInputsDesignTime;CollectStaticWebAssetOutputsDesignTime"); | ||
|
||
msbuild.ExecuteWithoutRestore("/p:DesignTimeBuild=true", "/p:BuildingInsideVisualStudio=true", "/bl:design.binlog").Should().Pass(); | ||
|
||
// Check the contents of the input and output files | ||
var inputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCInput.txt"); | ||
new FileInfo(inputFilePath).Should().Exist(); | ||
var inputFiles = File.ReadAllLines(inputFilePath); | ||
inputFiles.Should().HaveCount(1); | ||
inputFiles.Should().Contain(Path.Combine(ProjectDirectory.Path, "ClassLibrary", "obj", "Debug", DefaultTfm, "staticwebassets.build.json")); | ||
|
||
var outputFilePath = Path.Combine(build.GetIntermediateDirectory().FullName, "StaticWebAssetsUTDCOutput.txt"); | ||
new FileInfo(outputFilePath).Should().Exist(); | ||
var outputFiles = File.ReadAllLines(outputFilePath); | ||
outputFiles.Should().ContainSingle(); | ||
Path.GetFileName(outputFiles[0]).Should().Be("staticwebassets.build.json"); | ||
} | ||
|
||
private MSBuildCommand CreateMSBuildCommand(TestAsset testAsset, string relativeProjectPath, string targets) | ||
{ | ||
return (MSBuildCommand)new MSBuildCommand(testAsset.Log, targets, testAsset.TestRoot, relativeProjectPath) | ||
.WithWorkingDirectory(testAsset.TestRoot); | ||
} | ||
|
||
private TestAsset AddIntrospection(TestAsset testAsset) | ||
{ | ||
return testAsset | ||
.WithProjectChanges((name, project) => | ||
{ | ||
project.Document.Root.LastNode.AddAfterSelf( | ||
XElement.Parse(""" | ||
<Target Name="ReportInputOutputs" AfterTargets="CollectStaticWebAssetOutputsDesignTime"> | ||
<ItemGroup> | ||
<_StaticWebAssetsUTDCInput Include="@(UpToDateCheckInput)" Condition="'%(UpToDateCheckInput.Set)' == 'StaticWebAssets'" /> | ||
<_StaticWebAssetsUTDCOutput Include="@(UpToDateCheckOutput)" Condition="'%(UpToDateCheckOutput.Set)' == 'StaticWebAssets'" /> | ||
</ItemGroup> | ||
<WriteLinesToFile Condition="'@(_StaticWebAssetsUTDCInput)' != ''" | ||
File="$(IntermediateOutputPath)\StaticWebAssetsUTDCInput.txt" | ||
Lines="@(_StaticWebAssetsUTDCInput->'%(FullPath)')" | ||
Overwrite="true" | ||
Encoding="UTF-8" | ||
/> | ||
<WriteLinesToFile Condition="'@(_StaticWebAssetsUTDCOutput)' != ''" | ||
File="$(IntermediateOutputPath)\StaticWebAssetsUTDCOutput.txt" | ||
Lines="@(_StaticWebAssetsUTDCOutput->'%(FullPath)')" | ||
Overwrite="true" | ||
Encoding="UTF-8" | ||
/> | ||
</Target> | ||
""" | ||
)); | ||
}); | ||
} | ||
} |