-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.cake
177 lines (150 loc) · 4.32 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
var target = Argument("target", "Default");
var apiKey = Argument<string>("apiKey", null);
var testFailed = false;
var solutionDir = System.IO.Directory.GetCurrentDirectory();
var testResultDir = System.IO.Path.Combine(solutionDir, "testResults");
var artifactDir = "./artifacts";
Information("Solution Directory: {0}", solutionDir);
Information("Test Results Directory: {0}", testResultDir);
Task("PrepareDirectories")
.Does(() =>
{
EnsureDirectoryExists(testResultDir);
EnsureDirectoryExists(artifactDir);
});
Task("Clean")
.IsDependentOn("PrepareDirectories")
.Does(() =>
{
var delSettings = new DeleteDirectorySettings { Recursive = true, Force = true };
CleanDirectory(testResultDir);
CleanDirectory(artifactDir);
var binDirs = GetDirectories("./**/bin");
var objDirs = GetDirectories("./**/obj");
var testResDirs = GetDirectories("./**/test-results");
DeleteDirectories(binDirs, delSettings);
DeleteDirectories(objDirs, delSettings);
DeleteDirectories(testResDirs, delSettings);
});
Task("Restore")
.Does(() =>
{
DotNetCoreRestore();
});
Task("Build")
.IsDependentOn("Restore")
.Does(() =>
{
var solution = GetFiles("./*.sln").ElementAt(0);
Information("Build solution: {0}", solution);
var settings = new DotNetCoreBuildSettings
{
Configuration = "Release"
};
DotNetCoreBuild(solution.FullPath, settings);
});
Task("Test")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.ContinueOnError()
.Does(() =>
{
var tests = GetFiles("./test/**/*Test/*.csproj");
if(tests.Count == 0)
{
Information("Found no test projects");
return;
}
foreach(var test in tests)
{
var projectFolder = System.IO.Path.GetDirectoryName(test.FullPath);
try
{
DotNetCoreTest(test.FullPath, new DotNetCoreTestSettings
{
ArgumentCustomization = args => args.Append("-l trx"),
WorkingDirectory = projectFolder
});
}
catch(Exception e)
{
testFailed = true;
Error(e.Message.ToString());
}
}
// Copy test result files.
var tmpTestResultFiles = GetFiles("./**/*.trx");
CopyFiles(tmpTestResultFiles, testResultDir);
});
Task("Pack")
.IsDependentOn("Clean")
.IsDependentOn("Test")
.Does(() =>
{
if(testFailed)
{
Information("Do not pack because tests failed");
return;
}
var projects = GetFiles("./src/**/*.csproj");
var settings = new DotNetCorePackSettings
{
Configuration = "Release",
OutputDirectory = artifactDir
};
foreach(var project in projects)
{
Information("Pack {0}", project.FullPath);
DotNetCorePack(project.FullPath, settings);
}
});
Task("Publish")
.IsDependentOn("Clean")
.IsDependentOn("Test")
.Does(() =>
{
if(testFailed)
{
Information("Do not publish because tests failed");
return;
}
var projects = GetFiles("./src/**/*.csproj");
foreach(var project in projects)
{
var projectDir = System.IO.Path.GetDirectoryName(project.FullPath);
var projectName = new System.IO.DirectoryInfo(projectDir).Name;
var outputDir = System.IO.Path.Combine(artifactDir, projectName);
EnsureDirectoryExists(outputDir);
Information("Publish {0} to {1}", projectName, outputDir);
var settings = new DotNetCorePublishSettings
{
OutputDirectory = outputDir,
Configuration = "Release"
};
DotNetCorePublish(project.FullPath, settings);
}
});
Task("Push")
.IsDependentOn("Pack")
.Does(() =>
{
var package = GetFiles($"{artifactDir}/TransNet.*.nupkg").ElementAt(0).FullPath;
var source = "https://www.nuget.org/api/v2/package";
if(apiKey==null)
throw new ArgumentNullException(nameof(apiKey), "The \"apiKey\" argument must be set for this task.");
Information($"Push {package} to {source}");
DotNetCoreNuGetPush(package, new DotNetCoreNuGetPushSettings {
Source = source,
ApiKey = apiKey
});
});
Task("Default")
.IsDependentOn("Test")
.Does(() =>
{
Information("Build and test the whole solution.");
Information("To pack (nuget) the application use the cake build argument: --target=Pack");
Information("To publish (to run it somewhere else) the application use the cake build argument: --Target=Publish");
Information("To push the package to nuget.org use the cake build argument: --target=Push --apiKey=\"your nuget.org API key\"");
});
RunTarget(target);