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

Unified Diff: pkg/analysis_server/lib/src/sdk_ext.dart

Issue 1213613017: Add support for .sdkext file to analysis server (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 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
Index: pkg/analysis_server/lib/src/sdk_ext.dart
diff --git a/pkg/analysis_server/lib/src/sdk_ext.dart b/pkg/analysis_server/lib/src/sdk_ext.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bcd4c4e98c9489d63d08f1a85df0f81f0f8ae992
--- /dev/null
+++ b/pkg/analysis_server/lib/src/sdk_ext.dart
@@ -0,0 +1,115 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// 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.
+
+library analysis_server.src.sdk_ext;
+
+import 'dart:async';
+import 'dart:collection';
+import 'dart:convert';
+import 'dart:core' hide Resource;
+
+import 'package:analysis_server/uri/resolver_provider.dart';
+import 'package:analyzer/file_system/file_system.dart';
+import 'package:analyzer/source/package_map_resolver.dart';
+import 'package:analyzer/src/generated/source.dart';
+import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
+import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource;
+
+/// Given a packageMap (see [PackageMapProvider]), check in each package's lib
+/// directory for the existence of a `.sdkext` file. This file must contain a
+/// JSON encoded map. Each key in the map is a `dart:` library name. Each value
+/// is a path (relative to the directory containing `.sdkext`) to a dart script
+/// for the given library. For example:
+/// {
+/// "dart:sky": "../sdk_ext/dart_sky.dart"
+/// }
+///
+/// If a key doesn't begin with `dart:` it is ignored.
+class SdkExtUriResolver extends UriResolver {
Brian Wilkerson 2015/07/07 22:10:18 The lack of an implementation of restoreAbsolute w
Cutch 2015/07/08 14:18:17 I've implemented this and added tests but I'll not
Brian Wilkerson 2015/07/08 14:48:28 It's used in 5 places in the analysis server. I'm
+ static const String DOT_SDK_EXT_NAME = '.sdkext';
+ static const String DART_COLON_PREFIX = 'dart:';
+
+ final Map<String, String> _urlMappings = <String,String>{};
+
+ /// Construct a [SdkExtUriResolver] from a package map
+ /// (see [PackageMapProvider]).
+ SdkExtUriResolver(Map<String, List<Folder>> packageMap) {
+ if (packageMap == null) {
+ return;
+ }
+ packageMap.forEach(_processPackage);
+ }
+
+ /// Programmatically add a new SDK extension given a JSON description
+ /// ([sdkExtJSON]) and a lib directory ([libDir]).
+ void addSdkExt(String sdkExtJSON, Folder libDir) {
+ _processSdkExt(sdkExtJSON, libDir);
+ }
+
+ /// Return the path mapping for [libName] or null if there is none.
+ String operator[](String libName) => _urlMappings[libName];
+
+ /// Number of sdk extensions.
+ int get length => _urlMappings.length;
+
+ @override
+ Source resolveAbsolute(Uri uri) {
+ String mapping = _urlMappings[uri.toString()];
+ if (mapping == null) return null;
+ Uri fileUri = new Uri.file(mapping);
+ if (!fileUri.isAbsolute) return null;
+
+ JavaFile javaFile = new JavaFile.fromUri(fileUri);
+ return new FileBasedSource(javaFile, uri);
+ }
+
+ /// Given a package [name] and a list of folders ([libDirs]),
+ /// add any found sdk extensions.
+ void _processPackage(String name, List<Folder> libDirs) {
+ for (var libDir in libDirs) {
+ var sdkExt = _readDotSdkExt(libDir);
+ if (sdkExt != null) {
+ _processSdkExt(sdkExt, libDir);
+ }
+ }
+ }
+
+ /// Read the contents of [libDir]/[DOT_SDK_EXT_NAME] as a string.
+ /// Returns null if the file doesn't exist.
+ String _readDotSdkExt(Folder libDir) {
+ var file = libDir.getChild(DOT_SDK_EXT_NAME);
+ try {
+ return file.readAsStringSync();
+ } on FileSystemException catch (e) {
+ // File can't be read.
+ return null;
+ }
+ }
+
+ /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder
+ /// ([libDir]), setup the uri mapping.
+ void _processSdkExt(String sdkExtJSON, Folder libDir) {
+ var sdkExt;
+ try {
+ sdkExt = JSON.decode(sdkExtJSON);
+ } catch (e) {
+ return;
+ }
+ if ((sdkExt == null) || (sdkExt is! Map)) {
+ return;
+ }
+ sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
+ }
+
+ /// Install the mapping from [name] to [libDir]/[file].
+ void _processSdkExtension(String name, String file, Folder libDir) {
+ if (!name.startsWith(DART_COLON_PREFIX)) {
+ // SDK extensions must begin with 'dart:'.
+ return;
+ }
+ var key = name;
+ var value = libDir.canonicalizePath(file);
+ _urlMappings[key] = value;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698