-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbuild.cake
292 lines (250 loc) · 9.75 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
///////////////////////////////////////////////////////////////////////////////
// Simple Cake Build using MSBuild
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Install Addins
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Install Tools
///////////////////////////////////////////////////////////////////////////////
#tool "nuget:?package=xunit.runner.console&version=2.1.0"
#tool "nuget:?package=GitVersion.CommandLine"
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var runAllTests = Argument<bool>("runAllTests", false);
var skipNuGetRestore = Argument<bool>("skipNuGetRestore", false);
var skipTests = Argument<bool>("skiptests", false);
// Defaulting to the x86 version of the xunit runner
var xunitRunnerPath = Argument<string>("xunitRunnerPath", "./tools/xunit.runner.console/tools/xunit.console.x86.exe");
var nugetPublishSource = Argument("nugetPublishSource", "https://api.nuget.org/v3/index.json");
var nugetApiKey = Argument("nugetApiKey","");
//////////////////////////////////////////////////////////////////////
// CUSTOMIZE PER Project
//////////////////////////////////////////////////////////////////////
var traitsToExclude = new Dictionary<string, IList<string>>{
{"Category", new []{"Integration","Debug","Unstable"}}
};
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
// Define directories.
var rootDir = Directory("./");
var srcDir = rootDir + Directory("src");
var srcProjects = GetFiles("./src/**/*.csproj");
var testProjects = GetFiles("./test/**/*.csproj");
var allProjects = GetFiles("./**/*.csproj");
var rootOutDir = rootDir + Directory(".build");
var solutionFile = rootDir + File("Even.sln");
// Define nugetPushSource
var projects = new List<ProjectInfo>();
GitVersion version;
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Setup(context=>{
version = GitVersion(new GitVersionSettings {
UpdateAssemblyInfo = false,
OutputType = GitVersionOutput.BuildServer
});
foreach(var project in allProjects){
var projectInfo = new ProjectInfo(Context, project);
projectInfo.OutputDirectory = rootOutDir + Directory(projectInfo.DirectoryName);
projectInfo.BuildOutputDirectory = Directory(project.GetDirectory().FullPath) + Directory("bin") + Directory(configuration);
projects.Add(projectInfo);
}
foreach(var testProject in testProjects){
var testProjectPath = testProject.FullPath;
var project = projects.FirstOrDefault(p=> String.Equals(p.Path.FullPath, testProjectPath, StringComparison.OrdinalIgnoreCase));
if(project != null){
Information("{0} is a test project.", project.Path);
project.MarkAsTestProject();
}
}
});
Task("Print-Info").Does(()=>{
version = GitVersion(new GitVersionSettings {
UpdateAssemblyInfo = false
});
Information("###############################################");
Information("# VersionInfo:");
Information("# NuGetVersion: {0}", version.NuGetVersion);
Information("# SemVer: {0}", version.SemVer);
Information("# InformationalVersion: {0}", version.InformationalVersion);
Information("###############################################");
});
Task("Update-Versions").Does(()=>{
version = GitVersion(new GitVersionSettings {
UpdateAssemblyInfo = true,
UpdateAssemblyInfoFilePath = File(".\\CommonAssemblyInfo.cs")
});
});
Task("Clean")
.Does(() =>
{
foreach(var project in projects){
CleanDirectory(project.OutputDirectory);
//CleanDirectory(project.TestResultsOutputDirectory);
if(FileExists(project.NuSpec)) {
CleanDirectory(project.OutputDirectory.Combine(Directory("nuget")));
}
if(project.IsTestProject){
var testResultsDir = project.OutputDirectory.Combine(Directory("testResults"));
CleanDirectory(testResultsDir);
}
}
});
Task("Clean-BuildOutput").Does(() =>
{
foreach(var project in projects){
CleanDirectory(project.BuildOutputDirectory);
}
});
Task("Restore-NuGet-Packages")
.IsDependentOn("Clean")
.WithCriteria(() => !skipNuGetRestore)
.Does(() =>
{
NuGetRestore(solutionFile);
});
Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
if(IsRunningOnWindows())
{
// Use MSBuild
MSBuild(solutionFile, settings =>
settings.SetConfiguration(configuration));
}
else
{
// Use XBuild
XBuild(solutionFile, settings =>
settings.SetConfiguration(configuration));
}
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.WithCriteria(() => !skipTests)
.Does(() =>
{
foreach(var project in projects.Where(p=>p.IsTestProject)) {
var testAssembly = project.BuildOutputDirectory.CombineWithFilePath(File(project.Name+".dll"));
if(FileExists(testAssembly)){
var testResultsDir = project.OutputDirectory.Combine(Directory("testResults"));
EnsureDirectoryExists(testResultsDir);
var settings = new XUnit2Settings{
HtmlReport=true,
XmlReport=true,
OutputDirectory = testResultsDir
};
if(!string.IsNullOrEmpty(xunitRunnerPath)){
settings.ToolPath = xunitRunnerPath;
}
if(!runAllTests) {
foreach (var kvp in traitsToExclude) {
foreach(var value in kvp.Value) {
settings.ExcludeTrait(kvp.Key, value);
}
}
}
XUnit2(testAssembly.FullPath, settings);
}
}
});
Task("Copy-BuildOutput")
.Does(()=>{
foreach(var project in projects) {
CopyDirectory(project.BuildOutputDirectory, project.OutputDirectory.Combine(Directory("bin")));
}
});
Task("NuGet-Pack")
//.IsDependentOn("Copy-BuildOutput")
.Does(()=>{
foreach(var project in projects){
if(FileExists(project.NuSpec)){
var nugetFolder = project.OutputDirectory.Combine(Directory("nuget"));
//EnsureDirectoryExists(nugetFolder);
CopyFileToDirectory(project.NuSpec, project.OutputDirectory);
Information("Working Directory is: {0}", project.OutputDirectory);
var packSettings = new NuGetPackSettings {
OutputDirectory = nugetFolder,
Version = version.NuGetVersion,
WorkingDirectory = project.OutputDirectory,
Properties = new Dictionary<string,string>{
{"Configuration", configuration}
}
};
NuGetPack(project.NuSpec, packSettings);
}
}
});
Task("NuGet-Push")
.Does(()=>{
var packages = GetFiles("./.build/**/nuget/*.nupkg");
NuGetPush(packages, new NuGetPushSettings{
Source = nugetPublishSource,
ApiKey = nugetApiKey
});
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("About")
.IsDependentOn("Print-Info");
Task("DeepClean")
.IsDependentOn("Clean-BuildOutput")
.IsDependentOn("Clean");
Task("Package")
.IsDependentOn("NuGet-Pack");
Task("Publish")
.IsDependentOn("NuGet-Push");
Task("BuildAndPackage")
.IsDependentOn("Update-Versions")
.IsDependentOn("Print-Info")
.IsDependentOn("DeepClean")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Package");
Task("BuildAndPublish")
.IsDependentOn("BuildAndPackage")
.IsDependentOn("Publish");
Task("CI")
.IsDependentOn("BuildAndPublish");
Task("BLD")
.IsDependentOn("Clean")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Package");
Task("Default")
.IsDependentOn("BuildAndPackage");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);
//////////////////////////////////////////////////////////////////////
// Utility and Helper
//////////////////////////////////////////////////////////////////////
public class ProjectInfo {
public ProjectInfo(ICakeContext context, FilePath path) {
Path = path;
Name = path.GetFilenameWithoutExtension().ToString();
Directory = path.GetDirectory();
DirectoryName = Directory.GetDirectoryName();
NuSpec = Directory.CombineWithFilePath(path.ChangeExtension(".nuspec").GetFilename());
Configuration = context.Argument("configuration", "Release");
}
public FilePath Path {get; private set;}
public DirectoryPath Directory {get; private set;}
public string Name {get; private set;}
public string DirectoryName {get; private set;}
public FilePath NuSpec {get; set;}
public DirectoryPath OutputDirectory {get;set;}
public DirectoryPath BuildOutputDirectory {get;set;}
public bool IsTestProject {get; private set;}
public string Configuration {get; private set;}
public void MarkAsTestProject() {
IsTestProject = true;
}
}