forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrind.dart
1182 lines (1056 loc) · 39.3 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:io' hide ProcessException;
import 'package:dartdoc/src/io_utils.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:grinder/grinder.dart';
import 'package:io/io.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart' as yaml;
import '../test/src/utils.dart';
void main([List<String> args]) => grind(args);
/// Thrown on failure to find something in a file.
class GrindTestFailure {
final String message;
GrindTestFailure(this.message);
}
/// Kind of an inefficient grepper for now.
void expectFileContains(String path, List<Pattern> items) {
var source = File(path);
if (!source.existsSync()) {
throw GrindTestFailure('file not found: ${path}');
}
for (var item in items) {
if (!File(path).readAsStringSync().contains(item)) {
throw GrindTestFailure('Can not find ${item} in ${path}');
}
}
}
/// The pub cache inherited by grinder.
final String defaultPubCache =
Platform.environment['PUB_CACHE'] ?? resolveTildePath('~/.pub-cache');
/// Run no more than the number of processors available in parallel.
final MultiFutureTracker testFutures =
MultiFutureTracker(Platform.numberOfProcessors);
// Directory.systemTemp is not a constant. So wrap it.
Directory createTempSync(String prefix) =>
Directory.systemTemp.createTempSync(prefix);
/// Global so that the lock is retained for the life of the process.
Future<void> _lockFuture;
Completer<FlutterRepo> _cleanFlutterRepo;
/// Returns true if we need to replace the existing flutter. We never release
/// this lock until the program exits to prevent edge case runs from
/// spontaneously deciding to download a new Flutter SDK in the middle of a run.
Future<FlutterRepo> get cleanFlutterRepo async {
if (_cleanFlutterRepo == null) {
// No await is allowed between check of _cleanFlutterRepo and its assignment,
// to prevent reentering this function.
_cleanFlutterRepo = Completer();
// Figure out where the repository is supposed to be and lock updates for
// it.
await cleanFlutterDir.parent.create(recursive: true);
assert(_lockFuture == null);
_lockFuture = File(path.join(cleanFlutterDir.parent.path, 'lock'))
.openSync(mode: FileMode.write)
.lock();
await _lockFuture;
var lastSynced = File(path.join(cleanFlutterDir.parent.path, 'lastSynced'));
var newRepo = FlutterRepo.fromPath(cleanFlutterDir.path, {}, 'clean');
// We have a repository, but is it up to date?
DateTime lastSyncedTime;
if (lastSynced.existsSync()) {
lastSyncedTime = DateTime.fromMillisecondsSinceEpoch(
int.parse(lastSynced.readAsStringSync()));
}
if (lastSyncedTime == null ||
DateTime.now().difference(lastSyncedTime) > Duration(hours: 4)) {
// Rebuild the repository.
if (cleanFlutterDir.existsSync()) {
cleanFlutterDir.deleteSync(recursive: true);
}
cleanFlutterDir.createSync(recursive: true);
await newRepo._init();
await lastSynced
.writeAsString((DateTime.now()).millisecondsSinceEpoch.toString());
}
_cleanFlutterRepo.complete(newRepo);
}
return _cleanFlutterRepo.future;
}
Directory _dartdocDocsDir;
Directory get dartdocDocsDir => _dartdocDocsDir ??= createTempSync('dartdoc');
Directory _dartdocDocsDirRemote;
Directory get dartdocDocsDirRemote =>
_dartdocDocsDirRemote ??= createTempSync('dartdoc_remote');
Directory _sdkDocsDir;
Directory get sdkDocsDir => _sdkDocsDir ??= createTempSync('sdkdocs');
Directory cleanFlutterDir = Directory(
path.join(resolveTildePath('~/.dartdoc_grinder'), 'cleanFlutter'));
Directory _flutterDir;
Directory get flutterDir => _flutterDir ??= createTempSync('flutter');
Directory get testPackage =>
Directory(path.joinAll(['testing', 'test_package']));
Directory get testPackageExperiments =>
Directory(path.joinAll(['testing', 'test_package_experiments']));
Directory get pluginPackage =>
Directory(path.joinAll(['testing', 'test_package_flutter_plugin']));
Directory _testPackageDocsDir;
Directory get testPackageDocsDir =>
_testPackageDocsDir ??= createTempSync('test_package');
Directory _testPackageExperimentsDocsDir;
Directory get testPackageExperimentsDocsDir =>
_testPackageExperimentsDocsDir ??=
createTempSync('test_package_experiments');
Directory _pluginPackageDocsDir;
Directory get pluginPackageDocsDir =>
_pluginPackageDocsDir ??= createTempSync('test_package_flutter_plugin');
/// Version of dartdoc we should use when making comparisons.
String get dartdocOriginalBranch {
var branch = 'master';
if (Platform.environment.containsKey('DARTDOC_ORIGINAL')) {
branch = Platform.environment['DARTDOC_ORIGINAL'];
log('using branch/tag: $branch for comparison from \$DARTDOC_ORIGINAL');
}
return branch;
}
List<String> _extraDartdocParams;
/// If DARTDOC_PARAMS is set, add given parameters to the list.
List<String> get extraDartdocParameters {
if (_extraDartdocParams == null) {
var whitespace = RegExp(r'\s+');
_extraDartdocParams = [];
if (Platform.environment.containsKey('DARTDOC_PARAMS')) {
_extraDartdocParams
.addAll(Platform.environment['DARTDOC_PARAMS'].split(whitespace));
}
}
return _extraDartdocParams;
}
final Directory flutterDirDevTools =
Directory(path.join(flutterDir.path, 'dev', 'tools'));
/// Creates a throwaway pub cache and returns the environment variables
/// necessary to use it.
Map<String, String> _createThrowawayPubCache() {
var pubCache = Directory.systemTemp.createTempSync('pubcache');
var pubCacheBin = Directory(path.join(pubCache.path, 'bin'));
pubCacheBin.createSync();
return Map.fromIterables([
'PUB_CACHE',
'PATH'
], [
pubCache.path,
[pubCacheBin.path, Platform.environment['PATH']].join(':')
]);
}
// TODO(jcollins-g): make a library out of this
final FilePath _pkgDir = FilePath('lib/src/third_party/pkg');
final FilePath _mustache4dartDir =
FilePath('lib/src/third_party/pkg/mustache4dart');
final RegExp _mustache4dartPatches =
RegExp(r'^\d\d\d-mustache4dart-.*[.]patch$');
@Task('Update third_party forks')
void updateThirdParty() async {
run('rm', arguments: ['-rf', _mustache4dartDir.path]);
Directory(_pkgDir.path).createSync(recursive: true);
run('git', arguments: [
'clone',
'--branch',
'v2.1.2',
'--depth=1',
'[email protected]:valotas/mustache4dart',
_mustache4dartDir.path,
]);
run('rm', arguments: ['-rf', path.join(_mustache4dartDir.path, '.git')]);
for (var patchFileName in Directory(_pkgDir.path)
.listSync()
.map((e) => path.basename(e.path))
.where((String filename) => _mustache4dartPatches.hasMatch(filename))
.toList()
..sort()) {
run('patch',
arguments: [
'-p0',
'-i',
patchFileName,
],
workingDirectory: _pkgDir.path);
}
}
@Task('Analyze dartdoc to ensure there are no errors and warnings')
void analyze() async {
await SubprocessLauncher('analyze').runStreamed(
sdkBin('dartanalyzer'),
[
'--fatal-infos',
'--options',
'analysis_options_presubmit.yaml',
'bin',
'lib',
'test',
'tool',
],
);
}
@Task('Check for dartfmt cleanliness')
void dartfmt() async {
if (Platform.version.contains('dev')) {
var filesToFix = <String>[];
// Filter out test packages as they always have strange formatting.
// Passing parameters to dartfmt for directories to search results in
// filenames being stripped of the dirname so we have to filter here.
void addFileToFix(String fileName) {
var pathComponents = path.split(fileName);
if (pathComponents.isNotEmpty && pathComponents.first == 'testing') {
return;
}
filesToFix.add(fileName);
}
log('Validating dartfmt with version ${Platform.version}');
await SubprocessLauncher('dartfmt').runStreamed(
sdkBin('dartfmt'),
[
'-n',
'.',
],
perLine: addFileToFix);
if (filesToFix.isNotEmpty) {
fail(
'dartfmt found files needing reformatting. Use this command to reformat:\n'
'dartfmt -w ${filesToFix.map((f) => "\'$f\'").join(' ')}');
}
} else {
log('Skipping dartfmt check, requires latest dev version of SDK');
}
}
@Task('Run quick presubmit checks.')
@Depends(
analyze,
checkBuild,
smokeTest,
dartfmt,
tryPublish,
)
void presubmit() => null;
@Task('Run long tests, self-test dartdoc, and run the publish test')
@Depends(presubmit, test, testDartdoc)
void buildbot() => null;
@Task('Generate docs for the Dart SDK')
Future buildSdkDocs() async {
log('building SDK docs');
await _buildSdkDocs(sdkDocsDir.path, Future.value(Directory.current.path));
}
class WarningsCollection {
final String tempDir;
final Map<String, int> warningKeyCounts;
final String branch;
final String pubCachePath;
WarningsCollection(this.tempDir, this.pubCachePath, this.branch)
: warningKeyCounts = {};
static const String kPubCachePathReplacement = '_xxxPubDirectoryxxx_';
static const String kTempDirReplacement = '_xxxTempDirectoryxxx_';
String _toKey(String text) {
var key = text.replaceAll(tempDir, kTempDirReplacement);
if (pubCachePath != null) {
key = key.replaceAll(pubCachePath, kPubCachePathReplacement);
}
return key;
}
String _fromKey(String text) {
var key = text.replaceAll(kTempDirReplacement, tempDir);
if (pubCachePath != null) {
key = key.replaceAll(kPubCachePathReplacement, pubCachePath);
}
return key;
}
void add(String text) {
var key = _toKey(text);
warningKeyCounts.putIfAbsent(key, () => 0);
warningKeyCounts[key]++;
}
/// Output formatter for comparing warnings. [this] is the original.
String getPrintableWarningDelta(String title, WarningsCollection current) {
var printBuffer = StringBuffer();
var quantityChangedOuts = <String>{};
var onlyOriginal = <String>{};
var onlyCurrent = <String>{};
var identical = <String>{};
var allKeys = <String>{
...warningKeyCounts.keys,
...current.warningKeyCounts.keys
};
for (var key in allKeys) {
if (warningKeyCounts.containsKey(key) &&
!current.warningKeyCounts.containsKey(key)) {
onlyOriginal.add(key);
} else if (!warningKeyCounts.containsKey(key) &&
current.warningKeyCounts.containsKey(key)) {
onlyCurrent.add(key);
} else if (warningKeyCounts.containsKey(key) &&
current.warningKeyCounts.containsKey(key) &&
warningKeyCounts[key] != current.warningKeyCounts[key]) {
quantityChangedOuts.add(key);
} else {
identical.add(key);
}
}
if (onlyOriginal.isNotEmpty) {
printBuffer.writeln(
'*** $title : ${onlyOriginal.length} warnings from $branch, missing in ${current.branch}:');
onlyOriginal.forEach((key) => printBuffer.writeln(_fromKey(key)));
}
if (onlyCurrent.isNotEmpty) {
printBuffer.writeln(
'*** $title : ${onlyCurrent.length} new warnings in ${current.branch}, missing in $branch');
onlyCurrent.forEach((key) => printBuffer.writeln(current._fromKey(key)));
}
if (quantityChangedOuts.isNotEmpty) {
printBuffer.writeln('*** $title : Identical warning quantity changed');
for (var key in quantityChangedOuts) {
printBuffer.writeln(
'* Appeared ${warningKeyCounts[key]} times in $branch, ${current.warningKeyCounts[key]} in ${current.branch}:');
printBuffer.writeln(current._fromKey(key));
}
}
if (onlyOriginal.isEmpty &&
onlyCurrent.isEmpty &&
quantityChangedOuts.isEmpty) {
printBuffer.writeln(
'*** $title : No difference in warning output from $branch to ${current.branch}${allKeys.isEmpty ? "" : " (${allKeys.length} warnings found)"}');
} else if (identical.isNotEmpty) {
printBuffer.writeln(
'*** $title : Difference in warning output found for ${allKeys.length - identical.length} warnings (${allKeys.length} warnings found)"');
}
return printBuffer.toString();
}
}
/// Returns a map of warning texts to the number of times each has been seen.
WarningsCollection jsonMessageIterableToWarnings(Iterable<Map> messageIterable,
String tempPath, String pubDir, String branch) {
var warningTexts = WarningsCollection(tempPath, pubDir, branch);
if (messageIterable == null) return warningTexts;
for (Map<String, dynamic> message in messageIterable) {
if (message.containsKey('level') &&
message['level'] == 'WARNING' &&
message.containsKey('data')) {
warningTexts.add(message['data']['text']);
}
}
return warningTexts;
}
@Task('Display delta in SDK warnings')
Future compareSdkWarnings() async {
var originalDartdocSdkDocs =
Directory.systemTemp.createTempSync('dartdoc-comparison-sdkdocs');
Future originalDartdoc = createComparisonDartdoc();
Future currentDartdocSdkBuild = _buildSdkDocs(
sdkDocsDir.path, Future.value(Directory.current.path), 'current');
Future originalDartdocSdkBuild =
_buildSdkDocs(originalDartdocSdkDocs.path, originalDartdoc, 'original');
var currentDartdocWarnings = jsonMessageIterableToWarnings(
await currentDartdocSdkBuild, sdkDocsDir.absolute.path, null, 'HEAD');
var originalDartdocWarnings = jsonMessageIterableToWarnings(
await originalDartdocSdkBuild,
originalDartdocSdkDocs.absolute.path,
null,
dartdocOriginalBranch);
print(originalDartdocWarnings.getPrintableWarningDelta(
'SDK docs', currentDartdocWarnings));
}
/// Helper function to create a clean version of dartdoc (based on the current
/// directory, assumed to be a git repository). Uses [dartdocOriginalBranch]
/// to checkout a branch or tag.
Future<String> createComparisonDartdoc() async {
var launcher = SubprocessLauncher('create-comparison-dartdoc');
var dartdocClean = Directory.systemTemp.createTempSync('dartdoc-comparison');
await launcher
.runStreamed('git', ['clone', Directory.current.path, dartdocClean.path]);
await launcher.runStreamed('git', ['checkout', dartdocOriginalBranch],
workingDirectory: dartdocClean.path);
await launcher.runStreamed(sdkBin('pub'), ['get'],
workingDirectory: dartdocClean.path);
return dartdocClean.path;
}
/// Helper function to create a clean version of dartdoc (based on the current
/// directory, assumed to be a git repository), configured to use the head
/// version of the Dart SDK for analyzer, front-end, and kernel.
Future<String> createSdkDartdoc() async {
var launcher = SubprocessLauncher('create-sdk-dartdoc');
var dartdocSdk = Directory.systemTemp.createTempSync('dartdoc-sdk');
await launcher
.runStreamed('git', ['clone', Directory.current.path, dartdocSdk.path]);
await launcher.runStreamed('git', ['checkout'],
workingDirectory: dartdocSdk.path);
var sdkClone = Directory.systemTemp.createTempSync('sdk-checkout');
await launcher.runStreamed('git', [
'clone',
'--branch',
'master',
'--depth',
'1',
'https://dart.googlesource.com/sdk.git',
sdkClone.path
]);
var dartdocPubspec = File(path.join(dartdocSdk.path, 'pubspec.yaml'));
var pubspecLines = await dartdocPubspec.readAsLines();
var pubspecLinesFiltered = <String>[];
for (var line in pubspecLines) {
if (line.startsWith('dependency_overrides:')) {
pubspecLinesFiltered.add('#dependency_overrides:');
} else {
pubspecLinesFiltered.add(line);
}
}
await dartdocPubspec.writeAsString(pubspecLinesFiltered.join('\n'));
dartdocPubspec.writeAsStringSync('''
dependency_overrides:
analyzer:
path: '${sdkClone.path}/pkg/analyzer'
_fe_analyzer_shared:
path: '${sdkClone.path}/pkg/_fe_analyzer_shared'
''', mode: FileMode.append);
await launcher.runStreamed(sdkBin('pub'), ['get'],
workingDirectory: dartdocSdk.path);
return dartdocSdk.path;
}
@Task('Run grind tasks with the analyzer SDK.')
Future<void> testWithAnalyzerSdk() async {
var launcher = SubprocessLauncher('test-with-analyzer-sdk');
var sdkDartdoc = await createSdkDartdoc();
var defaultGrindParameter =
Platform.environment['DARTDOC_GRIND_STEP'] ?? 'test';
await launcher.runStreamed(
sdkBin('pub'), ['run', 'grinder', defaultGrindParameter],
workingDirectory: sdkDartdoc);
}
Future<List<Map>> _buildSdkDocs(String sdkDocsPath, Future<String> futureCwd,
[String label]) async {
label ??= '';
if (label != '') label = '-$label';
var launcher = SubprocessLauncher('build-sdk-docs$label');
var cwd = await futureCwd;
await launcher.runStreamed(sdkBin('pub'), ['get'], workingDirectory: cwd);
return await launcher.runStreamed(
Platform.resolvedExecutable,
[
'--enable-asserts',
path.join('bin', 'dartdoc.dart'),
'--output',
'${sdkDocsPath}',
'--sdk-docs',
'--json',
'--show-progress',
...extraDartdocParameters,
],
workingDirectory: cwd);
}
Future<List<Map>> _buildTestPackageDocs(String outputDir, String cwd,
{List<String> params, String label = '', String testPackagePath}) async {
if (label != '') label = '-$label';
testPackagePath ??= testPackage.absolute.path;
params ??= [];
var launcher = SubprocessLauncher('build-test-package-docs$label');
Future testPackagePubGet = launcher.runStreamed(sdkBin('pub'), ['get'],
workingDirectory: testPackagePath);
Future dartdocPubGet =
launcher.runStreamed(sdkBin('pub'), ['get'], workingDirectory: cwd);
await Future.wait([testPackagePubGet, dartdocPubGet]);
return await launcher.runStreamed(
Platform.resolvedExecutable,
[
'--enable-asserts',
path.join(cwd, 'bin', 'dartdoc.dart'),
'--output',
outputDir,
'--example-path-prefix',
'examples',
'--include-source',
'--json',
'--link-to-remote',
'--pretty-index-json',
...params,
...extraDartdocParameters,
],
workingDirectory: testPackagePath);
}
@Task('Build generated test package docs from the experiment test package')
@Depends(clean)
Future<void> buildTestExperimentsPackageDocs() async {
await _buildTestPackageDocs(
testPackageExperimentsDocsDir.absolute.path, Directory.current.path,
testPackagePath: testPackageExperiments.absolute.path,
params: ['--enable-experiment', 'non-nullable', '--no-link-to-remote']);
}
@Task('Serve experimental test package on port 8003.')
@Depends(buildTestExperimentsPackageDocs)
Future<void> serveTestExperimentsPackageDocs() async {
await _serveDocsFrom(testPackageExperimentsDocsDir.absolute.path, 8003,
'test-package-docs-experiments');
}
@Task('Build test package docs (HTML) with inherited docs and source code')
@Depends(clean)
Future<void> buildTestPackageDocs() async {
await _buildTestPackageDocs(
testPackageDocsDir.absolute.path, Directory.current.path);
}
@Task('Build test package docs (Markdown) with inherited docs and source code')
@Depends(clean)
Future<void> buildTestPackageDocsMd() async {
await _buildTestPackageDocs(
testPackageDocsDir.absolute.path, Directory.current.path,
params: ['--format', 'md']);
}
@Task('Serve test package docs locally with dhttpd on port 8002')
@Depends(buildTestPackageDocs)
Future<void> serveTestPackageDocs() async {
await startTestPackageDocsServer();
}
@Task('Serve test package docs (in Markdown) locally with dhttpd on port 8002')
@Depends(buildTestPackageDocsMd)
Future<void> serveTestPackageDocsMd() async {
await startTestPackageDocsServer();
}
Future<void> startTestPackageDocsServer() async {
log('launching dhttpd on port 8002 for SDK');
var launcher = SubprocessLauncher('serve-test-package-docs');
await launcher.runStreamed(sdkBin('pub'), [
'run',
'dhttpd',
'--port',
'8002',
'--path',
'${testPackageDocsDir.absolute.path}',
]);
}
bool _serveReady = false;
Future<void> _serveDocsFrom(String servePath, int port, String context) async {
log('launching dhttpd on port $port for $context');
var launcher = SubprocessLauncher(context);
if (!_serveReady) {
await launcher.runStreamed(sdkBin('pub'), ['get']);
await launcher.runStreamed(sdkBin('pub'), ['global', 'activate', 'dhttpd']);
_serveReady = true;
}
await launcher.runStreamed(
sdkBin('pub'), ['run', 'dhttpd', '--port', '$port', '--path', servePath]);
}
@Task('Serve generated SDK docs locally with dhttpd on port 8000')
@Depends(buildSdkDocs)
Future<void> serveSdkDocs() async {
log('launching dhttpd on port 8000 for SDK');
var launcher = SubprocessLauncher('serve-sdk-docs');
await launcher.runStreamed(sdkBin('pub'), [
'run',
'dhttpd',
'--port',
'8000',
'--path',
'${sdkDocsDir.path}',
]);
}
@Task('Compare warnings in Dartdoc for Flutter')
Future<void> compareFlutterWarnings() async {
var originalDartdocFlutter =
Directory.systemTemp.createTempSync('dartdoc-comparison-flutter');
Future originalDartdoc = createComparisonDartdoc();
var envCurrent = _createThrowawayPubCache();
var envOriginal = _createThrowawayPubCache();
Future currentDartdocFlutterBuild = _buildFlutterDocs(flutterDir.path,
Future.value(Directory.current.path), envCurrent, 'docs-current');
Future originalDartdocFlutterBuild = _buildFlutterDocs(
originalDartdocFlutter.path,
originalDartdoc,
envOriginal,
'docs-original');
var currentDartdocWarnings = jsonMessageIterableToWarnings(
await currentDartdocFlutterBuild,
flutterDir.absolute.path,
envCurrent['PUB_CACHE'],
'HEAD');
var originalDartdocWarnings = jsonMessageIterableToWarnings(
await originalDartdocFlutterBuild,
originalDartdocFlutter.absolute.path,
envOriginal['PUB_CACHE'],
dartdocOriginalBranch);
print(originalDartdocWarnings.getPrintableWarningDelta(
'Flutter repo', currentDartdocWarnings));
if (Platform.environment['SERVE_FLUTTER'] == '1') {
var launcher = SubprocessLauncher('serve-flutter-docs');
await launcher.runStreamed(sdkBin('pub'), ['get']);
Future original = launcher.runStreamed(sdkBin('pub'), [
'run',
'dhttpd',
'--port',
'9000',
'--path',
path.join(originalDartdocFlutter.absolute.path, 'dev', 'docs', 'doc'),
]);
Future current = launcher.runStreamed(sdkBin('pub'), [
'run',
'dhttpd',
'--port',
'9001',
'--path',
path.join(flutterDir.absolute.path, 'dev', 'docs', 'doc'),
]);
await Future.wait([original, current]);
}
}
@Task('Serve generated Flutter docs locally with dhttpd on port 8001')
@Depends(buildFlutterDocs)
Future<void> serveFlutterDocs() async {
log('launching dhttpd on port 8001 for Flutter');
var launcher = SubprocessLauncher('serve-flutter-docs');
await launcher.runStreamed(sdkBin('pub'), ['get']);
await launcher.runStreamed(sdkBin('pub'), [
'run',
'dhttpd',
'--port',
'8001',
'--path',
path.join(flutterDir.path, 'dev', 'docs', 'doc'),
]);
}
@Task('Validate flutter docs')
@Depends(buildFlutterDocs, testDartdocFlutterPlugin)
void validateFlutterDocs() {}
@Task('Build flutter docs')
Future<void> buildFlutterDocs() async {
log('building flutter docs into: $flutterDir');
var env = _createThrowawayPubCache();
await _buildFlutterDocs(
flutterDir.path, Future.value(Directory.current.path), env, 'docs');
var index =
File(path.join(flutterDir.path, 'dev', 'docs', 'doc', 'index.html'))
.readAsStringSync();
stdout.write(index);
}
/// A class wrapping a flutter SDK.
class FlutterRepo {
final String flutterPath;
final Map<String, String> env;
final String bin = path.join('bin', 'flutter');
FlutterRepo._(this.flutterPath, this.env, String label) {
cacheDart =
path.join(flutterPath, 'bin', 'cache', 'dart-sdk', 'bin', 'dart');
cachePub = path.join(flutterPath, 'bin', 'cache', 'dart-sdk', 'bin', 'pub');
env['PATH'] =
'${path.join(path.canonicalize(flutterPath), "bin")}:${env['PATH'] ?? Platform.environment['PATH']}';
env['FLUTTER_ROOT'] = flutterPath;
launcher =
SubprocessLauncher('flutter${label == null ? "" : "-$label"}', env);
}
Future<void> _init() async {
Directory(flutterPath).createSync(recursive: true);
await launcher.runStreamed(
'git', ['clone', 'https://github.com/flutter/flutter.git', '.'],
workingDirectory: flutterPath);
await launcher.runStreamed(
bin,
['--version'],
workingDirectory: flutterPath,
);
await launcher.runStreamed(
bin,
['precache'],
workingDirectory: flutterPath,
);
await launcher.runStreamed(
bin,
['update-packages'],
workingDirectory: flutterPath,
);
}
factory FlutterRepo.fromPath(String flutterPath, Map<String, String> env,
[String label]) {
var flutterRepo = FlutterRepo._(flutterPath, env, label);
return flutterRepo;
}
/// Copy an existing, initialized flutter repo.
static Future<FlutterRepo> copyFromExistingFlutterRepo(
FlutterRepo origRepo, String flutterPath, Map<String, String> env,
[String label]) async {
await copyPath(origRepo.flutterPath, flutterPath);
var flutterRepo = FlutterRepo._(flutterPath, env, label);
return flutterRepo;
}
/// Doesn't actually copy the existing repo; use for read-only operations only.
static Future<FlutterRepo> fromExistingFlutterRepo(FlutterRepo origRepo,
[String label]) async {
var flutterRepo = FlutterRepo._(origRepo.flutterPath, {}, label);
return flutterRepo;
}
String cacheDart;
String cachePub;
SubprocessLauncher launcher;
}
Future<List<Map>> _buildFlutterDocs(
String flutterPath, Future<String> futureCwd, Map<String, String> env,
[String label]) async {
var flutterRepo = await FlutterRepo.copyFromExistingFlutterRepo(
await cleanFlutterRepo, flutterPath, env, label);
await flutterRepo.launcher.runStreamed(
flutterRepo.cachePub,
['get'],
workingDirectory: path.join(flutterPath, 'dev', 'tools'),
);
await flutterRepo.launcher.runStreamed(
flutterRepo.cachePub,
['get'],
workingDirectory: path.join(flutterPath, 'dev', 'snippets'),
);
// TODO(jcollins-g): flutter's dart SDK pub tries to precompile the universe
// when using -spath. Why?
await flutterRepo.launcher.runStreamed(
'pub', ['global', 'activate', '-spath', '.', '-x', 'dartdoc'],
workingDirectory: await futureCwd);
return await flutterRepo.launcher.runStreamed(
flutterRepo.cacheDart,
[path.join('dev', 'tools', 'dartdoc.dart'), '-c', '--json'],
workingDirectory: flutterPath,
);
}
/// Returns the directory in which we generated documentation.
Future<String> _buildPubPackageDocs(
String pubPackageName,
List<String> dartdocParameters,
PackageMetaProvider packageMetaProvider, [
String version,
String label,
]) async {
var env = _createThrowawayPubCache();
var launcher = SubprocessLauncher(
'build-${pubPackageName}${version == null ? "" : "-$version"}${label == null ? "" : "-$label"}',
env);
var args = <String>['cache', 'add'];
if (version != null) args.addAll(<String>['-v', version]);
args.add(pubPackageName);
await launcher.runStreamed('pub', args);
var cache =
Directory(path.join(env['PUB_CACHE'], 'hosted', 'pub.dartlang.org'));
var pubPackageDirOrig =
cache.listSync().firstWhere((e) => e.path.contains(pubPackageName));
var pubPackageDir = Directory.systemTemp.createTempSync(pubPackageName);
await copyPath(pubPackageDirOrig.path, pubPackageDir.path);
if (packageMetaProvider.fromDir(pubPackageDir).requiresFlutter) {
var flutterRepo =
await FlutterRepo.fromExistingFlutterRepo(await cleanFlutterRepo);
await launcher.runStreamed(flutterRepo.cachePub, ['get'],
environment: flutterRepo.env,
workingDirectory: pubPackageDir.absolute.path);
await launcher.runStreamed(
flutterRepo.cacheDart,
[
'--enable-asserts',
path.join(Directory.current.absolute.path, 'bin', 'dartdoc.dart'),
'--json',
'--link-to-remote',
'--show-progress',
...dartdocParameters,
],
environment: flutterRepo.env,
workingDirectory: pubPackageDir.absolute.path);
} else {
await launcher.runStreamed('pub', ['get'],
workingDirectory: pubPackageDir.absolute.path);
await launcher.runStreamed(
Platform.resolvedExecutable,
[
'--enable-asserts',
path.join(Directory.current.absolute.path, 'bin', 'dartdoc.dart'),
'--json',
'--link-to-remote',
'--show-progress',
...dartdocParameters,
],
workingDirectory: pubPackageDir.absolute.path);
}
return path.join(pubPackageDir.absolute.path, 'doc', 'api');
}
@Task(
'Build an arbitrary pub package based on PACKAGE_NAME and PACKAGE_VERSION environment variables')
Future<String> buildPubPackage() async {
assert(Platform.environment.containsKey('PACKAGE_NAME'));
var packageName = Platform.environment['PACKAGE_NAME'];
var version = Platform.environment['PACKAGE_VERSION'];
return _buildPubPackageDocs(
packageName,
extraDartdocParameters,
pubPackageMetaProvider,
version,
);
}
@Task(
'Serve an arbitrary pub package based on PACKAGE_NAME and PACKAGE_VERSION environment variables')
Future<void> servePubPackage() async {
await _serveDocsFrom(await buildPubPackage(), 9000, 'serve-pub-package');
}
@Task('Checks that CHANGELOG mentions current version')
Future<void> checkChangelogHasVersion() async {
var changelog = File('CHANGELOG.md');
if (!changelog.existsSync()) {
fail('ERROR: No CHANGELOG.md found in ${Directory.current}');
}
var version = _getPackageVersion();
if (!changelog.readAsLinesSync().contains('## ${version}')) {
fail('ERROR: CHANGELOG.md does not mention version ${version}');
}
}
String _getPackageVersion() {
var pubspec = File('pubspec.yaml');
var yamlDoc;
if (pubspec.existsSync()) {
yamlDoc = yaml.loadYaml(pubspec.readAsStringSync());
}
if (yamlDoc == null) {
fail('Cannot find pubspec.yaml in ${Directory.current}');
}
var version = yamlDoc['version'];
return version;
}
@Task('Rebuild generated files')
Future<void> build() async {
var launcher = SubprocessLauncher('build');
await launcher.runStreamed(sdkBin('pub'),
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
// TODO(jcollins-g): port to build system?
var version = _getPackageVersion();
var dartdoc_options = File('dartdoc_options.yaml');
await dartdoc_options.writeAsString('''dartdoc:
linkToSource:
root: '.'
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v${version}/%f%#L%l%'
''');
}
/// Paths in this list are relative to lib/.
final _generated_files_list = <String>[
'../dartdoc_options.yaml',
'src/generator/html_resources.g.dart',
'src/version.dart',
].map((s) => path.joinAll(path.posix.split(s)));
@Task('Verify generated files are up to date')
Future<void> checkBuild() async {
var originalFileContents = <String, String>{};
var differentFiles = <String>[];
// Load original file contents into memory before running the builder;
// it modifies them in place.
for (var relPath in _generated_files_list) {
var origPath = path.joinAll(['lib', relPath]);
var oldVersion = File(origPath);
if (oldVersion.existsSync()) {
originalFileContents[relPath] = oldVersion.readAsStringSync();
}
}
await build();
for (var relPath in _generated_files_list) {
var newVersion = File(path.join('lib', relPath));
if (!await newVersion.exists()) {
log('${newVersion.path} does not exist\n');
differentFiles.add(relPath);
} else if (originalFileContents[relPath] !=
await newVersion.readAsString()) {
log('${newVersion.path} has changed to: \n${newVersion.readAsStringSync()})');
differentFiles.add(relPath);
}
}
if (differentFiles.isNotEmpty) {
fail('The following generated files needed to be rebuilt:\n'
' ${differentFiles.map((f) => path.join('lib', f)).join("\n ")}\n'
'Rebuild them with "grind build" and check the results in.');
}
}
@Task('Dry run of publish to pub.dartlang')
@Depends(checkChangelogHasVersion)
Future<void> tryPublish() async {
var launcher = SubprocessLauncher('try-publish');
await launcher.runStreamed(sdkBin('pub'), ['publish', '-n']);
}
@Task('Run a smoke test, only')
@Depends(clean)
Future<void> smokeTest() async {
await testDart2(smokeTestFiles);
await testFutures.wait();
}
@Task('Run non-smoke tests, only')
@Depends(clean)
Future<void> longTest() async {
await testDart2(testFiles);
await testFutures.wait();
}
@Task('Run all the tests.')
@Depends(clean)
Future<void> test() async {
await testDart2(smokeTestFiles.followedBy(testFiles));
await testFutures.wait();
}
@Task('Clean up pub data from test directories')
Future<void> clean() async {
var toDelete = nonRootPubData;
toDelete.forEach((e) => e.deleteSync(recursive: true));
}