-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.cake
More file actions
201 lines (164 loc) · 6.8 KB
/
Copy pathbuild.cake
File metadata and controls
201 lines (164 loc) · 6.8 KB
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
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
var projectName = "StarWarsNames";
var artifactsDir = Directory("./artifacts");
var isLocalBuild = BuildSystem.IsLocalBuild;
var nextSemanticVersionNumber = "0.0.0";
//////////////////////////////////////////////////////////////////////
// NUGET ADDINS AND TOOLS
//////////////////////////////////////////////////////////////////////
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Figlet&version=1.0.0"
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument<string>("target", "Default");
var configuration = Argument<string>("configuration", "Release");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information(Figlet(projectName));
});
Teardown(context =>
{
Information("Finished running tasks.");
});
//////////////////////////////////////////////////////////////////////
// PRIVATE TASKS
//////////////////////////////////////////////////////////////////////
Task("Build")
.IsDependentOn("DumpDotnetInfo")
.IsDependentOn("Clean")
.IsDependentOn("GetNextSemanticVersionNumber")
.IsDependentOn("BuildSolution")
.IsDependentOn("RunTests")
.IsDependentOn("Package")
.IsDependentOn("RunSemanticRelease")
;
Task("DumpDotnetInfo")
.Does(() =>
{
Information("dotnet --info");
StartProcess("dotnet", new ProcessSettings { Arguments = "--info" });
});
Task("Clean")
.Does(() =>
{
Information("Cleaning {0}, bin and obj folders", artifactsDir);
CleanDirectory(artifactsDir);
CleanDirectories("./src/**/bin");
CleanDirectories("./src/**/obj");
});
Task("GetNextSemanticVersionNumber")
// .WithCriteria(!isLocalBuild)
.Does(() =>
{
Information("Running semantic-release in dry run mode to extract next semantic version number");
var semanticReleaseOutput = ExecuteSemanticRelease(Context, dryRun: true);
nextSemanticVersionNumber = ExtractNextSemanticVersionNumber(semanticReleaseOutput);
Information("Next semantic version number is {0}", nextSemanticVersionNumber);
});
Task("BuildSolution")
.Does(() =>
{
var solutions = GetFiles("./src/*.sln");
foreach(var solution in solutions)
{
Information("Building solution {0} v{1}", solution.GetFilenameWithoutExtension(), nextSemanticVersionNumber);
DotNetCoreBuild(solution.FullPath, new DotNetCoreBuildSettings()
{
Configuration = configuration,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("SourceLinkCreate", "true")
.WithProperty("Version", $"{nextSemanticVersionNumber}.0")
.WithProperty("AssemblyVersion", $"{nextSemanticVersionNumber}.0")
.WithProperty("FileVersion", $"{nextSemanticVersionNumber}.0")
// 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
.SetMaxCpuCount(0)
});
}
});
Task("RunTests")
.Does(() =>
{
var xunitArgs = "-nobuild -configuration " + configuration;
var testProjects = GetFiles("./src/**/*.Tests.csproj");
foreach(var testProject in testProjects)
{
Information("Testing project {0} with args {1}", testProject.GetFilenameWithoutExtension(), xunitArgs);
DotNetCoreTool(testProject.FullPath, "xunit", xunitArgs);
}
});
Task("Package")
.Does(() =>
{
var projects = GetFiles("./src/**/*.csproj");
foreach(var project in projects)
{
var projectDirectory = project.GetDirectory().FullPath;
if(projectDirectory.EndsWith("Tests")) continue;
Information("Packaging project {0} v{1}", project.GetFilenameWithoutExtension(), nextSemanticVersionNumber);
DotNetCorePack(project.FullPath, new DotNetCorePackSettings {
Configuration = configuration,
OutputDirectory = artifactsDir,
NoBuild = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", $"{nextSemanticVersionNumber}.0")
.WithProperty("AssemblyVersion", $"{nextSemanticVersionNumber}.0")
.WithProperty("FileVersion", $"{nextSemanticVersionNumber}.0")
});
}
});
Task("RunSemanticRelease")
.WithCriteria(nextSemanticVersionNumber != null)
.Does(() =>
{
ExecuteSemanticRelease(Context, dryRun: false);
});
///////////////////////////////////////////////////////////////////////////////
// PRIMARY TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Build");
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);
///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
string[] ExecuteSemanticRelease(ICakeContext context, bool dryRun)
{
var npxPath = context.Tools.Resolve("npx.cmd");
if (npxPath == null) throw new Exception("Could not locate executable 'npm'.");
IEnumerable<string> redirectedStandardOutput;
var exitCode = StartProcess(
npxPath,
new ProcessSettings()
.SetRedirectStandardOutput(true)
.WithArguments(args => args
.AppendSwitch("-p", "semantic-release@next")
.AppendSwitch("-p", "@semantic-release/changelog")
.AppendSwitch("-p", "@semantic-release/git")
.Append("semantic-release")
.Append("--no-ci")
.Append(dryRun ? "--dry-run" : "")
),
out redirectedStandardOutput
);
var semanticReleaseOutput = redirectedStandardOutput.ToArray();
Information(string.Join(Environment.NewLine, semanticReleaseOutput));
if (exitCode != 0) throw new Exception($"Process returned an error (exit code {exitCode}).");
return semanticReleaseOutput;
}
string ExtractNextSemanticVersionNumber(string[] semanticReleaseOutput)
{
var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");
return semanticReleaseOutput
.Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
.Where(line => !string.IsNullOrWhiteSpace(line))
.SingleOrDefault();
}