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-
75import 'dart:async' ;
86import 'dart:convert' ;
97import '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.
1614class 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
140133class 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