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 analyzer.source.embedder; | 5 library analyzer.source.embedder; |
6 | 6 |
7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
9 | 9 |
10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 try { | 92 try { |
93 return file.readAsStringSync(); | 93 return file.readAsStringSync(); |
94 } on FileSystemException { | 94 } on FileSystemException { |
95 // File can't be read. | 95 // File can't be read. |
96 return null; | 96 return null; |
97 } | 97 } |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 /// Given the [embedderYamls] from [EmbedderYamlLocator] check each one for the | 101 /// Given the [embedderYamls] from [EmbedderYamlLocator] check each one for the |
102 /// top level key 'embedder_libs'. Under the 'embedder_libs' key are key value | 102 /// top level key 'embedded_libs'. Under the 'embedded_libs' key are key value |
103 /// pairs. Each key is a 'dart:' library uri and each value is a path | 103 /// pairs. Each key is a 'dart:' library uri and each value is a path |
104 /// (relative to the directory containing `_embedder.yaml`) to a dart script | 104 /// (relative to the directory containing `_embedder.yaml`) to a dart script |
105 /// for the given library. For example: | 105 /// for the given library. For example: |
106 /// | 106 /// |
107 /// embedder_libs: | 107 /// embedded_libs: |
108 /// 'dart:io': '../../sdk/io/io.dart' | 108 /// 'dart:io': '../../sdk/io/io.dart' |
109 /// | 109 /// |
110 /// If a key doesn't begin with `dart:` it is ignored. | 110 /// If a key doesn't begin with `dart:` it is ignored. |
111 /// | 111 /// |
112 class EmbedderUriResolver extends DartUriResolver { | 112 class EmbedderUriResolver extends DartUriResolver { |
113 final Map<String, String> _urlMappings = <String, String>{}; | 113 final Map<String, String> _urlMappings = <String, String>{}; |
114 | 114 |
115 /// Construct a [EmbedderUriResolver] from a package map | 115 /// Construct a [EmbedderUriResolver] from a package map |
116 /// (see [PackageMapProvider]). | 116 /// (see [PackageMapProvider]). |
117 EmbedderUriResolver(Map<Folder, YamlMap> embedderYamls) | 117 EmbedderUriResolver(Map<Folder, YamlMap> embedderYamls) |
118 : super(new EmbedderSdk()) { | 118 : super(new EmbedderSdk()) { |
119 (dartSdk as EmbedderSdk)._resolver = this; | 119 (dartSdk as EmbedderSdk)._resolver = this; |
120 if (embedderYamls == null) { | 120 if (embedderYamls == null) { |
121 return; | 121 return; |
122 } | 122 } |
123 embedderYamls.forEach(_processEmbedderYaml); | 123 embedderYamls.forEach(_processEmbedderYaml); |
124 } | 124 } |
125 | 125 |
126 void _processEmbedderYaml(Folder libDir, YamlMap map) { | 126 void _processEmbedderYaml(Folder libDir, YamlMap map) { |
127 YamlNode embedder_libs = map['embedder_libs']; | 127 YamlNode embedded_libs = map['embedded_libs']; |
128 if (embedder_libs == null) { | 128 if (embedded_libs == null) { |
129 return; | 129 return; |
130 } | 130 } |
131 if (embedder_libs is! YamlMap) { | 131 if (embedded_libs is! YamlMap) { |
132 return; | 132 return; |
133 } | 133 } |
134 (embedder_libs as YamlMap) | 134 (embedded_libs as YamlMap) |
135 .forEach((k, v) => _processEmbedderLibs(k, v, libDir)); | 135 .forEach((k, v) => _processEmbeddedLibs(k, v, libDir)); |
136 } | 136 } |
137 | 137 |
138 /// Install the mapping from [name] to [libDir]/[file]. | 138 /// Install the mapping from [name] to [libDir]/[file]. |
139 void _processEmbedderLibs(String name, String file, Folder libDir) { | 139 void _processEmbeddedLibs(String name, String file, Folder libDir) { |
140 if (!name.startsWith(_DART_COLON_PREFIX)) { | 140 if (!name.startsWith(_DART_COLON_PREFIX)) { |
141 // SDK libraries must begin with 'dart:'. | 141 // SDK libraries must begin with 'dart:'. |
142 // TODO(pquitslund): Notify developer that something is wrong with the | 142 // TODO(pquitslund): Notify developer that something is wrong with the |
143 // _embedder.yaml file in libDir. | 143 // _embedder.yaml file in libDir. |
144 return; | 144 return; |
145 } | 145 } |
146 String libPath = libDir.canonicalizePath(file); | 146 String libPath = libDir.canonicalizePath(file); |
147 _urlMappings[name] = libPath; | 147 _urlMappings[name] = libPath; |
148 String shortName = name.substring(_DART_COLON_PREFIX.length); | 148 String shortName = name.substring(_DART_COLON_PREFIX.length); |
149 SdkLibraryImpl library = new SdkLibraryImpl(shortName); | 149 SdkLibraryImpl library = new SdkLibraryImpl(shortName); |
150 library.path = libPath; | 150 library.path = libPath; |
151 (dartSdk as EmbedderSdk)._librariesMap.setLibrary(name, library); | 151 (dartSdk as EmbedderSdk)._librariesMap.setLibrary(name, library); |
152 } | 152 } |
153 | 153 |
154 /// Number of embedder libraries. | 154 /// Number of embedded libraries. |
155 int get length => _urlMappings.length; | 155 int get length => _urlMappings.length; |
156 | 156 |
157 @override | 157 @override |
158 Uri restoreAbsolute(Source source) { | 158 Uri restoreAbsolute(Source source) { |
159 String path = source.fullName; | 159 String path = source.fullName; |
160 if (path.length > 3 && path[1] == ':' && path[2] == '\\') { | 160 if (path.length > 3 && path[1] == ':' && path[2] == '\\') { |
161 path = '/${path[0]}:${path.substring(2).replaceAll('\\', '/')}'; | 161 path = '/${path[0]}:${path.substring(2).replaceAll('\\', '/')}'; |
162 } | 162 } |
163 Source sdkSource = dartSdk.fromFileUri(Uri.parse('file://$path')); | 163 Source sdkSource = dartSdk.fromFileUri(Uri.parse('file://$path')); |
164 return sdkSource?.uri; | 164 return sdkSource?.uri; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 } | 284 } |
285 String filePath = srcPath.replaceAll('/', JavaFile.separator); | 285 String filePath = srcPath.replaceAll('/', JavaFile.separator); |
286 try { | 286 try { |
287 JavaFile file = new JavaFile(filePath); | 287 JavaFile file = new JavaFile(filePath); |
288 return new FileBasedSource(file, parseUriWithException(dartUri)); | 288 return new FileBasedSource(file, parseUriWithException(dartUri)); |
289 } on URISyntaxException { | 289 } on URISyntaxException { |
290 return null; | 290 return null; |
291 } | 291 } |
292 } | 292 } |
293 } | 293 } |
OLD | NEW |