forked from Dotnet-Boxed/Templates
/
build.cake
166 lines (151 loc) · 5.4 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
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
var target = Argument("Target", "Default");
var configuration =
HasArgument("Configuration") ? Argument<string>("Configuration") :
EnvironmentVariable("Configuration") is object ? EnvironmentVariable("Configuration") :
"Release";
var artefactsDirectory = Directory("./Artefacts");
var templatePackProject = Directory("./Source/*.csproj");
var isDotnetRunEnabled = BuildSystem.IsLocalBuild || (!BuildSystem.IsLocalBuild && IsRunningOnWindows());
var isDockerInstalled = GetIsDockerInstalled();
Task("Clean")
.Description("Cleans the artefacts, bin and obj directories.")
.Does(() =>
{
CleanDirectory(artefactsDirectory);
DeleteDirectories(GetDirectories("**/bin"), new DeleteDirectorySettings() { Force = true, Recursive = true });
DeleteDirectories(GetDirectories("**/obj"), new DeleteDirectorySettings() { Force = true, Recursive = true });
});
Task("Restore")
.Description("Restores NuGet packages.")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetCoreRestore();
});
Task("Build")
.Description("Builds the solution.")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetCoreBuild(
".",
new DotNetCoreBuildSettings()
{
Configuration = configuration,
NoRestore = true
});
});
Task("InstallDeveloperCertificate")
.Description("Installs a developer certificate using the dotnet dev-certs tool.")
.Does(() =>
{
if (isDotnetRunEnabled)
{
var certificateFilePath = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".pfx");
try
{
StartProcess(
"dotnet",
new ProcessArgumentBuilder()
.Append("dev-certs")
.Append("https")
.AppendSwitch("--export-path", certificateFilePath));
var certificate = new X509Certificate2(certificateFilePath);
using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
}
Information($"Dotnet developer certificate installed to local machine's root certificates.");
}
finally
{
if (System.IO.File.Exists(certificateFilePath))
{
System.IO.File.Delete(certificateFilePath);
}
}
}
else
{
Information("This CI server does not support installing certificates");
}
});
Task("Test")
.Description("Runs unit tests and outputs test results to the artefacts directory.")
.DoesForEach(GetFiles("./Tests/**/*.csproj"), project =>
{
var filters = new List<string>();
if (!isDotnetRunEnabled)
{
filters.Add("IsUsingDotnetRun=false");
}
if (!isDockerInstalled)
{
filters.Add("IsUsingDocker=false");
}
DotNetCoreTest(
project.ToString(),
new DotNetCoreTestSettings()
{
Configuration = configuration,
Filter = string.Join("&", filters),
Logger = $"trx;LogFileName={project.GetFilenameWithoutExtension()}.trx",
NoBuild = true,
NoRestore = true,
ResultsDirectory = artefactsDirectory,
ArgumentCustomization = x => x
.Append("--blame")
.AppendSwitch("--logger", $"html;LogFileName={project.GetFilenameWithoutExtension()}.html")
.Append("--collect:\"XPlat Code Coverage\""),
});
});
Task("Pack")
.Description("Creates NuGet packages and outputs them to the artefacts directory.")
.Does(() =>
{
DotNetCorePack(
GetFiles(templatePackProject).Single().ToString(),
new DotNetCorePackSettings()
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
OutputDirectory = artefactsDirectory,
});
});
Task("Default")
.Description("Cleans, restores NuGet packages, builds the solution, runs unit tests and then creates NuGet packages.")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Pack");
RunTarget(target);
public bool GetIsDockerInstalled()
{
try
{
return StartProcess("docker", new ProcessSettings { Arguments = "--version" }) == 0;
}
catch
{
Information("Docker not installed.");
return false;
}
}
public void StartProcess(string processName, ProcessArgumentBuilder builder)
{
var command = $"{processName} {builder.RenderSafe()}";
Information($"Executing: {command}");
var exitCode = StartProcess(
processName,
new ProcessSettings()
{
Arguments = builder
});
if (exitCode != 0 && !TFBuild.IsRunningOnAzurePipelinesHosted)
{
throw new Exception($"'{command}' failed with exit code {exitCode}.");
}
}