forked from alaatm/Sejil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
253 lines (210 loc) · 7.22 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
#addin nuget:?package=AngleSharp&Version=0.16.1
#addin nuget:?package=Cake.Coverlet&Version=2.5.4
#addin nuget:?package=Cake.Git&Version=1.1.0
#addin nuget:?package=Cake.Npm&Version=1.0.0
#tool nuget:?package=ReportGenerator&Version=4.8.13
using System.Xml.Linq;
using AngleSharp.Html.Parser;
using IOFile = System.IO.File;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "release");
var rbv = Argument("rbv", ""); // On windows invoke as: ./build -t release -rbv=patch
// On linux invoke as : ./build.sh -t release --rbv=patch
var clientDir = Directory("./src/Sejil.Client");
var packPrj = Directory("./src/Sejil.Server/Sejil.csproj");
var packDir = Directory(".nupkg");
var coverageDir = Directory(".coverage");
var lcovFile = "./lcov.info";
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
Setup(context =>
{
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("ClientClean")
.Does(() =>
{
CleanDirectories(clientDir.Path.Combine("build").FullPath);
});
Task("Clean")
.Does(() =>
{
CleanDirectories("./src/**/bin/" + configuration);
CleanDirectories("./src/**/obj");
CleanDirectory(packDir);
CleanDirectory(coverageDir);
if (FileExists(lcovFile)) DeleteFile(lcovFile);
});
Task("ClientBuild")
.IsDependentOn("ClientClean")
.Does(() =>
{
if (target.ToUpper() == "COVER") { return; }
//./src/Sejil.Client/> npm install
NpmInstall(settings => settings.FromPath(clientDir));
//./src/Sejil.Client/> npm run build
NpmRunScript("build", settings => settings.FromPath(clientDir));
});
Task("CopyEmbeddedHtml")
.IsDependentOn("ClientBuild")
.Does(() =>
{
if (target.ToUpper() == "COVER") { return; }
var clientBuildDir = clientDir.Path.Combine("build");
var htmlPath = clientBuildDir.Combine("index.html").FullPath;
var html = IOFile.ReadAllText(htmlPath);
var tagValueList = new Dictionary<string, string>();
var parser = new HtmlParser();
var doc = parser.ParseDocument(html);
foreach (var css in doc.All.Where(p => p.TagName == "LINK" && p.Attributes["href"].Value.StartsWith("/static")))
{
var tag = css.OuterHtml;
var relPath = css.Attributes["href"].Value.Substring(1);
var fullPath = clientBuildDir.Combine(relPath).FullPath;
tagValueList.Add(tag, "<style>" + IOFile.ReadAllText(fullPath) + "</style>");
}
foreach (var js in doc.Scripts.Where(p => p.Source?.StartsWith("/static") ?? false))
{
var tag = js.OuterHtml;
var relPath = js.Source.Substring(1);
var fullPath = clientBuildDir.Combine(relPath).FullPath;
tagValueList.Add(tag, "<script>" + IOFile.ReadAllText(fullPath) + "</script>");
}
foreach (var kvp in tagValueList)
{
html = html.Replace(kvp.Key, kvp.Value);
}
IOFile.WriteAllText("./src/Sejil.Server/index.html", html);
});
Task("Restore")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetCoreRestore(".");
});
Task("Build")
.IsDependentOn("CopyEmbeddedHtml")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetCoreBuild(".", new DotNetCoreBuildSettings
{
NoRestore = true,
Configuration = configuration,
});
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
DotNetCoreTest(".", new DotNetCoreTestSettings
{
NoRestore = true,
NoBuild = true,
Configuration = configuration,
});
});
Task("Pack")
.IsDependentOn("Test")
.Does(() =>
{
DotNetCorePack(packPrj, new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = packDir,
NoRestore = true,
NoBuild = true,
});
});
Task("Cover")
.IsDependentOn("Build")
.Does(() =>
{
var testSettings = new DotNetCoreTestSettings
{
NoRestore = true,
NoBuild = true,
Configuration = configuration,
};
var coverletSettings = new CoverletSettings
{
CollectCoverage = true,
CoverletOutputFormat = CoverletOutputFormat.lcov | CoverletOutputFormat.opencover,
CoverletOutputDirectory = coverageDir,
CoverletOutputName = $"{DateTime.UtcNow.Ticks}",
};
DotNetCoreTest(".", testSettings, coverletSettings);
// Copy lcov file to root for code coverage highlight in vscode
var lcov = GetFiles(coverageDir.Path + "/*.info").First();
CopyFile(lcov, lcovFile);
// Generate coverage report
if (IsRunningOnWindows())
{
var opencover = GetFiles(coverageDir.Path + "/*.opencover.xml").First();
ReportGenerator(File(opencover.FullPath), coverageDir);
}
});
// This will create and push an annotated tagged commit to create a release.
// The version is determined by the passed rbv argument (major, minor or patch).
Task("Release")
.IsDependentOn("Test")
.Does(() =>
{
if (GitHasStagedChanges(".") || GitHasUncommitedChanges(".") || GitHasUntrackedFiles("."))
{
Error("Cannot release when staged changes, uncommited changes or untracked files exist.");
return;
}
var csproj = File("./src/Sejil.Server/Sejil.csproj").Path.FullPath;
var version = XDocument.Load(csproj).Root
.Element("PropertyGroup")
.Element("VersionPrefix").Value;
var split = version.Split('.');
var major = int.Parse(split[0]);
var minor = int.Parse(split[1]);
var patch = int.Parse(split[2]);
var releaseVersion = "";
switch (rbv)
{
case "major": releaseVersion = $"{major + 1}.0.0"; break;
case "minor": releaseVersion = $"{major}.{minor + 1}.0"; break;
case "patch": releaseVersion = $"{major}.{minor}.{patch + 1}"; break;
default: Error("Invalid rbv switch. Must be one of the following: major, minor, patch."); return;
}
// Using WriteAllText instead of XDocument so that format isn't screwed.
IOFile.WriteAllText(
csproj,
IOFile.ReadAllText(csproj)
.Replace(
$"<VersionPrefix>{version}</VersionPrefix>",
$"<VersionPrefix>{releaseVersion}</VersionPrefix>"));
IOFile.WriteAllText(
"./README.md",
IOFile.ReadAllText("./README.md")
.Replace(
$"dotnet add package Sejil --version {version}",
$"dotnet add package Sejil --version {releaseVersion}"));
var tag = "v" + releaseVersion;
var name = "Alaa Masoud";
var email = "[email protected]";
GitAdd(".", csproj);
GitAdd(".", "./README.md");
GitCommit(".", name, email, tag);
GitTag(".", tag, name, email, tag);
GitPush(".");
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Test");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);