diff --git a/tool/builder.dart b/tool/builder.dart index 31e7804ee5..c9d1aad05a 100644 --- a/tool/builder.dart +++ b/tool/builder.dart @@ -2,8 +2,6 @@ // 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. -// @dart=2.9 - import 'dart:async'; import 'package:build/build.dart'; diff --git a/tool/doc_packages.dart b/tool/doc_packages.dart index b8ea616150..244798f4a1 100644 --- a/tool/doc_packages.dart +++ b/tool/doc_packages.dart @@ -2,8 +2,6 @@ // 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. -// @dart=2.9 - /// A CLI tool to generate documentation for packages from pub.dartlang.org. library dartdoc.doc_packages; @@ -86,7 +84,7 @@ void performGenerate(int page) { _packageUrls(page).then((List packages) { return _getPackageInfos(packages).then((List infos) { - return Future.forEach(infos, (info) { + return Future.forEach(infos, (PackageInfo info) { return _printGenerationResult(info, _generateFor(info)); }); }); @@ -147,7 +145,7 @@ Future> _getPackageInfos(List packageUrls) { return Future.wait(futures); } -StringBuffer _logBuffer; +StringBuffer? _logBuffer; /// Generate the docs for the given package into _rootDir. Return whether /// generation was performed or was skipped (due to an older package). @@ -195,7 +193,7 @@ Future _generateFor(PackageInfo package) async { } Future _exec(String command, List args, - {String cwd, + {String? cwd, bool quiet = false, Duration timeout = const Duration(seconds: 60)}) { return Process.start(command, args, workingDirectory: cwd) @@ -209,20 +207,16 @@ Future _exec(String command, List args, if (code != 0) throw code; }); - if (timeout != null) { - return f.timeout(timeout, onTimeout: () { - _log('Timing out operation $command.'); - process.kill(); - throw 'timeout on $command'; - }); - } else { - return f; - } + return f.timeout(timeout, onTimeout: () { + _log('Timing out operation $command.'); + process.kill(); + throw 'timeout on $command'; + }); }); } bool _isOldSdkConstraint(Map pubspecInfo) { - var environment = pubspecInfo['environment'] as Map; + var environment = pubspecInfo['environment'] as Map?; if (environment != null) { var sdk = environment['sdk']; if (sdk != null) { @@ -243,8 +237,10 @@ bool _isOldSdkConstraint(Map pubspecInfo) { return false; } +/// Log entries will be dropped if [_logBuffer] has not been initialized. void _log(String str) { - _logBuffer.write(str); + assert(_logBuffer != null); + _logBuffer?.write(str); } class PackageInfo { diff --git a/tool/grind.dart b/tool/grind.dart index a1ebc02b6b..c8653315b9 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -553,7 +553,7 @@ Future testWithAnalyzerSdk() async { workingDirectory: sdkDartdoc); } -Future>> _buildSdkDocs( +Future>> _buildSdkDocs( String sdkDocsPath, Future futureCwd, [String label]) async { label ??= ''; @@ -576,7 +576,7 @@ Future>> _buildSdkDocs( workingDirectory: cwd); } -Future>> _buildTestPackageDocs( +Future>> _buildTestPackageDocs( String outputDir, String cwd, {List params, String label = '', String testPackagePath}) async { if (label != '') label = '-$label'; @@ -924,7 +924,7 @@ class FlutterRepo { SubprocessLauncher launcher; } -Future>> _buildFlutterDocs( +Future>> _buildFlutterDocs( String flutterPath, Future futureCwd, Map env, [String label]) async { var flutterRepo = await FlutterRepo.copyFromExistingFlutterRepo( diff --git a/tool/subprocess_launcher.dart b/tool/subprocess_launcher.dart index 679427ad70..c0ada7434d 100644 --- a/tool/subprocess_launcher.dart +++ b/tool/subprocess_launcher.dart @@ -2,8 +2,6 @@ // 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. -// @dart=2.9 - import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -14,11 +12,9 @@ import 'package:path/path.dart' as p; /// Keeps track of coverage data automatically for any processes run by this /// [CoverageSubprocessLauncher]. Requires that these be dart processes. class CoverageSubprocessLauncher extends SubprocessLauncher { - CoverageSubprocessLauncher(String context, [Map environment]) - : super(context, environment) { - environment ??= {}; - environment['DARTDOC_COVERAGE_DATA'] = tempDir.path; - } + CoverageSubprocessLauncher(String context, [Map? environment]) + : super( + context, {...?environment, 'DARTDOC_COVERAGE_DATA': tempDir.path}); static int nextRun = 0; @@ -31,24 +27,20 @@ class CoverageSubprocessLauncher extends SubprocessLauncher { // is enabled. static List>>> coverageResults = []; - static Directory _tempDir; - static Directory get tempDir { - if (_tempDir == null) { - if (Platform.environment['DARTDOC_COVERAGE_DATA'] != null) { - _tempDir = Directory(Platform.environment['DARTDOC_COVERAGE_DATA']); - } else { - _tempDir = Directory.systemTemp.createTempSync('dartdoc_coverage_data'); - } + static Directory tempDir = () { + var coverageData = Platform.environment['DARTDOC_COVERAGE_DATA']; + if (coverageData != null) { + return Directory(coverageData); } - return _tempDir; - } + return Directory.systemTemp.createTempSync('dartdoc_coverage_data'); + }(); static String buildNextCoverageFilename() => p.join(tempDir.path, 'dart-cov-$pid-${nextRun++}.json'); /// Call once all coverage runs have been generated by calling runStreamed /// on all [CoverageSubprocessLaunchers]. - static Future generateCoverageToFile( + static Future generateCoverageToFile( File outputFile, ResourceProvider resourceProvider) async { if (!coverageEnabled) return Future.value(null); var currentCoverageResults = coverageResults; @@ -75,12 +67,12 @@ class CoverageSubprocessLauncher extends SubprocessLauncher { } @override - Future>> runStreamed( + Future>> runStreamed( String executable, List arguments, - {String workingDirectory, - Map environment, + {String? workingDirectory, + Map? environment, bool includeParentEnvironment = true, - void Function(String) perLine}) async { + void Function(String)? perLine}) async { environment ??= {}; assert( executable == Platform.executable || @@ -91,16 +83,17 @@ class CoverageSubprocessLauncher extends SubprocessLauncher { void parsePortAsString(String line) { if (!portAsString.isCompleted && coverageEnabled) { var m = _observatoryPortRegexp.matchAsPrefix(line); - if (m?.group(1) != null) portAsString.complete(m.group(1)); + if (m != null) { + if (m.group(1) != null) portAsString.complete(m.group(1)); + } } else { if (perLine != null) perLine(line); } } - Completer>> coverageResult; + Completer>> coverageResult = Completer(); if (coverageEnabled) { - coverageResult = Completer(); // This must be added before awaiting in this method. coverageResults.add(coverageResult.future); arguments = [ @@ -139,29 +132,27 @@ class CoverageSubprocessLauncher extends SubprocessLauncher { class SubprocessLauncher { final String context; - final Map environmentDefaults; + final Map environmentDefaults = {}; String get prefix => context.isNotEmpty ? '$context: ' : ''; // from flutter:dev/tools/dartdoc.dart, modified static Future _printStream(Stream> stream, Stdout output, - {String prefix = '', Iterable Function(String line) filter}) { - assert(prefix != null); - filter ??= (line) => [line]; + {String prefix = '', + required Iterable Function(String line) filter}) { return stream .transform(utf8.decoder) .transform(const LineSplitter()) .expand(filter) .listen((String line) { - if (line != null) { - output.write('$prefix$line'.trim()); - output.write('\n'); - } + output.write('$prefix$line'.trim()); + output.write('\n'); }).asFuture(); } - SubprocessLauncher(this.context, [Map environment]) - : environmentDefaults = environment ?? {}; + SubprocessLauncher(this.context, [Map? environment]) { + environmentDefaults.addAll(environment ?? {}); + } /// A wrapper around start/await process.exitCode that will display the /// output of the executable continuously and fail on non-zero exit codes. @@ -173,22 +164,23 @@ class SubprocessLauncher { /// Windows (though some of the bashisms will no longer make sense). /// TODO(jcollins-g): refactor to return a stream of stderr/stdout lines /// and their associated JSON objects. - Future>> runStreamed( + Future>> runStreamed( String executable, List arguments, - {String workingDirectory, - Map environment, + {String? workingDirectory, + Map? environment, bool includeParentEnvironment = true, - void Function(String) perLine}) async { - environment ??= {}; - environment.addAll(environmentDefaults); - List> jsonObjects; + void Function(String)? perLine}) async { + environment = {} + ..addAll(environmentDefaults) + ..addAll(environment ?? {}); + List> jsonObjects = []; /// Allow us to pretend we didn't pass the JSON flag in to dartdoc by /// printing what dartdoc would have printed without it, yet storing /// json objects into [jsonObjects]. Iterable jsonCallback(String line) { if (perLine != null) perLine(line); - Map result; + Map? result; try { result = json.decoder.convert(line); } on FormatException { @@ -198,10 +190,9 @@ class SubprocessLauncher { // line. Just ignore it and leave result null. } if (result != null) { - jsonObjects ??= []; jsonObjects.add(result); if (result.containsKey('message')) { - line = result['message']; + line = result['message'] as String; } else if (result.containsKey('data')) { var data = result['data'] as Map; line = data['text']; @@ -212,12 +203,12 @@ class SubprocessLauncher { stderr.write('$prefix+ '); if (workingDirectory != null) stderr.write('(cd "$workingDirectory" && '); - if (environment != null) { - stderr.write(environment.keys.map((String key) { - if (environment[key].contains(_quotables)) { - return "$key='${environment[key]}'"; + if (environment.isNotEmpty) { + stderr.write(environment.entries.map((MapEntry entry) { + if (entry.key.contains(_quotables)) { + return "${entry.key}='${entry.value}'"; } else { - return '$key=${environment[key]}'; + return '${entry.key}=${entry.value}'; } }).join(' ')); stderr.write(' '); @@ -235,7 +226,7 @@ class SubprocessLauncher { if (workingDirectory != null) stderr.write(')'); stderr.write('\n'); - if (Platform.environment.containsKey('DRY_RUN')) return null; + if (Platform.environment.containsKey('DRY_RUN')) return {}; var realExecutable = executable; var realArguments = [];