Skip to content

Commit

Permalink
Wasm SDK packed as a nuget package (#31519)
Browse files Browse the repository at this point in the history
Co-authored-by: Larry Ewing <[email protected]>
Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 21, 2023
1 parent c1683d6 commit 7fdf9a6
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 21,385 deletions.
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "8.0.100-preview.4.23174.9",
"dotnet": "8.0.100-preview.4.23221.6",
"runtimes": {
"dotnet": [
"$(VSRedistCommonNetCoreSharedFrameworkx6480PackageVersion)"
Expand All @@ -14,4 +14,4 @@
"Microsoft.DotNet.Arcade.Sdk": "8.0.0-beta.23220.2",
"Microsoft.DotNet.Helix.Sdk": "8.0.0-beta.23220.2"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project ToolsVersion="14.0">
<Project ToolsVersion="14.0" TreatAsLocalProperty="RuntimeIdentifier">
<PropertyGroup>
<OutputType>exe</OutputType>

<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<CopyOutputSymbolsToPublishDirectory>false</CopyOutputSymbolsToPublishDirectory>

<!-- Trimmer defaults -->
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
Expand Down
35 changes: 35 additions & 0 deletions src/StaticWebAssetsSdk/Tasks/FileHasher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.Numerics;
using System.Security.Cryptography;
using System.Text;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;

public static class FileHasher
{
public static string GetFileHash(string filePath)
{
using var hash = SHA256.Create();
var bytes = Encoding.UTF8.GetBytes(filePath);
var hashBytes = hash.ComputeHash(bytes);
return ToBase36(hashBytes);
}

private static string ToBase36(byte[] hash)
{
const string chars = "0123456789abcdefghijklmnopqrstuvwxyz";

var result = new char[10];
var dividend = BigInteger.Abs(new BigInteger(hash.AsSpan().Slice(0, 9).ToArray()));
for (var i = 0; i < 10; i++)
{
dividend = BigInteger.DivRem(dividend, 36, out var remainder);
result[i] = chars[(int)remainder];
}

return new string(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@
<Compile Include="..\..\RazorSdk\Tasks\DotnetToolTask.cs">
<Link>Shared\DotnetToolTask.cs</Link>
</Compile>
<Compile Include="..\..\WasmSdk\Tasks\FileHasher.cs">
<Link>Shared\FileHasher.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/StaticWebAssetsSdk/Tasks/ResolveCompressedAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.NET.Sdk.WebAssembly;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;

Expand Down
75 changes: 44 additions & 31 deletions src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class ProcessFrameworkReferences : TaskBase

public ITaskItem[] KnownILLinkPacks { get; set; } = Array.Empty<ITaskItem>();

public ITaskItem[] KnownWebAssemblySdkPacks { get; set; } = Array.Empty<ITaskItem>();

public bool UsingMicrosoftNETSdkWebAssembly { get; set; }

[Required]
public string NETCoreSdkRuntimeIdentifier { get; set; }

Expand Down Expand Up @@ -368,7 +372,7 @@ var runtimeRequiredByDeployment
return;
}
}

if (AotEnabled)
{
if (!AddToolPack(ToolPackType.ILCompiler, _normalizedTargetFrameworkVersion, packagesToDownload, implicitPackageReferences))
Expand All @@ -387,6 +391,12 @@ var runtimeRequiredByDeployment
}
}

if (UsingMicrosoftNETSdkWebAssembly)
{
// WebAssemblySdk is used for .NET >= 6, it's ok if no pack is added.
AddToolPack(ToolPackType.WebAssemblySdk, _normalizedTargetFrameworkVersion, packagesToDownload, implicitPackageReferences);
}

if (packagesToDownload.Any())
{
PackagesToDownload = packagesToDownload.Distinct(new PackageToDownloadComparer<ITaskItem>()).ToArray();
Expand Down Expand Up @@ -594,7 +604,8 @@ private enum ToolPackType
{
Crossgen2,
ILCompiler,
ILLink
ILLink,
WebAssemblySdk
}

private bool AddToolPack(
Expand All @@ -603,10 +614,12 @@ private bool AddToolPack(
List<ITaskItem> packagesToDownload,
List<ITaskItem> implicitPackageReferences)
{
var knownPacks = toolPackType switch {
var knownPacks = toolPackType switch
{
ToolPackType.Crossgen2 => KnownCrossgen2Packs,
ToolPackType.ILCompiler => KnownILCompilerPacks,
ToolPackType.ILLink => KnownILLinkPacks,
ToolPackType.WebAssemblySdk => KnownWebAssemblySdkPacks,
_ => throw new ArgumentException($"Unknown package type {toolPackType}", nameof(toolPackType))
};

Expand All @@ -629,7 +642,6 @@ private bool AddToolPack(
packVersion = RuntimeFrameworkVersion;
}


// Crossgen and ILCompiler have RID-specific bits.
if (toolPackType is ToolPackType.Crossgen2 or ToolPackType.ILCompiler)
{
Expand Down Expand Up @@ -658,43 +670,44 @@ private bool AddToolPack(
runtimePackItem.SetMetadata(MetadataKeys.NuGetPackageId, runtimePackName);
runtimePackItem.SetMetadata(MetadataKeys.NuGetPackageVersion, packVersion);

switch (toolPackType) {
case ToolPackType.Crossgen2:
Crossgen2Packs = new[] { runtimePackItem };
break;
case ToolPackType.ILCompiler:
HostILCompilerPacks = new[] { runtimePackItem };
switch (toolPackType)
{
case ToolPackType.Crossgen2:
Crossgen2Packs = new[] { runtimePackItem };
break;
case ToolPackType.ILCompiler:
HostILCompilerPacks = new[] { runtimePackItem };

// ILCompiler supports cross target compilation. If there is a cross-target request, we need to download that package as well
// We expect RuntimeIdentifier to be defined during publish but can allow during build
if (RuntimeIdentifier != null)
{
var targetRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(runtimeGraph, RuntimeIdentifier, packSupportedRuntimeIdentifiers, out bool wasInGraph2);
if (targetRuntimeIdentifier == null)
// ILCompiler supports cross target compilation. If there is a cross-target request, we need to download that package as well
// We expect RuntimeIdentifier to be defined during publish but can allow during build
if (RuntimeIdentifier != null)
{
return false;
}
if (!hostRuntimeIdentifier.Equals(targetRuntimeIdentifier))
{
var targetIlcPackName = packNamePattern.Replace("**RID**", targetRuntimeIdentifier);
var targetIlcPack = new TaskItem(targetIlcPackName);
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageId, targetIlcPackName);
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageVersion, packVersion);
TargetILCompilerPacks = new[] { targetIlcPack };
var targetRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(runtimeGraph, RuntimeIdentifier, packSupportedRuntimeIdentifiers, out bool wasInGraph2);
if (targetRuntimeIdentifier == null)
{
return false;
}
if (!hostRuntimeIdentifier.Equals(targetRuntimeIdentifier))
{
var targetIlcPackName = packNamePattern.Replace("**RID**", targetRuntimeIdentifier);
var targetIlcPack = new TaskItem(targetIlcPackName);
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageId, targetIlcPackName);
targetIlcPack.SetMetadata(MetadataKeys.NuGetPackageVersion, packVersion);
TargetILCompilerPacks = new[] { targetIlcPack };
}
}
}
break;
break;
}
}

// ILCompiler and ILLink have RID-agnostic build packages that contain MSBuild targets.
if (toolPackType is ToolPackType.ILCompiler or ToolPackType.ILLink)
// Packs with RID-agnostic build packages that contain MSBuild targets.
if (toolPackType is not ToolPackType.Crossgen2)
{
var buildPackageName = knownPack.ItemSpec;
var buildPackage = new TaskItem(buildPackageName);
buildPackage.SetMetadata(MetadataKeys.Version, packVersion);
implicitPackageReferences.Add(buildPackage);
}
}

return true;
}
Expand Down Expand Up @@ -929,7 +942,7 @@ public KnownFrameworkReference(ITaskItem item)
public string RuntimePackRuntimeIdentifiers => _item.GetMetadata(MetadataKeys.RuntimePackRuntimeIdentifiers);

public bool IsWindowsOnly => _item.HasMetadataValue("IsWindowsOnly", "true");

public bool RuntimePackAlwaysCopyLocal =>
_item.HasMetadataValue(MetadataKeys.RuntimePackAlwaysCopyLocal, "true");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Copyright (c) .NET Foundation. All rights reserved.
KnownCrossgen2Packs="@(KnownCrossgen2Pack)"
KnownILCompilerPacks="@(KnownILCompilerPack)"
KnownILLinkPacks="@(KnownILLinkPack)"
KnownWebAssemblySdkPacks="@(KnownWebAssemblySdkPack)"
UsingMicrosoftNETSdkWebAssembly="$(UsingMicrosoftNETSdkWebAssembly)"
NETCoreSdkRuntimeIdentifier="$(NETCoreSdkRuntimeIdentifier)"
NetCoreRoot="$(NetCoreRoot)"
NETCoreSdkVersion="$(NETCoreSdkVersion)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\Microsoft.NET.Sdk.BlazorWebAssembly.Tests\BootJsonData.cs" Link="BootJsonData.cs" />
<Compile Include="..\Microsoft.NET.Sdk.BlazorWebAssembly.Tests\WasmPublishIntegrationTestBase.cs" Link="WasmPublishIntegrationTestBase.cs" />
<Compile Include="..\Microsoft.NET.Sdk.BlazorWebAssembly.Tests\ServiceWorkerAssert.cs" Link="ServiceWorkerAssert.cs" />
</ItemGroup>
Expand All @@ -51,7 +52,6 @@

<ItemGroup>
<ProjectReference Include="..\..\..\src\BlazorWasmSdk\Tasks\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.csproj" SkipGetTargetFrameworkProperties="true" />
<ProjectReference Include="..\..\..\src\WasmSdk\Tasks\Microsoft.NET.Sdk.WebAssembly.Tasks.csproj" SkipGetTargetFrameworkProperties="true" />
<ProjectReference Include="..\..\RazorSdk\Tasks\Microsoft.NET.Sdk.Razor.Tasks.csproj" />
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Runtime.Serialization;
using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary<string, string>;

// For test purposes only. Actual build time implementation lives in runtime repository with WasmSDK

namespace Microsoft.NET.Sdk.WebAssembly
{
#pragma warning disable IDE1006 // Naming Styles
Expand Down

This file was deleted.

Loading

0 comments on commit 7fdf9a6

Please sign in to comment.