-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
261 lines (203 loc) · 8.28 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
using System;
using System.Text.RegularExpressions;
using System.Diagnostics;
var target = Argument("target", "default");
var configuration = Argument("config", "Release");
var buildNumber = Argument<int>("buildnumber", 1);
var artifactOutput = "./artifacts";
var testResults = "results.trx";
string projectPath = "./src/LocalStack.AwsLocal/LocalStack.AwsLocal.csproj";
Task("Default")
.IsDependentOn("init")
.IsDependentOn("tests");
Task("init")
.Description("Initialize task prerequisites")
.Does(() =>
{
StartProcess("dotnet", new ProcessSettings {
Arguments = "--info"
});
if(IsRunningOnUnix())
{
StartProcess("git", new ProcessSettings {
Arguments = "config --global core.autocrlf true"
});
StartProcess("mono", new ProcessSettings {
Arguments = "--version"
});
InstallXUnitNugetPackage();
}
StartProcess("git", new ProcessSettings {
Arguments = "submodule update --init --recursive"
});
});
Task("compile")
.Description("Builds all the projects in the solution")
.Does(() =>
{
string slnPath = "./src/LocalStack.sln";
DotNetCoreBuildSettings settings = new DotNetCoreBuildSettings();
settings.Configuration = configuration;
DotNetCoreBuild(slnPath, settings);
});
Task("tests")
.Description("Run Tests")
.IsDependentOn("compile")
.Does(() =>
{
DotNetCoreTestSettings settings = new DotNetCoreTestSettings();
settings.NoRestore = true;
settings.NoBuild = true;
settings.Configuration = configuration;
IList<TestProjMetadata> testProjMetadatas = GetProjMetadata();
foreach (var testProj in testProjMetadatas)
{
string testProjectPath = testProj.CsProjPath;
Warning($"Target Frameworks {string.Join(" ",testProj.TargetFrameworks)}");
foreach(string targetFramework in testProj.TargetFrameworks)
{
Warning($"Running {targetFramework.ToUpper()} tests for {testProj.AssemblyName}");
settings.Framework = targetFramework;
if(IsRunningOnUnix() && targetFramework == "net461")
{
RunXunitUsingMono(targetFramework, $"{testProj.DirectoryPath}/bin/{configuration}/{targetFramework}/{testProj.AssemblyName}.dll");
}
else
{
string testFilePrefix = targetFramework.Replace(".","-");
settings.ArgumentCustomization = args => args.Append($" --logger \"trx;LogFileName={testFilePrefix}_{testResults}\"");
DotNetCoreTest(testProjectPath, settings);
}
}
}
});
Task("nuget-pack")
.Does(() =>
{
string outputDirectory = MakeAbsolute(Directory(artifactOutput)).FullPath;
string projectFullPath = MakeAbsolute(File(projectPath)).FullPath;
if(!System.IO.Directory.Exists(outputDirectory))
{
System.IO.Directory.CreateDirectory(outputDirectory);
}
var settings = new DotNetCorePackSettings();
settings.Configuration = configuration;
settings.OutputDirectory = artifactOutput;
settings.MSBuildSettings = new DotNetCoreMSBuildSettings();
settings.MSBuildSettings.SetVersion(GetProjectVersion());
DotNetCorePack(projectFullPath, settings);
});
Task("get-version")
.Description("Get version")
.Does(() =>
{
Warning(GetProjectVersion());
});
RunTarget(target);
/*
/ HELPER METHODS
*/
private void InstallXUnitNugetPackage()
{
NuGetInstallSettings nugetInstallSettings = new NuGetInstallSettings();
nugetInstallSettings.Version = "2.4.1";
nugetInstallSettings.Verbosity = NuGetVerbosity.Normal;
nugetInstallSettings.OutputDirectory = "testrunner";
nugetInstallSettings.WorkingDirectory = ".";
NuGetInstall("xunit.runner.console", nugetInstallSettings);
}
private void RunXunitUsingMono(string targetFramework, string assemblyPath)
{
int exitCode = StartProcess("mono", new ProcessSettings {
Arguments = $"./testrunner/xunit.runner.console.2.4.1/tools/{targetFramework}/xunit.console.exe {assemblyPath}"
});
if(exitCode != 0)
{
throw new InvalidOperationException($"Exit code: {exitCode}");
}
}
private IList<TestProjMetadata> GetProjMetadata()
{
var testsRoot = MakeAbsolute(Directory("./tests/"));
var csProjs = GetFiles($"{testsRoot}/**/*.csproj").Where(fp => fp.FullPath.EndsWith("Tests.csproj")).ToList();
IList<TestProjMetadata> testProjMetadatas = new List<TestProjMetadata>();
foreach (var csProj in csProjs)
{
string csProjPath = csProj.FullPath;
string[] targetFrameworks = GetProjectTargetFrameworks(csProjPath);
string directoryPath = csProj.GetDirectory().FullPath;
string assemblyName = GetAssemblyName(csProjPath);
var testProjMetadata = new TestProjMetadata(directoryPath, csProjPath, targetFrameworks, assemblyName);
testProjMetadatas.Add(testProjMetadata);
}
return testProjMetadatas;
}
private string[] GetProjectTargetFrameworks(string csprojPath)
{
var file = MakeAbsolute(File(csprojPath));
var project = System.IO.File.ReadAllText(file.FullPath, Encoding.UTF8);
bool multipleFrameworks = project.Contains("<TargetFrameworks>");
string startElement = multipleFrameworks ? "<TargetFrameworks>" : "<TargetFramework>";
string endElement = multipleFrameworks ? "</TargetFrameworks>" : "</TargetFramework>";
int startIndex = project.IndexOf(startElement) + startElement.Length;
int endIndex = project.IndexOf(endElement, startIndex);
string targetFrameworks = project.Substring(startIndex, endIndex - startIndex);
return targetFrameworks.Split(';');
}
private string GetAssemblyName(string csprojPath)
{
var file = MakeAbsolute(File(csprojPath));
var project = System.IO.File.ReadAllText(file.FullPath, Encoding.UTF8);
bool assemblyNameElementExists = project.Contains("<AssemblyName>");
string assemblyName = string.Empty;
if(assemblyNameElementExists)
{
int startIndex = project.IndexOf("<AssemblyName>") + "<AssemblyName>".Length;
int endIndex = project.IndexOf("</AssemblyName>", startIndex);
assemblyName = project.Substring(startIndex, endIndex - startIndex);
}
else
{
int startIndex = csprojPath.LastIndexOf("/") + 1;
int endIndex = csprojPath.IndexOf(".csproj", startIndex);
assemblyName = csprojPath.Substring(startIndex, endIndex - startIndex);
}
return assemblyName;
}
private void UpdateProjectVersion(string version)
{
Information("Setting version to " + version);
if(string.IsNullOrWhiteSpace(version))
{
throw new CakeException("No version specified! You need to pass in --targetversion=\"x.y.z\"");
}
var file = MakeAbsolute(File("./src/Directory.Build.props"));
Information(file.FullPath);
var project = System.IO.File.ReadAllText(file.FullPath, Encoding.UTF8);
var projectVersion = new Regex(@"<Version>.+<\/Version>");
project = projectVersion.Replace(project, string.Concat("<Version>", version, "</Version>"));
System.IO.File.WriteAllText(file.FullPath, project, Encoding.UTF8);
}
private string GetProjectVersion()
{
var file = MakeAbsolute(File("./src/Directory.Build.props"));
Information(file.FullPath);
var project = System.IO.File.ReadAllText(file.FullPath, Encoding.UTF8);
int startIndex = project.IndexOf("<Version>") + "<Version>".Length;
int endIndex = project.IndexOf("</Version>", startIndex);
string version = project.Substring(startIndex, endIndex - startIndex);
version = $"{version}.{buildNumber}";
return version;
}
/*
/ MODELS
*/
public class TestProjMetadata
{
public TestProjMetadata(string directoryPath, string csProjPath, string[] targetFrameworks, string assemblyName)
=> (DirectoryPath, CsProjPath, TargetFrameworks, AssemblyName) = (directoryPath, csProjPath, targetFrameworks, assemblyName);
public string DirectoryPath { get; }
public string CsProjPath { get; }
public string AssemblyName { get; set; }
public string[] TargetFrameworks { get; }
}