-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrind.dart
398 lines (349 loc) · 11 KB
/
grind.dart
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import 'dart:io';
import 'package:git/git.dart';
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;
import 'constants.dart';
main(args) => grind(args);
List<String> examples = [];
String homeDir = (Platform.isWindows
? Platform.environment['UserProfile']
: Platform.environment['HOME']) ??
'';
/// Every log here will be foldable in Github Actions logs.
/// See the [docs](https://github.com/actions/toolkit/blob/main/docs/citemsommands.md#group-and-ungroup-log-lines)
/// for more info.
void groupLogs(String name, Function task) {
print('::group::$name');
task.call();
print('::endgroup::');
}
@Task('Insert and update code excerpts in Markdown files')
@Depends('clean-frags')
void codeExcerpts() {}
@Task('Clean fragments (code excerpts)')
void cleanFrags() {
if (Directory(fragPath).existsSync()) {
delete(Directory(fragPath));
}
}
@Task('Create code excerpts')
void createCodeExcerpts() {
Pub.get();
// Check if tmp/_fragments exists
// It will create one if not
if (!Directory(fragPath).existsSync()) {
Directory(fragPath).createSync(recursive: true);
}
// Generate code excerpts
Pub.run(
'build_runner',
arguments: [
'build',
'--delete-conflicting-outputs',
'--config',
'excerpt',
'--output=$fragPath'
],
);
}
@Task('Update code excerpts in Markdown files')
void updateCodeExcerpts() {
// Check and create the $HOME/tmp directory.
// code_excerpt_updater uses it
if (!Directory(p.join(homeDir, 'tmp')).existsSync()) {
Directory(p.join(homeDir, 'tmp')).createSync();
}
Pub.run(
'code_excerpt_updater',
arguments: [
srcPath,
'--fragment-dir-path',
fragPath,
'--indentation',
'2',
'--write-in-place',
'tmp/code-excerpt-log.txt',
'--escape-ng-interpolation',
'--yaml',
r'--replace=/\s*\/\/!<br>//g;/ellipsis(<\w+>)?(\(\))?;?/.../g;/\/\*(\s*\.\.\.\s*)\*\//$1/g;/\{\/\*-(\s*\.\.\.\s*)-\*\/\}/$1/g;',
],
);
}
/// Use --refresh=all to delete everything and execute a clean new build;
/// --refresh=excerpts to only delete excerpts
/// --refresh=examples to delete code examples
///
/// If no arguments is appended, this program will only attempt to build
/// the site with `bundle exec jekyll build`
@Task('Build site')
// @Depends('activate-pkgs')
void build() {
TaskArgs args = context.invocation.arguments;
if (args.hasOption('refresh')) {
if (args.getOption('refresh') == 'excerpts') {
groupLogs('Clean code excerpts', cleanFrags);
groupLogs('Create code excerpts', createCodeExcerpts);
groupLogs('Update code excerpts in Markdown files', updateCodeExcerpts);
} else if (args.getOption('refresh') == 'examples') {
groupLogs('Clean built examples', deleteExamples);
getExampleList();
groupLogs('Get built examples', getBuiltExamples);
groupLogs('Copy built examples to site folder', cpBuiltExamples);
} else if (args.getOption('refresh') == 'all') {
groupLogs('Clean build artifacts and temporary directories', clean);
groupLogs('Create code excerpts', createCodeExcerpts);
groupLogs('Update code excerpts in Markdown files', updateCodeExcerpts);
getExampleList();
groupLogs('Get built examples', getBuiltExamples);
groupLogs('Copy built examples to site folder', cpBuiltExamples);
} else {
throw Exception('Can\'t find the option: ' + args.getOption('refresh')!);
}
}
// Run `bundle install`, similar to `pub get` in Dart
groupLogs('bundle install', () => run('bundle', arguments: ['install']));
// Build site using [Jekyll](https://jekyllrb.com)
groupLogs(
'bundle exec jekyll build',
() => run(
'bundle',
arguments: [
'exec',
'jekyll',
'build',
],
),
);
}
@DefaultTask()
void usage() => print('Run `grind --help` to list available tasks.');
@Task('Check and activate required global packages')
void activatePkgs() {
PubApp webdev = PubApp.global('webdev');
PubApp dartdoc = PubApp.global('dartdoc');
PubApp sass = PubApp.global('sass');
if (!webdev.isActivated) {
// webdev.activate();
run(
'dart',
arguments: [
'pub',
'global',
'activate',
'webdev',
'2.7.4',
],
);
}
if (!webdev.isGlobal) {
throw GrinderException(
'Can\'t find webdev! Did you add \"~/.pub-cache\" to your environment variables?');
}
log('webdev is activated');
if (!dartdoc.isActivated) {
dartdoc.activate();
}
if (!dartdoc.isGlobal) {
throw GrinderException(
'Can\'t find dartdoc! Did you add \"~/.pub-cache\" to your environment variables?');
}
log('dartdoc is activated');
if (!sass.isActivated) {
dartdoc.activate();
}
if (!sass.isGlobal) {
throw GrinderException(
'Can\'t find sass! Did you add \"~/.pub-cache\" to your environment variables?');
}
log('sass is activated');
}
//----------------------------Example Repos Related----------------------------//
@Task('Get the list of examples')
void getExampleList() {
if (examples.isEmpty) {
Directory('examples/acx').listSync().forEach((element) {
if (element is Directory) {
examples.add(p.basename(element.path));
}
});
Directory('examples/ng/doc').listSync().forEach((element) {
if (element is Directory) {
// All examples don't contain "_" symbol in their names
if (!p.basename(element.path).contains('_')) {
examples.add(p.basename(element.path));
}
}
});
examples.sort();
}
}
/// Every example has a corresponding live example.
/// We always upload the latest build to a Github repo,
/// so that we don't have to build an example for it to show
/// up on the site everytime
@Task('Get built examples')
@Depends('get-example-list')
void getBuiltExamples() async {
if (!builtExamplesDir.existsSync()) {
builtExamplesDir.createSync(recursive: true);
}
// We only need gh-pages branch
void pullRepo(String name) => run(
'git',
arguments: [
'clone',
'https://github.com/angulardart-community/$name',
'--branch',
'gh-pages',
'--single-branch',
name,
'--depth',
'1',
],
workingDirectory: builtExamplesDir.path,
quiet: true,
);
for (String example in examples) {
log('Fetching example $example');
pullRepo(example);
}
}
@Task('Copy built examples to the site folder')
void cpBuiltExamples() {
builtExamplesDir.listSync().forEach((example) {
String exampleName = p.basename(example.path);
if (example is Directory) {
copy(Directory(p.join(example.path, angularVersion.toString())),
Directory('publish/examples/$exampleName'));
// Modify <base href=...> to point to the correct directory
// The example "lottery" needs to be special treated
if (exampleName != 'lottery') {
String href = p.join('/examples', exampleName);
File index = File('publish/examples/$exampleName/index.html');
index.writeAsStringSync(index.readAsStringSync().replaceFirst(
'<base href=\"/$exampleName/$angularVersion/\">',
'<base href=\"$href/\">'));
}
}
});
}
@Task('Delete built examples')
void deleteExamples() {
if (builtExamplesDir.existsSync()) {
delete(builtExamplesDir);
}
}
//----------------------------Utility----------------------------//
/// By default this cleans every temporary directory and build artifacts
/// If you don't want to delete something, pass that thing with a "no-"
/// flag.
/// For example, if you **DON'T** want to delete the "publish" folder,
/// run `grind clean --no-publish`. It will delete everything else.
@Task('Clean temporary directories and build artifacts')
void clean() {
TaskArgs args = context.invocation.arguments;
// Cleans the "publish" directory
bool cleanSite = args.hasFlag('publish') ? args.getFlag('publish') : true;
if (cleanSite && Directory('publish').existsSync()) {
delete(Directory('publish'));
}
// Cleans the "$HOME/tmp" directory, used by some git stuffs
bool cleanTmp = args.hasFlag('tmp') ? args.getFlag('tmp') : true;
if (cleanTmp) {
String path = p.join(
// Please don't tell me you're on Android or iOS
homeDir,
'tmp',
);
if (Directory(path).existsSync()) {
delete(Directory(path));
}
}
// Cleans the "src./asset-cache" directory
bool cleanAssetCache =
args.hasFlag('asset-cache') ? args.getFlag('asset-cache') : true;
if (cleanAssetCache && Directory('src/.asset-cache').existsSync()) {
delete(Directory('src/.asset-cache'));
}
bool cleanCodeFrags =
args.hasFlag('code-frags') ? args.getFlag('code-frags') : true;
if (cleanCodeFrags) {
cleanFrags();
}
bool cleanExamples =
args.hasFlag('examples') ? args.getFlag('examples') : true;
if (cleanExamples) {
deleteExamples();
}
if (cleanCodeFrags && cleanExamples) {
delete(Directory('tmp'));
}
}
@Task('Clean temporary directories created when syncing examples to GitHub')
void deleteSync() {
delete(Directory('tmp/sync'));
}
@Task('Sync changes made on the local example to the GitHub examples repo')
@Depends('get-example-list', 'delete-sync')
void syncExamples() async {
final syncDir = Directory('tmp/sync')..createSync(recursive: true);
final commitMsg = 'Auto-commit: update to Angular Version 6';
print(examples);
for (String example in examples) {
if (!example.contains('lottery')) {
final exampleDir = Directory('tmp/sync/$example');
log('Cloning example $example');
await runGit(
[
'clone',
'https://github.com/angulardart-community/$example',
'--depth',
'1',
'--branch',
'master',
'--single-branch',
'$example',
],
throwOnError: true,
processWorkingDir: syncDir.path,
);
copy(Directory('examples/ng/doc/$example'), exampleDir);
log('Saving changes...');
await runGit(['add', '-u'], processWorkingDir: exampleDir.path);
log('Commit changes...');
await runGit([
'commit',
'-s',
'-m',
'"$commitMsg"',
], processWorkingDir: exampleDir.path);
log('Push to remote...');
await runGit(['push'], processWorkingDir: exampleDir.path);
log('Done!\n');
}
}
log('Cleaning up...');
for (String example in examples) {
delete(Directory('tmp/sync/$example'));
}
}
@Task('A one-time command for executing repetitive tasks for each example')
@Depends('get-example-list')
void forEachExample() {
for (var example in examples) {
// Do something for each example
// The following updates the default branch for each repo using GitHub API
// final uri = Uri.https(
// 'api.github.com',
// 'repos/angulardart-community/$example',
// );
// await http.patch(
// uri,
// headers: {
// 'Authorization': 'token <my token>',
// 'Accept': 'application/vnd.github.v3+json',
// },
// body: '{"default_branch": "master"}',
// );
}
}