Skip to content
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}