Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Unified Diff: pkg/analyzer_cli/lib/src/driver.dart

Issue 1720963003: Initial hermetic package analyzer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer_cli/lib/src/driver.dart
diff --git a/pkg/analyzer_cli/lib/src/driver.dart b/pkg/analyzer_cli/lib/src/driver.dart
index 3de4b4d12cb155e67deed6e9b18aa9e5550bc1e0..85c53ee2809121a4d3158aeadb80021ced4dfb00 100644
--- a/pkg/analyzer_cli/lib/src/driver.dart
+++ b/pkg/analyzer_cli/lib/src/driver.dart
@@ -34,6 +34,7 @@ import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer/src/task/options.dart';
import 'package:analyzer_cli/src/analyzer_impl.dart';
import 'package:analyzer_cli/src/options.dart';
+import 'package:analyzer_cli/src/package_analyzer.dart';
import 'package:analyzer_cli/src/perf_report.dart';
import 'package:analyzer_cli/starter.dart';
import 'package:linter/src/plugin/linter_plugin.dart';
@@ -113,7 +114,13 @@ class Driver implements CommandLineStarter {
_setupEnv(options);
// Do analysis.
- if (_isBatch) {
+ if (options.packageMode) {
+ ErrorSeverity severity = _analyzePackage(options);
+ // In case of error propagate exit code.
+ if (severity == ErrorSeverity.ERROR) {
+ exitCode = severity.ordinal;
+ }
+ } else if (_isBatch) {
_BatchRunner.runAsBatch(args, (List<String> args) {
CommandLineOptions options = CommandLineOptions.parse(args);
return _analyzeAll(options);
@@ -215,6 +222,13 @@ class Driver implements CommandLineStarter {
return allResult;
}
+ /// Perform package analysis according to the given [options].
+ ErrorSeverity _analyzePackage(CommandLineOptions options) {
+ return _analyzeAllTag.makeCurrentWhile(() {
+ return new PackageAnalyzer(options).analyze();
+ });
+ }
+
/// Determine whether the context created during a previous call to
/// [_analyzeAll] can be re-used in order to analyze using [options].
bool _canContextBeReused(CommandLineOptions options) {
@@ -461,6 +475,7 @@ class Driver implements CommandLineStarter {
// Create a context.
AnalysisContext context = AnalysisEngine.instance.createAnalysisContext();
+ _context = context;
// Choose a package resolution policy and a diet parsing policy based on
// the command-line options.
@@ -471,34 +486,10 @@ class Driver implements CommandLineStarter {
context.sourceFactory = sourceFactory;
- Map<String, String> definedVariables = options.definedVariables;
- if (!definedVariables.isEmpty) {
- DeclaredVariables declaredVariables = context.declaredVariables;
- definedVariables.forEach((String variableName, String value) {
- declaredVariables.define(variableName, value);
- });
- }
-
- if (options.log) {
- AnalysisEngine.instance.logger = new StdLogger();
- }
-
- // Set context options.
- AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl();
- contextOptions.hint = !options.disableHints;
- contextOptions.enableStrictCallChecks = options.enableStrictCallChecks;
- contextOptions.enableSuperMixins = options.enableSuperMixins;
- contextOptions.analyzeFunctionBodiesPredicate = dietParsingPolicy;
- contextOptions.generateImplicitErrors = options.showPackageWarnings;
- contextOptions.generateSdkErrors = options.showSdkWarnings;
- contextOptions.lint = options.lints;
- contextOptions.strongMode = options.strongMode;
- context.analysisOptions = contextOptions;
- sourceFactory.dartSdk.context.analysisOptions = contextOptions;
- _context = context;
-
- // Process analysis options file (and notify all interested parties).
- _processAnalysisOptions(options, context);
+ setAnalysisContextOptions(_context, options,
+ (AnalysisOptionsImpl contextOptions) {
+ contextOptions.analyzeFunctionBodiesPredicate = dietParsingPolicy;
+ });
}
/// Return discovered packagespec, or `null` if none is found.
@@ -515,22 +506,6 @@ class Driver implements CommandLineStarter {
return null;
}
- fileSystem.File _getOptionsFile(CommandLineOptions options) {
- fileSystem.File file;
- String filePath = options.analysisOptionsFile;
- if (filePath != null) {
- file = PhysicalResourceProvider.INSTANCE.getFile(filePath);
- if (!file.exists) {
- printAndFail('Options file not found: $filePath',
- exitCode: ErrorSeverity.ERROR.ordinal);
- }
- } else {
- filePath = AnalysisEngine.ANALYSIS_OPTIONS_FILE;
- file = PhysicalResourceProvider.INSTANCE.getFile(filePath);
- }
- return file;
- }
-
Map<String, List<fileSystem.Folder>> _getPackageMap(Packages packages) {
if (packages == null) {
return null;
@@ -546,34 +521,6 @@ class Driver implements CommandLineStarter {
return folderMap;
}
- void _processAnalysisOptions(
- CommandLineOptions options, AnalysisContext context) {
- fileSystem.File file = _getOptionsFile(options);
- List<OptionsProcessor> optionsProcessors =
- AnalysisEngine.instance.optionsPlugin.optionsProcessors;
- try {
- AnalysisOptionsProvider analysisOptionsProvider =
- new AnalysisOptionsProvider();
- Map<String, YamlNode> optionMap =
- analysisOptionsProvider.getOptionsFromFile(file);
- optionsProcessors.forEach(
- (OptionsProcessor p) => p.optionsProcessed(context, optionMap));
-
- // Fill in lint rule defaults in case lints are enabled and rules are
- // not specified in an options file.
- if (options.lints && !containsLintRuleEntry(optionMap)) {
- setLints(context, linterPlugin.contributedRules);
- }
-
- // Ask engine to further process options.
- if (optionMap != null) {
- configureContextOptions(context, optionMap);
- }
- } on Exception catch (e) {
- optionsProcessors.forEach((OptionsProcessor p) => p.onError(e));
- }
- }
-
void _processPlugins() {
List<Plugin> plugins = <Plugin>[];
plugins.addAll(AnalysisEngine.instance.requiredPlugins);
@@ -611,6 +558,41 @@ class Driver implements CommandLineStarter {
_isBatch = options.shouldBatch;
}
+ static void setAnalysisContextOptions(
+ AnalysisContext context,
+ CommandLineOptions options,
+ void configureContextOptions(AnalysisOptionsImpl contextOptions)) {
+ Map<String, String> definedVariables = options.definedVariables;
+ if (!definedVariables.isEmpty) {
+ DeclaredVariables declaredVariables = context.declaredVariables;
+ definedVariables.forEach((String variableName, String value) {
+ declaredVariables.define(variableName, value);
+ });
+ }
+
+ if (options.log) {
+ AnalysisEngine.instance.logger = new StdLogger();
+ }
+
+ // Prepare context options.
+ AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl();
+ contextOptions.hint = !options.disableHints;
+ contextOptions.enableStrictCallChecks = options.enableStrictCallChecks;
+ contextOptions.enableSuperMixins = options.enableSuperMixins;
+ contextOptions.generateImplicitErrors = options.showPackageWarnings;
+ contextOptions.generateSdkErrors = options.showSdkWarnings;
+ contextOptions.lint = options.lints;
+ contextOptions.strongMode = options.strongMode;
+ configureContextOptions(contextOptions);
+
+ // Set context options.
+ context.analysisOptions = contextOptions;
+ context.sourceFactory.dartSdk.context.analysisOptions = contextOptions;
+
+ // Process analysis options file (and notify all interested parties).
+ _processAnalysisOptions(context, options);
+ }
+
/// Perform a deep comparison of two string maps.
static bool _equalMaps(Map<String, String> m1, Map<String, String> m2) {
if (m1.length != m2.length) {
@@ -624,9 +606,53 @@ class Driver implements CommandLineStarter {
return true;
}
+ static fileSystem.File _getOptionsFile(CommandLineOptions options) {
+ fileSystem.File file;
+ String filePath = options.analysisOptionsFile;
+ if (filePath != null) {
+ file = PhysicalResourceProvider.INSTANCE.getFile(filePath);
+ if (!file.exists) {
+ printAndFail('Options file not found: $filePath',
+ exitCode: ErrorSeverity.ERROR.ordinal);
+ }
+ } else {
+ filePath = AnalysisEngine.ANALYSIS_OPTIONS_FILE;
+ file = PhysicalResourceProvider.INSTANCE.getFile(filePath);
+ }
+ return file;
+ }
+
/// Convert [sourcePath] into an absolute path.
static String _normalizeSourcePath(String sourcePath) =>
path.normalize(new File(sourcePath).absolute.path);
+
+ static void _processAnalysisOptions(
+ AnalysisContext context, CommandLineOptions options) {
+ fileSystem.File file = _getOptionsFile(options);
+ List<OptionsProcessor> optionsProcessors =
+ AnalysisEngine.instance.optionsPlugin.optionsProcessors;
+ try {
+ AnalysisOptionsProvider analysisOptionsProvider =
+ new AnalysisOptionsProvider();
+ Map<String, YamlNode> optionMap =
+ analysisOptionsProvider.getOptionsFromFile(file);
+ optionsProcessors.forEach(
+ (OptionsProcessor p) => p.optionsProcessed(context, optionMap));
+
+ // Fill in lint rule defaults in case lints are enabled and rules are
+ // not specified in an options file.
+ if (options.lints && !containsLintRuleEntry(optionMap)) {
+ setLints(context, linterPlugin.contributedRules);
+ }
+
+ // Ask engine to further process options.
+ if (optionMap != null) {
+ configureContextOptions(context, optionMap);
+ }
+ } on Exception catch (e) {
+ optionsProcessors.forEach((OptionsProcessor p) => p.onError(e));
+ }
+ }
}
/// Provides a framework to read command line options from stdin and feed them

Powered by Google App Engine
This is Rietveld 408576698