Skip to content

Commit d85056c

Browse files
authored
Migrate most of what's left in tools/ (#2840)
* squash * rebuild * migrate more things * subprocess * type adjustment in grinder * comments
1 parent db4f226 commit d85056c

File tree

4 files changed

+57
-72
lines changed

4 files changed

+57
-72
lines changed

tool/builder.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
import 'dart:async';
86

97
import 'package:build/build.dart';

tool/doc_packages.dart

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
/// A CLI tool to generate documentation for packages from pub.dartlang.org.
86
library dartdoc.doc_packages;
97

@@ -86,7 +84,7 @@ void performGenerate(int page) {
8684

8785
_packageUrls(page).then((List<String> packages) {
8886
return _getPackageInfos(packages).then((List<PackageInfo> infos) {
89-
return Future.forEach(infos, (info) {
87+
return Future.forEach(infos, (PackageInfo info) {
9088
return _printGenerationResult(info, _generateFor(info));
9189
});
9290
});
@@ -147,7 +145,7 @@ Future<List<PackageInfo>> _getPackageInfos(List<String> packageUrls) {
147145
return Future.wait(futures);
148146
}
149147

150-
StringBuffer _logBuffer;
148+
StringBuffer? _logBuffer;
151149

152150
/// Generate the docs for the given package into _rootDir. Return whether
153151
/// generation was performed or was skipped (due to an older package).
@@ -195,7 +193,7 @@ Future<bool> _generateFor(PackageInfo package) async {
195193
}
196194

197195
Future<void> _exec(String command, List<String> args,
198-
{String cwd,
196+
{String? cwd,
199197
bool quiet = false,
200198
Duration timeout = const Duration(seconds: 60)}) {
201199
return Process.start(command, args, workingDirectory: cwd)
@@ -209,20 +207,16 @@ Future<void> _exec(String command, List<String> args,
209207
if (code != 0) throw code;
210208
});
211209

212-
if (timeout != null) {
213-
return f.timeout(timeout, onTimeout: () {
214-
_log('Timing out operation $command.');
215-
process.kill();
216-
throw 'timeout on $command';
217-
});
218-
} else {
219-
return f;
220-
}
210+
return f.timeout(timeout, onTimeout: () {
211+
_log('Timing out operation $command.');
212+
process.kill();
213+
throw 'timeout on $command';
214+
});
221215
});
222216
}
223217

224218
bool _isOldSdkConstraint(Map<String, dynamic> pubspecInfo) {
225-
var environment = pubspecInfo['environment'] as Map;
219+
var environment = pubspecInfo['environment'] as Map?;
226220
if (environment != null) {
227221
var sdk = environment['sdk'];
228222
if (sdk != null) {
@@ -243,8 +237,10 @@ bool _isOldSdkConstraint(Map<String, dynamic> pubspecInfo) {
243237
return false;
244238
}
245239

240+
/// Log entries will be dropped if [_logBuffer] has not been initialized.
246241
void _log(String str) {
247-
_logBuffer.write(str);
242+
assert(_logBuffer != null);
243+
_logBuffer?.write(str);
248244
}
249245

250246
class PackageInfo {

tool/grind.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ Future<void> testWithAnalyzerSdk() async {
553553
workingDirectory: sdkDartdoc);
554554
}
555555

556-
Future<List<Map<Object, Object>>> _buildSdkDocs(
556+
Future<Iterable<Map<String, Object>>> _buildSdkDocs(
557557
String sdkDocsPath, Future<String> futureCwd,
558558
[String label]) async {
559559
label ??= '';
@@ -576,7 +576,7 @@ Future<List<Map<Object, Object>>> _buildSdkDocs(
576576
workingDirectory: cwd);
577577
}
578578

579-
Future<List<Map<Object, Object>>> _buildTestPackageDocs(
579+
Future<Iterable<Map<String, Object>>> _buildTestPackageDocs(
580580
String outputDir, String cwd,
581581
{List<String> params, String label = '', String testPackagePath}) async {
582582
if (label != '') label = '-$label';
@@ -924,7 +924,7 @@ class FlutterRepo {
924924
SubprocessLauncher launcher;
925925
}
926926

927-
Future<List<Map<Object, Object>>> _buildFlutterDocs(
927+
Future<Iterable<Map<String, Object>>> _buildFlutterDocs(
928928
String flutterPath, Future<String> futureCwd, Map<String, String> env,
929929
[String label]) async {
930930
var flutterRepo = await FlutterRepo.copyFromExistingFlutterRepo(

tool/subprocess_launcher.dart

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';
@@ -14,11 +12,9 @@ import 'package:path/path.dart' as p;
1412
/// Keeps track of coverage data automatically for any processes run by this
1513
/// [CoverageSubprocessLauncher]. Requires that these be dart processes.
1614
class CoverageSubprocessLauncher extends SubprocessLauncher {
17-
CoverageSubprocessLauncher(String context, [Map<String, String> environment])
18-
: super(context, environment) {
19-
environment ??= {};
20-
environment['DARTDOC_COVERAGE_DATA'] = tempDir.path;
21-
}
15+
CoverageSubprocessLauncher(String context, [Map<String, String>? environment])
16+
: super(
17+
context, {...?environment, 'DARTDOC_COVERAGE_DATA': tempDir.path});
2218

2319
static int nextRun = 0;
2420

@@ -31,24 +27,20 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
3127
// is enabled.
3228
static List<Future<Iterable<Map<Object, Object>>>> coverageResults = [];
3329

34-
static Directory _tempDir;
35-
static Directory get tempDir {
36-
if (_tempDir == null) {
37-
if (Platform.environment['DARTDOC_COVERAGE_DATA'] != null) {
38-
_tempDir = Directory(Platform.environment['DARTDOC_COVERAGE_DATA']);
39-
} else {
40-
_tempDir = Directory.systemTemp.createTempSync('dartdoc_coverage_data');
41-
}
30+
static Directory tempDir = () {
31+
var coverageData = Platform.environment['DARTDOC_COVERAGE_DATA'];
32+
if (coverageData != null) {
33+
return Directory(coverageData);
4234
}
43-
return _tempDir;
44-
}
35+
return Directory.systemTemp.createTempSync('dartdoc_coverage_data');
36+
}();
4537

4638
static String buildNextCoverageFilename() =>
4739
p.join(tempDir.path, 'dart-cov-$pid-${nextRun++}.json');
4840

4941
/// Call once all coverage runs have been generated by calling runStreamed
5042
/// on all [CoverageSubprocessLaunchers].
51-
static Future<void> generateCoverageToFile(
43+
static Future<dynamic> generateCoverageToFile(
5244
File outputFile, ResourceProvider resourceProvider) async {
5345
if (!coverageEnabled) return Future.value(null);
5446
var currentCoverageResults = coverageResults;
@@ -75,12 +67,12 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
7567
}
7668

7769
@override
78-
Future<Iterable<Map<Object, Object>>> runStreamed(
70+
Future<Iterable<Map<String, Object>>> runStreamed(
7971
String executable, List<String> arguments,
80-
{String workingDirectory,
81-
Map<String, String> environment,
72+
{String? workingDirectory,
73+
Map<String, String>? environment,
8274
bool includeParentEnvironment = true,
83-
void Function(String) perLine}) async {
75+
void Function(String)? perLine}) async {
8476
environment ??= {};
8577
assert(
8678
executable == Platform.executable ||
@@ -91,16 +83,17 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
9183
void parsePortAsString(String line) {
9284
if (!portAsString.isCompleted && coverageEnabled) {
9385
var m = _observatoryPortRegexp.matchAsPrefix(line);
94-
if (m?.group(1) != null) portAsString.complete(m.group(1));
86+
if (m != null) {
87+
if (m.group(1) != null) portAsString.complete(m.group(1));
88+
}
9589
} else {
9690
if (perLine != null) perLine(line);
9791
}
9892
}
9993

100-
Completer<Iterable<Map<Object, Object>>> coverageResult;
94+
Completer<Iterable<Map<Object, Object>>> coverageResult = Completer();
10195

10296
if (coverageEnabled) {
103-
coverageResult = Completer();
10497
// This must be added before awaiting in this method.
10598
coverageResults.add(coverageResult.future);
10699
arguments = [
@@ -139,29 +132,27 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
139132

140133
class SubprocessLauncher {
141134
final String context;
142-
final Map<String, String> environmentDefaults;
135+
final Map<String, String> environmentDefaults = {};
143136

144137
String get prefix => context.isNotEmpty ? '$context: ' : '';
145138

146139
// from flutter:dev/tools/dartdoc.dart, modified
147140
static Future<void> _printStream(Stream<List<int>> stream, Stdout output,
148-
{String prefix = '', Iterable<String> Function(String line) filter}) {
149-
assert(prefix != null);
150-
filter ??= (line) => [line];
141+
{String prefix = '',
142+
required Iterable<String> Function(String line) filter}) {
151143
return stream
152144
.transform(utf8.decoder)
153145
.transform(const LineSplitter())
154146
.expand(filter)
155147
.listen((String line) {
156-
if (line != null) {
157-
output.write('$prefix$line'.trim());
158-
output.write('\n');
159-
}
148+
output.write('$prefix$line'.trim());
149+
output.write('\n');
160150
}).asFuture();
161151
}
162152

163-
SubprocessLauncher(this.context, [Map<String, String> environment])
164-
: environmentDefaults = environment ?? <String, String>{};
153+
SubprocessLauncher(this.context, [Map<String, String>? environment]) {
154+
environmentDefaults.addAll(environment ?? {});
155+
}
165156

166157
/// A wrapper around start/await process.exitCode that will display the
167158
/// output of the executable continuously and fail on non-zero exit codes.
@@ -173,22 +164,23 @@ class SubprocessLauncher {
173164
/// Windows (though some of the bashisms will no longer make sense).
174165
/// TODO(jcollins-g): refactor to return a stream of stderr/stdout lines
175166
/// and their associated JSON objects.
176-
Future<Iterable<Map<Object, Object>>> runStreamed(
167+
Future<Iterable<Map<String, Object>>> runStreamed(
177168
String executable, List<String> arguments,
178-
{String workingDirectory,
179-
Map<String, String> environment,
169+
{String? workingDirectory,
170+
Map<String, String>? environment,
180171
bool includeParentEnvironment = true,
181-
void Function(String) perLine}) async {
182-
environment ??= {};
183-
environment.addAll(environmentDefaults);
184-
List<Map<Object, Object>> jsonObjects;
172+
void Function(String)? perLine}) async {
173+
environment = {}
174+
..addAll(environmentDefaults)
175+
..addAll(environment ?? {});
176+
List<Map<String, Object>> jsonObjects = [];
185177

186178
/// Allow us to pretend we didn't pass the JSON flag in to dartdoc by
187179
/// printing what dartdoc would have printed without it, yet storing
188180
/// json objects into [jsonObjects].
189181
Iterable<String> jsonCallback(String line) {
190182
if (perLine != null) perLine(line);
191-
Map<Object, Object> result;
183+
Map<String, Object>? result;
192184
try {
193185
result = json.decoder.convert(line);
194186
} on FormatException {
@@ -198,10 +190,9 @@ class SubprocessLauncher {
198190
// line. Just ignore it and leave result null.
199191
}
200192
if (result != null) {
201-
jsonObjects ??= [];
202193
jsonObjects.add(result);
203194
if (result.containsKey('message')) {
204-
line = result['message'];
195+
line = result['message'] as String;
205196
} else if (result.containsKey('data')) {
206197
var data = result['data'] as Map;
207198
line = data['text'];
@@ -212,12 +203,12 @@ class SubprocessLauncher {
212203

213204
stderr.write('$prefix+ ');
214205
if (workingDirectory != null) stderr.write('(cd "$workingDirectory" && ');
215-
if (environment != null) {
216-
stderr.write(environment.keys.map((String key) {
217-
if (environment[key].contains(_quotables)) {
218-
return "$key='${environment[key]}'";
206+
if (environment.isNotEmpty) {
207+
stderr.write(environment.entries.map((MapEntry<String, String> entry) {
208+
if (entry.key.contains(_quotables)) {
209+
return "${entry.key}='${entry.value}'";
219210
} else {
220-
return '$key=${environment[key]}';
211+
return '${entry.key}=${entry.value}';
221212
}
222213
}).join(' '));
223214
stderr.write(' ');
@@ -235,7 +226,7 @@ class SubprocessLauncher {
235226
if (workingDirectory != null) stderr.write(')');
236227
stderr.write('\n');
237228

238-
if (Platform.environment.containsKey('DRY_RUN')) return null;
229+
if (Platform.environment.containsKey('DRY_RUN')) return {};
239230

240231
var realExecutable = executable;
241232
var realArguments = <String>[];

0 commit comments

Comments
 (0)