-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.cake
244 lines (201 loc) · 8.54 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
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Figlet&version=1.1.0"
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Npx&version=1.3.0"
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
var projectName = "Typescriptr";
var buildContext = new BuildContext(projectName, Context);
Action<NpxSettings> requiredSemanticVersionPackages = settings => settings
.AddPackage("[email protected]")
.AddPackage("@semantic-release/[email protected]")
.AddPackage("@semantic-release/[email protected]")
.AddPackage("@semantic-release/[email protected]");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup<BuildContext>(_ =>
{
Information(Figlet(buildContext.ProjectName));
Information("Target {0}", buildContext.Target);
Information("Configuration {0}", buildContext.Configuration);
return buildContext;
});
Teardown(context =>
{
Information("Finished running tasks ✔");
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Build");
Task("Build")
.IsDependentOn("Run_dotnet_info")
.IsDependentOn("Clean")
.IsDependentOn("Get_next_release_number")
.IsDependentOn("Build_solution")
.IsDependentOn("Run_tests")
.IsDependentOn("Package")
.IsDependentOn("Release")
;
Task("Run_dotnet_info")
.Does(() =>
{
Information("dotnet --info");
StartProcess("dotnet", new ProcessSettings { Arguments = "--info" });
});
Task("Clean")
.Does<BuildContext>(buildContext =>
{
Information("Cleaning {0}, bin and obj folders", buildContext.ArtifactsDir);
CleanDirectory(buildContext.ArtifactsDir);
CleanDirectories("./src/**/bin");
CleanDirectories("./src/**/obj");
});
/*
Normally this task should only run based on the 'IsRunningOnAppveyorMasterBranch' condition,
however sometimes you want to run this locally to preview the next semantic version
number and changelog.
To do this run the following locally:
> $env:NUGET_TOKEN="insert_token_here"
> $env:GITHUB_TOKEN="insert_token_here"
> .\build.ps1 -target Get_next_release_number
NOTE: The GITHUB_TOKEN environment variable will need to be set
so that semantic-release can access the repository
*/
Task("Get_next_release_number")
.WithCriteria<BuildContext>(
(_, buildContext) => buildContext.IsRunningOnAppveyorMasterBranch ||
buildContext.Target == "Get_next_release_number",
"Skipped as build not triggered by Appveyor 'master' branch commit"
)
.Does<BuildContext>(buildContext =>
{
Information("Running semantic-release in dry run mode to extract next release number");
string[] semanticReleaseOutput;
Npx("semantic-release", "--dry-run", requiredSemanticVersionPackages, out semanticReleaseOutput);
Information(string.Join(Environment.NewLine, semanticReleaseOutput));
var hasReleaseVersionChanged = buildContext.SetReleaseVersionFrom(semanticReleaseOutput);
if (hasReleaseVersionChanged)
Information("Next release number is {0}", buildContext.ReleaseVersion);
else
Warning("There are no relevant changes, skipping publish to nuget");
});
Task("Build_solution")
.Does<BuildContext>(buildContext =>
{
foreach(var solution in buildContext.Solutions)
{
Information("Building solution {0} v{1}", solution.GetFilenameWithoutExtension(), buildContext.ReleaseVersion);
DotNetCoreBuild(solution.FullPath, new DotNetCoreBuildSettings()
{
Configuration = buildContext.Configuration,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.SetVersion(buildContext.ReleaseVersion)
.SetMaxCpuCount(buildContext.UseAsManyProcessesAsThereAreAvailableCPUs)
});
}
});
Task("Run_tests")
.Does<BuildContext>(buildContext =>
{
foreach(var testProject in buildContext.TestProjects)
{
Information("Testing project {0}", testProject.GetFilenameWithoutExtension());
DotNetCoreTest(testProject.FullPath, new DotNetCoreTestSettings
{
Configuration = buildContext.Configuration,
NoBuild = true,
NoRestore = true
});
}
});
Task("Package")
.Does<BuildContext>(buildContext =>
{
foreach(var project in buildContext.NonTestProjects)
{
Information("Packaging project {0} v{1}", project.GetFilenameWithoutExtension(), buildContext.ReleaseVersion);
DotNetCorePack(project.FullPath, new DotNetCorePackSettings {
Configuration = buildContext.Configuration,
OutputDirectory = buildContext.ArtifactsDir,
NoBuild = true,
NoRestore = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.SetVersion(buildContext.ReleaseVersion)
});
}
});
Task("Release")
.WithCriteria<BuildContext>((_, buildContext) =>
buildContext.IsRunningOnAppveyorMasterBranch,
"Skipped as build not triggered by Appveyor 'master' branch commit"
)
.WithCriteria<BuildContext>((_, buildContext) =>
buildContext.HasReleaseVersionChanged,
"Skipped as release version has not changed"
)
.WithCriteria<BuildContext>((_, buildContext) =>
buildContext.IsRunningOnWindows,
"Skipped as release was triggered by a linux build"
)
.Does<BuildContext>(buildContext =>
{
Information("Releasing v{0}", buildContext.ReleaseVersion);
Information("Updating CHANGELOG.md");
Information("Creating github release");
Information("Pushing to NuGet");
Npx("semantic-release", requiredSemanticVersionPackages);
});
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(buildContext.Target);
///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
public class BuildContext
{
public const string DefaultReleaseNumber = "1.0.0";
public string ProjectName { get; }
public string Configuration { get; }
public string Target { get; }
public string ArtifactsDir { get; }
public bool IsRunningOnAppveyorMasterBranch { get; }
public FilePathCollection Solutions { get; }
public FilePathCollection TestProjects { get; }
public FilePathCollection NonTestProjects { get; }
// 0 = use as many processes as there are available CPUs to build the project
// see: https://develop.cakebuild.net/api/Cake.Common.Tools.MSBuild/MSBuildSettings/60E763EA
public int UseAsManyProcessesAsThereAreAvailableCPUs { get; } = 0;
public string ReleaseVersion { get; private set; } = DefaultReleaseNumber;
public bool HasReleaseVersionChanged { get; private set; }
public bool IsRunningOnWindows { get; private set; }
public BuildContext(string projectName, ICakeContext context)
{
ProjectName = projectName;
Target = context.Argument<string>("target", "Default");
IsRunningOnWindows = context.IsRunningOnWindows();
var os = IsRunningOnWindows ? "Windows" : "Linux";
Configuration = context.Argument<string>("configuration", $"Release{os}");
IsRunningOnAppveyorMasterBranch = StringComparer.OrdinalIgnoreCase.Equals(
"master",
context.BuildSystem().AppVeyor.Environment.Repository.Branch
);
ArtifactsDir = context.Directory("./artifacts");
Solutions = context.GetFiles("./src/*.sln");
TestProjects = context.GetFiles("./src/**/*.Tests.csproj");
NonTestProjects = context.GetFiles("./src/**/*.csproj") - TestProjects;
}
public bool SetReleaseVersionFrom(string[] semanticReleaseOutput)
{
var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");
var nextReleaseNumber = semanticReleaseOutput
.Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
.Where(line => !string.IsNullOrWhiteSpace(line))
.SingleOrDefault();
HasReleaseVersionChanged = nextReleaseNumber != null;
ReleaseVersion = nextReleaseNumber ?? DefaultReleaseNumber;
return HasReleaseVersionChanged;
}
}