Skip to content

Follow ups for Blazor AOT #31098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Mar 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 36 additions & 65 deletions src/Components/WebAssembly/BlazorManifest/acquire/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@ static string GetDotnetPath()

static int Main(string[] args)
{
System.Console.WriteLine(MuxerPath);
var sdkDirectory = args.Length > 0 ? args[0] : Path.GetDirectoryName(MuxerPath);
var tempDirectory = Path.Combine(Directory.GetCurrentDirectory(), "tmp", Path.GetRandomFileName());
var restoreDirectory = Path.Combine(tempDirectory, ".nuget");

try
{
var restore = Restore(tempDirectory, restoreDirectory, out var packs);
var packs = GetPacks(sdkDirectory);
var restore = RestorePacks(tempDirectory, restoreDirectory, packs);
if (restore != 0)
{
return restore;
}

var sourceManifestDirectory = Path.Combine(restoreDirectory, "microsoft.net.sdk.blazorwebassembly.aot", ManifestVersion);
var targetManifestDirectory = Path.Combine(sdkDirectory, "sdk-manifests", ManifestVersion, "Microsoft.NET.Sdk.BlazorWebAssembly.AOT");
Move(sourceManifestDirectory, targetManifestDirectory);

foreach (var (id, version) in packs)
{
var source = Path.Combine(restoreDirectory, id.ToLowerInvariant(), version);
Expand All @@ -59,7 +55,7 @@ static int Main(string[] args)
sdkVersionProc.WaitForExit();
var sdkVersion = sdkVersionProc.StandardOutput.ReadToEnd().Trim();
var sentinelPath = Path.Combine(sdkDirectory, "sdk", sdkVersion, "EnableWorkloadResolver.sentinel");
Console.WriteLine($"Writing sentinel to {sentinelPath}.");
Console.WriteLine($"Enabling Workloads support in dotnet SDK v{sdkVersion}.");

File.WriteAllBytes(sentinelPath, Array.Empty<byte>());
}
Expand All @@ -84,64 +80,17 @@ static void Move(string source, string destination)
Directory.Move(source, destination);
}

static int Restore(string tempDirectory, string restoreDirectory, out List<(string, string)> packs)
static List<(string Id, string Version)> GetPacks(string sdkDirectory)
{
packs = null;

var restoreProject = Path.Combine(tempDirectory, "restore", "Restore.csproj");
var restoreProjectDirectory = Directory.CreateDirectory(Path.GetDirectoryName(restoreProject));

File.WriteAllText(Path.Combine(restoreProjectDirectory.FullName, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(restoreProjectDirectory.FullName, "Directory.Build.targets"), "<Project />");

var projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.NET.Sdk.BlazorWebAssembly.AOT"" Version=""6.0.0-*"" />
</ItemGroup>
</Project>
";
File.WriteAllText(restoreProject, projectFile);

Console.WriteLine("Restoring...");

var process = Process.Start(new ProcessStartInfo
{
FileName = MuxerPath,
ArgumentList = { "restore", restoreProject },
Environment =
{
["NUGET_PACKAGES"] = restoreDirectory,
},
});
process.WaitForExit();
if (process.ExitCode != 0)
var manifestDirectory = Path.Combine(sdkDirectory, "sdk-manifests", ManifestVersion, "Microsoft.NET.Workload.BlazorWebAssembly");
if (!Directory.Exists(manifestDirectory))
{
Console.Error.WriteLine("Unable to restore Microsoft.NET.Sdk.BlazorWebAssembly.AOT workload.");
return 1;
throw new DirectoryNotFoundException($"Cound not find directory {manifestDirectory}. A 6.0-preview3 SDK or newer is required for this tool to function.");
}

var manifestDirectory = Path.Combine(restoreDirectory, "microsoft.net.sdk.blazorwebassembly.aot");
var version = Directory.EnumerateDirectories(manifestDirectory).First();

manifestDirectory = Path.Combine(manifestDirectory, ManifestVersion);
Directory.Move(version, manifestDirectory);

var manifestPath = Path.Combine(manifestDirectory, "WorkloadManifest.json");
var manifest = JsonSerializer.Deserialize<PackInformation>(File.ReadAllBytes(manifestPath), new JsonSerializerOptions(JsonSerializerDefaults.Web));

projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<NoWarn>$(NoWarn);NU1213</NoWarn>
</PropertyGroup>
<ItemGroup>
";
packs = new List<(string id, string version)>();
var packs = new List<(string, string)>();
foreach (var item in manifest.Packs)
{
var packageName = item.Key;
Expand All @@ -161,34 +110,56 @@ static int Restore(string tempDirectory, string restoreDirectory, out List<(stri
}
else
{
Console.Error.WriteLine("Unsupported platform.");
return 1;
throw new NotSupportedException("Unsupported OS platform.");
}
}
projectFile += @$"<PackageReference Include=""{packageName}"" Version=""{item.Value.Version}"" />";
packs.Add((packageName, item.Value.Version));
}

return packs;
}

static int RestorePacks(string tempDirectory, string restoreDirectory, List<(string Id, string Version)> packs)
{
var restoreProject = Path.Combine(tempDirectory, "restore", "Restore.csproj");
var restoreProjectDirectory = Directory.CreateDirectory(Path.GetDirectoryName(restoreProject));

File.WriteAllText(Path.Combine(restoreProjectDirectory.FullName, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(restoreProjectDirectory.FullName, "Directory.Build.targets"), "<Project />");

var projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<NoWarn>$(NoWarn);NU1213</NoWarn>
</PropertyGroup>
<ItemGroup>
";
foreach (var (Id, Version) in packs)
{
projectFile += $"<PackageReference Include=\"{Id}\" Version=\"{Version}\" />";
}

projectFile += @"
</ItemGroup>
</Project>
";
File.WriteAllText(restoreProject, projectFile);

process = Process.Start(new ProcessStartInfo
var process = Process.Start(new ProcessStartInfo
{
FileName = MuxerPath,
ArgumentList = { "restore", restoreProject },
#if !DEBUG
RedirectStandardError = true,
RedirectStandardOutput = true,
#endif
Environment =
{
["NUGET_PACKAGES"] = restoreDirectory,
},
});
process.WaitForExit();


return 0;
}

Expand Down