This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
build.cake
258 lines (214 loc) · 8.23 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
#tool "nuget:?package=NUnit.Runners&version=2.6.4"
#tool "nuget:?package=GitVersion.CommandLine"
//////////////////////////////////////////////////////////////////////
// ARGUMENTS AND ENVIRONMENT VARIABLES
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Debug");
var nugetOrgApiKey = EnvironmentVariable("NuGetOrgApiKey");
var isTaggedBuild = Convert.ToBoolean(EnvironmentVariable("APPVEYOR_REPO_TAG"));
var tag = EnvironmentVariable("APPVEYOR_REPO_TAG_NAME") ?? "<no tag>";
var isAppVeyor = BuildSystem.IsRunningOnAppVeyor;
var isTravis = BuildSystem.IsRunningOnTravisCI;
//////////////////////////////////////////////////////////////////////
// VERSION
//////////////////////////////////////////////////////////////////////
// GitVersion doesn't work on Linux: https://github.com/cake-build/cake/issues/2126
var gitVersion = !IsRunningOnWindows() ? null : GitVersion(new GitVersionSettings
{
OutputType = GitVersionOutput.Json,
UpdateAssemblyInfo = false
});
var version = "0.0.1";
var branchName = "unknown";
if (gitVersion != null)
{
version = gitVersion.NuGetVersion;
branchName = gitVersion.BranchName.Trim();
}
//////////////////////////////////////////////////////////////////////
// CONSTS
//////////////////////////////////////////////////////////////////////
var artifactsDir = Directory("./artifacts");
var outputDir = Directory("./build");
var dotnetFrameworks = IsRunningOnWindows() ? new [] { "net471", "net45", "net40", "netstandard2.0" } : new string[] { };
// net35 can't be build by dotnet - https://github.com/Microsoft/msbuild/issues/1333
var msBuildFrameworks = IsRunningOnWindows() ? new [] { "net35" } : new [] { "net471", "net45", "net40", "net35", "netstandard2.0" };
var frameworks = dotnetFrameworks.Union(msBuildFrameworks).ToList();
var solution = "src/SharpRaven.sln";
var packages = new []
{
"src/app/SharpRaven/SharpRaven.csproj",
"src/app/SharpRaven.Nancy/SharpRaven.Nancy.csproj",
};
//////////////////////////////////////////////////////////////////////
// SETUP
//////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information("Building version {0} ({1}@{2}) of SharpRaven.",
version, branchName, tag);
if (isAppVeyor)
{
// If AppVeyor is building master with no tag, it should
// not update the build version, since it will be duplicate
// with the one for the tagged build
if (branchName == "master" && !isTaggedBuild)
{
return;
}
Information("Updating AppVeyor build version:" + gitVersion.FullBuildMetaData);
AppVeyor.UpdateBuildVersion(gitVersion.FullBuildMetaData);
}
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Description("Deletes all files in the artifact and output directories")
.Does(() =>
{
CleanDirectory(artifactsDir);
CleanDirectory(outputDir);
});
Task("RestorePackages")
.Description("Restores packages from nuget using 'dotnet'")
.Does(() =>
{
DotNetCoreRestore(solution);
});
Task("UpdateAssemblyInformation")
.Description("Update assembly information using GitVersion")
.WithCriteria(isAppVeyor)
.Does(() =>
{
GitVersion(new GitVersionSettings
{
UpdateAssemblyInfo = true,
UpdateAssemblyInfoFilePath = "src/CommonAssemblyInfo.cs",
});
Information("AssemblyVersion -> {0}", gitVersion.AssemblySemVer);
Information("AssemblyFileVersion -> {0}.0", gitVersion.MajorMinorPatch);
Information("AssemblyInformationalVersion -> {0}", gitVersion.InformationalVersion);
});
Task("Build")
.Description("Builds all versions")
.IsDependentOn("RestorePackages")
.IsDependentOn("UpdateAssemblyInformation")
.Does(() =>
{
EnsureDirectoryExists(outputDir);
foreach (var framework in msBuildFrameworks)
{
var settings = new MSBuildSettings
{
Configuration = configuration + "-" + framework,
ToolVersion = MSBuildToolVersion.VS2017
};
settings.WithProperty("TargetFramework", new string[] { framework })
.WithProperty("Optimize", new string[] { "true" });
MSBuild(solution, settings);
}
foreach (var framework in dotnetFrameworks)
{
var settings = new DotNetCoreBuildSettings
{
Framework = framework,
Configuration = configuration + "-" + framework,
MSBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Optimize", new string[] { "true" })
};
DotNetCoreBuild(solution, settings);
}
});
Task("Test")
.Description("Runs all the tests on all the versions")
.IsDependentOn("Build")
.Does(() =>
{
EnsureDirectoryExists(artifactsDir);
foreach (var framework in frameworks.Where(x => x != "netstandard2.0"))
{
var assemblies = GetFiles((outputDir + Directory(configuration) + Directory(framework)).ToString() + "/*.UnitTests.dll");
if (!assemblies.Any())
{
throw new FileNotFoundException("Could not find any test assemblies in: '" + configuration + "-" + framework + "'.");
}
var resultPath = artifactsDir + File(configuration + "-" + framework + "-tests.xml");
NUnit(assemblies, new NUnitSettings
{
ResultsFile = resultPath,
Exclude = IsRunningOnWindows() ? null : "NuGet,NoMono",
});
if (isAppVeyor)
{
AppVeyor.UploadTestResults(resultPath, AppVeyorTestResultsType.NUnit);
}
}
});
Task("Package")
.Description("Create NuGet packages")
.IsDependentOn("Test")
.Does(() =>
{
EnsureDirectoryExists(artifactsDir);
foreach (var package in packages)
{
MSBuild(package, c => c
.SetConfiguration("Release")
.SetVerbosity(Verbosity.Minimal)
.UseToolVersion(MSBuildToolVersion.VS2017)
.WithProperty("NoBuild", "true")
.WithProperty("Version", gitVersion.NuGetVersion)
.WithTarget("Pack"));
}
MoveFiles((outputDir + Directory(configuration)).ToString() + "/*.nupkg", artifactsDir);
});
Task("UploadAppVeyorArtifacts")
.Description("Uploads artifacts to AppVeyor")
.IsDependentOn("Package")
.Does(() =>
{
foreach (var zip in System.IO.Directory.GetFiles(artifactsDir, "*.nupkg"))
{
AppVeyor.UploadArtifact(zip);
}
});
Task("PublishNuGetPackages")
.Description("Publishes .nupkg files to nuget.org")
.IsDependentOn("Package")
.WithCriteria(isAppVeyor && (isTaggedBuild || branchName == "develop"))
.Does(() =>
{
if (String.IsNullOrEmpty(nugetOrgApiKey))
{
throw new ArgumentNullException("nugetOrgApiKey");
}
var nugetFiles = GetFiles(artifactsDir.ToString() + "/*.nupkg");
NuGetPush(nugetFiles, new NuGetPushSettings
{
ApiKey = nugetOrgApiKey,
Source = "https://api.nuget.org/v3/index.json"
});
});
//////////////////////////////////////////////////////////////////////
// META TASKS
//////////////////////////////////////////////////////////////////////
Task("Rebuild")
.Description("Rebuilds all versions")
.IsDependentOn("Clean")
.IsDependentOn("Build");
Task("Appveyor")
.Description("Builds, tests and publishes packages on AppVeyor")
.IsDependentOn("UploadAppVeyorArtifacts")
.IsDependentOn("PublishNuGetPackages");
Task("Travis")
.Description("Builds and tests on Travis")
.IsDependentOn("Test");
Task("Default")
.Description("Builds all versions")
.IsDependentOn("Build");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);