|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic;
|
5 | 6 | using System.IO;
|
6 | 7 | using Microsoft.Build.Framework;
|
@@ -59,52 +60,66 @@ public override bool Execute()
|
59 | 60 | continue;
|
60 | 61 | }
|
61 | 62 |
|
62 |
| - var dllFilePath = candidate.ItemSpec; |
63 |
| - var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension; |
64 |
| - string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture" |
65 |
| - ? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue")) |
66 |
| - : OutputPath; |
67 |
| - |
68 |
| - string finalWebcil = Path.Combine(candidatePath, webcilFileName); |
69 |
| - |
70 |
| - if (Utils.IsNewerThan(dllFilePath, finalWebcil)) |
| 63 | + try |
71 | 64 | {
|
72 |
| - var tmpWebcil = Path.Combine(tmpDir, webcilFileName); |
73 |
| - var logAdapter = new LogAdapter(Log); |
74 |
| - var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter); |
75 |
| - webcilWriter.ConvertToWebcil(); |
76 |
| - |
77 |
| - if (!Directory.Exists(candidatePath)) |
78 |
| - Directory.CreateDirectory(candidatePath); |
79 |
| - |
80 |
| - if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) |
81 |
| - Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); |
82 |
| - else |
83 |
| - Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); |
| 65 | + TaskItem webcilItem = ConvertDll(tmpDir, candidate); |
| 66 | + webCilCandidates.Add(webcilItem); |
84 | 67 | }
|
85 |
| - else |
| 68 | + catch (Exception ex) |
86 | 69 | {
|
87 |
| - Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}"); |
| 70 | + Log.LogError($"Failed to convert '{candidate.ItemSpec}' to webcil: {ex.Message}"); |
| 71 | + return false; |
88 | 72 | }
|
| 73 | + } |
| 74 | + |
| 75 | + WebCilCandidates = webCilCandidates.ToArray(); |
| 76 | + return true; |
| 77 | + } |
89 | 78 |
|
90 |
| - _fileWrites.Add(finalWebcil); |
| 79 | + private TaskItem ConvertDll(string tmpDir, ITaskItem candidate) |
| 80 | + { |
| 81 | + var dllFilePath = candidate.ItemSpec; |
| 82 | + var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension; |
| 83 | + string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture" |
| 84 | + ? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue")) |
| 85 | + : OutputPath; |
91 | 86 |
|
92 |
| - var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); |
93 |
| - webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension)); |
94 |
| - webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); |
| 87 | + string finalWebcil = Path.Combine(candidatePath, webcilFileName); |
95 | 88 |
|
96 |
| - if (webcilItem.GetMetadata("AssetTraitName") == "Culture") |
97 |
| - { |
98 |
| - string relatedAsset = webcilItem.GetMetadata("RelatedAsset"); |
99 |
| - relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension); |
100 |
| - webcilItem.SetMetadata("RelatedAsset", relatedAsset); |
101 |
| - Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}."); |
102 |
| - } |
| 89 | + if (Utils.IsNewerThan(dllFilePath, finalWebcil)) |
| 90 | + { |
| 91 | + var tmpWebcil = Path.Combine(tmpDir, webcilFileName); |
| 92 | + var logAdapter = new LogAdapter(Log); |
| 93 | + var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter); |
| 94 | + webcilWriter.ConvertToWebcil(); |
103 | 95 |
|
104 |
| - webCilCandidates.Add(webcilItem); |
| 96 | + if (!Directory.Exists(candidatePath)) |
| 97 | + Directory.CreateDirectory(candidatePath); |
| 98 | + |
| 99 | + if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true)) |
| 100 | + Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} ."); |
| 101 | + else |
| 102 | + Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged."); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}"); |
105 | 107 | }
|
106 | 108 |
|
107 |
| - WebCilCandidates = webCilCandidates.ToArray(); |
108 |
| - return true; |
| 109 | + _fileWrites.Add(finalWebcil); |
| 110 | + |
| 111 | + var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata()); |
| 112 | + webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension)); |
| 113 | + webcilItem.SetMetadata("OriginalItemSpec", finalWebcil); |
| 114 | + |
| 115 | + if (webcilItem.GetMetadata("AssetTraitName") == "Culture") |
| 116 | + { |
| 117 | + string relatedAsset = webcilItem.GetMetadata("RelatedAsset"); |
| 118 | + relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension); |
| 119 | + webcilItem.SetMetadata("RelatedAsset", relatedAsset); |
| 120 | + Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}."); |
| 121 | + } |
| 122 | + |
| 123 | + return webcilItem; |
109 | 124 | }
|
110 | 125 | }
|
0 commit comments