-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneric-variables.cake
699 lines (560 loc) · 24.5 KB
/
generic-variables.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
#l "buildserver.cake"
#tool "nuget:?package=GitVersion.CommandLine&version=5.12.0"
//-------------------------------------------------------------
public class GeneralContext : BuildContextWithItemsBase
{
public GeneralContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
SkipComponentsThatAreNotDeployable = true;
EnableMsBuildBinaryLog = true;
EnableMsBuildFileLog = true;
EnableMsBuildXmlLog = true;
}
public string Target { get; set; }
public string RootDirectory { get; set; }
public string OutputRootDirectory { get; set; }
public bool IsCiBuild { get; set; }
public bool IsAlphaBuild { get; set; }
public bool IsBetaBuild { get; set; }
public bool IsOfficialBuild { get; set; }
public bool IsLocalBuild { get; set; }
public bool MaximizePerformance { get; set; }
public bool UseVisualStudioPrerelease { get; set; }
public bool VerifyDependencies { get; set; }
public bool SkipComponentsThatAreNotDeployable { get; set; }
public bool EnableMsBuildBinaryLog { get; set; }
public bool EnableMsBuildFileLog { get; set; }
public bool EnableMsBuildXmlLog { get; set; }
public VersionContext Version { get; set; }
public CopyrightContext Copyright { get; set; }
public NuGetContext NuGet { get; set; }
public SolutionContext Solution { get; set; }
public SourceLinkContext SourceLink { get; set; }
public CodeSignContext CodeSign { get; set; }
public AzureCodeSignContext AzureCodeSign { get; set; }
public RepositoryContext Repository { get; set; }
public SonarQubeContext SonarQube { get; set; }
public List<string> Includes { get; set; }
public List<string> Excludes { get; set; }
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
CakeContext.Information($"Running target '{Target}'");
CakeContext.Information($"Using output directory '{OutputRootDirectory}'");
}
}
//-------------------------------------------------------------
public class VersionContext : BuildContextBase
{
private GitVersion _gitVersionContext;
public VersionContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public GitVersion GetGitVersionContext(GeneralContext generalContext)
{
if (_gitVersionContext is null)
{
var gitVersionSettings = new GitVersionSettings
{
UpdateAssemblyInfo = false,
Verbosity = GitVersionVerbosity.Verbose
};
var gitDirectory = ".git";
if (!CakeContext.DirectoryExists(gitDirectory))
{
CakeContext.Information("No local .git directory found, treating as dynamic repository");
// Make a *BIG* assumption that the solution name == repository name
var repositoryName = generalContext.Solution.Name;
var dynamicRepositoryPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), repositoryName);
if (ClearCache)
{
CakeContext.Warning("Cleaning the cloned temp directory, disable by setting 'GitVersion_ClearCache' to 'false'");
if (CakeContext.DirectoryExists(dynamicRepositoryPath))
{
CakeContext.DeleteDirectory(dynamicRepositoryPath, new DeleteDirectorySettings
{
Force = true,
Recursive = true
});
}
}
// Validate first
if (string.IsNullOrWhiteSpace(generalContext.Repository.BranchName))
{
throw new Exception("No local .git directory was found, but repository branch was not specified either. Make sure to specify the branch");
}
if (string.IsNullOrWhiteSpace(generalContext.Repository.Url))
{
throw new Exception("No local .git directory was found, but repository url was not specified either. Make sure to specify the branch");
}
CakeContext.Information($"Fetching dynamic repository from url '{generalContext.Repository.Url}' => '{dynamicRepositoryPath}'");
// Dynamic repository
gitVersionSettings.UserName = generalContext.Repository.Username;
gitVersionSettings.Password = generalContext.Repository.Password;
gitVersionSettings.Url = generalContext.Repository.Url;
gitVersionSettings.Branch = generalContext.Repository.BranchName;
gitVersionSettings.Commit = generalContext.Repository.CommitId;
gitVersionSettings.NoFetch = false;
gitVersionSettings.WorkingDirectory = generalContext.RootDirectory;
gitVersionSettings.DynamicRepositoryPath = dynamicRepositoryPath;
gitVersionSettings.Verbosity = GitVersionVerbosity.Verbose;
}
_gitVersionContext = CakeContext.GitVersion(gitVersionSettings);
}
return _gitVersionContext;
}
public bool ClearCache { get; set; }
private string _major;
public string Major
{
get
{
if (string.IsNullOrWhiteSpace(_major))
{
_major = GetVersion(MajorMinorPatch, 1);
}
return _major;
}
}
private string _majorMinor;
public string MajorMinor
{
get
{
if (string.IsNullOrWhiteSpace(_majorMinor))
{
_majorMinor = GetVersion(MajorMinorPatch, 2);
}
return _majorMinor;
}
}
public string MajorMinorPatch { get; set; }
public string FullSemVer { get; set; }
public string NuGet { get; set; }
public string CommitsSinceVersionSource { get; set; }
private string GetVersion(string version, int breakCount)
{
var finalVersion = string.Empty;
for (int i = 0; i < version.Length; i++)
{
var character = version[i];
if (!char.IsDigit(character))
{
breakCount--;
if (breakCount <= 0)
{
break;
}
}
finalVersion += character.ToString();
}
return finalVersion;
}
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
public class CopyrightContext : BuildContextBase
{
public CopyrightContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string Company { get; set; }
public string StartYear { get; set; }
protected override void ValidateContext()
{
if (string.IsNullOrWhiteSpace(Company))
{
throw new Exception($"Company must be defined");
}
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
public class NuGetContext : BuildContextBase
{
public NuGetContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string PackageSources { get; set; }
public string Executable { get; set; }
public string LocalPackagesDirectory { get; set; }
public bool RestoreUsingNuGet { get; set; }
public bool RestoreUsingDotNetRestore { get; set; }
public bool NoDependencies { get; set; }
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
CakeContext.Information($"Restore using NuGet: '{RestoreUsingNuGet}'");
CakeContext.Information($"Restore using dotnet restore: '{RestoreUsingDotNetRestore}'");
}
}
//-------------------------------------------------------------
public class SolutionContext : BuildContextBase
{
public SolutionContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string Name { get; set; }
public string AssemblyInfoFileName { get; set; }
public string FileName { get; set; }
public string Directory
{
get
{
var directory = System.IO.Directory.GetParent(FileName).FullName;
var separator = System.IO.Path.DirectorySeparatorChar.ToString();
if (!directory.EndsWith(separator))
{
directory += separator;
}
return directory;
}
}
public bool BuildSolution { get; set; }
public string PublishType { get; set; }
public string ConfigurationName { get; set; }
protected override void ValidateContext()
{
if (string.IsNullOrWhiteSpace(Name))
{
throw new Exception($"SolutionName must be defined");
}
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
public class SourceLinkContext : BuildContextBase
{
public SourceLinkContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public bool IsDisabled { get; set; }
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
public class CodeSignContext : BuildContextBase
{
public CodeSignContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string WildCard { get; set; }
public string CertificateSubjectName { get; set; }
public string TimeStampUri { get; set; }
public string HashAlgorithm { get; set; }
public bool IsAvailable
{
get
{
if (string.IsNullOrWhiteSpace(CertificateSubjectName))
{
return false;
}
return true;
}
}
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
if (!IsAvailable)
{
CakeContext.Information($"Code signing is not configured");
return;
}
CakeContext.Information($"Code signing subject name: '{CertificateSubjectName}'");
CakeContext.Information($"Code signing timestamp uri: '{TimeStampUri}'");
CakeContext.Information($"Code signing hash algorithm: '{HashAlgorithm}'");
}
}
//-------------------------------------------------------------
public class AzureCodeSignContext : BuildContextBase
{
public AzureCodeSignContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string VaultName { get; set; }
public string VaultUrl { get { return $"https://{VaultName}.vault.azure.net"; } }
public string CertificateName { get; set; }
public string TimeStampUri { get; set; }
public string HashAlgorithm { get; set; }
public string TenantId { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public bool IsAvailable
{
get
{
if (string.IsNullOrWhiteSpace(VaultName) ||
string.IsNullOrWhiteSpace(CertificateName) ||
string.IsNullOrWhiteSpace(TenantId) ||
string.IsNullOrWhiteSpace(ClientId) ||
string.IsNullOrWhiteSpace(ClientSecret))
{
return false;
}
return true;
}
}
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
if (!IsAvailable)
{
CakeContext.Information($"Azure Code signing is not configured");
return;
}
CakeContext.Information($"Azure Code vault name: '{VaultName}'");
CakeContext.Information($"Azure Code vault URL: '{VaultUrl}'");
CakeContext.Information($"Azure Code signing certificate name: '{CertificateName}'");
CakeContext.Information($"Azure Code signing timestamp uri: '{TimeStampUri}'");
CakeContext.Information($"Azure Code signing hash algorithm: '{HashAlgorithm}'");
}
}
//-------------------------------------------------------------
public class RepositoryContext : BuildContextBase
{
public RepositoryContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public string Url { get; set; }
public string BranchName { get; set; }
public string CommitId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
protected override void ValidateContext()
{
if (string.IsNullOrWhiteSpace(Url))
{
throw new Exception($"RepositoryUrl must be defined");
}
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
public class SonarQubeContext : BuildContextBase
{
public SonarQubeContext(IBuildContext parentBuildContext)
: base(parentBuildContext)
{
}
public bool IsDisabled { get; set; }
public bool SupportBranches { get; set; }
public string Url { get; set; }
public string Organization { get; set; }
public string Username { get; set; }
public string Token { get; set; }
public string Project { get; set; }
protected override void ValidateContext()
{
}
protected override void LogStateInfoForContext()
{
}
}
//-------------------------------------------------------------
private GeneralContext InitializeGeneralContext(BuildContext buildContext, IBuildContext parentBuildContext)
{
var data = new GeneralContext(parentBuildContext)
{
Target = buildContext.BuildServer.GetVariable("Target", "Default", showValue: true),
};
data.Version = new VersionContext(data)
{
ClearCache = buildContext.BuildServer.GetVariableAsBool("GitVersion_ClearCache", false, showValue: true),
MajorMinorPatch = buildContext.BuildServer.GetVariable("GitVersion_MajorMinorPatch", "unknown", showValue: true),
FullSemVer = buildContext.BuildServer.GetVariable("GitVersion_FullSemVer", "unknown", showValue: true),
NuGet = buildContext.BuildServer.GetVariable("GitVersion_NuGetVersion", "unknown", showValue: true),
CommitsSinceVersionSource = buildContext.BuildServer.GetVariable("GitVersion_CommitsSinceVersionSource", "unknown", showValue: true)
};
data.Copyright = new CopyrightContext(data)
{
Company = buildContext.BuildServer.GetVariable("Company", showValue: true),
StartYear = buildContext.BuildServer.GetVariable("StartYear", showValue: true)
};
data.NuGet = new NuGetContext(data)
{
PackageSources = buildContext.BuildServer.GetVariable("NuGetPackageSources", showValue: true),
Executable = "./tools/nuget.exe",
LocalPackagesDirectory = "c:\\source\\_packages",
RestoreUsingNuGet = buildContext.BuildServer.GetVariableAsBool("NuGet_RestoreUsingNuGet", false, showValue: true),
RestoreUsingDotNetRestore = buildContext.BuildServer.GetVariableAsBool("NuGet_RestoreUsingDotNetRestore", true, showValue: true),
NoDependencies = buildContext.BuildServer.GetVariableAsBool("NuGet_NoDependencies", true, showValue: true)
};
var solutionName = buildContext.BuildServer.GetVariable("SolutionName", showValue: true);
data.Solution = new SolutionContext(data)
{
Name = solutionName,
AssemblyInfoFileName = "./src/SolutionAssemblyInfo.cs",
FileName = string.Format("./src/{0}", string.Format("{0}.sln", solutionName)),
PublishType = buildContext.BuildServer.GetVariable("PublishType", "Unknown", showValue: true),
ConfigurationName = buildContext.BuildServer.GetVariable("ConfigurationName", "Release", showValue: true),
BuildSolution = buildContext.BuildServer.GetVariableAsBool("BuildSolution", false, showValue: true)
};
data.IsCiBuild = buildContext.BuildServer.GetVariableAsBool("IsCiBuild", false, showValue: true);
data.IsAlphaBuild = buildContext.BuildServer.GetVariableAsBool("IsAlphaBuild", false, showValue: true);
data.IsBetaBuild = buildContext.BuildServer.GetVariableAsBool("IsBetaBuild", false, showValue: true);
data.IsOfficialBuild = buildContext.BuildServer.GetVariableAsBool("IsOfficialBuild", false, showValue: true);
data.IsLocalBuild = data.Target.ToLower().Contains("local");
data.MaximizePerformance = buildContext.BuildServer.GetVariableAsBool("MaximizePerformance", true, showValue: true);
data.UseVisualStudioPrerelease = buildContext.BuildServer.GetVariableAsBool("UseVisualStudioPrerelease", false, showValue: true);
data.VerifyDependencies = !buildContext.BuildServer.GetVariableAsBool("DependencyCheckDisabled", false, showValue: true);
data.SkipComponentsThatAreNotDeployable = buildContext.BuildServer.GetVariableAsBool("SkipComponentsThatAreNotDeployable", true, showValue: true);
data.EnableMsBuildBinaryLog = buildContext.BuildServer.GetVariableAsBool("EnableMsBuildBinaryLog", true, showValue: true);
data.EnableMsBuildFileLog = buildContext.BuildServer.GetVariableAsBool("EnableMsBuildFileLog", true, showValue: true);
data.EnableMsBuildXmlLog = buildContext.BuildServer.GetVariableAsBool("EnableMsBuildXmlLog", true, showValue: true);
// If local, we want full pdb, so do a debug instead
if (data.IsLocalBuild)
{
parentBuildContext.CakeContext.Warning("Enforcing configuration 'Debug' because this is seems to be a local build, do not publish this package!");
data.Solution.ConfigurationName = "Debug";
}
// Important: do *after* initializing the configuration name
data.RootDirectory = System.IO.Path.GetFullPath(".");
data.OutputRootDirectory = System.IO.Path.GetFullPath(buildContext.BuildServer.GetVariable("OutputRootDirectory", string.Format("./output/{0}", data.Solution.ConfigurationName), showValue: true));
data.SourceLink = new SourceLinkContext(data)
{
IsDisabled = buildContext.BuildServer.GetVariableAsBool("SourceLinkDisabled", false, showValue: true)
};
data.CodeSign = new CodeSignContext(data)
{
WildCard = buildContext.BuildServer.GetVariable("CodeSignWildcard", showValue: true),
CertificateSubjectName = buildContext.BuildServer.GetVariable("CodeSignCertificateSubjectName", showValue: true),
TimeStampUri = buildContext.BuildServer.GetVariable("CodeSignTimeStampUri", "http://timestamp.digicert.com", showValue: true),
HashAlgorithm = buildContext.BuildServer.GetVariable("CodeSignHashAlgorithm", "SHA256", showValue: true)
};
data.AzureCodeSign = new AzureCodeSignContext(data)
{
VaultName = buildContext.BuildServer.GetVariable("AzureCodeSignVaultName", showValue: true),
CertificateName = buildContext.BuildServer.GetVariable("AzureCodeSignCertificateName", showValue: true),
TimeStampUri = buildContext.BuildServer.GetVariable("AzureCodeSignTimeStampUri", "http://timestamp.digicert.com", showValue: true),
HashAlgorithm = buildContext.BuildServer.GetVariable("AzureCodeSignHashAlgorithm", "SHA256", showValue: true),
TenantId = buildContext.BuildServer.GetVariable("AzureCodeSignTenantId", showValue: false),
ClientId = buildContext.BuildServer.GetVariable("AzureCodeSignClientId", showValue: false),
ClientSecret = buildContext.BuildServer.GetVariable("AzureCodeSignClientSecret", showValue: false),
};
data.Repository = new RepositoryContext(data)
{
Url = buildContext.BuildServer.GetVariable("RepositoryUrl", showValue: true),
BranchName = buildContext.BuildServer.GetVariable("RepositoryBranchName", showValue: true),
CommitId = buildContext.BuildServer.GetVariable("RepositoryCommitId", showValue: true),
Username = buildContext.BuildServer.GetVariable("RepositoryUsername", showValue: false),
Password = buildContext.BuildServer.GetVariable("RepositoryPassword", showValue: false)
};
data.SonarQube = new SonarQubeContext(data)
{
IsDisabled = buildContext.BuildServer.GetVariableAsBool("SonarDisabled", false, showValue: true),
SupportBranches = buildContext.BuildServer.GetVariableAsBool("SonarSupportBranches", true, showValue: true),
Url = buildContext.BuildServer.GetVariable("SonarUrl", showValue: true),
Organization = buildContext.BuildServer.GetVariable("SonarOrganization", showValue: true),
Username = buildContext.BuildServer.GetVariable("SonarUsername", showValue: false),
Token = buildContext.BuildServer.GetVariable("SonarToken", showValue: false),
Project = buildContext.BuildServer.GetVariable("SonarProject", data.Solution.Name, showValue: true)
};
data.Includes = SplitCommaSeparatedList(buildContext.BuildServer.GetVariable("Include", string.Empty, showValue: true));
data.Excludes = SplitCommaSeparatedList(buildContext.BuildServer.GetVariable("Exclude", string.Empty, showValue: true));
// Specific overrides, done when we have *all* info
parentBuildContext.CakeContext.Information("Ensuring correct runtime data based on version");
var versionContext = data.Version;
if (string.IsNullOrWhiteSpace(versionContext.NuGet) || versionContext.NuGet == "unknown")
{
parentBuildContext.CakeContext.Information("No version info specified, falling back to GitVersion");
var gitVersion = versionContext.GetGitVersionContext(data);
versionContext.MajorMinorPatch = gitVersion.MajorMinorPatch;
versionContext.FullSemVer = gitVersion.FullSemVer;
versionContext.NuGet = gitVersion.NuGetVersionV2;
versionContext.CommitsSinceVersionSource = (gitVersion.CommitsSinceVersionSource ?? 0).ToString();
}
parentBuildContext.CakeContext.Information("Defined version: '{0}', commits since version source: '{1}'", versionContext.FullSemVer, versionContext.CommitsSinceVersionSource);
if (string.IsNullOrWhiteSpace(data.Repository.CommitId))
{
parentBuildContext.CakeContext.Information("No commit id specified, falling back to GitVersion");
var gitVersion = versionContext.GetGitVersionContext(data);
data.Repository.BranchName = gitVersion.BranchName;
data.Repository.CommitId = gitVersion.Sha;
}
if (string.IsNullOrWhiteSpace(data.Repository.BranchName))
{
parentBuildContext.CakeContext.Information("No branch name specified, falling back to GitVersion");
var gitVersion = versionContext.GetGitVersionContext(data);
data.Repository.BranchName = gitVersion.BranchName;
}
var versionToCheck = versionContext.FullSemVer;
if (versionToCheck.Contains("alpha"))
{
data.IsAlphaBuild = true;
}
else if (versionToCheck.Contains("beta"))
{
data.IsBetaBuild = true;
}
else
{
data.IsOfficialBuild = true;
}
return data;
}
//-------------------------------------------------------------
private static string DetermineChannel(GeneralContext context)
{
var version = context.Version.FullSemVer;
var channel = "stable";
if (context.IsAlphaBuild)
{
channel = "alpha";
}
else if (context.IsBetaBuild)
{
channel = "beta";
}
return channel;
}
//-------------------------------------------------------------
private static string DeterminePublishType(GeneralContext context)
{
var publishType = "Unknown";
if (context.IsOfficialBuild)
{
publishType = "Official";
}
else if (context.IsBetaBuild)
{
publishType = "Beta";
}
else if (context.IsAlphaBuild)
{
publishType = "Alpha";
}
return publishType;
}