This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
nuget.cake
699 lines (604 loc) · 27.1 KB
/
nuget.cake
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
///////////////////////////////////////////////////////////////////////////////
// COMMAND LINE ARGUMENTS
//
// --localSource [a directory path or URL to a NuGet source]
// This can be used to provide access to the build
// artifacts to use as the base for the fat
// packages.
// --packagesPath [a directory path to download NuGet packages to]
// This can be used to change where the existing
// packages get downloaded to temporarily.
// --workingPath [a directory path to perform work inside of]
// This can be used to change the directory which
// is used to process files for packaging.
// --outputPath [a directory path to output processed packages to]
// This can be used to change the output path of the
// processed packages.
// --keepLatestVersion [True|False]
// This can be used to indicate that the latest
// version should not be incremented, rather use
// the version in the fat package.
// --incrementVersion [True|False]
// This can be used to indicate that the fat packages
// should have new versions.
// --packLatestOnly [True|False]
// This can be used to indicate that only the latest
// version should be packed.
// --useExplicitVersion [True|False]
// This can be used to indicate that the dependencies
// should use hard/exact versions: [x.x.x]
// --prereleaseLabel [Prelease label to use for the new package version]
// This can be used to add a prerelease label to the
// nuget package version being created
///////////////////////////////////////////////////////////////////////////////
// EXAMPLE USE CASE
//
// In order to have this script run as part of CI to make fat packages, use the
// following options:
//
// .\build.ps1 `
// --localSource="<path-to-build-directory>" `
// --incrementVersion=False `
// --packLatestOnly=True `
// --useExplicitVersion=True
// --prereleaseLabel=rc1
// --packagesPath="./externals/packages"
// --workingPath="./working/packages"
// --outputPath="./output/packages"
//
///////////////////////////////////////////////////////////////////////////////
#addin nuget:?package=Cake.FileHelpers&version=3.2.1
#addin nuget:?package=NuGet.Packaging&version=4.7.0&loaddependencies=true
#addin nuget:?package=NuGet.Protocol&version=4.7.0&loaddependencies=true
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
var target = Argument("target", "Default");
var localSource = Argument("localSource", "");
var keepLatestVersion = Argument("keepLatestVersion", false);
var incrementVersion = Argument("incrementVersion", true);
var packLatestOnly = Argument("packLatestOnly", false);
var useExplicitVersion = Argument("useExplicitVersion", true);
var prereleaseLabel = Argument("prereleaseLabel", (string)null);
var packagesPath = (DirectoryPath)Argument("packagesPath", "externals/packages");
var workingPath = (DirectoryPath)Argument("workingPath", "working/packages");
var outputPath = (DirectoryPath)Argument("outputPath", "output/packages");
if (!string.IsNullOrEmpty(localSource)) {
localSource = MakeAbsolute((DirectoryPath)localSource).FullPath;
}
var apiLevelVersion = new Dictionary<int, NuGetFramework> {
// { 1, NuGetFramework.Parse("monoandroid1.0") },
// { 15, NuGetFramework.Parse("monoandroid4.0.3") },
// { 19, NuGetFramework.Parse("monoandroid4.4") },
// { 21, NuGetFramework.Parse("monoandroid5.0") },
// { 22, NuGetFramework.Parse("monoandroid5.1") },
{ 23, NuGetFramework.Parse("monoandroid6.0") },
{ 24, NuGetFramework.Parse("monoandroid7.0") },
{ 25, NuGetFramework.Parse("monoandroid7.1") },
{ 26, NuGetFramework.Parse("monoandroid8.0") },
{ 27, NuGetFramework.Parse("monoandroid8.1") },
{ 28, NuGetFramework.Parse("monoandroid9.0") },
};
var minimumVersion = new Dictionary<string, NuGetVersion> {
{ "xamarin.android.arch", new NuGetVersion(1, 0, 0) },
{ "xamarin.android.support", new NuGetVersion(23, 4, 0) },
};
var blacklistIdPrefix = new List<string> {
"xamarin.build.download",
"xamarin.android.support.constraint.layout",
"xamarin.google.guava",
};
var seedPackages = new [] {
"Xamarin.Android.Arch.Core.Common",
"Xamarin.Android.Arch.Core.Runtime",
"Xamarin.Android.Arch.Lifecycle.Common",
"Xamarin.Android.Arch.Lifecycle.Extensions",
"Xamarin.Android.Arch.Lifecycle.LiveData",
"Xamarin.Android.Arch.Lifecycle.LiveData.Core",
"Xamarin.Android.Arch.Lifecycle.Runtime",
"Xamarin.Android.Arch.Lifecycle.ViewModel",
"Xamarin.Android.Arch.Persistence.Db",
"Xamarin.Android.Arch.Persistence.Db.Framework",
"Xamarin.Android.Arch.Persistence.Room.Common",
"Xamarin.Android.Arch.Persistence.Room.Runtime",
"Xamarin.Android.Arch.Work.Runtime",
"Xamarin.Android.Support.Animated.Vector.Drawable",
"Xamarin.Android.Support.Annotations",
"Xamarin.Android.Support.AsyncLayoutInflater",
"Xamarin.Android.Support.Collections",
"Xamarin.Android.Support.Compat",
"Xamarin.Android.Support.CoordinatorLayout",
"Xamarin.Android.Support.Core.UI",
"Xamarin.Android.Support.Core.Utils",
"Xamarin.Android.Support.CursorAdapter",
"Xamarin.Android.Support.CustomTabs",
"Xamarin.Android.Support.CustomView",
"Xamarin.Android.Support.Design",
"Xamarin.Android.Support.DocumentFile",
"Xamarin.Android.Support.DrawerLayout",
"Xamarin.Android.Support.Dynamic.Animation",
"Xamarin.Android.Support.Emoji",
"Xamarin.Android.Support.Emoji.AppCompat",
"Xamarin.Android.Support.Emoji.Bundled",
"Xamarin.Android.Support.Exif",
"Xamarin.Android.Support.Fragment",
"Xamarin.Android.Support.HeifWriter",
"Xamarin.Android.Support.Interpolator",
"Xamarin.Android.Support.Loader",
"Xamarin.Android.Support.LocalBroadcastManager",
"Xamarin.Android.Support.Media.Compat",
"Xamarin.Android.Support.Percent",
"Xamarin.Android.Support.Print",
"Xamarin.Android.Support.Recommendation",
"Xamarin.Android.Support.RecyclerView.Selection",
"Xamarin.Android.Support.Slices.Builders",
"Xamarin.Android.Support.Slices.Core",
"Xamarin.Android.Support.Slices.View",
"Xamarin.Android.Support.SlidingPaneLayout",
"Xamarin.Android.Support.SwipeRefreshLayout",
"Xamarin.Android.Support.Transition",
"Xamarin.Android.Support.TV.Provider",
"Xamarin.Android.Support.v13",
"Xamarin.Android.Support.v14.Preference",
"Xamarin.Android.Support.v17.Leanback",
"Xamarin.Android.Support.v17.Preference.Leanback",
"Xamarin.Android.Support.v4",
"Xamarin.Android.Support.v7.AppCompat",
"Xamarin.Android.Support.v7.CardView",
"Xamarin.Android.Support.v7.GridLayout",
"Xamarin.Android.Support.v7.MediaRouter",
"Xamarin.Android.Support.v7.Palette",
"Xamarin.Android.Support.v7.Preference",
"Xamarin.Android.Support.v7.RecyclerView",
"Xamarin.Android.Support.Vector.Drawable",
"Xamarin.Android.Support.VersionedParcelable",
"Xamarin.Android.Support.ViewPager",
"Xamarin.Android.Support.Wear",
"Xamarin.Android.Support.WebKit"
};
bool IsBlacklisted(string id)
{
id = id.ToLower();
if (blacklistIdPrefix.Contains(id))
return true;
if (blacklistIdPrefix.Any(p => id.StartsWith(p)))
return true;
return false;
}
NuGetVersion GetSupportVersion(FilePath nuspec)
{
var range = GetSupportVersionRange(nuspec);
return range.MinVersion ?? range.MaxVersion;
}
VersionRange GetSupportVersionRange(FilePath nuspec)
{
var xdoc = XDocument.Load(nuspec.FullPath);
var ns = xdoc.Root.Name.Namespace;
var xmd = xdoc.Root.Element(ns + "metadata");
var xid = xmd.Element(ns + "id");
string version;
if (xid.Value.ToLower().StartsWith("xamarin.android.arch")) {
// search this nuget for a support version
version = xmd
.Descendants(ns + "dependency")
.LastOrDefault(e => e.Attribute("id").Value.ToLower().StartsWith("xamarin.android.support"))
?.Attribute("version")
?.Value;
// if none was found, look in the dependencies
if (string.IsNullOrEmpty(version)) {
foreach (var xdep in xmd.Descendants(ns + "dependency").Reverse()) {
var id = xdep.Attribute("id").Value.ToLower();
var range = VersionRange.Parse(xdep.Attribute("version").Value);
var depNuspec = $"{packagesPath}/{id}/{range.MinVersion ?? range.MaxVersion}/{id}.nuspec";
if (!FileExists(depNuspec))
continue;
// if a support version was found, use it, otherwise continue looking
var suppRange = GetSupportVersionRange(depNuspec);
if (suppRange != null)
return suppRange;
}
}
} else {
version = xmd.Element(ns + "version").Value;
}
return VersionRange.Parse(version);
}
NuGetVersion GetArchVersion(FilePath nuspec)
{
var range = GetArchVersionRange(nuspec);
return range.MinVersion ?? range.MaxVersion;
}
VersionRange GetArchVersionRange(FilePath nuspec)
{
var xdoc = XDocument.Load(nuspec.FullPath);
var ns = xdoc.Root.Name.Namespace;
var xmd = xdoc.Root.Element(ns + "metadata");
var xid = xmd.Element(ns + "id");
string version;
if (xid.Value.ToLower().StartsWith("xamarin.android.support")) {
version = xmd
.Descendants(ns + "dependency")
.Last(e => e.Attribute("id").Value.ToLower().StartsWith("xamarin.android.arch"))
.Attribute("version")
.Value;
} else {
version = xmd.Element(ns + "version").Value;
}
return VersionRange.Parse(version);
}
NuGetVersion GetNewVersion(string id, string old)
{
return GetNewVersion(id, NuGetVersion.Parse(old));
}
NuGetVersion GetNewVersion(string id, NuGetVersion old)
{
if (keepLatestVersion) {
var latest = GetLatestVersion(id);
if (old == latest)
return old;
}
return new NuGetVersion(
old.Major,
old.Minor,
old.Patch,
incrementVersion ? 990 + old.Revision : old.Revision,
prereleaseLabel ?? old.Release,
(string)null);
}
NuGetVersion GetNewVersion(string id, VersionRange old)
{
return GetNewVersion(id, old.MinVersion ?? old.MaxVersion);
}
NuGetVersion GetLatestVersion(string id)
{
return GetDirectories($"{packagesPath}/{id}/*")
.Select(d => NuGetVersion.Parse(d.GetDirectoryName()))
.OrderByDescending(v => v)
.FirstOrDefault();
}
Task("DownloadNuGets")
.Does(async () =>
{
var nugetSources = new List<SourceRepository> {
Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json")
};
if (!string.IsNullOrEmpty(localSource)) {
nugetSources.Add(Repository.Factory.GetCoreV3(localSource));
}
var nugetCache = new SourceCacheContext();
var nugetLogger = NullLogger.Instance;
var processedIds = new List<string>();
// download the bits from nuget.org and any local packages
await DownloadNuGetsAsync(seedPackages);
async Task DownloadNuGetsAsync(IEnumerable<string> ids)
{
EnsureDirectoryExists(packagesPath);
foreach (var id in ids.Select(i => i.ToLower())) {
// skip ids that have already been downloaded
if (processedIds.Contains(id))
continue;
// skip packages that we don't want
if (IsBlacklisted(id))
continue;
// mark this id as processed
processedIds.Add(id);
// get the versions for each nuget
Information($"Making sure that all the versions of '{id}' are available for processing...");
foreach (var nugetSource in nugetSources) {
var mdRes = await nugetSource.GetResourceAsync<MetadataResource>();
var includePrerelease = nugetSource.PackageSource.Source == localSource;
var allVersions = await mdRes.GetVersions(id, includePrerelease, false, nugetCache, nugetLogger, default);
// process the versions
foreach (var version in allVersions) {
// skip versions tht are lower than what we want to support
var min = minimumVersion[minimumVersion.Keys.First(k => id.StartsWith(k))];
if (version < min)
continue;
var identity = new PackageIdentity(id, version);
var dest = packagesPath.Combine(id).Combine(version.ToNormalizedString());
var destFile = dest.CombineWithFilePath($"{id}.{version.ToNormalizedString()}.nupkg");
// download the packages, if needed
if (!FileExists(destFile)) {
Information($" - Downloading '{identity}'...");
EnsureDirectoryExists(dest);
var byIdRes = await nugetSource.GetResourceAsync<FindPackageByIdResource>();
using (var downloader = await byIdRes.GetPackageDownloaderAsync(identity, nugetCache, nugetLogger, default)) {
await downloader.CopyNupkgFileToAsync(destFile.FullPath, default);
}
Unzip(destFile, dest);
}
// download dependencies
var packageRes = await nugetSource.GetResourceAsync<PackageMetadataResource>();
var metadata = await packageRes.GetMetadataAsync(identity, nugetCache, nugetLogger, default);
var deps = metadata.DependencySets.SelectMany(g => g.Packages).Select(p => p.Id);
await DownloadNuGetsAsync(deps);
}
}
}
}
});
Task("PrepareWorkingDirectory")
.IsDependentOn("DownloadNuGets")
.Does(() =>
{
EnsureDirectoryExists(workingPath);
CleanDirectories(workingPath.FullPath);
// copy the downloaded files into the working directory
Information($"Copying packages...");
foreach (var idDir in GetDirectories($"{packagesPath}/*")) {
var id = idDir.GetDirectoryName();
// skip packages that don't want
if (IsBlacklisted(id))
continue;
Information($" - Copying all versions of '{id}'...");
// we only want to copy the latest of each major version
var versions = GetFiles($"{idDir}/*/*.nuspec")
.Select(n => new { Dir = n.GetDirectory().GetDirectoryName(), Ver = GetSupportVersion(n) })
.OrderByDescending(n => n.Ver)
.GroupBy(n => n.Ver.Major, n => n.Dir)
.Select(g => g.FirstOrDefault());
foreach (var version in versions) {
CopyDirectory($"{packagesPath}/{id}/{version}", $"{workingPath}/{id}/{GetNewVersion(id, version)}");
}
}
// remove all the files we do not want in the nugets
Information($"Removing junk...");
var junkFiles =
GetFiles($"{workingPath}/*/*.json") +
GetFiles($"{workingPath}/*/*/*.nupkg") +
GetFiles($"{workingPath}/*/*/.signature.p7s") +
GetFiles($"{workingPath}/*/*/*Content_Types*.xml");
foreach (var junk in junkFiles) {
DeleteFile(junk);
}
var junkDirs =
GetDirectories($"{workingPath}/*/*/_rels") +
GetDirectories($"{workingPath}/*/*/package");
foreach (var junk in junkDirs) {
DeleteDirectory(junk, new DeleteDirectorySettings {
Force = true,
Recursive = true
});
}
// put all the nuspecs and contents into a standard format for processing
Information($"Normalizing packages...");
foreach (var idDir in GetDirectories($"{workingPath}/*")) {
var id = idDir.GetDirectoryName();
// skip packages that don't want
if (IsBlacklisted(id))
continue;
Information($" - Normalizing all versions of '{id}'...");
var nuspecs = GetFiles($"{idDir}/*/*.nuspec");
foreach (var nuspec in nuspecs) {
var targetFw = NormalizeNuspec(nuspec);
NormalizeContents(nuspec.GetDirectory(), targetFw, "lib");
NormalizeContents(nuspec.GetDirectory(), targetFw, "build");
NormalizeContents(nuspec.GetDirectory(), targetFw, "proguard");
NormalizeContents(nuspec.GetDirectory(), targetFw, "aar");
// change the path to the proguard.txt files, nothing clever, just a replace
var oldLink = @"..\..\proguard\";
var newLink = $@"..\..\proguard\{targetFw.GetShortFolderName()}\";
var targets = $"{nuspec.GetDirectory()}/build/{targetFw.GetShortFolderName()}/*.targets";
ReplaceTextInFiles(targets, oldLink, newLink);
// same with the aar
oldLink = @"..\..\aar\";
newLink = $@"..\..\aar\{targetFw.GetShortFolderName()}\";
targets = $"{nuspec.GetDirectory()}/build/{targetFw.GetShortFolderName()}/*.targets";
ReplaceTextInFiles(targets, oldLink, newLink);
}
}
void NormalizeContents(DirectoryPath dir, NuGetFramework fw, string subdir)
{
if (!DirectoryExists($"{dir}/{subdir}"))
return;
// temp checks
if (GetDirectories($"{dir}/{subdir}/*/*").Any())
throw new Exception($"'{dir}' contains sub directories.");
// make sure the files are in the right folder:
// - 0 means that this is a thin package
// probably not the core build/lib folders (probably proguard)
// - 1 means that this is a thin package
// - 2+ more means this is already a fat package
if (GetDirectories($"{dir}/{subdir}/*").Count() <= 1) {
EnsureDirectoryExists(dir.Combine("temp"));
MoveFiles($"{dir}/{subdir}/**/*", dir.Combine("temp"));
CleanDirectories($"{dir}/{subdir}");
MoveDirectory(dir.Combine("temp"), dir.Combine(subdir).Combine(fw.GetShortFolderName()));
}
}
NuGetFramework NormalizeNuspec(FilePath nuspec)
{
var supportVersion = GetSupportVersion(nuspec);
var targetFw = apiLevelVersion[supportVersion.Major];
var xdoc = XDocument.Load(nuspec.FullPath);
var ns = xdoc.Root.Name.Namespace;
var xmd = xdoc.Root.Element(ns + "metadata");
// set the new version of the package
var xv = xmd.Element(ns + "version");
var version = NuGetVersion.Parse(xv.Value);
xv.Value = GetNewVersion(xmd.Element(ns + "id").Value, version).ToNormalizedString();
// make sure the <dependencies> element exists
var xdeps = xmd.Element(ns + "dependencies");
if (xdeps == null) {
xdeps = new XElement(ns + "dependencies");
xmd.Add(xdeps);
}
// move the loose dependencies into a group
var xlooseDeps = xdeps.Elements(ns + "dependency");
if (xlooseDeps.Any()) {
xdeps.Add(new XElement(ns + "group",
new XAttribute("targetFramework", targetFw.GetShortFolderName()),
xlooseDeps));
}
// some packages have the wrong <group> targets, so recreate everything
var xnewGroups = new Dictionary<int, XElement>();
foreach (var pair in apiLevelVersion) {
if (pair.Key > supportVersion.Major)
continue;
xnewGroups.Add(pair.Key, new XElement(ns + "group",
new XAttribute("targetFramework", pair.Value.GetShortFolderName())));
}
// move the old dependencies into the new groups if they contain a support version
// if not, then just use the version of the nuspec
// only select the last group as the rest will be re-created
var xoldGroup = xdeps.Elements(ns + "group").LastOrDefault();
if (xoldGroup != null) {
var xgroupDeps = xoldGroup.Elements(ns + "dependency");
var firstSupportVersion = xgroupDeps
.Where(x => x.Attribute("id").Value.ToLower().StartsWith("xamarin.android.support"))
.Select(x => VersionRange.Parse(x.Attribute("version").Value))
.FirstOrDefault();
var minVersion = firstSupportVersion?.MinVersion ?? supportVersion;
xnewGroups[minVersion.Major].Add(xgroupDeps);
}
// set the new versions of the dependencies
foreach (var xdep in xnewGroups.Values.Elements(ns + "dependency")) {
if (IsBlacklisted(xdep.Attribute("id").Value))
continue;
var xdv = xdep.Attribute("version");
var range = VersionRange.Parse(xdv.Value);
xdv.Value = GetNewVersion(xdep.Attribute("id").Value.ToLower(), range).ToNormalizedString();
if (useExplicitVersion) {
xdv.Value = $"[{xdv.Value}]";
}
}
// swap out the old dependencies for the new ones
xdeps.RemoveAll();
xdeps.Add(xnewGroups.Values);
xdoc.Save(nuspec.FullPath);
return targetFw;
}
});
Task("CreateFatNuGets")
.IsDependentOn("PrepareWorkingDirectory")
.Does(async () =>
{
var idDirs = GetDirectories($"{workingPath}/*");
foreach (var idDir in idDirs) {
var id = idDir.GetDirectoryName();
// skip packages that don't want
if (IsBlacklisted(id))
continue;
Information($"Processing all versions of '{id}'...");
var versions = GetDirectories($"{idDir}/*")
.Select(d => d.GetDirectoryName())
.Select(v => NuGetVersion.Parse(v))
.OrderByDescending(v => v)
.ToList();
// merge the older packages into the latest
foreach (var cutoffVersion in versions) {
Information($" - Processing version '{cutoffVersion}' of '{id}'...");
var includedVersions = versions.Where(v => v < cutoffVersion).ToArray();
MergeNuspecs(id, cutoffVersion, includedVersions);
MergeContents(id, cutoffVersion, includedVersions, "lib");
MergeContents(id, cutoffVersion, includedVersions, "build");
MergeContents(id, cutoffVersion, includedVersions, "proguard");
CreateDummyLibs(id, cutoffVersion);
}
}
void CreateDummyLibs(string id, NuGetVersion version)
{
var root = $"{workingPath}/{id}/{version.ToNormalizedString()}/lib";
foreach (var pair in apiLevelVersion) {
var dir = $"{root}/{pair.Value.GetShortFolderName()}";
if (!DirectoryExists(dir)) {
EnsureDirectoryExists(dir);
FileWriteText($"{dir}/_._", "");
} else {
// jump out as soon as we find a folder because we don't
// want to add newer dummy folders
break;
}
}
}
void MergeContents(string id, NuGetVersion version, NuGetVersion[] includedVersions, string subdir)
{
var dest = $"{workingPath}/{id}/{version.ToNormalizedString()}/{subdir}";
foreach (var included in includedVersions) {
var src = $"{workingPath}/{id}/{included.ToNormalizedString()}/{subdir}";
if (DirectoryExists(src)) {
CopyDirectory(src, dest);
}
}
}
void MergeNuspecs(string id, NuGetVersion version, NuGetVersion[] includedVersions)
{
var nuspec = $"{workingPath}/{id}/{version.ToNormalizedString()}/{id}.nuspec";
var xdoc = XDocument.Load(nuspec);
var ns = xdoc.Root.Name.Namespace;
var xmd = xdoc.Root.Element(ns + "metadata");
var xversion = xmd.Element(ns + "version");
var xdeps = xmd.Element(ns + "dependencies");
var xgroups = xdeps.Elements(ns + "group");
var isArch = id.ToLower().StartsWith("xamarin.android.arch");
foreach (var included in includedVersions) {
var includedNuspec = $"{workingPath}/{id}/{included.ToNormalizedString()}/{id}.nuspec";
var xincluded = XDocument.Load(includedNuspec);
var ins = xincluded.Root.Name.Namespace;
var ixmd = xincluded.Root.Element(ins + "metadata");
var ixdeps = ixmd.Element(ins + "dependencies");
var ixgroups = ixdeps.Elements(ins + "group");
foreach (var ixgroup in ixgroups) {
var xgroup = xgroups.FirstOrDefault(g => g.Attribute("targetFramework").Value == ixgroup.Attribute("targetFramework").Value);
foreach (var ixdep in ixgroup.Elements(ins + "dependency")) {
var includedArch = ixdep.Attribute("id").Value.ToLower().StartsWith("xamarin.android.arch");
string newVersion = null;
if (isArch == includedArch) {
// if both are arch, or both are support, use the current version
newVersion = xversion.Value;
} else if (isArch) {
// if the main is arch, but this is support
newVersion = GetSupportVersion(nuspec).ToNormalizedString();
} else {
// if the main is support and this is arch
newVersion = GetArchVersion(nuspec).ToNormalizedString();
}
ixdep.SetAttributeValue("version", useExplicitVersion ? $"[{newVersion}]" : newVersion);
}
xgroup.Add(ixgroup.Elements());
}
}
xdoc.Save(nuspec);
}
});
Task("PackNuGets")
.IsDependentOn("CreateFatNuGets")
.Does(async () =>
{
EnsureDirectoryExists(outputPath);
CleanDirectories(outputPath.FullPath);
var idDirs = GetDirectories($"{workingPath}/*");
foreach (var idDir in idDirs) {
var id = idDir.GetDirectoryName();
// skip packages that don't want
if (IsBlacklisted(id))
continue;
var versions = GetDirectories($"{workingPath}/{id}/*")
.Select(d => new { Dir = d, Ver = NuGetVersion.Parse(d.GetDirectoryName()) })
.OrderByDescending(v => v.Ver);
foreach (var version in versions) {
var nuspec = GetFiles($"{version.Dir}/*.nuspec").FirstOrDefault();
Information($"Packing {nuspec}...");
NuGetPack(nuspec, new NuGetPackSettings {
BasePath = nuspec.GetDirectory(),
OutputDirectory = outputPath,
RequireLicenseAcceptance = true, // TODO: work around a bug: https://github.com/cake-build/cake/issues/2061
});
if (packLatestOnly)
break;
}
}
});
Task("Default")
.IsDependentOn("DownloadNuGets")
.IsDependentOn("PrepareWorkingDirectory")
.IsDependentOn("CreateFatNuGets")
.IsDependentOn("PackNuGets");
RunTarget(target);