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

Unified Diff: pkg/analyzer/lib/src/generated/sdk_io.dart

Issue 2069483002: Refactor common portions of the two SDK implementations into a base class (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: clean-up Created 4 years, 6 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 | « pkg/analyzer/lib/source/embedder.dart ('k') | pkg/analyzer/test/source/embedder_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/generated/sdk_io.dart
diff --git a/pkg/analyzer/lib/src/generated/sdk_io.dart b/pkg/analyzer/lib/src/generated/sdk_io.dart
index 0d053f6b1f2e5b2cf6a60c9705a35b802a2c5dda..acdc3ab11fce604592a26c79bbdcdde5735a1499 100644
--- a/pkg/analyzer/lib/src/generated/sdk_io.dart
+++ b/pkg/analyzer/lib/src/generated/sdk_io.dart
@@ -25,6 +25,205 @@ import 'package:analyzer/src/summary/summary_sdk.dart';
import 'package:path/path.dart' as pathos;
/**
+ * An abstract implementation of a Dart SDK in which the available libraries are
+ * stored in a library map. Subclasses are responsible for populating the
+ * library map.
+ */
+abstract class AbstractDartSdk implements DartSdk {
+ /**
+ * A mapping from Dart library URI's to the library represented by that URI.
+ */
+ LibraryMap libraryMap = new LibraryMap();
pquitslund 2016/06/14 17:50:59 Any reason not to make this final?
Brian Wilkerson 2016/06/14 20:53:03 Yes. Unless we change it, DirectoryBasedDartSdk cr
+
+ /**
+ * The [AnalysisOptions] to use to create the [context].
+ */
+ AnalysisOptions _analysisOptions;
+
+ /**
+ * The flag that specifies whether an SDK summary should be used. This is a
+ * temporary flag until summaries are enabled by default.
+ */
+ bool _useSummary = false;
+
+ /**
+ * The [AnalysisContext] which is used for all of the sources in this sdk.
pquitslund 2016/06/14 17:50:59 sdk => SDK ?
Brian Wilkerson 2016/06/14 20:53:03 Done
+ */
+ InternalAnalysisContext _analysisContext;
+
+ /**
+ * The mapping from Dart URI's to the corresponding sources.
+ */
+ Map<String, Source> _uriToSourceMap = new HashMap<String, Source>();
+
+ /**
+ * Set the [options] for this SDK analysis context. Throw [StateError] if the
+ * context has been already created.
+ */
+ void set analysisOptions(AnalysisOptions options) {
+ if (_analysisContext != null) {
+ throw new StateError(
+ 'Analysis options cannot be changed after context creation.');
+ }
+ _analysisOptions = options;
+ }
+
+ @override
+ AnalysisContext get context {
+ if (_analysisContext == null) {
+ _analysisContext = new SdkAnalysisContext(_analysisOptions);
+ SourceFactory factory = new SourceFactory([new DartUriResolver(this)]);
+ _analysisContext.sourceFactory = factory;
+ if (_useSummary) {
+ bool strongMode = _analysisOptions?.strongMode ?? false;
+ PackageBundle sdkBundle = getSummarySdkBundle(strongMode);
+ if (sdkBundle != null) {
+ _analysisContext.resultProvider = new SdkSummaryResultProvider(
+ _analysisContext, sdkBundle, strongMode);
+ }
+ }
+ }
+ return _analysisContext;
+ }
+
+ @override
+ List<SdkLibrary> get sdkLibraries => libraryMap.sdkLibraries;
+
+ @override
+ List<String> get uris => libraryMap.uris;
+
+ /**
+ * Return `true` if the SDK summary will be used when available.
+ */
+ bool get useSummary => _useSummary;
+
+ /**
+ * Specify whether SDK summary should be used.
+ */
+ void set useSummary(bool use) {
+ if (_analysisContext != null) {
+ throw new StateError(
+ 'The "useSummary" flag cannot be changed after context creation.');
+ }
+ _useSummary = use;
+ }
+
+ @override
+ Source fromFileUri(Uri uri) {
+ JavaFile file = new JavaFile.fromUri(uri);
+
+ String path = _getPath(file);
+ if (path == null) {
+ return null;
+ }
+ try {
+ return new FileBasedSource(file, parseUriWithException(path));
+ } on URISyntaxException catch (exception, stackTrace) {
+ AnalysisEngine.instance.logger.logInformation(
+ "Failed to create URI: $path",
+ new CaughtException(exception, stackTrace));
+ }
+ return null;
+ }
+
+ String getRelativePathFromFile(JavaFile file);
+
+ @override
+ SdkLibrary getSdkLibrary(String dartUri) => libraryMap.getLibrary(dartUri);
+
+ /**
+ * Return the [PackageBundle] for this SDK, if it exists, or `null` otherwise.
+ * This method should not be used outside of `analyzer` and `analyzer_cli`
+ * packages.
+ */
+ PackageBundle getSummarySdkBundle(bool strongMode);
+
+ FileBasedSource internalMapDartUri(String dartUri) {
+ // TODO(brianwilkerson) Figure out how to unify the implementations in the
+ // two subclasses.
+ String libraryName;
+ String relativePath;
+ int index = dartUri.indexOf('/');
+ if (index >= 0) {
+ libraryName = dartUri.substring(0, index);
+ relativePath = dartUri.substring(index + 1);
+ } else {
+ libraryName = dartUri;
+ relativePath = "";
+ }
+ SdkLibrary library = getSdkLibrary(libraryName);
+ if (library == null) {
+ return null;
+ }
+ String srcPath;
+ if (relativePath.isEmpty) {
+ srcPath = library.path;
+ } else {
+ String libraryPath = library.path;
+ int index = libraryPath.lastIndexOf(JavaFile.separator);
+ if (index == -1) {
+ index = libraryPath.lastIndexOf('/');
+ if (index == -1) {
+ return null;
+ }
+ }
+ String prefix = libraryPath.substring(0, index + 1);
+ srcPath = '$prefix$relativePath';
+ }
+ String filePath = srcPath.replaceAll('/', JavaFile.separator);
+ try {
+ JavaFile file = new JavaFile(filePath);
+ return new FileBasedSource(file, parseUriWithException(dartUri));
+ } on URISyntaxException {
+ return null;
+ }
+ }
+
+ @override
+ Source mapDartUri(String dartUri) {
+ Source source = _uriToSourceMap[dartUri];
+ if (source == null) {
+ source = internalMapDartUri(dartUri);
+ _uriToSourceMap[dartUri] = source;
+ }
+ return source;
+ }
+
+ String _getPath(JavaFile file) {
+ List<SdkLibrary> libraries = libraryMap.sdkLibraries;
+ int length = libraries.length;
+ List<String> paths = new List(length);
+ String filePath = getRelativePathFromFile(file);
+ if (filePath == null) {
+ return null;
+ }
+ for (int i = 0; i < length; i++) {
+ SdkLibrary library = libraries[i];
+ String libraryPath = library.path.replaceAll('/', JavaFile.separator);
+ if (filePath == libraryPath) {
+ return library.shortName;
+ }
+ paths[i] = libraryPath;
+ }
+ for (int i = 0; i < length; i++) {
+ SdkLibrary library = libraries[i];
+ String libraryPath = paths[i];
+ int index = libraryPath.lastIndexOf(JavaFile.separator);
+ if (index >= 0) {
+ String prefix = libraryPath.substring(0, index + 1);
+ if (filePath.startsWith(prefix)) {
+ String relPath = filePath
+ .substring(prefix.length)
+ .replaceAll(JavaFile.separator, '/');
+ return '${library.shortName}/$relPath';
+ }
+ }
+ }
+ return null;
+ }
+}
+
+/**
* A Dart SDK installed in a specified directory. Typical Dart SDK layout is
* something like...
*
@@ -40,7 +239,7 @@ import 'package:path/path.dart' as pathos;
* ... Dart utilities ...
* Chromium/ <-- Dartium typically exists in a sibling directory
*/
-class DirectoryBasedDartSdk implements DartSdk {
+class DirectoryBasedDartSdk extends AbstractDartSdk {
/**
* The default SDK, or `null` if the default SDK either has not yet been
* created or cannot be created for some reason.
@@ -193,11 +392,6 @@ class DirectoryBasedDartSdk implements DartSdk {
}
/**
- * The [AnalysisContext] which is used for all of the sources in this sdk.
- */
- InternalAnalysisContext _analysisContext;
-
- /**
* The directory containing the SDK.
*/
JavaFile _sdkDirectory;
@@ -208,11 +402,6 @@ class DirectoryBasedDartSdk implements DartSdk {
JavaFile _libraryDirectory;
/**
- * The flag that specifies whether SDK summary should be used.
- */
- bool _useSummary = false;
-
- /**
* The revision number of this SDK, or `"0"` if the revision number cannot be
* discovered.
*/
@@ -239,58 +428,13 @@ class DirectoryBasedDartSdk implements DartSdk {
JavaFile _vmExecutable;
/**
- * A mapping from Dart library URI's to the library represented by that URI.
- */
- LibraryMap _libraryMap;
-
- /**
- * The mapping from Dart URI's to the corresponding sources.
- */
- Map<String, Source> _uriToSourceMap = new HashMap<String, Source>();
-
- /**
- * The [AnalysisOptions] to use to create the [context].
- */
- AnalysisOptions _analysisOptions;
-
- /**
* Initialize a newly created SDK to represent the Dart SDK installed in the
* [sdkDirectory]. The flag [useDart2jsPaths] is `true` if the dart2js path
* should be used when it is available
*/
DirectoryBasedDartSdk(JavaFile sdkDirectory, [bool useDart2jsPaths = false]) {
this._sdkDirectory = sdkDirectory.getAbsoluteFile();
- _libraryMap = initialLibraryMap(useDart2jsPaths);
- }
-
- /**
- * Set the [options] for this SDK analysis context. Throw [StateError] if the
- * context has been already created.
- */
- void set analysisOptions(AnalysisOptions options) {
- if (_analysisContext != null) {
- throw new StateError(
- 'Analysis options cannot be changed after context creation.');
- }
- _analysisOptions = options;
- }
-
- @override
- AnalysisContext get context {
- if (_analysisContext == null) {
- _analysisContext = new SdkAnalysisContext(_analysisOptions);
- SourceFactory factory = new SourceFactory([new DartUriResolver(this)]);
- _analysisContext.sourceFactory = factory;
- if (_useSummary) {
- PackageBundle sdkBundle = getSummarySdkBundle();
- if (sdkBundle != null) {
- bool strongMode = _analysisOptions?.strongMode ?? false;
- _analysisContext.resultProvider = new SdkSummaryResultProvider(
- _analysisContext, sdkBundle, strongMode);
- }
- }
- }
- return _analysisContext;
+ libraryMap = initialLibraryMap(useDart2jsPaths);
}
/**
@@ -386,9 +530,6 @@ class DirectoryBasedDartSdk implements DartSdk {
return _pubExecutable;
}
- @override
- List<SdkLibrary> get sdkLibraries => _libraryMap.sdkLibraries;
-
/**
* Return the revision number of this SDK, or `"0"` if the revision number
* cannot be discovered.
@@ -411,25 +552,6 @@ class DirectoryBasedDartSdk implements DartSdk {
return _sdkVersion;
}
- @override
- List<String> get uris => _libraryMap.uris;
-
- /**
- * Whether an SDK summary should be used.
- */
- bool get useSummary => _useSummary;
-
- /**
- * Specify whether SDK summary should be used.
- */
- void set useSummary(bool use) {
- if (_analysisContext != null) {
- throw new StateError(
- 'The "useSummary" flag cannot be changed after context creation.');
- }
- _useSummary = use;
- }
-
/**
* Return the name of the file containing the VM executable.
*/
@@ -470,45 +592,6 @@ class DirectoryBasedDartSdk implements DartSdk {
_LIBRARIES_FILE);
}
- @override
- Source fromFileUri(Uri uri) {
- JavaFile file = new JavaFile.fromUri(uri);
- String filePath = file.getAbsolutePath();
- String libPath = libraryDirectory.getAbsolutePath();
- if (!filePath.startsWith("$libPath${JavaFile.separator}")) {
- return null;
- }
- filePath = filePath.substring(libPath.length + 1);
- for (SdkLibrary library in _libraryMap.sdkLibraries) {
- String libraryPath = library.path;
- if (filePath.replaceAll('\\', '/') == libraryPath) {
- String path = library.shortName;
- try {
- return new FileBasedSource(file, parseUriWithException(path));
- } on URISyntaxException catch (exception, stackTrace) {
- AnalysisEngine.instance.logger.logInformation(
- "Failed to create URI: $path",
- new CaughtException(exception, stackTrace));
- return null;
- }
- }
- libraryPath = new JavaFile(library.path).getParent();
- if (filePath.startsWith("$libraryPath${JavaFile.separator}")) {
- String path =
- "${library.shortName}/${filePath.substring(libraryPath.length + 1)}";
- try {
- return new FileBasedSource(file, parseUriWithException(path));
- } on URISyntaxException catch (exception, stackTrace) {
- AnalysisEngine.instance.logger.logInformation(
- "Failed to create URI: $path",
- new CaughtException(exception, stackTrace));
- return null;
- }
- }
- }
- return null;
- }
-
/**
* Return the directory where dartium can be found (the directory that will be
* the working directory if Dartium is invoked without changing the default),
@@ -536,16 +619,22 @@ class DirectoryBasedDartSdk implements DartSdk {
}
@override
- SdkLibrary getSdkLibrary(String dartUri) => _libraryMap.getLibrary(dartUri);
+ String getRelativePathFromFile(JavaFile file) {
+ String filePath = file.getAbsolutePath();
+ String libPath = libraryDirectory.getAbsolutePath();
+ if (!filePath.startsWith("$libPath${JavaFile.separator}")) {
+ return null;
+ }
+ return filePath.substring(libPath.length + 1);
+ }
/**
* Return the [PackageBundle] for this SDK, if it exists, or `null` otherwise.
* This method should not be used outside of `analyzer` and `analyzer_cli`
* packages.
*/
- PackageBundle getSummarySdkBundle() {
+ PackageBundle getSummarySdkBundle(bool strongMode) {
String rootPath = directory.getAbsolutePath();
- bool strongMode = _analysisOptions?.strongMode ?? false;
String name = strongMode ? 'strong.sum' : 'spec.sum';
String path = pathos.join(rootPath, 'lib', '_internal', name);
try {
@@ -589,16 +678,7 @@ class DirectoryBasedDartSdk implements DartSdk {
}
@override
- Source mapDartUri(String dartUri) {
- Source source = _uriToSourceMap[dartUri];
- if (source == null) {
- source = _mapDartUri(dartUri);
- _uriToSourceMap[dartUri] = source;
- }
- return source;
- }
-
- FileBasedSource _mapDartUri(String dartUri) {
+ FileBasedSource internalMapDartUri(String dartUri) {
String libraryName;
String relativePath;
int index = dartUri.indexOf('/');
« no previous file with comments | « pkg/analyzer/lib/source/embedder.dart ('k') | pkg/analyzer/test/source/embedder_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698