-
Notifications
You must be signed in to change notification settings - Fork 0
/
build1.cake
225 lines (185 loc) · 7.55 KB
/
build1.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
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
var projectName = "StarWarsNames";
var artifactsDir = Directory("./artifacts");
var isLocalBuild = BuildSystem.IsLocalBuild;
var semanticVersionNumber = "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(() =>
{
var semanticReleaseOutput = ExecuteSemanticRelease(Context, dryRun := false);
Information("{0}", semanticReleaseOutput.Count());
// semanticVersionNumber = ExtractNextSemanticVersionNumber(semanticReleaseOutputLines);
// Information("Next semantic version number is {0}", semanticVersionNumber);
});
Task("BuildSolution")
.Does(() =>
{
var solutions = GetFiles("./src/*.sln");
foreach(var solution in solutions)
{
Information("Building solution {0}", solution.GetFilenameWithoutExtension());
DotNetCoreBuild(solution.FullPath, new DotNetCoreBuildSettings()
{
Configuration = configuration,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("SourceLinkCreate", "true")
.WithProperty("Version", $"{semanticVersionNumber}.0")
.WithProperty("AssemblyVersion", $"{semanticVersionNumber}.0")
.WithProperty("FileVersion", $"{semanticVersionNumber}.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}", project.GetFilenameWithoutExtension());
DotNetCorePack(project.FullPath, new DotNetCorePackSettings {
Configuration = configuration,
OutputDirectory = artifactsDir,
NoBuild = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", $"{semanticVersionNumber}.0")
.WithProperty("AssemblyVersion", $"{semanticVersionNumber}.0")
.WithProperty("FileVersion", $"{semanticVersionNumber}.0")
});
}
});
Task("RunSemanticRelease")
// .WithCriteria(isContinuousIntegrationBuild)
.Does(() =>
{
var npxPath = Context.Tools.Resolve("npx.cmd");
var exitCode = StartProcess(
npxPath,
new ProcessSettings()
.WithArguments(args => args
.AppendSwitch("-p", "semantic-release@next")
.AppendSwitch("-p", "@semantic-release/changelog")
.Append("semantic-release")
//.Append("--no-ci")
)
);
if (exitCode != 0) {
throw new Exception($"semantic-release exited with exit code {exitCode}");
}
});
///////////////////////////////////////////////////////////////////////////////
// PRIMARY TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Build");
///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
string ExtractNextSemanticVersionNumber(IEnumerable<string> semanticReleaseOutputLines)
{
Information("{0}", semanticReleaseOutputLines.Count());
return "1";
// var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");
// var nextSemanticVersionNumber = semanticReleaseOutputLines
// .Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
// .Where(line => !string.IsNullOrWhiteSpace(line))
// .SingleOrDefault();
// if (nextSemanticVersionNumber == null)
// {
// throw new Exception("Could not extract next semantic version number from semantic-release output");
// }
// return nextSemanticVersionNumber;
}
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);
///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
string[] ExecuteSemanticRelease(ICakeContext context, bool dryRunMode)
{
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")
.Append("semantic-release")
.Append(dryRunMode ? "--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;
}