@@ -172,15 +172,8 @@ class ToolEnvironment {
172
172
return toolEnv;
173
173
}
174
174
175
- Future <String > runAnalyzer (
176
- String packageDir,
177
- String dir,
178
- bool usesFlutter, {
179
- required InspectOptions inspectOptions,
180
- }) async {
181
- final command =
182
- usesFlutter ? _flutterSdk.dartAnalyzeCmd : _dartSdk.dartAnalyzeCmd;
183
-
175
+ Future <T > withRestrictedAnalysisOptions <T >(
176
+ String packageDir, Future <T > Function () fn) async {
184
177
final analysisOptionsFile =
185
178
File (p.join (packageDir, 'analysis_options.yaml' ));
186
179
String ? originalOptions;
@@ -192,6 +185,26 @@ class ToolEnvironment {
192
185
original: originalOptions, custom: rawOptionsContent);
193
186
try {
194
187
await analysisOptionsFile.writeAsString (customOptionsContent);
188
+ return await fn ();
189
+ } finally {
190
+ if (originalOptions == null ) {
191
+ await analysisOptionsFile.delete ();
192
+ } else {
193
+ await analysisOptionsFile.writeAsString (originalOptions);
194
+ }
195
+ }
196
+ }
197
+
198
+ Future <String > runAnalyzer (
199
+ String packageDir,
200
+ String dir,
201
+ bool usesFlutter, {
202
+ required InspectOptions inspectOptions,
203
+ }) async {
204
+ final command =
205
+ usesFlutter ? _flutterSdk.dartAnalyzeCmd : _dartSdk.dartAnalyzeCmd;
206
+
207
+ return await withRestrictedAnalysisOptions (packageDir, () async {
195
208
final proc = await runConstrained (
196
209
[...command, '--format' , 'machine' , dir],
197
210
environment:
@@ -218,13 +231,7 @@ class ToolEnvironment {
218
231
throw ToolException ('dart analyze exception: $errorMessage ' , proc);
219
232
}
220
233
return output;
221
- } finally {
222
- if (originalOptions == null ) {
223
- await analysisOptionsFile.delete ();
224
- } else {
225
- await analysisOptionsFile.writeAsString (originalOptions);
226
- }
227
- }
234
+ });
228
235
}
229
236
230
237
Future <List <String >> filesNeedingFormat (String packageDir, bool usesFlutter,
@@ -233,56 +240,59 @@ class ToolEnvironment {
233
240
if (dirs.isEmpty) {
234
241
return const [];
235
242
}
236
- final files = < String > {};
237
- for (final dir in dirs) {
238
- final fullPath = p.join (packageDir, dir);
239
-
240
- final params = < String > [
241
- 'format' ,
242
- '--output=none' ,
243
- '--set-exit-if-changed' ,
244
- ];
245
- if (lineLength != null && lineLength > 0 ) {
246
- params.addAll (< String > ['--line-length' , lineLength.toString ()]);
247
- }
248
- params.add (fullPath);
243
+ return withRestrictedAnalysisOptions (packageDir, () async {
244
+ final files = < String > {};
245
+ for (final dir in dirs) {
246
+ final fullPath = p.join (packageDir, dir);
247
+
248
+ final params = < String > [
249
+ 'format' ,
250
+ '--output=none' ,
251
+ '--set-exit-if-changed' ,
252
+ ];
253
+ if (lineLength != null && lineLength > 0 ) {
254
+ params.addAll (< String > ['--line-length' , lineLength.toString ()]);
255
+ }
256
+ params.add (fullPath);
249
257
250
- final result = await runConstrained (
251
- [..._dartSdk.dartCmd, ...params],
252
- environment:
253
- usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
254
- timeout: _dartFormatTimeout,
255
- );
256
- if (result.exitCode == 0 ) {
257
- continue ;
258
- }
258
+ final result = await runConstrained (
259
+ [..._dartSdk.dartCmd, ...params],
260
+ environment:
261
+ usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
262
+ timeout: _dartFormatTimeout,
263
+ );
264
+ if (result.exitCode == 0 ) {
265
+ continue ;
266
+ }
259
267
260
- final dirPrefix = packageDir.endsWith ('/' ) ? packageDir : '$packageDir /' ;
261
- final output = result.asJoinedOutput;
262
- final lines = LineSplitter .split (result.asJoinedOutput)
263
- .where ((l) => l.startsWith ('Changed' ))
264
- .map ((l) => l.substring (8 ).replaceAll (dirPrefix, '' ).trim ())
265
- .toList ();
266
-
267
- // `dart format` exits with code = 1
268
- if (result.exitCode == 1 ) {
269
- assert (lines.isNotEmpty);
270
- files.addAll (lines);
271
- continue ;
272
- }
268
+ final dirPrefix =
269
+ packageDir.endsWith ('/' ) ? packageDir : '$packageDir /' ;
270
+ final output = result.asJoinedOutput;
271
+ final lines = LineSplitter .split (result.asJoinedOutput)
272
+ .where ((l) => l.startsWith ('Changed' ))
273
+ .map ((l) => l.substring (8 ).replaceAll (dirPrefix, '' ).trim ())
274
+ .toList ();
275
+
276
+ // `dart format` exits with code = 1
277
+ if (result.exitCode == 1 ) {
278
+ assert (lines.isNotEmpty);
279
+ files.addAll (lines);
280
+ continue ;
281
+ }
273
282
274
- final errorMsg = LineSplitter .split (output).take (10 ).join ('\n ' );
275
- final isUserProblem = output.contains (
276
- 'Could not format because the source could not be parsed' ) ||
277
- output.contains ('The formatter produced unexpected output.' );
278
- if (! isUserProblem) {
279
- throw Exception (
280
- 'dart format on $dir / failed with exit code ${result .exitCode }\n $output ' ,
281
- );
283
+ final errorMsg = LineSplitter .split (output).take (10 ).join ('\n ' );
284
+ final isUserProblem = output.contains (
285
+ 'Could not format because the source could not be parsed' ) ||
286
+ output.contains ('The formatter produced unexpected output.' );
287
+ if (! isUserProblem) {
288
+ throw Exception (
289
+ 'dart format on $dir / failed with exit code ${result .exitCode }\n $output ' ,
290
+ );
291
+ }
292
+ throw ToolException (errorMsg, result);
282
293
}
283
- throw ToolException (errorMsg, result);
284
- }
285
- return files.toList ()..sort ();
294
+ return files.toList ()..sort ();
295
+ });
286
296
}
287
297
288
298
Future <Map <String , dynamic >> _getFlutterVersion () async {
0 commit comments