Skip to content

Commit

Permalink
Scoped css back compat
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed Aug 4, 2021
1 parent 460f816 commit a1c4ccb
Show file tree
Hide file tree
Showing 7 changed files with 602 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.ScopedCss.5_0.targets
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ Integration with static web assets:
_AddGeneratedScopedCssFilesForReference;
</GenerateStaticWebAssetsPackTargetsDependsOn>

<GetCurrentProjectStaticWebAssetsV2DependsOn>
$(GetCurrentProjectStaticWebAssetsV2DependsOn);
_UpdateScopedCssStaticWebAssetsForV2;
</GetCurrentProjectStaticWebAssetsV2DependsOn>

<!-- We are going to use .rz.scp.css as the extension to mark scoped css files that come from packages or that have been pre-procesed by
referenced class libraries. This way, we can use that information to adjust the build pipeline without having to rely on external
sources like an additional itemgroup or metadata.
Expand Down Expand Up @@ -390,4 +395,43 @@ Integration with static web assets:
</ItemGroup>
</Target>

<!-- Forward compat with new static web assets -->

<Target Name="_UpdateScopedCssStaticWebAssetsForV2">
<PropertyGroup>
<_AppBundleStaticWebAssetIdentity>@(_AppBundleStaticWebAsset)</_AppBundleStaticWebAssetIdentity>
<_ProjectBundleStaticWebAssetIdentity>@(_ProjectBundleStaticWebAsset)</_ProjectBundleStaticWebAssetIdentity>
</PropertyGroup>

<ItemGroup>
<!-- <<App>>.bundle.scp.css -->
<_CurrentProjectBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(_ThisProjectStaticWebAssets.Identity)' == '$(_ProjectBundleStaticWebAssetIdentity)'">
<AssetMode>Reference</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName>ScopedCss</AssetTraitName>
<AssetTraitValue>ProjectBundle</AssetTraitValue>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</_CurrentProjectBundle>

<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectBundle)" />
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectBundle)" />

<!-- <<App>>.styles.css -->
<_CurrentProjectAppBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(Identity)' == '$(_AppBundleStaticWebAssetIdentity)'">
<AssetMode>CurrentProject</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName>ScopedCss</AssetTraitName>
<AssetTraitValue>ApplicationBundle</AssetTraitValue>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</_CurrentProjectAppBundle>

<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectAppBundle)" />
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectAppBundle)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ Copyright (c) .NET Foundation. All rights reserved.
<ResultType>StaticWebAsset</ResultType>
<SourceType>Project</SourceType>
<AssetKind Condition="'%(_ThisProjectStaticWebAssets.AssetKind)' == ''">All</AssetKind>
<AssetMode>All</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName></AssetTraitName>
<AssetTraitValue></AssetTraitValue>
<AssetMode Condition="'%(_ThisProjectStaticWebAssets.AssetMode)' == ''">All</AssetMode>
<AssetRole Condition="'%(_ThisProjectStaticWebAssets.AssetRole)' == ''">Primary</AssetRole>
<RelatedAsset Condition="'%(_ThisProjectStaticWebAssets.RelatedAsset)' == ''"></RelatedAsset>
<AssetTraitName Condition="'%(_ThisProjectStaticWebAssets.AssetTraitName)' == ''"></AssetTraitName>
<AssetTraitValue Condition="'%(_ThisProjectStaticWebAssets.AssetTraitValue)' == ''"></AssetTraitValue>
<CopyToOutputDirectory Condition="'%(_ThisProjectStaticWebAssets.CopyToOutputDirectory)' == ''">Never</CopyToOutputDirectory>
<CopyToPublishDirectory Condition="'%(_ThisProjectStaticWebAssets.CopyToPublishDirectory)' == ''">PreserveNewest</CopyToPublishDirectory>
<OriginalItemSpec>%(Identity)</OriginalItemSpec>
<OriginalItemSpec Condition="'%(_ThisProjectStaticWebAssets.OriginalItemSpec)' == ''">%(Identity)</OriginalItemSpec>
</_ThisProjectStaticWebAssetItems>
<_ThisProjectStaticWebAssetItems Include="$(PackageId)\wwwroot" Condition="Exists('$(MSBuildProjectDirectory)\wwwroot')">
<ResultType>StaticWebAssetDiscoveryPattern</ResultType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.AspNetCore.Razor.Tasks;
using Microsoft.Build.Evaluation;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
Expand Down Expand Up @@ -437,5 +440,94 @@ public void BuildProjectWithReferences_CorrectlyBundlesScopedCssFiles()
appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
}

[Fact]
public void ScopedCss_IsBackwardsCompatible_WithPreviousVersions()
{
var testAsset = "RazorAppWithPackageAndP2PReference";
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
.WithProjectChanges((project, document) =>
{
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
{
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.1");
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
}
if (Path.GetFileName(project) == "ClassLibrary.csproj")
{
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.0");
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
}
});

var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
build.Execute("/bl").Should().Pass();

var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();

// GenerateStaticWebAssetsManifest should copy the file to the output folder.
var finalPath = Path.Combine(outputPath, "AppWithPackageAndP2PReference.staticwebassets.json");
new FileInfo(finalPath).Should().Exist();
AssertManifest(
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
LoadBuildManifest());

AssertBuildAssets(
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
outputPath,
intermediateOutputPath);

var appBundle = new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "AppWithPackageAndP2PReference.styles.css"));
appBundle.Should().Exist();

appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
}

[Fact]
public void ScopedCss_PublishIsBackwardsCompatible_WithPreviousVersions()
{
var testAsset = "RazorAppWithPackageAndP2PReference";
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
.WithProjectChanges((project, document) =>
{
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
{
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.1");
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
}
if (Path.GetFileName(project) == "ClassLibrary.csproj")
{
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.0");
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
}
});

var build = new PublishCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
build.Execute("/bl").Should().Pass();

var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();

var finalPath = Path.Combine(intermediateOutputPath, "StaticWebAssets.publish.json");
new FileInfo(finalPath).Should().Exist();
AssertManifest(
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
LoadPublishManifest());

AssertBuildAssets(
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
outputPath,
intermediateOutputPath);

var appBundle = new FileInfo(Path.Combine(outputPath, "wwwroot", "AppWithPackageAndP2PReference.styles.css"));
appBundle.Should().Exist();

appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"${ProjectPath}\\AppWithPackageAndP2PReference\\obj\\Debug\\net6.0\\scopedcss\\bundle\\AppWithPackageAndP2PReference.styles.css"
]
Loading

0 comments on commit a1c4ccb

Please sign in to comment.