forked from alphaleonis/AlphaFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
275 lines (236 loc) · 8.82 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
262
263
264
265
266
267
268
269
270
271
using System.Xml.Linq;
#addin nuget:?package=Cake.DocFx
#addin nuget:?package=Cake.Git
#addin nuget:?package=Cake.Incubator
#l "build/appveyor-util.cake"
#l "build/common.cake"
#l "build/git-util.cake"
#tool "docfx.console"
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var ToolsDirectory = Directory("./tools/");
var ArtifactsDirectory = Directory("./artifacts");
var SolutionFile = "./AlphaFs.sln";
var DocFxFile = "./docs/docfx.json";
var DocFxArtifactsDirectory = ArtifactsDirectory.Path.Combine("docs");
var WorkDirectory = ToolsDirectory.Path.Combine("_work");
var GitHubProject = BuildSystem.AppVeyor.IsRunningOnAppVeyor ? BuildSystem.AppVeyor.Environment.Repository.Name : "alphaleonis/AlphaFS";
var DocFxBranchName = "gh-pages-lab";
var DocFxArtifactName = "artifacts/docs.zip";
var GitHubCommitName = "AppVeyor";
var GitHubCommitEMail = "[email protected]";
var AppVeyorApiBaseUrl = "https://ci.appveyor.com/api";
var TestProjectsPattern = "./tests/**/*.csproj";
var PackProjectsPattern = "./src/**/*.csproj";
var msBuildSettings = new DotNetCoreMSBuildSettings
{
//MaxCpuCount = 1
};
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(ctx =>
{
if (BuildSystem.AppVeyor.IsRunningOnAppVeyor)
{
Information("Updating build number");
string targetVersion;
if (AppVeyor.Environment.Repository.Branch == DocFxBranchName)
{
foreach (var variable in EnvironmentVariables())
{
if (variable.Key.StartsWithIgnoreCase("ALPHALEONIS"))
{
Verbose($"{variable.Key}={variable.Value}");
}
}
targetVersion = EnvironmentVariable<string>("ALPHALEONIS_DEPLOY_BUILD_VERSION", null);
}
else
{
// Update BuildNumber.
var propsFile = File("build/common.props");
string major = XmlPeek(propsFile, "//PropertyGroup/Major");
string minor = XmlPeek(propsFile, "//PropertyGroup/Minor");
string revision = XmlPeek(propsFile, "//PropertyGroup/Revision");
if (String.IsNullOrEmpty(major) ||
String.IsNullOrEmpty(minor) ||
String.IsNullOrEmpty(revision))
{
throw new Exception($"Unable to find Major, Minor and/or Build version in {propsFile}.");
}
targetVersion = $"{major}.{minor}.{revision}";
}
if (targetVersion != null)
BuildSystem.AppVeyor.UpdateBuildVersion($"{targetVersion}.{AppVeyor.Environment.Build.Number}");
}
});
Teardown(ctx =>
{
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectory(ArtifactsDirectory);
});
Task("Restore")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetCoreRestore(SolutionFile);
});
Task("Build")
.IsDependentOn("Restore")
.Does(() =>
{
var path = MakeAbsolute(new DirectoryPath(SolutionFile));
DotNetCoreBuild(path.FullPath, new DotNetCoreBuildSettings
{
Configuration = configuration,
NoRestore = true,
DiagnosticOutput = true,
MSBuildSettings = msBuildSettings,
Verbosity = DotNetCoreVerbosity.Minimal
});
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
DotNetCoreTestSettings settings = new DotNetCoreTestSettings()
{
NoBuild = true,
NoRestore = true,
Logger = "trx"
};
var projects = GetFiles(TestProjectsPattern);
foreach (var project in projects)
{
DotNetCoreTest(project.FullPath, settings);
}
})
.OnError(error =>
{
UploadTestResults();
});
void UploadTestResults()
{
if (BuildSystem.IsRunningOnAppVeyor)
{
foreach (var trx in GetFiles("./**/*.trx"))
{
PublishAppVeyorTestResult(trx, AppVeyorApiBaseUrl);
}
}
}
Task("PublishTestResults")
.IsDependentOn("Test")
.WithCriteria(() => BuildSystem.IsRunningOnAppVeyor)
.Does(() =>
{
UploadTestResults();
});
Task("Pack")
.IsDependentOn("PublishTestResults")
.Does(() =>
{
var settings = new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = ArtifactsDirectory,
MSBuildSettings = msBuildSettings,
NoBuild = true
};
var projects = GetFiles(PackProjectsPattern);
foreach(var project in projects)
{
DotNetCorePack(project.FullPath, settings);
}
});
Task("DocClean")
.Does(() =>
{
CleanDirectory(DocFxArtifactsDirectory, true);
});
Task("DocBuild")
.IsDependentOn("DocClean")
.Does(() =>
{
DocFxMetadata(DocFxFile);
DocFxBuild(DocFxFile);
});
var DocPublishTask = Task("DocPublish")
.Does(() =>
{
var artifactTempZipPath = WorkDirectory.CombineWithFilePath("docfx-artifact.zip");
var gitCloneDir = MakeAbsolute(WorkDirectory.Combine(Directory("repo")));
var gitCloneUrl = $"[email protected]:{GitHubProject}.git";
var gitCommitMessage = "Updated documentation";
var sourceBuildVersion = EnvironmentVariable("ALPHALEONIS_DEPLOY_BUILD_VERSION", "0.0.0");
EnsureDirectoryExists(WorkDirectory);
if (!BuildSystem.IsLocalBuild)
{
var jobId = EnvironmentVariable<string>("ALPHALEONIS_DEPLOY_JOB_ID", null);
if (String.IsNullOrEmpty(jobId))
throw new ArgumentException($"Missing environment variable ALPHALEONIS_DEPLOY_JOB_ID");
// Download the artifact from the build server if this is not a local build.
DownloadAppVeyorArtifact(AppVeyorApiBaseUrl, jobId, DocFxArtifactName, sourceBuildVersion, artifactTempZipPath);
}
else
{
if (!DirectoryExists(DocFxArtifactsDirectory) || !GetFiles(DocFxArtifactsDirectory.FullPath + "/**/*").Any())
throw new DirectoryNotFoundException($"{DocFxArtifactsDirectory} does not exist or is empty.");
}
DeleteDirectoryIfExists(gitCloneDir);
Information($"Cloning {gitCloneUrl} to {gitCloneDir}");
GitCommand(null, "clone", "-b", DocFxBranchName, "--single-branch", gitCloneUrl, gitCloneDir.FullPath);
var allFilesInGitRepo = GetFiles(gitCloneDir.FullPath + "/**/*").Where(f => !MakeAbsolute(gitCloneDir).GetRelativePath(f).FullPath.StartsWith(".git/")).ToArray();
if (allFilesInGitRepo.Length > 0)
{
Verbose($"Removing all files from GIT repository in {gitCloneDir}");
GitRemove(gitCloneDir, true, allFilesInGitRepo);
}
if (!BuildSystem.IsLocalBuild)
{
Unzip(artifactTempZipPath, gitCloneDir);
}
else
{
Verbose("Copy all files from local build to the local temporary git repository.");
CopyDirectory(DocFxArtifactsDirectory, gitCloneDir);
}
Verbose($"Staging all files in {gitCloneDir}");
GitAddAll(gitCloneDir);
if (GitHasUncommitedChanges(gitCloneDir))
{
Verbose("Changes detected, committing.");
var commit = GitCommit(gitCloneDir, GitHubCommitName, GitHubCommitEMail, gitCommitMessage);
Information("Pushing changes to remote.");
GitCommand(gitCloneDir, "push");
if (BuildSystem.AppVeyor.IsRunningOnAppVeyor)
AppVeyor.AddInformationalMessage($"Pushed changes from build {sourceBuildVersion}.");
}
else
{
Information("No changes made to documentation files, nothing to publish.");
if (BuildSystem.AppVeyor.IsRunningOnAppVeyor)
AppVeyor.AddInformationalMessage("No changes made to documentation files, nothing to publish.");
}
Information($"Cleaning up {gitCloneDir}");
CleanDirectory(gitCloneDir, true);
});
Task("Default")
.IsDependentOn("Pack")
.IsDependentOn("DocBuild");
// Sort of an ugly hack due to conditional dependencies not being supported.
if (BuildSystem.IsLocalBuild)
{
DocPublishTask.IsDependentOn("DocBuild");
}
RunTarget(target);