-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[browser] Enable webcil in Wasm SDK (#84977)
* Webcil in WasmSDK for build * Fix file writes. Wip on publish * Make publish work
- Loading branch information
Showing
4 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Json; | ||
using System.Text; | ||
using System.Xml; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
using ResourceHashesByNameDictionary = System.Collections.Generic.Dictionary<string, string>; | ||
|
||
namespace Microsoft.NET.Sdk.WebAssembly; | ||
|
||
public class ConvertDllsToWebCil : Task | ||
{ | ||
[Required] | ||
public ITaskItem[] Candidates { get; set; } | ||
|
||
[Required] | ||
public string OutputPath { get; set; } | ||
|
||
[Required] | ||
public bool IsEnabled { get; set; } | ||
|
||
[Output] | ||
public ITaskItem[] WebCilCandidates { get; set; } | ||
|
||
protected readonly List<string> _fileWrites = new(); | ||
|
||
[Output] | ||
public string[]? FileWrites => _fileWrites.ToArray(); | ||
|
||
public override bool Execute() | ||
{ | ||
var webCilCandidates = new List<ITaskItem>(); | ||
|
||
if (!IsEnabled) | ||
{ | ||
WebCilCandidates = Candidates; | ||
return true; | ||
} | ||
|
||
for (int i = 0; i < Candidates.Length; i++) | ||
{ | ||
var candidate = Candidates[i]; | ||
|
||
var extension = candidate.GetMetadata("Extension"); | ||
var filePath = candidate.ItemSpec; | ||
|
||
if (!Directory.Exists(OutputPath)) | ||
Directory.CreateDirectory(OutputPath); | ||
|
||
if (extension == ".dll") | ||
{ | ||
var tmpWebcil = Path.GetTempFileName(); | ||
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: filePath, outputPath: tmpWebcil, logger: Log); | ||
webcilWriter.ConvertToWebcil(); | ||
|
||
var finalWebcil = Path.Combine(OutputPath, Path.GetFileNameWithoutExtension(filePath) + ".webcil"); | ||
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) | ||
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); | ||
else | ||
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); | ||
|
||
_fileWrites.Add(finalWebcil); | ||
|
||
var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); | ||
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), ".webcil")); | ||
webcilItem.SetMetadata("AssetTraitName", "WasmResource"); | ||
webcilItem.SetMetadata("AssetTraitValue", "runtime"); | ||
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); | ||
|
||
webCilCandidates.Add(webcilItem); | ||
} | ||
else | ||
{ | ||
webCilCandidates.Add(candidate); | ||
} | ||
} | ||
|
||
WebCilCandidates = webCilCandidates.ToArray(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters