Skip to content

Commit ae63f2b

Browse files
committed
More cleaning up Blazor.Build package
* Clean up MonoRuntime targets * Convert executables in to tasks * Add tests
1 parent f8dd670 commit ae63f2b

35 files changed

+2080
-786
lines changed

src/Components/Blazor/Build/src/Cli/Commands/ResolveRuntimeDependenciesCommand.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Components/Blazor/Build/src/Cli/Commands/WriteBootJsonCommand.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Components/Blazor/Build/src/Cli/Program.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Components/Blazor/Build/src/Microsoft.AspNetCore.Blazor.Build.csproj

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<TargetName>Microsoft.AspNetCore.Blazor.Build.Tasks</TargetName>
6+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
7+
<AssemblyName>Microsoft.AspNetCore.Blazor.Build</AssemblyName>
58
<Description>Build mechanism for ASP.NET Core Blazor applications.</Description>
6-
<OutputType>Exe</OutputType>
79
<IsShippingPackage>true</IsShippingPackage>
810
<HasReferenceAssembly>false</HasReferenceAssembly>
911
</PropertyGroup>
@@ -12,7 +14,6 @@
1214
<PropertyGroup>
1315
<!-- Producing this package requires building with NodeJS enabled. -->
1416
<IsPackable Condition="'$(BuildNodeJS)' == 'false'">false</IsPackable>
15-
<GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);Publish</GenerateNuspecDependsOn>
1617
<NoPackageAnalysis>true</NoPackageAnalysis>
1718
<NuspecFile>Microsoft.AspNetCore.Blazor.Build.nuspec</NuspecFile>
1819
</PropertyGroup>
@@ -27,10 +28,26 @@
2728

2829
<ItemGroup>
2930
<ProjectReference Condition="'$(BuildNodeJS)' != 'false' and '$(BuildingInsideVisualStudio)' != 'true'" Include="$(RepoRoot)src\Components\Web.JS\Microsoft.AspNetCore.Components.Web.JS.npmproj" ReferenceOutputAssembly="false" />
30-
<Reference Include="Microsoft.Extensions.CommandLineUtils.Sources" />
31+
<Reference Include="Microsoft.Build.Framework" />
32+
<Reference Include="Microsoft.Build.Utilities.Core" />
3133
<Reference Include="System.Reflection.Metadata" />
34+
<Reference Include="System.Text.Json" />
3235
</ItemGroup>
3336

37+
<Target Name="CopyBuildTask" BeforeTargets="Build" AfterTargets="PrepareForRun">
38+
<!-- If you see a build warning here and you mean to update the tasks dll when building in VS, manually stop MSBuild.exe processes. -->
39+
<Copy SourceFiles="@(IntermediateAssembly)" DestinationFolder="$(OutputPath)tools" SkipUnchangedFiles="true" Retries="1" ContinueOnError="true">
40+
<Output TaskParameter="CopiedFiles" ItemName="FileWrites" />
41+
<Output TaskParameter="CopiedFiles" ItemName="_CopiedIntermediateAssembly"/>
42+
</Copy>
43+
44+
<Copy SourceFiles="@(ReferenceCopyLocalPaths);@(_DebugSymbolsIntermediatePath)" DestinationFolder="$(OutputPath)tools" SkipUnchangedFiles="true" Retries="1" ContinueOnError="true">
45+
<Output TaskParameter="CopiedFiles" ItemName="FileWrites" />
46+
</Copy>
47+
48+
<Message Text="$(MSBuildThisFileName) -> @(_CopiedIntermediateAssembly->'%(FullPath)')" Importance="high"/>
49+
</Target>
50+
3451
<ItemGroup>
3552
<Compile Include="..\..\..\Shared\src\JsonSerializerOptionsProvider.cs" />
3653
</ItemGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project>
2+
<!--
3+
Importing this file is equivalent to having:
4+
<PackageDependency Include="Microsoft.AspNetCore.Blazor.Build" />
5+
... except it's much more convenient when working in this repo, because it consumes the
6+
Blazor.Build targets/exe directly without needing this project to be packed into a .nupkg.
7+
8+
This is only intended for use by other projects in this repo.
9+
-->
10+
11+
<PropertyGroup>
12+
<ComponentsRoot Condition="'$(ComponentsRoot)'==''">$(MSBuildThisFileDirectory)..\..\..\</ComponentsRoot>
13+
<BlazorJsPath>$(ComponentsRoot)Web.JS\dist\$(Configuration)\blazor.webassembly.js</BlazorJsPath>
14+
<BlazorJsMapPath>$(ComponentsRoot)Web.JS\dist\$(Configuration)\blazor.webassembly.js.map</BlazorJsMapPath>
15+
<BlazorToolsDir>$(MSBuildThisFileDirectory)bin\$(Configuration)\netstandard2.0\tools\</BlazorToolsDir>
16+
</PropertyGroup>
17+
18+
<Target Name="CheckBlazorJSFiles" BeforeTargets="Build">
19+
<Error Text="blazor.webassembly.js file could not be found at $(BlazorJsPath)" Condition="!Exists($(BlazorJsPath))" />
20+
</Target>
21+
22+
<Import Project="$(MSBuildThisFileDirectory)targets/All.props" />
23+
<Import Project="$(MSBuildThisFileDirectory)targets/All.targets" />
24+
25+
</Project>

