forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
486 lines (396 loc) · 15.9 KB
/
build.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
#tool "nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner&version=3.7.0"
#tool "nuget:https://www.nuget.org/api/v2?package=GitReleaseNotes&version=0.7.1"
#tool "nuget:https://www.nuget.org/api/v2?package=ILRepack&version=2.0.15"
#addin "nuget:?package=Cake.Incubator"
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
string version = null;
string nugetVersion = null;
string preReleaseTag = null;
string semVersion = null;
string milestone = null;
bool publishingError = false;
bool IsTagged = (BuildSystem.AppVeyor.Environment.Repository.Tag.IsTag &&
!string.IsNullOrWhiteSpace(BuildSystem.AppVeyor.Environment.Repository.Tag.Name));
bool IsMainGitVersionRepo = StringComparer.OrdinalIgnoreCase.Equals("gittools/gitversion", BuildSystem.AppVeyor.Environment.Repository.Name);
bool IsPullRequest = BuildSystem.AppVeyor.Environment.PullRequest.IsPullRequest;
bool IsMainGitVersionBranch = StringComparer.OrdinalIgnoreCase.Equals("master", BuildSystem.AppVeyor.Environment.Repository.Branch);
string buildDir = "./build/";
void Build(string configuration, string nugetVersion, string semVersion, string version, string preReleaseTag)
{
MSBuild("./src/GitVersion.sln", settings =>
{
settings.SetConfiguration(configuration)
.SetVerbosity(Verbosity.Minimal)
.WithTarget("Build")
.WithProperty("POSIX",IsRunningOnUnix().ToString());
if (BuildSystem.AppVeyor.IsRunningOnAppVeyor)
{
if (!string.IsNullOrWhiteSpace(nugetVersion))
{
settings.WithProperty("GitVersion_NuGetVersion", nugetVersion);
}
if (!string.IsNullOrWhiteSpace(semVersion))
{
settings.WithProperty("GitVersion_SemVer", semVersion);
}
if (!string.IsNullOrWhiteSpace(version))
{
settings.WithProperty("GitVersion_MajorMinorPatch", version);
}
if (!string.IsNullOrWhiteSpace(preReleaseTag))
{
settings.WithProperty("GitVersion_PreReleaseTag", preReleaseTag);
}
}
});
}
// This build task can be run to just build
Task("DogfoodBuild")
.IsDependentOn("Clean")
.IsDependentOn("NuGet-Package-Restore")
.Does(() =>
{
Build(configuration, nugetVersion, semVersion, version, preReleaseTag);
});
Task("Version")
.IsDependentOn("DogfoodBuild")
.Does(() =>
{
GitVersion(new GitVersionSettings
{
UpdateAssemblyInfo = true,
LogFilePath = "console",
OutputType = GitVersionOutput.BuildServer,
ToolPath = @"src\GitVersionExe\bin\Release\net40\GitVersion.exe"
});
GitVersion assertedVersions = GitVersion(new GitVersionSettings
{
OutputType = GitVersionOutput.Json,
ToolPath = @"src\GitVersionExe\bin\Release\net40\GitVersion.exe"
});
version = assertedVersions.MajorMinorPatch;
nugetVersion = assertedVersions.NuGetVersion;
preReleaseTag = assertedVersions.PreReleaseTag;
semVersion = assertedVersions.LegacySemVerPadded;
});
Task("NuGet-Package-Restore")
.Does(() =>
{
DotNetCoreRestore("./src/GitVersion.sln");
//NuGetRestore("./src/GitVersion.sln");
});
Task("Clean")
.Does(() =>
{
CleanDirectories("./build");
CleanDirectories("./**/obj");
var binDirs = GetDirectories("./**/bin") - GetDirectories("**/GemAssets/bin");
CleanDirectories(binDirs);
});
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("Version")
.IsDependentOn("NuGet-Package-Restore")
.Does(() =>
{
Build(configuration, nugetVersion, semVersion, version, preReleaseTag);
});
Task("Run-Tests-In-NUnitConsole")
.IsDependentOn("DogfoodBuild")
.Does(() =>
{
var settings = new NUnit3Settings();
var targetFramework = "net461";
if(IsRunningOnUnix())
{
settings.Where = "cat != NoMono";
}
NUnit3(new [] {
"src/GitVersionCore.Tests/bin/" + configuration + "/" + targetFramework + "/GitVersionCore.Tests.dll",
"src/GitVersionExe.Tests/bin/" + configuration + "/" + targetFramework + "/GitVersionExe.Tests.dll",
"src/GitVersionTask.Tests/bin/" + configuration + "/" + targetFramework + "/GitVersionTask.Tests.dll" },
settings);
if (AppVeyor.IsRunningOnAppVeyor)
{
Information("Uploading test results");
AppVeyor.UploadTestResults("TestResult.xml", AppVeyorTestResultsType.NUnit3);
}
});
// Note: this task is not used for time being as unable to produce sensible test results output file via dotnet cli.. so using nunit console runner above instead.
Task("Run-Tests")
.IsDependentOn("DogfoodBuild")
.Does(() =>
{
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild = true,
Filter = "TestCategory!=NoMono"
};
DotNetCoreTest("./src/GitVersionCore.Tests/GitVersionCore.Tests.csproj", settings);
DotNetCoreTest("./src/GitVersionExe.Tests/GitVersionExe.Tests.csproj", settings);
DotNetCoreTest("./src/GitVersionTask.Tests/GitVersionTask.Tests.csproj", settings);
});
void ILRepackGitVersionExe(bool includeLibGit2Sharp)
{
var tempMergeDir = "ILMergeTemp";
var exeName = "GitVersion.exe";
var keyFilePath = "./src/key.snk";
var targetDir = "./src/GitVersionExe/bin/" + configuration + "/net40/";
var targetPath = targetDir + exeName;
CreateDirectory(tempMergeDir);
string outFilePath = "./" + tempMergeDir + "/" + exeName;
var sourcePattern = targetDir + "*.dll";
var sourceFiles = GetFiles(sourcePattern);
if(!includeLibGit2Sharp)
{
var excludePattern = "**/LibGit2Sharp.dll";
sourceFiles = sourceFiles - GetFiles(excludePattern);
}
var settings = new ILRepackSettings { AllowDup = "", Keyfile = keyFilePath, Internalize = true, NDebug = true, TargetKind = TargetKind.Exe, TargetPlatform = TargetPlatformVersion.v4, XmlDocs = false };
ILRepack(outFilePath, targetPath, sourceFiles, settings);
}
Task("Commandline-Package")
.IsDependentOn("Build")
.Does(() =>
{
ILRepackGitVersionExe(false);
var outputDir = buildDir + "NuGetCommandLineBuild";
var toolsDir = outputDir + "/tools";
var libDir = toolsDir + "/lib";
CreateDirectory(outputDir);
CreateDirectory(toolsDir);
CreateDirectory(libDir);
var targetDir = "./src/GitVersionExe/bin/" + configuration + "/net40/";
var libGitFiles = GetFiles(targetDir + "LibGit2Sharp.dll*");
var nugetAssetsPath = "./src/GitVersionExe/NugetAssets/";
Information("Copying files to packaging direcory..");
CopyFiles(targetDir + "GitVersion.pdb", outputDir + "/tools/");
CopyFiles(targetDir + "GitVersion.exe.mdb", outputDir + "/tools/");
Information("Copying IL Merged exe file..");
CopyFiles("./ILMergeTemp/GitVersion.exe", outputDir + "/tools/");
Information("Copying nuget assets..");
CopyFiles(nugetAssetsPath + "GitVersion.CommandLine.nuspec", outputDir);
Information("Copying libgit2sharp files..");
CopyFiles(libGitFiles, outputDir + "/tools/");
CopyDirectory(targetDir + "lib/", outputDir + "/tools/lib/");
Information("Creating Nuget Package..");
var nuGetPackSettings = new NuGetPackSettings { Version = nugetVersion, BasePath = outputDir, OutputDirectory = outputDir };
NuGetPack(outputDir + "/GitVersion.CommandLine.nuspec", nuGetPackSettings);
})
.ReportError(exception =>
{
Error(exception.Dump());
// Report the error.
});
Task("Portable-Package")
.IsDependentOn("Build")
.Does(() =>
{
ILRepackGitVersionExe(true);
var outputDir = buildDir + "NuGetExeBuild";
var toolsDir = outputDir + "/tools";
var libDir = toolsDir + "/lib";
CreateDirectory(outputDir);
CreateDirectory(toolsDir);
CreateDirectory(libDir);
var targetDir = "./src/GitVersionExe/bin/" + configuration + "/net40/";
var nugetAssetsPath = "./src/GitVersionExe/NugetAssets/";
Information("Copying files to packaging direcory..");
CopyFiles(targetDir + "GitVersion.pdb", outputDir + "/tools/");
CopyFiles(targetDir + "GitVersion.exe.mdb", outputDir + "/tools/");
Information("Copying IL Merged exe file..");
CopyFiles("./ILMergeTemp/GitVersion.exe", outputDir + "/tools/");
Information("Copying nuget assets..");
CopyFiles(nugetAssetsPath + "*.ps1", outputDir + "/tools/");
CopyFiles(nugetAssetsPath + "GitVersion.Portable.nuspec", outputDir);
Information("Copying libgit2sharp files..");
CopyDirectory(targetDir + "lib/", outputDir + "/tools/lib/");
var nuGetPackSettings = new NuGetPackSettings { Version = nugetVersion, BasePath = outputDir, OutputDirectory = outputDir };
NuGetPack(outputDir + "/GitVersion.Portable.nuspec", nuGetPackSettings);
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("GitVersionCore-Package")
.IsDependentOn("Build")
.Does(() =>
{
var outputDir = buildDir + "NuGetRefBuild";
CreateDirectory(outputDir);
var msBuildSettings = new DotNetCoreMSBuildSettings();
msBuildSettings.SetVersion(nugetVersion);
msBuildSettings.Properties["PackageVersion"] = new string[]{ nugetVersion };
var settings = new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = outputDir,
NoBuild = true,
MSBuildSettings = msBuildSettings
};
DotNetCorePack("./src/GitVersionCore", settings);
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("GitVersion-DotNet-Package")
.IsDependentOn("Build")
.Does(() =>
{
// var publishDir = buildDir + "Published";
// CreateDirectory(outputDir);
var outputDir = buildDir + "NuGetExeDotNetCoreBuild";
var toolsDir = outputDir + "/tools";
var libDir = toolsDir + "/lib";
CreateDirectory(outputDir);
CreateDirectory(toolsDir);
CreateDirectory(libDir);
var msBuildSettings = new DotNetCoreMSBuildSettings();
msBuildSettings.SetVersion(nugetVersion);
msBuildSettings.Properties["PackageVersion"] = new string[]{ nugetVersion };
var framework = "netcoreapp20";
var settings = new DotNetCorePublishSettings
{
Framework = framework,
Configuration = configuration,
OutputDirectory = toolsDir,
MSBuildSettings = msBuildSettings
};
DotNetCorePublish("./src/GitVersionExe", settings);
// var targetDir = "./src/GitVersionExe/bin/" + configuration + "/" + framework + "/";
var nugetAssetsPath = "./src/GitVersionExe/NugetAssets/";
Information("Copying files to packaging direcory..");
Information("Copying nuget assets..");
CopyFiles(nugetAssetsPath + "GitVersion.CommandLine.DotNetCore.nuspec", outputDir);
//Information("Copying libgit2sharp files..");
//CopyDirectory(targetDir + "lib/", outputDir + "/tools/lib/");
var nuGetPackSettings = new NuGetPackSettings { Version = nugetVersion, BasePath = outputDir, OutputDirectory = outputDir };
NuGetPack(outputDir + "/GitVersion.CommandLine.DotNetCore.nuspec", nuGetPackSettings);
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("GitVersionTaskPackage")
.Description("Produces the nuget package for GitVersionTask")
.Does(() =>
{
var outputDir = buildDir + "NuGetTaskBuild";
CreateDirectory(outputDir);
var msBuildSettings = new DotNetCoreMSBuildSettings();
msBuildSettings.SetVersion(nugetVersion);
msBuildSettings.Properties["PackageVersion"] = new string[]{ nugetVersion };
var settings = new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = outputDir,
NoBuild = true,
MSBuildSettings = msBuildSettings
};
DotNetCorePack("./src/GitVersionTask", settings);
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("Zip-Files")
.IsDependentOn("Build")
.IsDependentOn("Commandline-Package")
.IsDependentOn("Portable-Package")
.IsDependentOn("GitVersionCore-Package")
.IsDependentOn("GitVersionTaskPackage")
.IsDependentOn("GitVersion-DotNet-Package")
.IsDependentOn("Run-Tests-In-NUnitConsole")
.Does(() =>
{
Zip("./build/NuGetCommandLineBuild/tools/", "build/GitVersion_" + nugetVersion + ".zip");
Zip("./build/NuGetExeDotNetCoreBuild/tools/", "build/GitVersionDotNetCore_" + nugetVersion + ".zip");
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("Create-Release-Notes")
.IsDependentOn("Build")
.WithCriteria(() => IsMainGitVersionRepo && IsMainGitVersionBranch && !IsPullRequest)
.Does(() =>
{
var githubToken = EnvironmentVariable("GitHubToken");
if(!string.IsNullOrWhiteSpace(githubToken))
{
IEnumerable<string> redirectedOutput;
var gitReleasNotesExePath = Context.Tools.Resolve("GitReleaseNotes.exe");
EnsureDirectoryExists(buildDir);
var releaseNotesExitCode = StartProcess(
gitReleasNotesExePath,
new ProcessSettings {
Arguments = ". /o build/releasenotes.md /repoToken " + githubToken,
RedirectStandardOutput = true
},
out redirectedOutput);
Information(string.Join("\n", redirectedOutput));
if (!System.IO.File.Exists("./build/releasenotes.md") || string.IsNullOrEmpty(System.IO.File.ReadAllText("./build/releasenotes.md"))) {
System.IO.File.WriteAllText("./build/releasenotes.md", "No issues closed since last release");
}
}
else
{
Information("Create-Release-Notes is being skipped, as GitHub Token is not present.");
}
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("Package")
.IsDependentOn("Create-Release-Notes")
.IsDependentOn("Zip-Files");
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Package")
.WithCriteria(() => AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
var gem = string.IsNullOrEmpty(preReleaseTag) ?
"gitversion-" + version + ".gem" :
"gitversion-" + version + "." + preReleaseTag + ".gem";
System.IO.File.WriteAllLines("build/artifacts", new[]{
"NuGetExeBuild:GitVersion.Portable." + nugetVersion +".nupkg",
"NuGetCommandLineBuild:GitVersion.CommandLine." + nugetVersion +".nupkg",
"NuGetExeDotNetCoreBuild:GitVersion.CommandLine.DotNetCore." + nugetVersion +".nupkg",
"NuGetRefBuild:GitVersionCore." + nugetVersion +".nupkg",
"NuGetTaskBuild:GitVersionTask." + nugetVersion +".nupkg",
"zip:GitVersion_" + nugetVersion + ".zip",
"zip-dotnetcore:GitVersionDotNetCore_" + nugetVersion + ".zip"
// "GitVersionTfsTaskBuild:gittools.gitversion-" + semVersion +".vsix",
// "GemBuild:" + gem
});
AppVeyor.UploadArtifact("build/NuGetExeBuild/GitVersion.Portable." + nugetVersion +".nupkg");
AppVeyor.UploadArtifact("build/NuGetCommandLineBuild/GitVersion.CommandLine." + nugetVersion +".nupkg");
AppVeyor.UploadArtifact("build/NuGetExeDotNetCoreBuild/GitVersion.CommandLine.DotNetCore." + nugetVersion +".nupkg");
AppVeyor.UploadArtifact("build/NuGetRefBuild/GitVersionCore." + nugetVersion +".nupkg");
AppVeyor.UploadArtifact("build/NuGetTaskBuild/GitVersionTask." + nugetVersion +".nupkg");
AppVeyor.UploadArtifact("build/GitVersion_" + nugetVersion + ".zip");
AppVeyor.UploadArtifact("build/GitVersionDotNetCore_" + nugetVersion + ".zip");
// AppVeyor.UploadArtifact("build/GitVersionTfsTaskBuild/gittools.gitversion-" + semVersion + ".vsix");
// AppVeyor.UploadArtifact("build/GemBuild/" + gem);
AppVeyor.UploadArtifact("build/artifacts");
if(IsMainGitVersionRepo && IsMainGitVersionBranch && !IsPullRequest)
{
if(FileExists("build/releasenotes.md"))
{
AppVeyor.UploadArtifact("build/releasenotes.md");
}
}
})
.ReportError(exception =>
{
Error(exception.Dump());
});
Task("Travis")
.IsDependentOn("Run-Tests");
Task("Default")
.IsDependentOn("Upload-AppVeyor-Artifacts");
RunTarget(target);