| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library source.sdk_ext; | 5 library source.sdk_ext; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 import 'dart:core' hide Resource; | 10 import 'dart:core' hide Resource; |
| 11 | 11 |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 17 import 'package:path/path.dart' as pathos; | 17 import 'package:path/path.dart' as pathos; |
| 18 | 18 |
| 19 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib | 19 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib |
| 20 /// directory for the existence of a `.sdkext` file. This file must contain a | 20 /// directory for the existence of a `_sdkext` file. This file must contain a |
| 21 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value | 21 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value |
| 22 /// is a path (relative to the directory containing `.sdkext`) to a dart script | 22 /// is a path (relative to the directory containing `_sdkext`) to a dart script |
| 23 /// for the given library. For example: | 23 /// for the given library. For example: |
| 24 /// { | 24 /// { |
| 25 /// "dart:sky": "../sdk_ext/dart_sky.dart" | 25 /// "dart:sky": "../sdk_ext/dart_sky.dart" |
| 26 /// } | 26 /// } |
| 27 /// | 27 /// |
| 28 /// If a key doesn't begin with `dart:` it is ignored. | 28 /// If a key doesn't begin with `dart:` it is ignored. |
| 29 class SdkExtUriResolver extends UriResolver { | 29 class SdkExtUriResolver extends UriResolver { |
| 30 static const String DOT_SDK_EXT_NAME = '.sdkext'; | 30 static const String SDK_EXT_NAME = '_sdkext'; |
| 31 static const String DART_COLON_PREFIX = 'dart:'; | 31 static const String DART_COLON_PREFIX = 'dart:'; |
| 32 | 32 |
| 33 final Map<String, String> _urlMappings = <String,String>{}; | 33 final Map<String, String> _urlMappings = <String,String>{}; |
| 34 | 34 |
| 35 /// Construct a [SdkExtUriResolver] from a package map | 35 /// Construct a [SdkExtUriResolver] from a package map |
| 36 /// (see [PackageMapProvider]). | 36 /// (see [PackageMapProvider]). |
| 37 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { | 37 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { |
| 38 if (packageMap == null) { | 38 if (packageMap == null) { |
| 39 return; | 39 return; |
| 40 } | 40 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 /// add any found sdk extensions. | 132 /// add any found sdk extensions. |
| 133 void _processPackage(String name, List<Folder> libDirs) { | 133 void _processPackage(String name, List<Folder> libDirs) { |
| 134 for (var libDir in libDirs) { | 134 for (var libDir in libDirs) { |
| 135 var sdkExt = _readDotSdkExt(libDir); | 135 var sdkExt = _readDotSdkExt(libDir); |
| 136 if (sdkExt != null) { | 136 if (sdkExt != null) { |
| 137 _processSdkExt(sdkExt, libDir); | 137 _processSdkExt(sdkExt, libDir); |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 /// Read the contents of [libDir]/[DOT_SDK_EXT_NAME] as a string. | 142 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string. |
| 143 /// Returns null if the file doesn't exist. | 143 /// Returns null if the file doesn't exist. |
| 144 String _readDotSdkExt(Folder libDir) { | 144 String _readDotSdkExt(Folder libDir) { |
| 145 var file = libDir.getChild(DOT_SDK_EXT_NAME); | 145 var file = libDir.getChild(SDK_EXT_NAME); |
| 146 try { | 146 try { |
| 147 return file.readAsStringSync(); | 147 return file.readAsStringSync(); |
| 148 } on FileSystemException catch (e) { | 148 } on FileSystemException catch (e) { |
| 149 // File can't be read. | 149 // File can't be read. |
| 150 return null; | 150 return null; |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 | 153 |
| 154 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder | 154 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder |
| 155 /// ([libDir]), setup the uri mapping. | 155 /// ([libDir]), setup the uri mapping. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 170 void _processSdkExtension(String name, String file, Folder libDir) { | 170 void _processSdkExtension(String name, String file, Folder libDir) { |
| 171 if (!name.startsWith(DART_COLON_PREFIX)) { | 171 if (!name.startsWith(DART_COLON_PREFIX)) { |
| 172 // SDK extensions must begin with 'dart:'. | 172 // SDK extensions must begin with 'dart:'. |
| 173 return; | 173 return; |
| 174 } | 174 } |
| 175 var key = name; | 175 var key = name; |
| 176 var value = libDir.canonicalizePath(file); | 176 var value = libDir.canonicalizePath(file); |
| 177 _urlMappings[key] = value; | 177 _urlMappings[key] = value; |
| 178 } | 178 } |
| 179 } | 179 } |
| OLD | NEW |