-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub-pages-tasks.cake
223 lines (169 loc) · 8.49 KB
/
github-pages-tasks.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
#l "github-pages-variables.cake"
#addin "nuget:?package=Cake.Git&version=5.0.1"
//-------------------------------------------------------------
public class GitHubPagesProcessor : ProcessorBase
{
public GitHubPagesProcessor(BuildContext buildContext)
: base(buildContext)
{
}
public override bool HasItems()
{
return BuildContext.GitHubPages.Items.Count > 0;
}
private string GetGitHubPagesRepositoryUrl(string projectName)
{
// Allow per project overrides via "GitHubPagesRepositoryUrlFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "GitHubPagesRepositoryUrlFor", BuildContext.GitHubPages.RepositoryUrl);
}
private string GetGitHubPagesBranchName(string projectName)
{
// Allow per project overrides via "GitHubPagesBranchNameFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "GitHubPagesBranchNameFor", BuildContext.GitHubPages.BranchName);
}
private string GetGitHubPagesEmail(string projectName)
{
// Allow per project overrides via "GitHubPagesEmailFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "GitHubPagesEmailFor", BuildContext.GitHubPages.Email);
}
private string GetGitHubPagesUserName(string projectName)
{
// Allow per project overrides via "GitHubPagesUserNameFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "GitHubPagesUserNameFor", BuildContext.GitHubPages.UserName);
}
private string GetGitHubPagesApiToken(string projectName)
{
// Allow per project overrides via "GitHubPagesApiTokenFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "GitHubPagesApiTokenFor", BuildContext.GitHubPages.ApiToken);
}
public override async Task PrepareAsync()
{
if (!HasItems())
{
return;
}
// Check whether projects should be processed, `.ToList()`
// is required to prevent issues with foreach
foreach (var gitHubPage in BuildContext.GitHubPages.Items.ToList())
{
if (!ShouldProcessProject(BuildContext, gitHubPage))
{
BuildContext.GitHubPages.Items.Remove(gitHubPage);
}
}
}
public override async Task UpdateInfoAsync()
{
if (!HasItems())
{
return;
}
foreach (var gitHubPage in BuildContext.GitHubPages.Items)
{
CakeContext.Information("Updating version for GitHub page '{0}'", gitHubPage);
var projectFileName = GetProjectFileName(BuildContext, gitHubPage);
CakeContext.TransformConfig(projectFileName, new TransformationCollection
{
{ "Project/PropertyGroup/PackageVersion", BuildContext.General.Version.NuGet }
});
}
}
public override async Task BuildAsync()
{
if (!HasItems())
{
return;
}
foreach (var gitHubPage in BuildContext.GitHubPages.Items)
{
BuildContext.CakeContext.LogSeparator("Building GitHub page '{0}'", gitHubPage);
var projectFileName = GetProjectFileName(BuildContext, gitHubPage);
var msBuildSettings = new MSBuildSettings {
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
ToolVersion = MSBuildToolVersion.Default,
Configuration = BuildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
ConfigureMsBuild(BuildContext, msBuildSettings, gitHubPage, "build");
// Always disable SourceLink
msBuildSettings.WithProperty("EnableSourceLink", "false");
RunMsBuild(BuildContext, gitHubPage, projectFileName, msBuildSettings, "build");
}
}
public override async Task PackageAsync()
{
if (!HasItems())
{
return;
}
foreach (var gitHubPage in BuildContext.GitHubPages.Items)
{
if (!ShouldPackageProject(BuildContext, gitHubPage))
{
CakeContext.Information("GitHub page '{0}' should not be packaged", gitHubPage);
continue;
}
BuildContext.CakeContext.LogSeparator("Packaging GitHub pages '{0}'", gitHubPage);
var projectFileName = GetProjectFileName(BuildContext, gitHubPage);
var outputDirectory = GetProjectOutputDirectory(BuildContext, gitHubPage);
CakeContext.Information("Output directory: '{0}'", outputDirectory);
CakeContext.Information("1) Using 'dotnet publish' to package '{0}'", gitHubPage);
var msBuildSettings = new DotNetMSBuildSettings();
ConfigureMsBuildForDotNet(BuildContext, msBuildSettings, gitHubPage, "pack");
msBuildSettings.WithProperty("ConfigurationName", BuildContext.General.Solution.ConfigurationName);
msBuildSettings.WithProperty("PackageVersion", BuildContext.General.Version.NuGet);
var publishSettings = new DotNetPublishSettings
{
MSBuildSettings = msBuildSettings,
OutputDirectory = outputDirectory,
Configuration = BuildContext.General.Solution.ConfigurationName
};
CakeContext.DotNetPublish(projectFileName, publishSettings);
}
}
public override async Task DeployAsync()
{
if (!HasItems())
{
return;
}
foreach (var gitHubPage in BuildContext.GitHubPages.Items)
{
if (!ShouldDeployProject(BuildContext, gitHubPage))
{
CakeContext.Information("GitHub page '{0}' should not be deployed", gitHubPage);
continue;
}
BuildContext.CakeContext.LogSeparator("Deploying GitHub page '{0}'", gitHubPage);
CakeContext.Warning("Only Blazor apps are supported as GitHub pages");
var temporaryDirectory = GetTempDirectory(BuildContext, "gh-pages", gitHubPage);
CakeContext.CleanDirectory(temporaryDirectory);
var repositoryUrl = GetGitHubPagesRepositoryUrl(gitHubPage);
var branchName = GetGitHubPagesBranchName(gitHubPage);
var email = GetGitHubPagesEmail(gitHubPage);
var userName = GetGitHubPagesUserName(gitHubPage);
var apiToken = GetGitHubPagesApiToken(gitHubPage);
CakeContext.Information("1) Cloning repository '{0}' using branch name '{1}'", repositoryUrl, branchName);
CakeContext.GitClone(repositoryUrl, temporaryDirectory, userName, apiToken, new GitCloneSettings
{
BranchName = branchName
});
CakeContext.Information("2) Updating the GitHub pages branch with latest source");
// Special directory we need to distribute (e.g. output\Release\Blazorc.PatternFly.Example\Blazorc.PatternFly.Example\dist)
var sourceDirectory = string.Format("{0}/{1}/wwwroot", BuildContext.General.OutputRootDirectory, gitHubPage);
var sourcePattern = string.Format("{0}/**/*", sourceDirectory);
CakeContext.Debug("Copying all files from '{0}' => '{1}'", sourcePattern, temporaryDirectory);
CakeContext.CopyFiles(sourcePattern, temporaryDirectory, true);
CakeContext.Information("3) Committing latest GitHub pages");
CakeContext.GitAddAll(temporaryDirectory);
CakeContext.GitCommit(temporaryDirectory, "Build server", email, string.Format("Auto-update GitHub pages: '{0}'", BuildContext.General.Version.NuGet));
CakeContext.Information("4) Pushing code back to repository '{0}'", repositoryUrl);
CakeContext.GitPush(temporaryDirectory, userName, apiToken);
await BuildContext.Notifications.NotifyAsync(gitHubPage, string.Format("Deployed to GitHub pages"), TargetType.GitHubPages);
}
}
public override async Task FinalizeAsync()
{
}
}