Support for Bazel workspaces in ContextBuilder.

This replaces custom fileResolverProvider and packageResolverProvider
that were used for the same purpose internally.

R=brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org/2405953002 .
This commit is contained in:
Konstantin Shcheglov 2016-10-10 13:51:50 -07:00
parent eaea9d58fe
commit 4c05421229
2 changed files with 29 additions and 57 deletions

View file

@ -14,6 +14,7 @@ import 'package:analyzer/plugin/resolver_provider.dart';
import 'package:analyzer/source/analysis_options_provider.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/bazel.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
@ -70,12 +71,14 @@ class ContextBuilder {
* The resolver provider used to create a package: URI resolver, or `null` if
* the normal (Package Specification DEP) lookup mechanism is to be used.
*/
@deprecated
ResolverProvider packageResolverProvider;
/**
* The resolver provider used to create a file: URI resolver, or `null` if
* the normal file URI resolver is to be used.
*/
@deprecated
ResolverProvider fileResolverProvider;
/**
@ -220,38 +223,24 @@ class ContextBuilder {
return findPackagesFromFile(rootDirectoryPath);
}
SourceFactory createSourceFactory(
String rootDirectoryPath, AnalysisOptions options) {
Folder _folder = null;
Folder folder() {
return _folder ??= resourceProvider.getFolder(rootDirectoryPath);
SourceFactory createSourceFactory(String rootPath, AnalysisOptions options) {
BazelWorkspace bazelWorkspace =
BazelWorkspace.find(resourceProvider, rootPath);
if (bazelWorkspace != null) {
List<UriResolver> resolvers = <UriResolver>[
new DartUriResolver(findSdk(null, options)),
new BazelPackageUriResolver(bazelWorkspace),
new BazelFileUriResolver(bazelWorkspace)
];
return new SourceFactory(resolvers, null, resourceProvider);
}
UriResolver fileResolver;
if (fileResolverProvider != null) {
fileResolver = fileResolverProvider(folder());
}
fileResolver ??= new ResourceUriResolver(resourceProvider);
if (packageResolverProvider != null) {
UriResolver packageResolver = packageResolverProvider(folder());
if (packageResolver != null) {
// TODO(brianwilkerson) This doesn't support either embedder files or
// sdk extensions because we don't have a way to get the package map
// from the resolver.
List<UriResolver> resolvers = <UriResolver>[
new DartUriResolver(findSdk(null, options)),
packageResolver,
fileResolver
];
return new SourceFactory(resolvers, null, resourceProvider);
}
}
Packages packages = createPackageMap(rootDirectoryPath);
Packages packages = createPackageMap(rootPath);
Map<String, List<Folder>> packageMap = convertPackagesToMap(packages);
List<UriResolver> resolvers = <UriResolver>[
new DartUriResolver(findSdk(packageMap, options)),
new PackageMapUriResolver(resourceProvider, packageMap),
fileResolver
new ResourceUriResolver(resourceProvider)
];
return new SourceFactory(resolvers, packages, resourceProvider);
}

View file

@ -7,9 +7,9 @@ library analyzer.test.src.context.context_builder_test;
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:analyzer/plugin/options.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/src/context/builder.dart';
import 'package:analyzer/src/context/source.dart';
import 'package:analyzer/src/generated/bazel.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
@ -273,26 +273,22 @@ bar:$barUri
expect(packages, same(Packages.noPackages));
}
void test_createSourceFactory_fileProvider() {
String rootPath = resourceProvider.convertPath('/root');
Folder rootFolder = resourceProvider.getFolder(rootPath);
createDefaultSdk(rootFolder);
String projectPath = pathContext.join(rootPath, 'project');
String packageFilePath = pathContext.join(projectPath, '.packages');
String packageA = pathContext.join(rootPath, 'pkgs', 'a');
String packageB = pathContext.join(rootPath, 'pkgs', 'b');
createFile(
packageFilePath,
'''
a:${pathContext.toUri(packageA)}
b:${pathContext.toUri(packageB)}
''');
void test_createSourceFactory_bazelWorkspace_fileProvider() {
String _p(String path) => resourceProvider.convertPath(path);
String projectPath = _p('/workspace/my/module');
resourceProvider.newFile(_p('/workspace/WORKSPACE'), '');
resourceProvider.newFolder(_p('/workspace/bazel-bin'));
resourceProvider.newFolder(_p('/workspace/bazel-genfiles'));
resourceProvider.newFolder(projectPath);
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
UriResolver resolver = new ResourceUriResolver(resourceProvider);
builder.fileResolverProvider = (folder) => resolver;
SourceFactoryImpl factory =
builder.createSourceFactory(projectPath, options);
expect(factory.resolvers, contains(same(resolver)));
expect(factory.resolvers,
contains(predicate((r) => r is BazelFileUriResolver)));
expect(factory.resolvers,
contains(predicate((r) => r is BazelPackageUriResolver)));
}
void test_createSourceFactory_noProvider_packages_embedder_extensions() {
@ -413,19 +409,6 @@ b:${pathContext.toUri(packageB)}
expect(packageSource.fullName, pathContext.join(packageA, 'a.dart'));
}
void test_createSourceFactory_packageProvider() {
String rootPath = resourceProvider.convertPath('/root');
Folder rootFolder = resourceProvider.getFolder(rootPath);
createDefaultSdk(rootFolder);
String projectPath = pathContext.join(rootPath, 'project');
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
UriResolver resolver = new PackageMapUriResolver(resourceProvider, {});
builder.packageResolverProvider = (folder) => resolver;
SourceFactoryImpl factory =
builder.createSourceFactory(projectPath, options);
expect(factory.resolvers, contains(same(resolver)));
}
void test_declareVariables_emptyMap() {
AnalysisContext context = AnalysisEngine.instance.createAnalysisContext();
Iterable<String> expected = context.declaredVariables.variableNames;