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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
//
// PackageSpecCreator.cs
//
// Author:
// Matt Ward <matt.ward@xamarin.com>
//
// Copyright (c) 2016 Xamarin Inc. (http://xamarin.com)
// Copyright (c) .NET Foundation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Based on parts of src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs
using System;
using System.Collections.Generic;
using System.Linq;
using MonoDevelop.Projects;
using NuGet.Commands;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.ProjectModel;
using NuGet.RuntimeModel;
using NuGet.Versioning;
namespace MonoDevelop.PackageManagement
{
static class PackageSpecCreator
{
public static PackageSpec CreatePackageSpec (DotNetProject project)
{
return CreatePackageSpec (new DotNetProjectProxy (project));
}
public static PackageSpec CreatePackageSpec (IDotNetProject project)
{
var packageSpec = new PackageSpec (GetTargetFrameworks (project));
packageSpec.FilePath = project.FileName;
packageSpec.Name = project.Name;
packageSpec.Version = GetVersion (project);
packageSpec.RestoreMetadata = CreateRestoreMetadata (packageSpec, project);
packageSpec.RuntimeGraph = GetRuntimeGraph (project);
AddProjectReferences (packageSpec, project);
AddPackageReferences (packageSpec, project);
AddPackageTargetFallbacks (packageSpec, project);
return packageSpec;
}
static IList<TargetFrameworkInformation> GetTargetFrameworks (IDotNetProject project)
{
return GetFrameworks (project)
.Select (framework => new TargetFrameworkInformation {
FrameworkName = framework
})
.ToList();
}
static IEnumerable<NuGetFramework> GetFrameworks (IDotNetProject project)
{
return GetOriginalTargetFrameworks (project)
.Select (NuGetFramework.Parse);
}
/// <summary>
/// Return the parsed version or 1.0.0 if the property does not exist.
/// </summary>
static NuGetVersion GetVersion (IDotNetProject project)
{
string versionString = project.EvaluatedProperties.GetValue ("Version");
if (string.IsNullOrEmpty(versionString)) {
// Default to 1.0.0 if the property does not exist
return new NuGetVersion (1, 0, 0);
}
// Snapshot versions are not allowed in .NETCore
return NuGetVersion.Parse (versionString);
}
static ProjectRestoreMetadata CreateRestoreMetadata (PackageSpec packageSpec, IDotNetProject project)
{
return new ProjectRestoreMetadata {
ProjectStyle = ProjectStyle.PackageReference,
ProjectPath = project.FileName,
ProjectName = packageSpec.Name,
ProjectUniqueName = project.FileName,
OutputPath = project.BaseIntermediateOutputPath,
OriginalTargetFrameworks = GetOriginalTargetFrameworks (project).ToList ()
};
}
static IEnumerable<string> GetOriginalTargetFrameworks (IDotNetProject project)
{
var properties = project.EvaluatedProperties;
if (properties != null) {
string targetFramework = properties.GetValue ("TargetFramework");
if (targetFramework != null) {
return new [] { targetFramework };
}
string targetFrameworks = properties.GetValue ("TargetFrameworks");
return MSBuildStringUtility.Split (targetFrameworks);
}
return new string[0];
}
static void AddProjectReferences (PackageSpec spec, IDotNetProject project)
{
// Add groups for each spec framework
var frameworkGroups = new Dictionary<NuGetFramework, List<ProjectRestoreReference>> ();
foreach (var framework in spec.TargetFrameworks.Select (e => e.FrameworkName).Distinct ()) {
frameworkGroups.Add (framework, new List<ProjectRestoreReference> ());
}
var flatReferences = project.References.Where (projectReference => projectReference.ReferenceType == ReferenceType.Project)
.Select (projectReference => GetProjectRestoreReference (projectReference, project));
// Add project paths
foreach (var frameworkPair in flatReferences) {
// If no frameworks were given, apply to all
var addToFrameworks = frameworkPair.Item1.Count == 0
? frameworkGroups.Keys.ToList ()
: frameworkPair.Item1;
foreach (var framework in addToFrameworks) {
List<ProjectRestoreReference> references;
if (frameworkGroups.TryGetValue (framework, out references)) {
// Ensure unique
if (!references
.Any(e => e.ProjectUniqueName
.Equals (frameworkPair.Item2.ProjectUniqueName, StringComparison.OrdinalIgnoreCase))) {
references.Add (frameworkPair.Item2);
}
}
}
}
// Add groups to spec
foreach (var frameworkPair in frameworkGroups) {
spec.RestoreMetadata.TargetFrameworks.Add (new ProjectRestoreMetadataFrameworkInfo (frameworkPair.Key) {
ProjectReferences = frameworkPair.Value
});
}
}
static Tuple<List<NuGetFramework>, ProjectRestoreReference> GetProjectRestoreReference (
ProjectReference item,
IDotNetProject project)
{
var frameworks = GetFrameworks (project).ToList ();
var referencedProject = project.ParentSolution.ResolveProject (item);
var reference = new ProjectRestoreReference () {
ProjectPath = referencedProject.FileName,
ProjectUniqueName = referencedProject.FileName,
};
ApplyIncludeFlags (
reference,
item.Metadata.GetValue ("IncludeAssets"),
item.Metadata.GetValue ("ExcludeAssets"),
item.Metadata.GetValue ("PrivateAssets"));
return new Tuple<List<NuGetFramework>, ProjectRestoreReference> (frameworks, reference);
}
static void AddPackageReferences (PackageSpec packageSpec, IDotNetProject project)
{
foreach (var packageReference in project.GetPackageReferences ()) {
var dependency = new LibraryDependency ();
dependency.LibraryRange = new LibraryRange (
name: packageReference.Include,
versionRange: GetVersionRange (packageReference),
typeConstraint: LibraryDependencyTarget.Package);
ApplyIncludeFlags (dependency, packageReference);
var frameworks = GetProjectFrameworks (project);
if (frameworks.Count == 0) {
AddDependencyIfNotExist (packageSpec, dependency);
} else {
foreach (var framework in frameworks) {
AddDependencyIfNotExist (packageSpec, framework, dependency);
}
}
}
}
static VersionRange GetVersionRange (ProjectPackageReference packageReference)
{
string versionRange = packageReference.Metadata.GetValue ("Version");
if (!string.IsNullOrEmpty (versionRange)) {
return VersionRange.Parse (versionRange);
}
return VersionRange.All;
}
static void ApplyIncludeFlags (LibraryDependency dependency, ProjectPackageReference packageReference)
{
var includeFlags = GetIncludeFlags (packageReference.Metadata.GetValue ("IncludeAssets"), LibraryIncludeFlags.All);
var excludeFlags = GetIncludeFlags (packageReference.Metadata.GetValue ("ExcludeAssets"), LibraryIncludeFlags.None);
dependency.IncludeType = includeFlags & ~excludeFlags;
dependency.SuppressParent = GetIncludeFlags (packageReference.Metadata.GetValue ("PrivateAssets"), LibraryIncludeFlagUtils.DefaultSuppressParent);
}
static void ApplyIncludeFlags (
ProjectRestoreReference dependency,
string includeAssets,
string excludeAssets,
string privateAssets)
{
dependency.IncludeAssets = GetIncludeFlags (includeAssets, LibraryIncludeFlags.All);
dependency.ExcludeAssets = GetIncludeFlags (excludeAssets, LibraryIncludeFlags.None);
dependency.PrivateAssets = GetIncludeFlags (privateAssets, LibraryIncludeFlagUtils.DefaultSuppressParent);
}
static LibraryIncludeFlags GetIncludeFlags (string value, LibraryIncludeFlags defaultValue)
{
var parts = MSBuildStringUtility.Split (value);
if (parts.Length > 0) {
return LibraryIncludeFlagUtils.GetFlags (parts);
} else {
return defaultValue;
}
}
static HashSet<NuGetFramework> GetProjectFrameworks (IDotNetProject project)
{
return new HashSet<NuGetFramework> (
GetOriginalTargetFrameworks (project).Select (NuGetFramework.Parse));
}
static HashSet<NuGetFramework> GetFrameworks (ProjectItem item)
{
return new HashSet<NuGetFramework> (
GetFrameworksStrings (item).Select (NuGetFramework.Parse));
}
static HashSet<string> GetFrameworksStrings (ProjectItem item)
{
var frameworks = new HashSet<string> (StringComparer.OrdinalIgnoreCase);
string frameworksString = item.Metadata.GetValue ("TargetFrameworks");
if (!string.IsNullOrEmpty(frameworksString)) {
frameworks.UnionWith (MSBuildStringUtility.Split (frameworksString));
}
return frameworks;
}
private static bool AddDependencyIfNotExist (PackageSpec packageSpec, LibraryDependency dependency)
{
if (!packageSpec.Dependencies
.Select (d => d.Name)
.Contains (dependency.Name, StringComparer.OrdinalIgnoreCase)) {
packageSpec.Dependencies.Add (dependency);
return true;
}
return false;
}
static bool AddDependencyIfNotExist (PackageSpec packageSpec, NuGetFramework framework, LibraryDependency dependency)
{
var frameworkInfo = packageSpec.GetTargetFramework (framework);
if (!packageSpec.Dependencies
.Concat (frameworkInfo.Dependencies)
.Select (d => d.Name)
.Contains (dependency.Name, StringComparer.OrdinalIgnoreCase)) {
frameworkInfo.Dependencies.Add (dependency);
return true;
}
return false;
}
static void AddPackageTargetFallbacks (PackageSpec packageSpec, IDotNetProject project)
{
var fallbackList = GetPackageTargetFallbackList (project)
.Select (NuGetFramework.Parse)
.ToList ();
if (!fallbackList.Any ())
return;
var frameworks = GetProjectFrameworks (project);
foreach (var framework in frameworks) {
var frameworkInfo = packageSpec.GetTargetFramework (framework);
frameworkInfo.Imports = fallbackList;
frameworkInfo.FrameworkName = new FallbackFramework (frameworkInfo.FrameworkName, fallbackList);
}
}
static IEnumerable<string> GetPackageTargetFallbackList (IDotNetProject project)
{
var properties = project.EvaluatedProperties;
if (properties != null) {
return MSBuildStringUtility.Split (properties.GetValue ("PackageTargetFallback"));
}
return new string[0];
}
static RuntimeGraph GetRuntimeGraph (IDotNetProject project)
{
var runtimes = MSBuildStringUtility.Split (project.EvaluatedProperties.GetValue ("RuntimeIdentifiers"))
.Distinct (StringComparer.Ordinal)
.Select (rid => new RuntimeDescription (rid))
.ToList ();
var supports = MSBuildStringUtility.Split (project.EvaluatedProperties.GetValue ("RuntimeSupports"))
.Distinct (StringComparer.Ordinal)
.Select (s => new CompatibilityProfile(s))
.ToList ();
return new RuntimeGraph (runtimes, supports);
}
}
}
|