src/Components/Blazor/Build/src/ReferenceFromSource.props

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
<Project>
22

3-
<!--
4-
Importing this file is equivalent to having:
5-
<PackageDependency Include="Microsoft.AspNetCore.Blazor.Build" />
6-
... except it's much more convenient when working in this repo, because it consumes the
7-
Blazor.Build targets/exe directly without needing this project to be packed into a .nupkg.
8-
9-
This is only intended for use by other projects in this repo.
10-
-->
11-
12-
<PropertyGroup>
13-
<BlazorBuildReferenceFromSource>true</BlazorBuildReferenceFromSource>
14-
<BlazorJsPath>$(RepoRoot)src\Components\Web.JS\dist\$(Configuration)\blazor.*.js.*</BlazorJsPath>
15-
</PropertyGroup>
16-
17-
<Import Project="$(MSBuildThisFileDirectory)targets/All.props" />
18-
<Import Project="$(MSBuildThisFileDirectory)targets/All.targets" />
3+
<Import Project="ReferenceBlazorBuildFromSource.props" />
194

205
<!--
216
Debugging support using blazor-devserver serve.

src/Components/Blazor/Build/src/Core/BootJsonWriter.cs renamed to src/Components/Blazor/Build/src/Tasks/GenerateBlazorBootJson.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.IO;
6+
using System.Linq;
77
using System.Reflection;
88
using System.Text.Json;
99
using Microsoft.AspNetCore.Components;
10+
using Microsoft.Build.Framework;
11+
using Microsoft.Build.Utilities;
1012

