2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- // @dart=2.9
6
-
7
5
import 'dart:async' ;
8
6
import 'dart:convert' ;
9
7
import 'dart:io' ;
@@ -14,11 +12,9 @@ import 'package:path/path.dart' as p;
14
12
/// Keeps track of coverage data automatically for any processes run by this
15
13
/// [CoverageSubprocessLauncher] . Requires that these be dart processes.
16
14
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});
22
18
23
19
static int nextRun = 0 ;
24
20
@@ -31,24 +27,20 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
31
27
// is enabled.
32
28
static List <Future <Iterable <Map <Object , Object >>>> coverageResults = [];
33
29
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);
42
34
}
43
- return _tempDir ;
44
- }
35
+ return Directory .systemTemp. createTempSync ( 'dartdoc_coverage_data' ) ;
36
+ }();
45
37
46
38
static String buildNextCoverageFilename () =>
47
39
p.join (tempDir.path, 'dart-cov-$pid -${nextRun ++}.json' );
48
40
49
41
/// Call once all coverage runs have been generated by calling runStreamed
50
42
/// on all [CoverageSubprocessLaunchers] .
51
- static Future <void > generateCoverageToFile (
43
+ static Future <dynamic > generateCoverageToFile (
52
44
File outputFile, ResourceProvider resourceProvider) async {
53
45
if (! coverageEnabled) return Future .value (null );
54
46
var currentCoverageResults = coverageResults;
@@ -75,12 +67,12 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
75
67
}
76
68
77
69
@override
78
- Future <Iterable <Map <Object , Object >>> runStreamed (
70
+ Future <Iterable <Map <String , Object >>> runStreamed (
79
71
String executable, List <String > arguments,
80
- {String workingDirectory,
81
- Map <String , String > environment,
72
+ {String ? workingDirectory,
73
+ Map <String , String >? environment,
82
74
bool includeParentEnvironment = true ,
83
- void Function (String ) perLine}) async {
75
+ void Function (String )? perLine}) async {
84
76
environment ?? = {};
85
77
assert (
86
78
executable == Platform .executable ||
@@ -91,16 +83,17 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
91
83
void parsePortAsString (String line) {
92
84
if (! portAsString.isCompleted && coverageEnabled) {
93
85
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
+ }
95
89
} else {
96
90
if (perLine != null ) perLine (line);
97
91
}
98
92
}
99
93
100
- Completer <Iterable <Map <Object , Object >>> coverageResult;
94
+ Completer <Iterable <Map <Object , Object >>> coverageResult = Completer () ;
101
95
102
96
if (coverageEnabled) {
103
- coverageResult = Completer ();
104
97
// This must be added before awaiting in this method.
105
98
coverageResults.add (coverageResult.future);
106
99
arguments = [
@@ -139,29 +132,27 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
139
132
140
133
class SubprocessLauncher {
141
134
final String context;
142
- final Map <String , String > environmentDefaults;
135
+ final Map <String , String > environmentDefaults = {} ;
143
136
144
137
String get prefix => context.isNotEmpty ? '$context : ' : '' ;
145
138
146
139
// from flutter:dev/tools/dartdoc.dart, modified
147
140
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}) {
151
143
return stream
152
144
.transform (utf8.decoder)
153
145
.transform (const LineSplitter ())
154
146
.expand (filter)
155
147
.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 ' );
160
150
}).asFuture ();
161
151
}
162
152
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
+ }
165
156
166
157
/// A wrapper around start/await process.exitCode that will display the
167
158
/// output of the executable continuously and fail on non-zero exit codes.
@@ -173,22 +164,23 @@ class SubprocessLauncher {
173
164
/// Windows (though some of the bashisms will no longer make sense).
174
165
/// TODO(jcollins-g): refactor to return a stream of stderr/stdout lines
175
166
/// and their associated JSON objects.
176
- Future <Iterable <Map <Object , Object >>> runStreamed (
167
+ Future <Iterable <Map <String , Object >>> runStreamed (
177
168
String executable, List <String > arguments,
178
- {String workingDirectory,
179
- Map <String , String > environment,
169
+ {String ? workingDirectory,
170
+ Map <String , String >? environment,
180
171
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 = [];
185
177
186
178
/// Allow us to pretend we didn't pass the JSON flag in to dartdoc by
187
179
/// printing what dartdoc would have printed without it, yet storing
188
180
/// json objects into [jsonObjects] .
189
181
Iterable <String > jsonCallback (String line) {
190
182
if (perLine != null ) perLine (line);
191
- Map <Object , Object > result;
183
+ Map <String , Object >? result;
192
184
try {
193
185
result = json.decoder.convert (line);
194
186
} on FormatException {
@@ -198,10 +190,9 @@ class SubprocessLauncher {
198
190
// line. Just ignore it and leave result null.
199
191
}
200
192
if (result != null ) {
201
- jsonObjects ?? = [];
202
193
jsonObjects.add (result);
203
194
if (result.containsKey ('message' )) {
204
- line = result['message' ];
195
+ line = result['message' ] as String ;
205
196
} else if (result.containsKey ('data' )) {
206
197
var data = result['data' ] as Map ;
207
198
line = data['text' ];
@@ -212,12 +203,12 @@ class SubprocessLauncher {
212
203
213
204
stderr.write ('$prefix + ' );
214
205
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 }'" ;
219
210
} else {
220
- return '$key =${environment [ key ] }' ;
211
+ return '${ entry . key } =${entry . value }' ;
221
212
}
222
213
}).join (' ' ));
223
214
stderr.write (' ' );
@@ -235,7 +226,7 @@ class SubprocessLauncher {
235
226
if (workingDirectory != null ) stderr.write (')' );
236
227
stderr.write ('\n ' );
237
228
238
- if (Platform .environment.containsKey ('DRY_RUN' )) return null ;
229
+ if (Platform .environment.containsKey ('DRY_RUN' )) return {} ;
239
230
240
231
var realExecutable = executable;
241
232
var realArguments = < String > [];
0 commit comments