|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-Present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Diagnostics; |
| 13 | +using System.IO; |
| 14 | +using System.Reflection; |
| 15 | + |
| 16 | +namespace React.MSBuild |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Hacks around the fact that it's not possible to do assembly binding redirects in MSBuild. |
| 20 | + /// |
| 21 | + /// https://github.com/Microsoft/msbuild/issues/1309 |
| 22 | + /// http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/ |
| 23 | + /// </summary> |
| 24 | + public static class AssemblyBindingRedirect |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Redirects that have been configured |
| 28 | + /// </summary> |
| 29 | + private static readonly Dictionary<string, Version> _redirects = new Dictionary<string, Version>(); |
| 30 | + |
| 31 | + static AssemblyBindingRedirect() |
| 32 | + { |
| 33 | + // This is in a static constructor because it needs to run as early as possible |
| 34 | + ConfigureRedirect("JavaScriptEngineSwitcher.Core"); |
| 35 | + AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Enables assembly binding redirects |
| 40 | + /// </summary> |
| 41 | + public static void Enable() |
| 42 | + { |
| 43 | + // Intentionally empty. This is just meant to ensure the static constructor |
| 44 | + // has run. |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Configures a redirect for the specified assembly. Redirects to the version in the bin directory. |
| 49 | + /// </summary> |
| 50 | + /// <param name="name">Name of the assembly to redirect</param> |
| 51 | + private static void ConfigureRedirect(string name) |
| 52 | + { |
| 53 | + var currentAssemblyPath = Assembly.GetExecutingAssembly().Location; |
| 54 | + var redirectAssemblyPath = Path.Combine( |
| 55 | + Path.GetDirectoryName(currentAssemblyPath), |
| 56 | + name + ".dll" |
| 57 | + ); |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + var realAssembly = Assembly.LoadFile(redirectAssemblyPath); |
| 62 | + var version = realAssembly.GetName().Version; |
| 63 | + _redirects[name] = version; |
| 64 | + } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + Trace.WriteLine("Warning: Could not determine version of " + name + " to use! " + ex.Message); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Overrides assembly resolution to redirect if necessary. |
| 73 | + /// </summary> |
| 74 | + private static Assembly ResolveAssembly(object sender, ResolveEventArgs args) |
| 75 | + { |
| 76 | + var requestedAssembly = new AssemblyName(args.Name); |
| 77 | + |
| 78 | + if (_redirects.ContainsKey(requestedAssembly.Name) && requestedAssembly.Version != _redirects[requestedAssembly.Name]) |
| 79 | + { |
| 80 | + requestedAssembly.Version = _redirects[requestedAssembly.Name]; |
| 81 | + return Assembly.Load(requestedAssembly); |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments