-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
164 lines (140 loc) · 5.05 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
var _target = Argument("target", "Default");
var _configuration = Argument("configuration", "Release");
var _versionSuffix = BuildVersionSuffix();
var PROJECT_DIR = Context.Environment.WorkingDirectory.FullPath + "/";
var SRC_DIR = PROJECT_DIR + "src/";
var TEST_DIR = PROJECT_DIR + "test/";
var ARTIFACTS_DIR = PROJECT_DIR + "artifacts/";
var PACKAGES_DIR = ARTIFACTS_DIR + "packages/";
var TEST_RESULTS_DIR = ARTIFACTS_DIR + "test-results/";
var CSPROJ = SRC_DIR + "ByValue/ByValue.csproj";
string BuildVersionSuffix() {
var buildNumber =
HasArgument("BuildNumber") ? Argument("BuildNumber", "0") :
AppVeyor.IsRunningOnAppVeyor ? AppVeyor.Environment.Build.Number.ToString() :
TravisCI.IsRunningOnTravisCI ? TravisCI.Environment.Build.BuildNumber.ToString() :
EnvironmentVariable("BUILD_NUMBER") ?? "localbuild";
if (!BuildSystem.IsRunningOnAppVeyor)
return buildNumber;
var tag = AppVeyor.Environment.Repository.Tag;
if (tag.IsTag)
return null; // only version prefix used for NuGet.org
var dbgSuffix = _configuration == "Debug" ? "-dbg" : "";
var result = "ci-" + buildNumber + dbgSuffix;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var branch = AppVeyor.Environment.Repository.Branch;
if (isPullRequest)
result += "-pr-" + AppVeyor.Environment.PullRequest.Number;
else
result += "-" + System.Text.RegularExpressions.Regex.Replace(branch, "[^0-9A-Za-z-]+", "-");
// Nuget limits
if (result.Length > 20)
result = result.Substring(0, 20);
return result;
}
Setup(context =>
{
var versionPrefix = XmlPeek("Directory.Build.props", "//VersionPrefix");
var packageVersion = _versionSuffix == null ? versionPrefix : versionPrefix + "-" + _versionSuffix;
Information("Building {0} version {1}", _configuration, packageVersion);
});
Task("Clean")
.Does(() => {
CleanDirectory(ARTIFACTS_DIR);
// dotnet clean does not work on root dir: https://github.com/dotnet/cli/issues/7240
var projects = GetFiles("./**/*.csproj");
foreach (var project in projects)
{
DotNetCoreClean(project.FullPath,
new DotNetCoreCleanSettings() {
Configuration = _configuration
}
);
}
});
Task("Build")
.Does(() => {
DotNetCoreBuild(".",
new DotNetCoreBuildSettings() {
Configuration = _configuration
}
);
});
Task("Test")
.Does(() => {
var testProjects = GetFiles("test/**/*.Tests.csproj").ToList();
for(int i = 0; i < testProjects.Count; i++) {
var testProject = testProjects[i];
var testResultsFileName = $"test-result-{i+1}.xml";
// wait nunit support for xml results https://github.com/nunit/nunit3-vs-adapter/issues/323
// use NunitXml.TestLogger at now
DotNetCoreTest(
testProject.FullPath,
new DotNetCoreTestSettings() {
Configuration = _configuration,
NoBuild = true,
NoRestore = true,
Logger = $"nunit;LogFilePath={TEST_RESULTS_DIR + testResultsFileName}"
}
);
}
})
.Finally(() =>
{
if (BuildSystem.IsRunningOnAppVeyor)
{
Information("Uploading test results...");
var testResults = GetFiles(TEST_RESULTS_DIR + "test-result*").ToList();
foreach (var file in testResults)
{
AppVeyor.UploadTestResults(file.FullPath, AppVeyorTestResultsType.NUnit);
}
}
});
Task("Package")
.Does(() => {
DotNetCorePack(CSPROJ,
new DotNetCorePackSettings {
Configuration = _configuration,
NoBuild = true,
NoRestore = true,
OutputDirectory = PACKAGES_DIR,
VersionSuffix = _versionSuffix
}
);
});
Task("Publish-MyGet")
.Does(() =>
{
var apiKey = EnvironmentVariable("MYGET_API_KEY");
var apiUrl = EnvironmentVariable("MYGET_API_URL");
if(string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(apiUrl)) {
Information("Could not resolve key and url for MyGet.");
return;
}
DotNetCoreNuGetPush($"{PACKAGES_DIR}*.nupkg",
new DotNetCoreNuGetPushSettings {
WorkingDirectory = PACKAGES_DIR,
Source = EnvironmentVariable("MYGET_API_URL"),
ApiKey = EnvironmentVariable("MYGET_API_KEY"),
}
);
});
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package");
Task("Travis")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test")
;
Task("AppVeyor")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package")
.IsDependentOn("Publish-MyGet")
;
RunTarget(_target);