forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyUtils.cs
More file actions
185 lines (163 loc) · 8.38 KB
/
Copy pathAssemblyUtils.cs
File metadata and controls
185 lines (163 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using Microsoft.Internal.Web.Utils;
using Microsoft.Web.Infrastructure;
namespace System.Web.WebPages.Deployment
{
internal static class AssemblyUtils
{
// Copied from AssemblyRefs.cs
private const string SharedLibPublicKey = "31bf3856ad364e35";
internal static readonly AssemblyName ThisAssemblyName = new AssemblyName(typeof(AssemblyUtils).Assembly.FullName);
internal static readonly Version WebPagesV1Version = new Version(1, 0, 0, 0);
private static readonly string _binFileName = Path.GetFileName(ThisAssemblyName.Name) + ".dll";
// Special case MWI because it does not share the same assembly version as the rest of WebPages.
private static readonly Version _mwiVersion = new AssemblyName(typeof(InfrastructureHelper).Assembly.FullName).Version;
private static readonly AssemblyName _mwiAssemblyName = GetFullName("Microsoft.Web.Infrastructure", _mwiVersion);
private static readonly AssemblyName[] _version1AssemblyList = new[]
{
_mwiAssemblyName,
GetFullName("System.Web.Razor", WebPagesV1Version),
GetFullName("System.Web.Helpers", WebPagesV1Version),
GetFullName("System.Web.WebPages", WebPagesV1Version),
GetFullName("System.Web.WebPages.Administration", WebPagesV1Version),
GetFullName("System.Web.WebPages.Razor", WebPagesV1Version),
GetFullName("WebMatrix.Data", WebPagesV1Version),
GetFullName("WebMatrix.WebData", WebPagesV1Version)
};
private static readonly AssemblyName[] _versionCurrentAssemblyList = new[]
{
_mwiAssemblyName,
GetFullName("System.Web.Razor", ThisAssemblyName.Version),
GetFullName("System.Web.Helpers", ThisAssemblyName.Version),
GetFullName("System.Web.WebPages", ThisAssemblyName.Version),
GetFullName("System.Web.WebPages.Administration", ThisAssemblyName.Version),
GetFullName("System.Web.WebPages.Razor", ThisAssemblyName.Version),
GetFullName("WebMatrix.Data", ThisAssemblyName.Version),
GetFullName("WebMatrix.WebData", ThisAssemblyName.Version)
};
internal static Version GetMaxWebPagesVersion()
{
return GetMaxWebPagesVersion(GetLoadedAssemblies());
}
internal static Version GetMaxWebPagesVersion(IEnumerable<AssemblyName> loadedAssemblies)
{
return GetWebPagesAssemblies(loadedAssemblies).Max(c => c.Version);
}
internal static bool IsVersionAvailable(Version version)
{
return IsVersionAvailable(GetLoadedAssemblies(), version);
}
internal static bool IsVersionAvailable(IEnumerable<AssemblyName> loadedAssemblies, Version version)
{
return GetWebPagesAssemblies(loadedAssemblies).Any(c => c.Version == version);
}
private static IEnumerable<AssemblyName> GetWebPagesAssemblies(IEnumerable<AssemblyName> loadedAssemblies)
{
return (from otherName in loadedAssemblies
where NamesMatch(ThisAssemblyName, otherName, matchVersion: false)
select otherName);
}
/// <summary>
/// Returns the version of a System.Web.WebPages.Deployment.dll if it is present in the bin and matches the name and
/// public key token of the current assembly.
/// </summary>
/// <returns>Version from bin if present, null otherwise.</returns>
internal static Version GetVersionFromBin(string binDirectory, IFileSystem fileSystem, Func<string, AssemblyName> getAssemblyNameThunk = null)
{
// If a version of the assembly is present both in the bin and the GAC, the GAC would win.
// To work around this, we'll look for a physical file on disk with the same name as the current assembly and load it to determine the version.
// Determine if the Deployment assembly is present in the bin
var assemblyInBin = Path.Combine(binDirectory, _binFileName);
if (fileSystem.FileExists(assemblyInBin))
{
try
{
getAssemblyNameThunk = getAssemblyNameThunk ?? AssemblyName.GetAssemblyName;
AssemblyName assemblyName = getAssemblyNameThunk(assemblyInBin);
if (NamesMatch(ThisAssemblyName, assemblyName, matchVersion: false))
{
return assemblyName.Version;
}
}
catch (BadImageFormatException)
{
// Do nothing.
}
catch (SecurityException)
{
// Do nothing
}
catch (FileLoadException)
{
// Do nothing.
}
}
return null;
}
internal static bool NamesMatch(AssemblyName left, AssemblyName right, bool matchVersion)
{
return Equals(left.Name, right.Name) &&
Equals(left.CultureInfo, right.CultureInfo) &&
Enumerable.SequenceEqual(left.GetPublicKeyToken(), right.GetPublicKeyToken()) &&
(!matchVersion || Equals(left.Version, right.Version));
}
internal static IEnumerable<AssemblyName> GetLoadedAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies()
.Select(GetAssemblyName)
.ToList();
}
internal static IEnumerable<AssemblyName> GetAssembliesForVersion(Version version)
{
if (version == WebPagesV1Version)
{
return _version1AssemblyList;
}
return _versionCurrentAssemblyList;
}
private static AssemblyName GetAssemblyName(Assembly assembly)
{
return new AssemblyName(assembly.FullName);
}
private static AssemblyName GetFullName(string name, Version version, string publicKeyToken)
{
return new AssemblyName(String.Format(CultureInfo.InvariantCulture,
"{0}, Version={1}, Culture=neutral, PublicKeyToken={2}",
name, version, publicKeyToken));
}
internal static AssemblyName GetFullName(string name, Version version)
{
return GetFullName(name, version, SharedLibPublicKey);
}
public static IDictionary<string, Version> GetAssembliesMatchingOtherVersions(IDictionary<string, IEnumerable<string>> references)
{
var webPagesAssemblies = AssemblyUtils.GetAssembliesForVersion(AssemblyUtils.ThisAssemblyName.Version);
if (references == null || webPagesAssemblies == null || !webPagesAssemblies.Any())
{
return new Dictionary<string, Version>(0);
}
var matchingVersions = from item in references
let matchedVersion = GetMatchingVersion(webPagesAssemblies, item.Value)
where matchedVersion != null
select new KeyValuePair<string, Version>(item.Key, matchedVersion);
return matchingVersions.ToDictionary(k => k.Key, k => k.Value);
}
private static Version GetMatchingVersion(IEnumerable<AssemblyName> webPagesAssemblies, IEnumerable<string> references)
{
// Return assemblies that match in name but not in version.
var matchingVersions = from webPagesAssembly in webPagesAssemblies
from referenceName in references
let referencedAssembly = new AssemblyName(referenceName)
where AssemblyUtils.NamesMatch(webPagesAssembly, referencedAssembly, matchVersion: false) && webPagesAssembly.Version != referencedAssembly.Version
select referencedAssembly.Version;
return matchingVersions.FirstOrDefault();
}
}
}