Skip to content
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

Catches failed NuGet.Commands resource events to localize NuGet.Commands with ILMerged nuget.exe resources #4802

Merged
merged 7 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
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
Expand Up @@ -81,6 +81,8 @@
</Compile>
<!-- nuget.exe localized assemblies -->
<EmbeddedResource Include="$(OutputPath)\**\$(AssemblyName).resources.dll" />
<!-- NuGet.Commands localized assemblies -->
<LocResource Include="$(OutputPath)\**\NuGet.Commands.resources.dll" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -115,6 +117,7 @@
<IlmergeCommand>$(ILMergeExePath) /lib:$(OutputPath) /out:$(PathToMergedNuGetExe) @(MergeAllowDup -> '/allowdup:%(Identity)', ' ') /log:$(OutputPath)IlMergeLog.txt</IlmergeCommand>
<IlmergeCommand Condition="Exists($(MS_PFX_PATH))">$(IlmergeCommand) /delaysign /keyfile:$(MS_PFX_PATH)</IlmergeCommand>
<IlmergeCommand>$(IlmergeCommand) $(PathToBuiltNuGetExe) @(BuildArtifacts->'%(fullpath)', ' ')</IlmergeCommand>
<IlmergeCommand>$(IlmergeCommand) @(LocResource->'%(fullpath)', ' ')</IlmergeCommand>
</PropertyGroup>
<MakeDir Directories="$([System.IO.Path]::GetDirectoryName($(PathToMergedNuGetExe)))" />
<Exec Command="$(IlmergeCommand)" ContinueOnError="false" />
Expand Down
19 changes: 19 additions & 0 deletions src/NuGet.Clients/NuGet.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public static int MainCore(string workingDirectory, string[] args)
private void Initialize(CoreV2.NuGet.IFileSystem fileSystem, IConsole console)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
AppDomain.CurrentDomain.ResourceResolve += CurrentDomain_ResourceResolve;

using (var catalog = new AggregateCatalog(new AssemblyCatalog(GetType().Assembly)))
{
Expand All @@ -231,6 +232,24 @@ private void Initialize(CoreV2.NuGet.IFileSystem fileSystem, IConsole console)
}
}

private Assembly CurrentDomain_ResourceResolve(object sender, ResolveEventArgs args)
{
Assembly returnedResource = null;

// We want to intercept NuGet.Resources resources and redirect it to nuget.exe assembly
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
if (!args.Name.StartsWith("NuGet.CommandLine", StringComparison.OrdinalIgnoreCase) && args.Name.StartsWith("NuGet", StringComparison.OrdinalIgnoreCase) && string.Equals("NuGet.Resources", args.RequestingAssembly.GetName().Name, StringComparison.OrdinalIgnoreCase))
{
ManifestResourceInfo resource = NuGetExeAssembly.GetManifestResourceInfo(args.Name);
if (resource != null)
{
// Return nuget.exe assembly, since it contains the requested resource by NuGet.Resources assembly
returnedResource = NuGetExeAssembly;
}
}

return returnedResource;
}

// This method acts as a binding redirect
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Expand Down