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

Unified Diff: lib/src/analyzer/context.dart

Issue 2188033002: fix #607, add support for package config file Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: remove obsolete todo Created 4 years, 5 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
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/analyzer/context.dart
diff --git a/lib/src/analyzer/context.dart b/lib/src/analyzer/context.dart
index d418c4209e1ae28a406c3870c7460c8a44af29b6..52dae3017b8cf78053a1c0ef14b9d587be6eab5c 100644
--- a/lib/src/analyzer/context.dart
+++ b/lib/src/analyzer/context.dart
@@ -3,6 +3,7 @@
// for details. 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:io' show Directory, File;
import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver;
import 'package:analyzer/file_system/physical_file_system.dart'
show PhysicalResourceProvider;
@@ -26,6 +27,10 @@ import 'package:analyzer/src/summary/package_bundle_reader.dart'
SummaryDataStore;
import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk;
import 'package:cli_util/cli_util.dart' show getSdkDir;
+import 'package:package_config/discovery.dart' show findPackagesFromFile;
+import 'package:package_config/packages.dart' show Packages;
+import 'package:package_config/packages_file.dart' as pkgfile show parse;
+import 'package:package_config/src/packages_impl.dart';
import 'package:path/path.dart' as path;
import 'multi_package_resolver.dart' show MultiPackageResolver;
@@ -35,9 +40,14 @@ class AnalyzerOptions {
/// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart"
final Map<String, String> customUrlMappings;
- /// Package root when resolving 'package:' urls the standard way.
+ /// Package root directory when resolving 'package:' urls, mutually exclusive
+ /// with [packageConfig].
final String packageRoot;
+ /// Package spec file, typically called `.packages`, mutually exclusive with
+ /// [packageRoot].
+ final String packageConfig;
+
/// List of summary file paths.
final List<String> summaryPaths;
@@ -57,7 +67,8 @@ class AnalyzerOptions {
String dartSdkPath,
this.dartSdkSummaryPath,
this.customUrlMappings: const {},
- this.packageRoot: 'packages/',
+ this.packageRoot,
+ this.packageConfig,
this.packagePaths: const []})
: dartSdkPath = dartSdkPath ?? getSdkDir().path;
@@ -67,6 +78,7 @@ class AnalyzerOptions {
dartSdkSummaryPath = args['dart-sdk-summary'],
customUrlMappings = _parseUrlMappings(args['url-mapping']),
packageRoot = args['package-root'],
+ packageConfig = args['packages'],
packagePaths = (args['package-paths'] as String)?.split(',') ?? [];
/// Whether to resolve 'package:' uris using the multi-package resolver.
@@ -79,10 +91,14 @@ class AnalyzerOptions {
..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null)
..addOption('dart-sdk-summary',
help: 'Dart SDK Summary Path', defaultsTo: null)
+ ..addOption('packages',
+ help: 'Path to the package resolution configuration file, which '
+ 'supplies a mapping of package names to paths. This option '
+ 'cannot be used with --package-root.')
nweiz 2016/07/27 23:15:11 It would be good to explicitly throw an error if b
..addOption('package-root',
abbr: 'p',
- help: 'Package root to resolve "package:" imports',
- defaultsTo: 'packages/')
+ help: 'Path to a package root directory (deprecated). This option '
+ 'cannot be used with --packages.')
..addOption('url-mapping',
help: '--url-mapping=libraryUri,/path/to/library.dart uses \n'
'library.dart as the source for an import of of "libraryUri".',
@@ -107,7 +123,9 @@ class AnalyzerOptions {
/// Creates an [AnalysisContext] with dev_compiler type rules and inference,
/// using [createSourceFactory] to set up its [SourceFactory].
AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options,
- {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) {
+ {DartUriResolver sdkResolver,
+ List<UriResolver> fileResolvers,
+ Packages packages}) {
AnalysisEngine.instance.processRequiredPlugins();
sdkResolver ??=
@@ -122,6 +140,7 @@ AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options,
var srcFactory = _createSourceFactory(options,
sdkResolver: sdkResolver,
fileResolvers: fileResolvers,
+ packages: packages,
summaryData: summaryData);
var context = createAnalysisContext();
@@ -153,6 +172,7 @@ AnalysisContextImpl createAnalysisContext() {
SourceFactory _createSourceFactory(AnalyzerOptions options,
{DartUriResolver sdkResolver,
List<UriResolver> fileResolvers,
+ Packages packages,
SummaryDataStore summaryData}) {
var resolvers = <UriResolver>[];
if (options.customUrlMappings.isNotEmpty) {
@@ -163,18 +183,27 @@ SourceFactory _createSourceFactory(AnalyzerOptions options,
resolvers.add(new InSummaryPackageUriResolver(summaryData));
}
- if (fileResolvers == null) fileResolvers = createFileResolvers(options);
- resolvers.addAll(fileResolvers);
- return new SourceFactory(resolvers);
-}
-
-List<UriResolver> createFileResolvers(AnalyzerOptions options) {
- return [
- new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
- options.useMultiPackage
- ? new MultiPackageResolver(options.packagePaths)
- : new PackageUriResolver([new JavaFile(options.packageRoot)])
- ];
+ if (fileResolvers == null) {
+ resolvers.add(new ResourceUriResolver(PhysicalResourceProvider.INSTANCE));
+ } else {
+ resolvers.addAll(fileResolvers);
+ }
+ if (packages == null) {
+ if (options.useMultiPackage) {
+ resolvers.add(new MultiPackageResolver(options.packagePaths));
+ } else if (options.packageConfig != null) {
+ var f = new File(options.packageConfig).absolute;
+ packages = new MapPackages(pkgfile.parse(f.readAsBytesSync(), f.uri));
+ } else if (options.packageRoot != null) {
+ resolvers
+ .add(new PackageUriResolver([new JavaFile(options.packageRoot)]));
+ } else {
+ // TODO(jmesserly): should we use the input file paths or base directory
+ // instead?
+ packages = findPackagesFromFile(Directory.current.uri);
Jennifer Messerly 2016/07/27 22:56:16 is using .packages the right default behavior now?
nweiz 2016/07/27 23:15:11 I believe the default is: * If a packages/ direct
Brian Wilkerson 2016/07/29 17:07:40 Almost. A '.packages' file in the initial director
+ }
+ }
+ return new SourceFactory(resolvers, packages);
}
DirectoryBasedDartSdk _createDirectoryBasedDartSdk(String sdkPath) {
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698