1113
namespace Microsoft.AspNetCore.Blazor.Build
1214
{
13-
internal class BootJsonWriter
15+
public class GenerateBlazorBootJson : Task
1416
{
15-
public static void WriteFile(
16-
string assemblyPath,
17-
string[] assemblyReferences,
18-
bool linkerEnabled,
19-
string outputPath)
20-
{
21-
var bootJsonText = GetBootJsonContent(
22-
AssemblyName.GetAssemblyName(assemblyPath).Name,
23-
assemblyReferences,
24-
linkerEnabled);
25-
var normalizedOutputPath = Path.GetFullPath(outputPath);
26-
Console.WriteLine("Writing boot data to: " + normalizedOutputPath);
27-
File.WriteAllText(normalizedOutputPath, bootJsonText);
28-
}
17+
[Required]
18+
public string AssemblyPath { get; set; }
19+
20+
[Required]
21+
public ITaskItem[] References { get; set; }
22+
23+
[Required]
24+
public bool LinkerEnabled { get; set; }
2925

30-
public static string GetBootJsonContent(string entryAssembly, string[] assemblyReferences, bool linkerEnabled)
26+
[Required]
27+
public string OutputPath { get; set; }
28+
29+
public override bool Execute()
3130
{
31+
var assemblyReferences = References.Select(c => Path.GetFileName(c.ItemSpec)).ToArray();
32+
3233
var data = new BootJsonData(
33-
entryAssembly,
34+
AssemblyName.GetAssemblyName(AssemblyPath).Name,
3435
assemblyReferences,
35-
linkerEnabled);
36-
return JsonSerializer.Serialize(data, JsonSerializerOptionsProvider.Options);
36+
LinkerEnabled);
37+
38+
var bootJsonText = JsonSerializer.Serialize(data, JsonSerializerOptionsProvider.Options);
39+
File.WriteAllText(OutputPath, bootJsonText);
40+
41+
return true;
3742
}
3843

3944
/// <summary>
4045
/// Defines the structure of a Blazor boot JSON file
4146
/// </summary>
42-
readonly struct BootJsonData
47+
private readonly struct BootJsonData
4348
{
4449
public string EntryAssembly { get; }
4550
public IEnumerable<string> AssemblyReferences { get; }

src/Components/Blazor/Build/src/Core/RuntimeDependenciesResolver.cs renamed to src/Components/Blazor/Build/src/Tasks/ResolveBlazorRuntimeDependencies.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,45 @@
99
using System.Reflection;
1010
using System.Reflection.Metadata;
1111
using System.Reflection.PortableExecutable;
12+
using Microsoft.Build.Framework;
13+
using Microsoft.Build.Utilities;
1214

1315
namespace Microsoft.AspNetCore.Blazor.Build
1416
{
15-
internal class RuntimeDependenciesResolver
17+
public class ResolveBlazorRuntimeDependencies : Task
1618
{
17-
public static void ResolveRuntimeDependencies(
18-
string entryPoint,
19-
string[] applicationDependencies,
20-
string[] monoBclDirectories,
21-
string outputFile)
19+
[Required]
20+
public string EntryPoint { get; set; }
21+
22+
[Required]
23+
public ITaskItem[] ApplicationDependencies { get; set; }
24+
25+
[Required]
26+
public ITaskItem[] MonoBCLAssemblies { get; set; }
27+
28+
[Output]
29+
public ITaskItem[] Dependencies { get; set; }
30+
31+
public override bool Execute()
2232
{
23-
var paths = ResolveRuntimeDependenciesCore(entryPoint, applicationDependencies, monoBclDirectories);
24-
File.WriteAllLines(outputFile, paths);
33+
var paths = ResolveRuntimeDependenciesCore(EntryPoint, ApplicationDependencies.Select(c => c.ItemSpec), MonoBCLAssemblies.Select(c => c.ItemSpec));
34+
Dependencies = paths.Select(p => new TaskItem(p)).ToArray();
35+
36+
return true;
2537
}
2638

2739
public static IEnumerable<string> ResolveRuntimeDependenciesCore(
2840
string entryPoint,
29-
string[] applicationDependencies,
30-
string[] monoBclDirectories)
41+
IEnumerable<string> applicationDependencies,
42+
IEnumerable<string> monoBclAssemblies)
3143
{
3244
var assembly = new AssemblyEntry(entryPoint, GetAssemblyName(entryPoint));
3345

3446
var dependencies = applicationDependencies
3547
.Select(a => new AssemblyEntry(a, GetAssemblyName(a)))
3648
.ToArray();
3749

38-
var bcl = monoBclDirectories
39-
.SelectMany(d => Directory.EnumerateFiles(d, "*.dll").Select(f => Path.Combine(d, f)))
50+
var bcl = monoBclAssemblies
4051
.Select(a => new AssemblyEntry(a, GetAssemblyName(a)))
4152
.ToArray();
4253

@@ -90,8 +101,9 @@ internal void ResolveAssemblies()
90101

91102
void ResolveAssembliesCore()
92103
{
93-
while (pendingAssemblies.TryPop(out var current))
104+
while (pendingAssemblies.Count > 0)
94105
{
106+
var current = pendingAssemblies.Pop();
95107
if (visitedAssemblies.Add(current))
96108
{
97109
// Not all references will be resolvable within the Mono BCL.

0 commit comments

Comments
 (0)