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

(GH-769) API - Resolve Merged Assemblies #776

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
10 changes: 9 additions & 1 deletion src/chocolatey/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace chocolatey
{
using System.IO;
using System.Linq;
using System.Reflection;
using infrastructure.adapters;

/// <summary>
Expand Down Expand Up @@ -71,7 +72,14 @@ public static string get_public_key_token(this IAssembly assembly)
{
if (assembly == null) return string.Empty;

byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken();
return assembly.GetName().get_public_key_token();
}

public static string get_public_key_token(this AssemblyName assemblyName)
{
if (assemblyName == null) return string.Empty;

byte[] publicKeyToken = assemblyName.GetPublicKeyToken();

if (publicKeyToken == null || publicKeyToken.Length == 0) return string.Empty;

Expand Down
23 changes: 21 additions & 2 deletions src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ namespace chocolatey
{
using System;
using System.Collections.Generic;
using System.Reflection;
using infrastructure.licensing;
using NuGet;
using SimpleInjector;
using infrastructure.adapters;
using infrastructure.app;
using infrastructure.app.builders;
using infrastructure.app.configuration;
Expand All @@ -30,6 +29,7 @@ namespace chocolatey
using infrastructure.logging;
using infrastructure.registration;
using resources;
using Assembly = infrastructure.adapters.Assembly;
using IFileSystem = infrastructure.filesystem.IFileSystem;

// ReSharper disable InconsistentNaming
Expand All @@ -41,8 +41,27 @@ public static class Lets
{
public static GetChocolatey GetChocolatey()
{
add_assembly_resolver();
return new GetChocolatey();
}

private static ResolveEventHandler _handler = null;

private static void add_assembly_resolver()
{
_handler = (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.get_public_key_token().is_equal_to(ApplicationParameters.OfficialChocolateyPublicKey))
{
return typeof(Lets).Assembly;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RichiCoder1 It's checking the public key token of the requested assembly - meaning that when choco is pulling an item is has merged in, the keys will match. So it will use the internal instead of an external conflicting Assembly. Does this answer your question?

}

return null;
};

AppDomain.CurrentDomain.AssemblyResolve += _handler;
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe
}

var token = Assembly.GetExecutingAssembly().get_public_key_token();
if (string.IsNullOrWhiteSpace(token) || token != ApplicationParameters.OfficialChocolateyPublicKey)
if (string.IsNullOrWhiteSpace(token) || !token.is_equal_to(ApplicationParameters.OfficialChocolateyPublicKey))
{
if (!config.AllowUnofficialBuild)
{
Expand Down