-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
261 lines (227 loc) · 7.49 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
#tool "nuget:?package=xunit.runner.console"
#tool "nuget:?package=ReportUnit"
///////////////////////////////////////////////////////////////////////////////
// BUILD ENVIRONMENT
///////////////////////////////////////////////////////////////////////////////
var local = BuildSystem.IsLocalBuild;
var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument<string>("target", "Default");
var configuration = Argument<string>("configuration", "Release");
var buildNumber = Argument<int>("buildnumber", 0);
var preRelease = Argument<bool>("preRelease", local);
///////////////////////////////////////////////////////////////////////////////
// PREPARATION
///////////////////////////////////////////////////////////////////////////////
var projectName = "Hyperspec";
// Parse release notes.
var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");
// Get version.
var semanticVersion = releaseNotes.Version.ToString();
// Get build number
if (isRunningOnAppVeyor) {
buildNumber = AppVeyor.Environment.Build.Number;
}
// Define directories.
var sourceDirectory = Directory("./src");
var outputDirectory = Directory("./output");
var testResultsDirectory = outputDirectory + Directory("tests");
var artifactsDirectory = outputDirectory + Directory("artifacts");
var solutions = GetFiles("./**/*.sln");
var solutionPaths = solutions.Select(solution => solution.GetDirectory());
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
// Executed BEFORE the first task.
Information("Target: " + target);
Information("Configuration: " + configuration);
Information("Is local build: " + local.ToString());
Information("Is running on AppVeyor: " + isRunningOnAppVeyor.ToString());
Information("Semantic Version: " + semanticVersion);
Information("NuGet Api Key: " + EnvironmentVariable("NuGetApiKey"));
});
Teardown(context =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
///////////////////////////////////////////////////////////////////////////////
// TASK DEFINITIONS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
// Clean solution directories.
foreach(var path in solutionPaths)
{
Information("Cleaning {0}", path);
CleanDirectories(artifactsDirectory.Path + "/");
CleanDirectories(path + "/**/bin/" + configuration);
CleanDirectories(path + "/**/obj/" + configuration);
}
CleanDirectories(outputDirectory);
});
Task("Create-Directories")
.IsDependentOn("Clean")
.Does(() =>
{
var directories = new List<DirectoryPath>{ outputDirectory, testResultsDirectory, artifactsDirectory };
directories.ForEach(directory =>
{
if (!DirectoryExists(directory))
{
CreateDirectory(directory);
}
});
});
Task("Restore-NuGet-Packages")
.IsDependentOn("Create-Directories")
.Does(() =>
{
// Restore all NuGet packages.
foreach(var solution in solutions)
{
Information("Restoring {0}...", solution);
NuGetRestore(solution);
}
});
Task("Patch-Assembly-Info")
.IsDependentOn("Restore-NuGet-Packages")
.WithCriteria(() => !local)
.Does(() =>
{
var assemblyInfoFiles = GetFiles("./**/AssemblyInfo.cs");
var assemblyVersion = semanticVersion + "." + buildNumber;
foreach(var assemblyInfoFile in assemblyInfoFiles)
{
var parsedAssemblyInfo = ParseAssemblyInfo(assemblyInfoFile);
CreateAssemblyInfo(assemblyInfoFile, new AssemblyInfoSettings {
// Copy everything except version from the old AssemblyInfo file
Product = parsedAssemblyInfo.Product,
Company = parsedAssemblyInfo.Company,
Title = parsedAssemblyInfo.Title,
Description = parsedAssemblyInfo.Description,
Guid = parsedAssemblyInfo.Guid,
Copyright = parsedAssemblyInfo.Copyright,
Trademark = parsedAssemblyInfo.Trademark,
ComVisible = parsedAssemblyInfo.ComVisible,
CLSCompliant = parsedAssemblyInfo.ClsCompliant,
InternalsVisibleTo = parsedAssemblyInfo.InternalsVisibleTo,
Configuration = parsedAssemblyInfo.Configuration,
Version = assemblyVersion,
FileVersion = assemblyVersion,
InformationalVersion = assemblyVersion
});
}
});
Task("Build")
.IsDependentOn("Patch-Assembly-Info")
.Does(() =>
{
// Build all solutions.
foreach(var solution in solutions)
{
Information("Building {0}", solution);
MSBuild(solution, settings =>
settings
.WithTarget("Build")
.SetConfiguration(configuration));
}
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.Does(() =>
{
var testAssemblies = GetFiles(sourceDirectory.Path + "/**/bin/" + configuration + "/*.Tests.dll");
if(testAssemblies.Count() > 0)
{
XUnit2(sourceDirectory.Path + "/**/bin/" + configuration + "/*.Tests.dll",
new XUnit2Settings
{
OutputDirectory = testResultsDirectory.Path,
XmlReport = true
}
);
}
}).
Finally(() => {
ReportUnit(testResultsDirectory.Path);
});
Task("Create-NuGet-Packages")
.IsDependentOn("Run-Unit-Tests")
.Does(() =>
{
var nugetVersion = semanticVersion;
if (preRelease) {
nugetVersion = string.Format("{0}-build{1:0000}", nugetVersion, buildNumber);
}
var nuspecFiles = GetFiles(sourceDirectory.Path + "/**/*.nuspec");
foreach(var nuspecFile in nuspecFiles)
{
NuGetPack(nuspecFile,
new NuGetPackSettings
{
BasePath = nuspecFile.GetDirectory() + "/bin/" + configuration,
OutputDirectory = artifactsDirectory.Path,
Version = nugetVersion
}
);
}
});
Task("Update-AppVeyor-Build-Number")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
var appVeyorVersion = semanticVersion + "." + buildNumber;
AppVeyor.UpdateBuildVersion(appVeyorVersion);
});
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Package")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
var artifacts = GetFiles(artifactsDirectory.Path + "/**/*.nupkg");
foreach(var artifact in artifacts)
{
AppVeyor.UploadArtifact(artifact);
}
});
Task("Publish-NuGet-Packages")
.IsDependentOn("Upload-AppVeyor-Artifacts")
.WithCriteria(() => !local)
.WithCriteria(() => !isPullRequest)
.Does(() =>
{
// Resolve the API key.
var apiKey = EnvironmentVariable("NuGetApiKey");
if(string.IsNullOrEmpty(apiKey)) {
throw new InvalidOperationException("Could not resolve NuGet API key.");
}
var nugetPackages = GetFiles(artifactsDirectory.Path + "/**/*.nupkg");
foreach(var nugetPackage in nugetPackages)
{
NuGetPush(nugetPackage, new NuGetPushSettings {
ApiKey = apiKey
});
}
});
///////////////////////////////////////////////////////////////////////////////
// TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Run-Unit-Tests");
Task("Package")
.IsDependentOn("Create-NuGet-Packages");
Task("Publish")
.IsDependentOn("Update-AppVeyor-Build-Number")
.IsDependentOn("Upload-AppVeyor-Artifacts")
/*.IsDependentOn("Publish-NuGet-Packages")*/;
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);