-
Notifications
You must be signed in to change notification settings - Fork 695
/
ManifestMetadata.cs
342 lines (281 loc) · 12.2 KB
/
ManifestMetadata.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
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
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using NuGet.Packaging.Core;
using NuGet.Packaging.PackageCreation.Resources;
using NuGet.Versioning;
namespace NuGet.Packaging
{
public class ManifestMetadata : IPackageMetadata
{
private string _minClientVersionString;
private IEnumerable<string> _authors = Enumerable.Empty<string>();
private IEnumerable<string> _owners = Enumerable.Empty<string>();
private string _iconUrl;
private string _licenseUrl;
private string _projectUrl;
public ManifestMetadata()
{
}
/// <summary>
/// Constructs a ManifestMetadata instance from an IPackageMetadata instance
/// </summary>
public ManifestMetadata(IPackageMetadata copy)
{
Id = copy.Id?.Trim();
Version = copy.Version;
Title = copy.Title?.Trim();
Authors = copy.Authors;
Owners = copy.Owners;
Tags = string.IsNullOrEmpty(copy.Tags) ? null : copy.Tags.Trim();
Serviceable = copy.Serviceable;
_licenseUrl = copy.LicenseUrl?.OriginalString;
_projectUrl = copy.ProjectUrl?.OriginalString;
_iconUrl = copy.IconUrl?.OriginalString;
RequireLicenseAcceptance = copy.RequireLicenseAcceptance;
Description = copy.Description?.Trim();
Copyright = copy.Copyright?.Trim();
Summary = copy.Summary?.Trim();
ReleaseNotes = copy.ReleaseNotes?.Trim();
Language = copy.Language?.Trim();
DependencyGroups = copy.DependencyGroups;
FrameworkReferences = copy.FrameworkReferences;
PackageAssemblyReferences = copy.PackageAssemblyReferences;
PackageTypes = copy.PackageTypes;
MinClientVersionString = copy.MinClientVersion?.ToString();
ContentFiles = copy.ContentFiles;
DevelopmentDependency = copy.DevelopmentDependency;
Repository = copy.Repository;
LicenseMetadata = copy.LicenseMetadata;
}
[ManifestVersion(5)]
public string MinClientVersionString
{
get { return _minClientVersionString; }
set
{
Version version = null;
if (!String.IsNullOrEmpty(value) && !System.Version.TryParse(value, out version))
{
throw new InvalidDataException(NuGetResources.Manifest_InvalidMinClientVersion);
}
_minClientVersionString = value;
MinClientVersion = version;
}
}
public Version MinClientVersion { get; private set; }
public string Id { get; set; }
public NuGetVersion Version { get; set; }
public string Title { get; set; }
public IEnumerable<string> Authors
{
get { return _authors; }
set { _authors = value ?? Enumerable.Empty<string>(); }
}
public IEnumerable<string> Owners
{
get { return (_owners == null || !_owners.Any()) ? _authors : _owners; }
set { _owners = value ?? Enumerable.Empty<string>(); }
}
// The (Icon/License/Project)Url properties have backing strings as we need to be able to differentiate
// between the property not being set (valid) and set to an empty value (invalid).
public void SetIconUrl(string iconUrl)
{
_iconUrl = iconUrl;
}
public Uri IconUrl
{
get
{
if (_iconUrl == null)
{
return null;
}
return new Uri(_iconUrl);
}
}
public void SetLicenseUrl(string licenseUrl)
{
_licenseUrl = licenseUrl;
}
public Uri LicenseUrl
{
get
{
if (_licenseUrl == null)
{
return null;
}
return new Uri(_licenseUrl);
}
}
public void SetProjectUrl(string projectUrl)
{
_projectUrl = projectUrl;
}
public Uri ProjectUrl
{
get
{
if (_projectUrl == null)
{
return null;
}
return new Uri(_projectUrl);
}
}
public bool RequireLicenseAcceptance { get; set; }
public bool DevelopmentDependency { get; set; }
public string Description { get; set; }
public string Summary { get; set; }
[ManifestVersion(2)]
public string ReleaseNotes { get; set; }
[ManifestVersion(2)]
public string Copyright { get; set; }
public string Language { get; set; }
public string Tags { get; set; }
public bool Serviceable { get; set; }
public RepositoryMetadata Repository { get; set; }
private IEnumerable<PackageDependencyGroup> _dependencyGroups = new List<PackageDependencyGroup>();
public IEnumerable<PackageDependencyGroup> DependencyGroups
{
get
{
return _dependencyGroups;
}
set
{
_dependencyGroups = MergeDependencyGroups(value);
}
}
public IEnumerable<FrameworkAssemblyReference> FrameworkReferences { get; set; } = new List<FrameworkAssemblyReference>();
private IEnumerable<PackageReferenceSet> _packageAssemblyReferences = new List<PackageReferenceSet>();
[ManifestVersion(2)]
public IEnumerable<PackageReferenceSet> PackageAssemblyReferences
{
get
{
return _packageAssemblyReferences;
}
set
{
_packageAssemblyReferences = MergePackageAssemblyReferences(value);
}
}
private static IEnumerable<PackageReferenceSet> MergePackageAssemblyReferences(IEnumerable<PackageReferenceSet> referenceSets)
{
if (referenceSets == null)
{
Enumerable.Empty<PackageReferenceSet>();
}
var referenceSetGroups = referenceSets.GroupBy(set => set.TargetFramework);
var groupedReferenceSets = referenceSetGroups.Select(group => new PackageReferenceSet(group.Key, group.SelectMany(g => g.References)))
.ToList();
int nullTargetFrameworkIndex = groupedReferenceSets.FindIndex(set => set.TargetFramework == null);
if (nullTargetFrameworkIndex > -1)
{
var nullFxReferenceSet = groupedReferenceSets[nullTargetFrameworkIndex];
groupedReferenceSets.RemoveAt(nullTargetFrameworkIndex);
groupedReferenceSets.Insert(0, nullFxReferenceSet);
}
return groupedReferenceSets;
}
public IEnumerable<ManifestContentFiles> ContentFiles { get; set; } = new List<ManifestContentFiles>();
public IEnumerable<PackageType> PackageTypes { get; set; } = new List<PackageType>();
public LicenseMetadata LicenseMetadata { get; set; } = null;
private static IEnumerable<PackageDependencyGroup> MergeDependencyGroups(IEnumerable<PackageDependencyGroup> actualDependencyGroups)
{
if (actualDependencyGroups == null)
{
return Enumerable.Empty<PackageDependencyGroup>();
}
var dependencyGroups = actualDependencyGroups.Select(CreatePackageDependencyGroup);
// group the dependency sets with the same target framework together.
var dependencySetGroups = dependencyGroups.GroupBy(set => set.TargetFramework);
var groupedDependencySets = dependencySetGroups.Select(group => new PackageDependencyGroup(group.Key, new HashSet<PackageDependency>(group.SelectMany(g => g.Packages))))
.ToList();
// move the group with the any target framework (if any) to the front just for nicer display in UI
int anyTargetFrameworkIndex = groupedDependencySets.FindIndex(set => set.TargetFramework.IsAny);
if (anyTargetFrameworkIndex > -1)
{
var anyFxDependencySet = groupedDependencySets[anyTargetFrameworkIndex];
groupedDependencySets.RemoveAt(anyTargetFrameworkIndex);
groupedDependencySets.Insert(0, anyFxDependencySet);
}
return groupedDependencySets;
}
private static PackageDependencyGroup CreatePackageDependencyGroup(PackageDependencyGroup dependencyGroup)
{
ISet<PackageDependency> dependencies;
if (dependencyGroup.Packages == null)
{
dependencies = new HashSet<PackageDependency>();
}
else
{
var dependenciesList = dependencyGroup.Packages.Select(dependency =>
new PackageDependency(
dependency.Id.SafeTrim(),
dependency.VersionRange,
dependency.Include,
dependency.Exclude)).ToList();
dependencies = new HashSet<PackageDependency>(dependenciesList);
}
return new PackageDependencyGroup(dependencyGroup.TargetFramework, dependencies);
}
public IEnumerable<string> Validate()
{
if (String.IsNullOrEmpty(Id))
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Id");
}
else
{
if (Id.Length > PackageIdValidator.MaxPackageIdLength)
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_IdMaxLengthExceeded);
}
else if (!PackageIdValidator.IsValidPackageId(Id))
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.InvalidPackageId, Id);
}
}
if (Version == null)
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Version");
}
if ((Authors == null || !Authors.Any(author => !String.IsNullOrEmpty(author))) && !PackageTypes.Contains(PackageType.SymbolsPackage))
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Authors");
}
if (String.IsNullOrEmpty(Description))
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Description");
}
if (_licenseUrl == String.Empty)
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "LicenseUrl");
}
if (_iconUrl == String.Empty)
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "IconUrl");
}
if (_projectUrl == String.Empty)
{
yield return String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "ProjectUrl");
}
if (RequireLicenseAcceptance && (string.IsNullOrWhiteSpace(_licenseUrl) && LicenseMetadata == null))
{
yield return NuGetResources.Manifest_RequireLicenseAcceptanceRequiresLicenseUrl;
}
if(_licenseUrl != null && LicenseMetadata != null && !_licenseUrl.Equals(LicenseMetadata.LicenseUrl))
{
yield return NuGetResources.Manifest_LicenseUrlCannotBeUsedWithLicenseMetadata;
}
}
}
}