From 7d42fec9653592930a5b908df68efa5ec5563368 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 16 Apr 2024 19:19:12 -0600 Subject: [PATCH 1/3] Fix ConvertDllsToWebCil message --- .../ConvertDllsToWebCil.cs | 89 +++++++++++-------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs index 2d21f3820a5584..01c5d4dda4dd0f 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs @@ -1,6 +1,7 @@ // 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.IO; using Microsoft.Build.Framework; @@ -59,52 +60,66 @@ public override bool Execute() continue; } - var dllFilePath = candidate.ItemSpec; - var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension; - string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture" - ? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue")) - : OutputPath; - - string finalWebcil = Path.Combine(candidatePath, webcilFileName); - - if (Utils.IsNewerThan(dllFilePath, finalWebcil)) + try { - var tmpWebcil = Path.Combine(tmpDir, webcilFileName); - var logAdapter = new LogAdapter(Log); - var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter); - webcilWriter.ConvertToWebcil(); - - if (!Directory.Exists(candidatePath)) - Directory.CreateDirectory(candidatePath); - - 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."); + TaskItem webcilItem = ConvertDll(tmpDir, candidate); + webCilCandidates.Add(webcilItem); } - else + catch (Exception ex) { - Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}"); + Log.LogError($"Failed to convert '{candidate.ItemSpec}' to webcil: {ex.Message}"); + return false; } + } + + WebCilCandidates = webCilCandidates.ToArray(); + return true; + } - _fileWrites.Add(finalWebcil); + private TaskItem ConvertDll(string tmpDir, ITaskItem candidate) + { + var dllFilePath = candidate.ItemSpec; + var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension; + string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture" + ? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue")) + : OutputPath; - var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); - webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension)); - webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); + string finalWebcil = Path.Combine(candidatePath, webcilFileName); - if (webcilItem.GetMetadata("AssetTraitName") == "Culture") - { - string relatedAsset = webcilItem.GetMetadata("RelatedAsset"); - relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension); - webcilItem.SetMetadata("RelatedAsset", relatedAsset); - Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}."); - } + if (Utils.IsNewerThan(dllFilePath, finalWebcil)) + { + var tmpWebcil = Path.Combine(tmpDir, webcilFileName); + var logAdapter = new LogAdapter(Log); + var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter); + webcilWriter.ConvertToWebcil(); - webCilCandidates.Add(webcilItem); + if (!Directory.Exists(candidatePath)) + Directory.CreateDirectory(candidatePath); + + 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."); + } + else + { + Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}"); } - WebCilCandidates = webCilCandidates.ToArray(); - return true; + _fileWrites.Add(finalWebcil); + + var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); + webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension)); + webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); + + if (webcilItem.GetMetadata("AssetTraitName") == "Culture") + { + string relatedAsset = webcilItem.GetMetadata("RelatedAsset"); + relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension); + webcilItem.SetMetadata("RelatedAsset", relatedAsset); + Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}."); + } + + return webcilItem; } } From 9d15d5ddcbc9983e8c694cbc51d2886a87826737 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Wed, 17 Apr 2024 00:04:35 -0600 Subject: [PATCH 2/3] Add basic tests --- src/tasks/tests/ConvertDllsToWebCilTests.cs | 75 +++++++++++++++++++ ...ET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj | 18 +++++ src/tasks/tests/tests.sln | 31 ++++++++ 3 files changed, 124 insertions(+) create mode 100644 src/tasks/tests/ConvertDllsToWebCilTests.cs create mode 100644 src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj create mode 100644 src/tasks/tests/tests.sln diff --git a/src/tasks/tests/ConvertDllsToWebCilTests.cs b/src/tasks/tests/ConvertDllsToWebCilTests.cs new file mode 100644 index 00000000000000..f1c5b50adc74af --- /dev/null +++ b/src/tasks/tests/ConvertDllsToWebCilTests.cs @@ -0,0 +1,75 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Moq; +using System.Collections.Generic; +using System.IO; +using Xunit; + +namespace Microsoft.NET.Sdk.WebAssembly.Tests +{ + public class ConvertDllsToWebCilTests + { + private ConvertDllsToWebCil task = new ConvertDllsToWebCil(); + private List errors = new List(); + + public ConvertDllsToWebCilTests() + { + var buildEngine = new Mock(); + buildEngine.Setup(x => x.LogErrorEvent(It.IsAny())).Callback(e => errors.Add(e)); + task.BuildEngine = buildEngine.Object; + } + + [Fact] + public void TestEmptyInput() + { + string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll"); + + try + { + File.Create(input).Dispose(); + + task.Candidates = new ITaskItem[] { new TaskItem(input) }; + task.IsEnabled = true; + task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath(); + + bool result = task.Execute(); + + Assert.False(result); + Assert.Single(errors); + Assert.Contains(input, errors[0].Message); + } + finally + { + File.Delete(input); + } + } + + [Fact] + public void TestInvalidDirectoryInput() + { + string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll"); + + try + { + Directory.CreateDirectory(input); + + task.Candidates = new ITaskItem[] { new TaskItem(input) }; + task.IsEnabled = true; + task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath(); + + bool result = task.Execute(); + + Assert.False(result); + Assert.Single(errors); + Assert.Contains(input, errors[0].Message); + } + finally + { + Directory.Delete(input); + } + } + } +} diff --git a/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj b/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj new file mode 100644 index 00000000000000..8032c5f55d7991 --- /dev/null +++ b/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj @@ -0,0 +1,18 @@ + + + + $(NetCoreAppToolCurrent) + enable + true + + + + + + + + + + + + diff --git a/src/tasks/tests/tests.sln b/src/tasks/tests/tests.sln new file mode 100644 index 00000000000000..63a3d5d2710655 --- /dev/null +++ b/src/tasks/tests/tests.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.34815.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests", "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj", "{035240F3-31CE-409E-8D55-2A89ADE522D6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks", "..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj", "{4C68406D-BBFC-4637-A634-917C0621A883}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.Build.0 = Release|Any CPU + {4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {15EFCF6A-C86C-474F-BBAD-F2DB4B3DBAA3} + EndGlobalSection +EndGlobal From 5801429e7236f748a34f0ca0a2ac4adfeb71db7a Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 30 Apr 2024 00:11:16 -0600 Subject: [PATCH 3/3] Revert "Add basic tests" This reverts commit 9d15d5ddcbc9983e8c694cbc51d2886a87826737. --- src/tasks/tests/ConvertDllsToWebCilTests.cs | 75 ------------------- ...ET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj | 18 ----- src/tasks/tests/tests.sln | 31 -------- 3 files changed, 124 deletions(-) delete mode 100644 src/tasks/tests/ConvertDllsToWebCilTests.cs delete mode 100644 src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj delete mode 100644 src/tasks/tests/tests.sln diff --git a/src/tasks/tests/ConvertDllsToWebCilTests.cs b/src/tasks/tests/ConvertDllsToWebCilTests.cs deleted file mode 100644 index f1c5b50adc74af..00000000000000 --- a/src/tasks/tests/ConvertDllsToWebCilTests.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.Build.Framework; -using Microsoft.Build.Utilities; -using Moq; -using System.Collections.Generic; -using System.IO; -using Xunit; - -namespace Microsoft.NET.Sdk.WebAssembly.Tests -{ - public class ConvertDllsToWebCilTests - { - private ConvertDllsToWebCil task = new ConvertDllsToWebCil(); - private List errors = new List(); - - public ConvertDllsToWebCilTests() - { - var buildEngine = new Mock(); - buildEngine.Setup(x => x.LogErrorEvent(It.IsAny())).Callback(e => errors.Add(e)); - task.BuildEngine = buildEngine.Object; - } - - [Fact] - public void TestEmptyInput() - { - string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll"); - - try - { - File.Create(input).Dispose(); - - task.Candidates = new ITaskItem[] { new TaskItem(input) }; - task.IsEnabled = true; - task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath(); - - bool result = task.Execute(); - - Assert.False(result); - Assert.Single(errors); - Assert.Contains(input, errors[0].Message); - } - finally - { - File.Delete(input); - } - } - - [Fact] - public void TestInvalidDirectoryInput() - { - string input = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".dll"); - - try - { - Directory.CreateDirectory(input); - - task.Candidates = new ITaskItem[] { new TaskItem(input) }; - task.IsEnabled = true; - task.OutputPath = task.IntermediateOutputPath = Path.GetTempPath(); - - bool result = task.Execute(); - - Assert.False(result); - Assert.Single(errors); - Assert.Contains(input, errors[0].Message); - } - finally - { - Directory.Delete(input); - } - } - } -} diff --git a/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj b/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj deleted file mode 100644 index 8032c5f55d7991..00000000000000 --- a/src/tasks/tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - $(NetCoreAppToolCurrent) - enable - true - - - - - - - - - - - - diff --git a/src/tasks/tests/tests.sln b/src/tasks/tests/tests.sln deleted file mode 100644 index 63a3d5d2710655..00000000000000 --- a/src/tasks/tests/tests.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.11.34815.271 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests", "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj", "{035240F3-31CE-409E-8D55-2A89ADE522D6}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.WebAssembly.Pack.Tasks", "..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj", "{4C68406D-BBFC-4637-A634-917C0621A883}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {035240F3-31CE-409E-8D55-2A89ADE522D6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {035240F3-31CE-409E-8D55-2A89ADE522D6}.Release|Any CPU.Build.0 = Release|Any CPU - {4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4C68406D-BBFC-4637-A634-917C0621A883}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4C68406D-BBFC-4637-A634-917C0621A883}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {15EFCF6A-C86C-474F-BBAD-F2DB4B3DBAA3} - EndGlobalSection -EndGlobal