Skip to content

Commit 97179b8

Browse files
committed
Fix restoring NPM package through MSBuild on VS
1 parent 3e58485 commit 97179b8

File tree

14 files changed

+94
-56
lines changed

14 files changed

+94
-56
lines changed

.config/dotnet-tools.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"dotnet-format"
99
],
1010
"rollForward": false
11+
},
12+
"microsoft.web.librarymanager.cli": {
13+
"version": "3.0.71",
14+
"commands": [
15+
"libman"
16+
],
17+
"rollForward": false
1118
}
1219
}
13-
}
20+
}

Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
<IsTestProject Condition="'$(IsTestProject)' == '' and $(MSBuildProjectName.EndsWith('Tests'))">true</IsTestProject>
1515

16-
<GovUkFrontendLibrary>$([MSBuild]::NormalizeDirectory('$(RepoRoot)', 'lib', 'govuk-frontend-$(GovUkFrontendVersion)'))</GovUkFrontendLibrary>
16+
<_NpmLibDirectory>$([MSBuild]::NormalizeDirectory('$(RepoRoot)', 'lib', 'govuk-frontend-$(GovUkFrontendVersion)'))</_NpmLibDirectory>
17+
<GovUkFrontendLibrary>$([MSBuild]::NormalizeDirectory('$(_NpmLibDirectory)', 'dist', 'govuk'))</GovUkFrontendLibrary>
1718
</PropertyGroup>
1819

1920
<ItemGroup Condition="'$(IsTestProject)' == 'true'">

samples/Samples.Sass/wwwroot/govuk-frontend.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/Samples.Sass/wwwroot/site.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GovUk.Frontend.AspNetCore.Build/DownloadNpmPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override bool Execute()
3434

3535
async Task ExecuteAsync()
3636
{
37-
var includePatterns = Include?.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ?? ["**"];
37+
var includePatterns = Include?.Split(['\n'], StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()) ?? ["**"];
3838

3939
var downloader = new NpmPackageDownloader();
4040
await downloader.DownloadPackage(Package, Version, PackageBaseDirectory ?? "", DestinationDirectory, includePatterns);
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<PackDependencies>false</PackDependencies>
7-
<PackFolder>tools</PackFolder>
8-
<OutputPath>bin\</OutputPath>
6+
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
7+
<OutputPath>bin</OutputPath>
8+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
99
<MinVerTagPrefix>$(CoreLibMinVerTagPrefix)</MinVerTagPrefix>
1010
<MinVerMinimumMajorMinor>$(CoreLibMinVerMinimumMajorMinor)</MinVerMinimumMajorMinor>
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.27" />
15-
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.8.27" />
16-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.27" />
17-
<!-- TODO Figure out how to get this out of the generated NuGet package's dependency list -->
18-
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="8.0.0" Pack="true" PrivateAssets="all" />
14+
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.27" />
15+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.8.27" />
16+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.27" />
17+
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="8.0.0" />
1918
<PackageReference Include="MinVer" Version="2.3.1">
2019
<PrivateAssets>all</PrivateAssets>
2120
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2221
</PackageReference>
2322
<PackageReference Include="NuGetizer" Version="*" />
2423
</ItemGroup>
2524

25+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
26+
<PackageReference Include="System.Text.Json" Version="8.0.5" />
27+
</ItemGroup>
28+
29+
<Target Name="CopyDllsToLibProject" AfterTargets="Build">
30+
<ItemGroup>
31+
<_Dlls Include="bin\**\$(AssemblyName).dll" />
32+
<_Dlls Include="bin\**\Microsoft.Extensions.FileSystemGlobbing.dll" />
33+
<_Dlls Include="bin\**\System.Text.Json.dll" />
34+
</ItemGroup>
35+
36+
<Copy
37+
SourceFiles="@(_Dlls)"
38+
DestinationFolder="$(RepoRoot)\src\GovUk.Frontend.AspNetCore\Package\tools\%(RecursiveDir)" />
39+
</Target>
40+
2641
</Project>

src/GovUk.Frontend.AspNetCore.Build/NpmPackageDownloader.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,29 @@ public async Task DownloadPackage(
2727
string destinationDirectory,
2828
IEnumerable<string> includePatterns)
2929
{
30-
ArgumentNullException.ThrowIfNull(package);
31-
ArgumentNullException.ThrowIfNull(version);
32-
ArgumentNullException.ThrowIfNull(destinationDirectory);
30+
if (package is null)
31+
{
32+
throw new ArgumentNullException(nameof(package));
33+
}
34+
if (version is null)
35+
{
36+
throw new ArgumentNullException(nameof(version));
37+
}
38+
if (destinationDirectory is null)
39+
{
40+
throw new ArgumentNullException(nameof(destinationDirectory));
41+
}
3342

3443
Directory.CreateDirectory(destinationDirectory);
3544

3645
var contents = await GetPackageContentsAsync(package, version);
3746

3847
packageBaseDirectory = packageBaseDirectory.TrimStart('/').TrimEnd('/');
39-
if (!packageBaseDirectory.StartsWith(PathSeparator))
48+
if (packageBaseDirectory.First() != PathSeparator)
4049
{
4150
packageBaseDirectory = PathSeparator + packageBaseDirectory;
4251
}
43-
if (!packageBaseDirectory.EndsWith(PathSeparator))
52+
if (packageBaseDirectory.Last() != PathSeparator)
4453
{
4554
packageBaseDirectory = packageBaseDirectory + PathSeparator;
4655
}
@@ -60,13 +69,13 @@ public async Task DownloadPackage(
6069

6170
var results = matcher.Execute(contents);
6271

63-
await Parallel.ForEachAsync(
64-
results.Files,
65-
async (r, ct) =>
66-
{
67-
var hash = ((NpmPackageFileInfo)contents.GetFile(r.Path)!).Hash;
68-
await DownloadFileAsync(package, version, r.Path, hash, packageBaseDirectory, destinationDirectory, ct);
69-
});
72+
var tasks = results.Files.Select(r =>
73+
{
74+
var hash = ((NpmPackageFileInfo)contents.GetFile(r.Path)!).Hash;
75+
return DownloadFileAsync(package, version, r.Path, hash, packageBaseDirectory, destinationDirectory, CancellationToken.None);
76+
});
77+
78+
await Task.WhenAll(tasks);
7079
}
7180

7281
private async Task DownloadFileAsync(
@@ -83,9 +92,9 @@ private async Task DownloadFileAsync(
8392

8493
if (File.Exists(destinationPath))
8594
{
86-
await using var efs = File.OpenRead(destinationPath);
95+
using var efs = File.OpenRead(destinationPath);
8796
using var sha = SHA256.Create();
88-
var currentFileHash = Convert.ToBase64String(await sha.ComputeHashAsync(efs, cancellationToken));
97+
var currentFileHash = Convert.ToBase64String(sha.ComputeHash(efs));
8998

9099
if (currentFileHash == hash)
91100
{
@@ -101,8 +110,8 @@ private async Task DownloadFileAsync(
101110

102111
Directory.CreateDirectory(destinationDirectory);
103112

104-
await using var fs = File.Create(destinationPath);
105-
await response.Content.CopyToAsync(fs, cancellationToken);
113+
using var fs = File.Create(destinationPath);
114+
await response.Content.CopyToAsync(fs);
106115
}
107116

108117
private async Task<DirectoryInfoBase> GetPackageContentsAsync(string package, string version)
@@ -195,7 +204,7 @@ private class NpmPackageDirectoryInfo(string name, string fullName, DirectoryInf
195204
private static FileSystemInfoBase? Find(DirectoryInfoBase directory, string[] pathParts)
196205
{
197206
var head = pathParts.First();
198-
var tail = pathParts[1..];
207+
var tail = pathParts.Skip(1).ToArray();
199208

200209
foreach (var child in directory.EnumerateFileSystemInfos())
201210
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Package/tools
2+
Package/govuk-frontend-version

src/GovUk.Frontend.AspNetCore/GovUk.Frontend.AspNetCore.csproj

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,20 @@
2727
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2828
</PropertyGroup>
2929

30-
<UsingTask
31-
TaskName="GovUk.Frontend.AspNetCore.Build.DownloadNpmPackage"
32-
AssemblyFile="../GovUk.Frontend.AspNetCore.Build/bin/net8.0/GovUk.Frontend.AspNetCore.Build.dll" />
33-
3430
<Target Name="InstallGovUkFrontendPackage" Condition="!Exists('$(GovUkFrontendLibrary)')">
35-
<MSBuild Projects="../GovUk.Frontend.AspNetCore.Build/GovUk.Frontend.AspNetCore.Build.csproj" Targets="Restore;Build" />
36-
37-
<GovUk.Frontend.AspNetCore.Build.DownloadNpmPackage
38-
Package="govuk-frontend"
39-
Version="$(GovUkFrontendVersion)"
40-
PackageBaseDirectory="dist/govuk"
41-
DestinationDirectory="$(GovUkFrontendLibrary)" />
31+
<Message Text="Restoring govuk-frontend@$(GovUkFrontendVersion)" />
32+
<RemoveDir Directories="$(_NpmLibDirectory)" />
33+
<MakeDir Directories="$(_NpmLibDirectory)" />
34+
<Exec Command="dotnet libman init -p jsdelivr -d $(_NpmLibDirectory)" WorkingDirectory="$(_NpmLibDirectory)" LogStandardErrorAsError="true" />
35+
<Exec Command="dotnet libman install govuk-frontend@$(GovUkFrontendVersion) --files dist/govuk/**" WorkingDirectory="$(_NpmLibDirectory)" LogStandardErrorAsError="true" />
4236
</Target>
4337

4438
<Target Name="WriteGovUkFrontendVersionFile">
4539
<ItemGroup>
4640
<_VersionFileLines Include="$(GovUkFrontendVersion)" />
4741
</ItemGroup>
4842

49-
<WriteLinesToFile File="govuk-frontend-version.txt" Lines="@(_VersionFileLines)" Overwrite="true" />
50-
51-
<ItemGroup>
52-
<Content Include="govuk-frontend-version.txt" Pack="true" PackagePath="\" />
53-
</ItemGroup>
43+
<WriteLinesToFile File="Package/govuk-frontend-version.txt" Lines="@(_VersionFileLines)" Overwrite="true" />
5444
</Target>
5545

5646
<Target Name="CopyXmlDocToDocsProject" AfterTargets="Build">
@@ -84,15 +74,14 @@
8474
<PrivateAssets>all</PrivateAssets>
8575
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
8676
</PackageReference>
87-
<PackageReference Include="NuGetizer" Version="*" />
8877
</ItemGroup>
8978

9079
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
9180
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.*" />
9281
</ItemGroup>
9382

9483
<ItemGroup>
95-
<ProjectReference Include="..\GovUk.Frontend.AspNetCore.Build\GovUk.Frontend.AspNetCore.Build.csproj" Pack="true" />
84+
<ProjectReference Include="..\GovUk.Frontend.AspNetCore.Build\GovUk.Frontend.AspNetCore.Build.csproj" PrivateAssets="all" />
9685
</ItemGroup>
9786

9887
<ItemGroup>
@@ -119,12 +108,24 @@
119108
</ItemGroup>
120109

121110
<ItemGroup>
122-
<None Include="icon.png" Pack="true" PackagePath="\" />
123-
</ItemGroup>
124-
125-
<ItemGroup>
126-
<Content Include="GovUk.Frontend.AspNetCore.props" Pack="true" PackFolder="build" />
127-
<Content Include="GovUk.Frontend.AspNetCore.targets" Pack="true" PackFolder="build" />
111+
<Content Update="Package\README.md" Pack="true" PackagePath="" />
112+
<Content Update="Package\icon.png" Pack="true" PackagePath="" />
113+
<Content Update="Package\GovUk.Frontend.AspNetCore.props" Pack="true" PackagePath="build" />
114+
<Content Update="Package\GovUk.Frontend.AspNetCore.targets" Pack="true" PackagePath="build" />
128115
</ItemGroup>
116+
117+
<Target Name="PackTaskDependencies" BeforeTargets="GenerateNuspec">
118+
<!--
119+
The include needs to happen after output has been copied to build output folder
120+
but before NuGet generates a nuspec. See https://github.com/NuGet/Home/issues/4704.
121+
-->
122+
<ItemGroup>
123+
<_PackageFiles Include="Package\**">
124+
<PackagePath>%(RecursiveDir)</PackagePath>
125+
<Visible>false</Visible>
126+
<BuildAction>Content</BuildAction>
127+
</_PackageFiles>
128+
</ItemGroup>
129+
</Target>
129130

130131
</Project>
File renamed without changes.

0 commit comments

Comments
 (0)