-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
GetReferenceAssemblyPaths.cs
295 lines (255 loc) · 11.1 KB
/
GetReferenceAssemblyPaths.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
// <summary>Get the reference assembly paths for a given target framework version / moniker.</summary>
//-----------------------------------------------------------------------
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Build.Shared;
using System.Collections.Generic;
using FrameworkNameVersioning = System.Runtime.Versioning.FrameworkName;
using SystemProcessorArchitecture = System.Reflection.ProcessorArchitecture;
namespace Microsoft.Build.Tasks
{
/// <summary>
/// Returns the reference assembly paths to the various frameworks
/// </summary>
public class GetReferenceAssemblyPaths : TaskExtension
{
#region Data
/// <summary>
/// This is the sentinel assembly for .NET FX 3.5 SP1
/// Used to determine if SP1 of 3.5 is installed
/// </summary>
private static readonly string s_NET35SP1SentinelAssemblyName = "System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL";
/// <summary>
/// Cache in a static whether or not we have found the 35sp1sentinel assembly.
/// </summary>
private static bool? s_net35SP1SentinelAssemblyFound;
/// <summary>
/// Hold the reference assembly paths based on the passed in targetframeworkmoniker.
/// </summary>
private IList<string> _tfmPaths;
/// <summary>
/// Hold the reference assembly paths based on the passed in targetframeworkmoniker without considering any profile passed in.
/// </summary>
private IList<string> _tfmPathsNoProfile;
/// <summary>
/// Target framework moniker string passd into the task
/// </summary>
private string _targetFrameworkMoniker;
/// <summary>
/// The root path to use to generate the reference assemblyPaths
/// </summary>
private string _rootPath;
/// <summary>
/// By default GetReferenceAssemblyPaths performs simple checks
/// to ensure that certain runtime frameworks are installed depending on the
/// target framework.
/// set bypassFrameworkInstallChecks to true in order to bypass those checks.
/// </summary>
private bool _bypassFrameworkInstallChecks;
#endregion
#region Properties
/// <summary>
/// Returns the path based on the passed in TargetFrameworkMoniker. If the TargetFrameworkMoniker is null or empty
/// this path will be empty.
/// </summary>
[Output]
public string[] ReferenceAssemblyPaths
{
get
{
if (_tfmPaths != null)
{
string[] pathsToReturn = new string[_tfmPaths.Count];
_tfmPaths.CopyTo(pathsToReturn, 0);
return pathsToReturn;
}
else
{
return new string[0];
}
}
}
/// <summary>
/// Returns the path based on the passed in TargetFrameworkMoniker without considering the profile part of the moniker. If the TargetFrameworkMoniker is null or empty
/// this path will be empty.
/// </summary>
[Output]
public string[] FullFrameworkReferenceAssemblyPaths
{
get
{
if (_tfmPathsNoProfile != null)
{
string[] pathsToReturn = new string[_tfmPathsNoProfile.Count];
_tfmPathsNoProfile.CopyTo(pathsToReturn, 0);
return pathsToReturn;
}
else
{
return new string[0];
}
}
}
/// <summary>
/// The target framework moniker to get the reference assembly paths for
/// </summary>
public string TargetFrameworkMoniker
{
get
{
return _targetFrameworkMoniker;
}
set
{
_targetFrameworkMoniker = value;
}
}
/// <summary>
/// The root path to use to generate the reference assembly path
/// </summary>
public string RootPath
{
get
{
return _rootPath;
}
set
{
_rootPath = value;
}
}
/// <summary>
/// By default GetReferenceAssemblyPaths performs simple checks
/// to ensure that certain runtime frameworks are installed depending on the
/// target framework.
/// set BypassFrameworkInstallChecks to true in order to bypass those checks.
/// </summary>
public bool BypassFrameworkInstallChecks
{
get
{
return _bypassFrameworkInstallChecks;
}
set
{
_bypassFrameworkInstallChecks = value;
}
}
/// <summary>
/// Gets the display name for the targetframeworkmoniker
/// </summary>
[Output]
public string TargetFrameworkMonikerDisplayName
{
get;
set;
}
#endregion
#region ITask Members
/// <summary>
/// If the target framework moniker is set, generate the correct Paths.
/// </summary>
public override bool Execute()
{
FrameworkNameVersioning moniker = null;
FrameworkNameVersioning monikerWithNoProfile = null;
// Are we targeting a profile.
bool targetingProfile = false;
try
{
moniker = new FrameworkNameVersioning(TargetFrameworkMoniker);
targetingProfile = !String.IsNullOrEmpty(moniker.Profile);
// If we are targeting a profile we need to generate a set of reference assembly paths which describe where the full framework
// exists, to do so we need to get the reference assembly location without the profile as part of the moniker.
if (targetingProfile)
{
monikerWithNoProfile = new FrameworkNameVersioning(moniker.Identifier, moniker.Version);
}
// This is a very specific "hack" to ensure that when we're targeting certain .NET Framework versions that
// WPF gets to rely on .NET FX 3.5 SP1 being installed on the build machine.
// This only needs to occur when we are targeting a .NET FX prior to v4.0
if (!_bypassFrameworkInstallChecks && moniker.Identifier.Equals(".NETFramework", StringComparison.OrdinalIgnoreCase) &&
moniker.Version.Major < 4)
{
// We have not got a value for whether or not the 35 sentinel assembly has been found
if (!s_net35SP1SentinelAssemblyFound.HasValue)
{
// get an assemblyname from the string representation of the sentinel assembly name
AssemblyNameExtension sentinelAssemblyName = new AssemblyNameExtension(s_NET35SP1SentinelAssemblyName);
string path = GlobalAssemblyCache.GetLocation(sentinelAssemblyName, SystemProcessorArchitecture.MSIL, runtimeVersion => "v2.0.50727", new Version("2.0.57027"), false, new FileExists(FileUtilities.FileExistsNoThrow), GlobalAssemblyCache.pathFromFusionName, GlobalAssemblyCache.gacEnumerator, false);
s_net35SP1SentinelAssemblyFound = !String.IsNullOrEmpty(path);
}
// We did not find the SP1 sentinel assembly in the GAC. Therefore we must assume that SP1 isn't installed
if (!s_net35SP1SentinelAssemblyFound.Value)
{
Log.LogErrorWithCodeFromResources("GetReferenceAssemblyPaths.NETFX35SP1NotIntstalled", TargetFrameworkMoniker);
}
}
}
catch (ArgumentException e)
{
Log.LogErrorWithCodeFromResources("GetReferenceAssemblyPaths.InvalidTargetFrameworkMoniker", TargetFrameworkMoniker, e.Message);
return false;
}
try
{
_tfmPaths = GetPaths(_rootPath, moniker);
if (_tfmPaths != null && _tfmPaths.Count > 0)
{
TargetFrameworkMonikerDisplayName = ToolLocationHelper.GetDisplayNameForTargetFrameworkDirectory(_tfmPaths[0], moniker);
}
// If there is a profile get the paths without the profile.
// There is no point in generating the full framework paths if profile path could not be found.
if (targetingProfile && _tfmPaths != null)
{
_tfmPathsNoProfile = GetPaths(_rootPath, monikerWithNoProfile);
}
// The path with out the profile is just the referecne assembly paths.
if (!targetingProfile)
{
_tfmPathsNoProfile = _tfmPaths;
}
}
catch (Exception e)
{
// The reason we need to do exception E here is because we are in a task and have the ability to log the message and give the user
// feedback as to its cause, tasks if at all possible should not have exception leave them.
Log.LogErrorWithCodeFromResources("GetReferenceAssemblyPaths.ProblemGeneratingReferencePaths", TargetFrameworkMoniker, e.Message);
if (ExceptionHandling.IsCriticalException(e))
{
throw;
}
_tfmPathsNoProfile = null;
TargetFrameworkMonikerDisplayName = null;
}
return !Log.HasLoggedErrors;
}
/// <summary>
/// Generate the set of chained reference assembly paths
/// </summary>
private IList<String> GetPaths(string rootPath, FrameworkNameVersioning frameworkmoniker)
{
IList<String> pathsToReturn = null;
if (String.IsNullOrEmpty(rootPath))
{
pathsToReturn = ToolLocationHelper.GetPathToReferenceAssemblies(frameworkmoniker);
}
else
{
pathsToReturn = ToolLocationHelper.GetPathToReferenceAssemblies(rootPath, frameworkmoniker);
}
// No reference assembly paths could be found, log a warning as there could be future errors which may be confusing because of this.
if (pathsToReturn.Count == 0)
{
Log.LogWarningWithCodeFromResources("GetReferenceAssemblyPaths.NoReferenceAssemblyDirectoryFound", frameworkmoniker.ToString());
}
return pathsToReturn;
}
#endregion
}
}