forked from flutter/gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrind.dart
More file actions
161 lines (138 loc) · 4.96 KB
/
grind.dart
File metadata and controls
161 lines (138 loc) · 4.96 KB
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
// Copyright 2019 The Flutter team. 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:convert';
import 'dart:io';
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as path;
void main(List<String> args) => grind(args);
@Task('Get packages')
Future<void> pubGet({String directory}) async {
await _runProcess(
'flutter',
['pub', 'get', if (directory != null) directory],
);
}
@Task('Format dart files')
Future<void> format({String path = '.'}) async {
await _runProcess('flutter', ['format', path]);
}
@Task('Generate localizations files')
Future<void> generateLocalizations() async {
final l10nScriptFile = path.join(
_flutterRootPath(),
'dev',
'tools',
'localization',
'bin',
'gen_l10n.dart',
);
await pubGet(directory: l10nScriptFile);
Dart.run(l10nScriptFile, arguments: [
'--template-arb-file=intl_en.arb',
'--output-localization-file=gallery_localizations.dart',
'--output-class=GalleryLocalizations',
'--preferred-supported-locales=["en"]',
'--use-deferred-loading',
]);
await format(path: path.join('lib', 'l10n'));
}
@Task('Transform arb to xml for English')
@Depends(generateLocalizations)
Future<void> l10n() async {
final l10nPath =
path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart');
Dart.run(l10nPath);
}
@Task('Verify xml localizations')
Future<void> verifyL10n() async {
final l10nPath =
path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart');
// Run the tool to transform arb to xml, and write the output to stdout.
final xmlOutput = Dart.run(l10nPath, arguments: ['--dry-run'], quiet: true);
// Read the original xml file.
final xmlPath =
path.join(Directory.current.path, 'lib', 'l10n', 'intl_en_US.xml');
final expectedXmlOutput = await File(xmlPath).readAsString();
if (xmlOutput.trim() != expectedXmlOutput.trim()) {
stderr.writeln(
'The contents of $xmlPath are different from that produced by '
'l10n_cli. Did you forget to run `flutter pub run grinder '
'l10n` after updating an .arb file?',
);
exit(1);
}
}
@Task('Update code segments')
Future<void> updateCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');
Dart.run(codeviewerPath);
final codeSegmentsPath = path.join('lib', 'codeviewer', 'code_segments.dart');
await format(path: codeSegmentsPath);
}
@Task('Verify code segments')
Future<void> verifyCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');
// We use stdin and stdout to write and format the code segments, to avoid
// creating any files.
final codeSegmentsUnformatted =
Dart.run(codeviewerPath, arguments: ['--dry-run'], quiet: true);
final codeSegmentsFormatted = await _startProcess(
path.normalize(path.join(dartVM.path, '../dartfmt')),
input: codeSegmentsUnformatted,
);
// Read the original code segments file.
final codeSegmentsPath = path.join(
Directory.current.path, 'lib', 'codeviewer', 'code_segments.dart');
final expectedCodeSegmentsOutput =
await File(codeSegmentsPath).readAsString();
if (codeSegmentsFormatted.trim() != expectedCodeSegmentsOutput.trim()) {
stderr.writeln(
'The contents of $codeSegmentsPath are different from that produced by '
'codeviewer_cli. Did you forget to run `flutter pub run grinder '
'update-code-segments` after updating a demo?',
);
exit(1);
}
}
Future<void> _runProcess(String executable, List<String> arguments) async {
final result = await Process.run(executable, arguments);
stdout.write(result.stdout);
stderr.write(result.stderr);
}
// Function to make sure we capture all of the stdout.
// Reference: https://github.com/dart-lang/sdk/issues/31666
Future<String> _startProcess(String executable,
{List<String> arguments = const [], String input}) async {
final output = <int>[];
final completer = Completer<int>();
final process = await Process.start(executable, arguments);
process.stdin.writeln(input);
process.stdout.listen(
(event) {
output.addAll(event);
},
onDone: () async => completer.complete(await process.exitCode),
);
await process.stdin.close();
final exitCode = await completer.future;
if (exitCode != 0) {
stderr.write(
'Running "$executable ${arguments.join(' ')}" failed with $exitCode.\n',
);
exit(exitCode);
}
return Future<String>.value(utf8.decoder.convert(output));
}
/// Return the flutter root path from the environment variables.
String _flutterRootPath() {
final separator = (Platform.isWindows) ? ';' : ':';
final flutterBinPath =
Platform.environment['PATH'].split(separator).lastWhere((setting) {
return path.canonicalize(setting).endsWith(path.join('flutter', 'bin'));
});
return Directory(flutterBinPath).parent.path;
}