forked from daveaglick/discoverdotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
286 lines (247 loc) · 10.7 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// The following environment variables need to be set:
// DISCOVERDOTNET_GITHUB_TOKEN
// DISCOVERDOTNET_NETLIFY_TOKEN
// DISCOVERDOTNET_MEETUP_TOKEN
// DISCOVERDOTNET_ALGOLIA_TOKEN
#tool nuget:?package=Wyam&version=1.4.1
#addin nuget:?package=Cake.Wyam&version=1.4.1
#addin nuget:?package=NetlifySharp&version=0.1.0
#addin nuget:?package=System.Runtime.Serialization.Formatters&version=4.3.0
#addin nuget:?package=Algolia.Search&version=5.0.0
using System.Reflection;
using NetlifySharp;
using Algolia.Search;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var isLocal = BuildSystem.IsLocalBuild;
var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var buildNumber = AppVeyor.Environment.Build.Number;
var outputDir = Directory("./output");
var issuesDir = Directory("./output.issues");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information("Building Discover .NET");
// See https://github.com/cake-build/cake/issues/2116
AppDomain.CurrentDomain.AssemblyResolve += (_, eventArgs) =>
{
AssemblyName name = new AssemblyName(eventArgs.Name);
Verbose($"Resolving assembly {eventArgs.Name}");
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(x => !x.IsDynamic && x.GetName().Name == name.Name)
?? Assembly.Load(name.Name);
if(assembly != null)
{
Verbose($"Resolved by assembly {assembly.FullName}");
}
else
{
Verbose($"Assembly not resolved");
}
return assembly;
};
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(new DirectoryPath[] { outputDir, issuesDir });
});
Task("Build")
.Description("Generates the site.")
.IsDependentOn("Clean")
.Does(() =>
{
var gitHubToken = EnvironmentVariable("DISCOVERDOTNET_GITHUB_TOKEN");
var meetupToken = EnvironmentVariable("DISCOVERDOTNET_MEETUP_TOKEN");
Wyam(new WyamSettings
{
//UpdatePackages = true,
ConfigurationFile = File("./config.wyam"),
OutputPath = outputDir,
Settings = new Dictionary<string, object>
{
{ "GitHubToken", gitHubToken },
{ "MeetupToken", meetupToken }
}
});
//StartProcess("../Wyam/src/clients/Wyam/bin/Debug/net462/wyam.exe",
// "-a \"../Wyam/tests/integration/Wyam.Examples.Tests/bin/Debug/net462/**/*.dll\""
// + $" --config \"config.wyam\""
// + $" --output \"{MakeAbsolute(outputDir).ToString()}\""
// + $" --setting GitHubToken=\"{gitHubToken}\""
// + $" --setting MeetupToken=\"{meetupToken}\"");
});
Task("Issues")
.Description("Generates the issue data for GitHub projects.")
.IsDependentOn("Clean")
.Does(() =>
{
var gitHubToken = EnvironmentVariable("DISCOVERDOTNET_GITHUB_TOKEN");
Wyam(new WyamSettings
{
//UpdatePackages = true,
ConfigurationFile = File("./issues.wyam"),
OutputPath = issuesDir,
Settings = new Dictionary<string, object>
{
{ "GitHubToken", gitHubToken },
{ "Preview", string.Equals(target, "Preview", StringComparison.OrdinalIgnoreCase ) }
}
});
// StartProcess("../Wyam/src/clients/Wyam/bin/Debug/net462/wyam.exe",
// "-a \"../Wyam/tests/integration/Wyam.Examples.Tests/bin/Debug/net462/**/*.dll\""
// + $" --config \"issues.wyam\""
// + $" --output \"{MakeAbsolute(issuesDir).ToString()}\""
// + $" --setting GitHubToken=\"{gitHubToken}\""
// + $" --setting Preview=\"{ string.Equals(target, "Preview", StringComparison.OrdinalIgnoreCase) }\"");
});
Task("Preview")
.Description("Generates and previews the site.")
.IsDependentOn("Issues")
.Does(() =>
{
// Copy the issue data
var issuesDataDir = outputDir + Directory("data/issues");
EnsureDirectoryExists(issuesDataDir);
CopyFiles("output.issues/**/*", issuesDataDir, true);
// Generate the main site, but don't clean it out
var gitHubToken = EnvironmentVariable("DISCOVERDOTNET_GITHUB_TOKEN");
var meetupToken = EnvironmentVariable("DISCOVERDOTNET_MEETUP_TOKEN");
Wyam(new WyamSettings
{
//UpdatePackages = true,
ConfigurationFile = File("./config.wyam"),
OutputPath = outputDir,
NoClean = true,
Preview = true,
Watch = true,
Settings = new Dictionary<string, object>
{
{ "GitHubToken", gitHubToken },
{ "MeetupToken", meetupToken },
{ "Preview", true }
},
ContentTypes = new Dictionary<string, string>
{
{ ".vue", "text/plain" }
}
});
// StartProcess("../Wyam/src/clients/Wyam/bin/Debug/net462/wyam.exe",
// "-a \"../Wyam/tests/integration/Wyam.Examples.Tests/bin/Debug/net462/**/*.dll\""
// + $" --config \"config.wyam\""
// + $" --output \"{MakeAbsolute(outputDir).ToString()}\""
// + " --noclean -p -w"
// + $" --setting GitHubToken=\"{gitHubToken}\""
// + $" --setting MeetupToken=\"{meetupToken}\""
// + $" --setting Preview=\"true\""
// + $" --content-type .vue=text/plain");
});
Task("Publish-Issues")
.Description("Generates and deploys the issue data.")
.IsDependentOn("Issues")
.Does(() =>
{
Information("Deploying issue data to Netlify");
var netlifyToken = EnvironmentVariable("DISCOVERDOTNET_NETLIFY_TOKEN");
var client = new NetlifyClient(netlifyToken);
client.UpdateSite("discoverdotnet-issues.netlify.com", MakeAbsolute(issuesDir).FullPath).SendAsync().Wait();
Information("Updating search indexes");
var algoliaToken = EnvironmentVariable("DISCOVERDOTNET_ALGOLIA_TOKEN");
var algoliaClient = new AlgoliaClient("7TKEQH0O12", algoliaToken);
UpdateSearchIndex(algoliaClient, "issues", System.IO.Path.Combine(MakeAbsolute(issuesDir).FullPath, "search.json"));
});
Task("Publish-Site")
.Description("Generates and deploys the site.")
.IsDependentOn("Build")
.Does(() =>
{
Information("Deploying site to Netlify");
var netlifyToken = EnvironmentVariable("DISCOVERDOTNET_NETLIFY_TOKEN");
var client = new NetlifyClient(netlifyToken);
client.UpdateSite("discoverdotnet.netlify.com", MakeAbsolute(outputDir).FullPath).SendAsync().Wait();
Information("Updating search indexes");
var algoliaToken = EnvironmentVariable("DISCOVERDOTNET_ALGOLIA_TOKEN");
var algoliaClient = new AlgoliaClient("7TKEQH0O12", algoliaToken);
string indexPath = System.IO.Path.Combine(MakeAbsolute(outputDir).FullPath, "data", "search");
UpdateSearchIndex(algoliaClient, "projects", System.IO.Path.Combine(indexPath, "projects.json"));
UpdateSearchIndex(algoliaClient, "blogs", System.IO.Path.Combine(indexPath, "blogs.json"));
UpdateSearchIndex(algoliaClient, "posts", System.IO.Path.Combine(indexPath, "posts.json"));
UpdateSearchIndex(algoliaClient, "broadcasts", System.IO.Path.Combine(indexPath, "broadcasts.json"));
UpdateSearchIndex(algoliaClient, "episodes", System.IO.Path.Combine(indexPath, "episodes.json"));
UpdateSearchIndex(algoliaClient, "resources", System.IO.Path.Combine(indexPath, "resources.json"));
});
//////////////////////////////////////////////////////////////////////
// HELPERS
//////////////////////////////////////////////////////////////////////
public class SearchIndexItemEqualityComparer : IEqualityComparer<JObject>
{
public bool Equals(JObject a, JObject b)
{
return a.Properties().Where(x => x.Name != "objectID").Select(x => (x.Name, x.Value.ToString())).OrderBy(x => x.Name)
.SequenceEqual(b.Properties().Where(x => x.Name != "objectID").Select(x => (x.Name, x.Value.ToString())).OrderBy(x => x.Name));
}
public int GetHashCode(JObject obj)
{
int hash = 27;
foreach (var prop in obj.Properties().Where(x => x.Name != "objectID").Select(x => (x.Name, x.Value.ToString())).OrderBy(x => x.Name))
{
hash = (13 * hash) + prop.Name.GetHashCode();
hash = (13 * hash) + prop.Item2.GetHashCode();
}
return hash;
}
}
public void UpdateSearchIndex(AlgoliaClient client, string indexName, string path)
{
// Read from Algolia
Index index = client.InitIndex(indexName);
Query query = new Query("");
Index.IndexIterator results = index.BrowseAll(query);
HashSet<JObject> existing = new HashSet<JObject>(new SearchIndexItemEqualityComparer());
foreach (JObject result in results)
{
existing.Add(result);
}
// Read from the file
List<JObject> adds = new List<JObject>();
JArray file = JArray.Parse(System.IO.File.ReadAllText(path));
foreach(JObject item in file)
{
// Is it already in the index?
if(!existing.Remove(item))
{
// It wasn't matched, so this is a new one
adds.Add(item);
}
}
Information($"Search index \"{indexName}\": deleting {existing.Count}, adding {adds.Count}");
// Delete
index.DeleteObjects(existing.Select(x => x.Property("objectID").Value.ToString()));
// Add
index.AddObjects(adds);
}
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Preview");
Task("Publish")
.IsDependentOn("Publish-Issues")
.IsDependentOn("Publish-Site");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);