Dan Rubel 34e466f1fd Refactor flutter command exit code - part 3 of 3 (#6838)
* Remove the workaround that pinned args to v0.13.6
This reverts most of the changes in commit 6331b6c8b5d964ec0dbf2cd9bb84c60c650a0878
* throw exception if exit code is not an integer
* rework command infrastructure to throw ToolExit when non-zero exitCode
* convert commands to return Future<Null>
* cleanup remaining commands to use throwToolExit for non-zero exit code
* remove isUnusual exception message
* add type annotations for updated args package
2016-11-14 14:21:30 -05:00

68 lines
2.3 KiB
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import '../base/utils.dart';
import '../cache.dart';
import '../globals.dart';
/// Common behavior for `flutter analyze` and `flutter analyze --watch`
abstract class AnalyzeBase {
/// The parsed argument results for execution.
final ArgResults argResults;
AnalyzeBase(this.argResults);
/// Called by [AnalyzeCommand] to start the analysis process.
Future<Null> analyze();
void dumpErrors(Iterable<String> errors) {
if (argResults['write'] != null) {
try {
final RandomAccessFile resultsFile = new File(argResults['write']).openSync(mode: FileMode.WRITE);
try {
resultsFile.lockSync();
resultsFile.writeStringSync(errors.join('\n'));
} finally {
resultsFile.close();
}
} catch (e) {
printError('Failed to save output to "${argResults['write']}": $e');
}
}
}
void writeBenchmark(Stopwatch stopwatch, int errorCount, int membersMissingDocumentation) {
final String benchmarkOut = 'analysis_benchmark.json';
Map<String, dynamic> data = <String, dynamic>{
'time': (stopwatch.elapsedMilliseconds / 1000.0),
'issues': errorCount,
'missingDartDocs': membersMissingDocumentation
};
new File(benchmarkOut).writeAsStringSync(toPrettyJson(data));
printStatus('Analysis benchmark written to $benchmarkOut ($data).');
}
bool get isBenchmarking => argResults['benchmark'];
}
/// Return `true` if [fileList] contains a path that resides inside the Flutter repository.
/// If [fileList] is empty, then return `true` if the current directory resides inside the Flutter repository.
bool inRepo(List<String> fileList) {
if (fileList == null || fileList.isEmpty)
fileList = <String>[path.current];
String root = path.normalize(path.absolute(Cache.flutterRoot));
String prefix = root + Platform.pathSeparator;
for (String file in fileList) {
file = path.normalize(path.absolute(file));
if (file == root || file.startsWith(prefix))
return true;
}
return false;
}