Chromium Code Reviews| 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..f7f5f72c6e8ad7136fe86894ab36681d242fce3e |
| --- /dev/null |
| +++ b/pkg/analysis_server/lib/src/sdk_ext.dart |
| @@ -0,0 +1,96 @@ |
| +// 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 sdk.ext; |
|
Brian Wilkerson
2015/07/07 19:59:12
nit: I think following the library naming conventi
Cutch
2015/07/07 20:33:47
Done.
|
| + |
| +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'; |
| + |
| +// Given a packageMap (from a PackageMapProvider), check in each package's lib |
|
Brian Wilkerson
2015/07/07 19:59:11
nit: The comments should be valid dartdoc comments
Cutch
2015/07/07 20:33:47
Done.
|
| +// 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 the filename 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 { |
| + static const String DOT_SDK_EXT_NAME = '.sdkext'; |
| + static const String DART_COLON_PREFIX = 'dart:'; |
| + |
| + final Map<String, String> _urlMappings = <String,String>{}; |
| + |
| + SdkExtUriResolver(Map<String, List<Folder>> packageMap) { |
| + if (packageMap == null) { |
| + return; |
| + } |
| + packageMap.forEach(_processPackage); |
| + } |
| + |
| + @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); |
|
Brian Wilkerson
2015/07/07 19:59:11
This needs to be "new FileBasedSource(javaFile, ur
Cutch
2015/07/07 20:33:48
Done.
|
| + } |
| + |
| + // Given a package name and a list of folders, add any 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 and a folder, 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)); |
| + } |
| + |
| + 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; |
| + } |
| +} |