| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 /// existence of an `_embedder.yaml` file. If the file contains a top level | 180 /// existence of an `_embedder.yaml` file. If the file contains a top level |
| 181 /// YamlMap, it will be added to the [embedderYamls] map. | 181 /// YamlMap, it will be added to the [embedderYamls] map. |
| 182 class EmbedderYamlLocator { | 182 class EmbedderYamlLocator { |
| 183 static const String EMBEDDER_FILE_NAME = '_embedder.yaml'; | 183 static const String EMBEDDER_FILE_NAME = '_embedder.yaml'; |
| 184 | 184 |
| 185 /// Map from package's library directory to the parsed YamlMap. | 185 /// Map from package's library directory to the parsed YamlMap. |
| 186 final Map<Folder, YamlMap> embedderYamls = new HashMap<Folder, YamlMap>(); | 186 final Map<Folder, YamlMap> embedderYamls = new HashMap<Folder, YamlMap>(); |
| 187 | 187 |
| 188 EmbedderYamlLocator(Map<String, List<Folder>> packageMap) { | 188 EmbedderYamlLocator(Map<String, List<Folder>> packageMap) { |
| 189 if (packageMap != null) { | 189 if (packageMap != null) { |
| 190 refresh(packageMap); | 190 _processPackageMap(packageMap); |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 | 193 |
| 194 /// Programatically add an _embedder.yaml mapping. | 194 /// Programatically add an _embedder.yaml mapping. |
| 195 void addEmbedderYaml(Folder libDir, String embedderYaml) { | 195 void addEmbedderYaml(Folder libDir, String embedderYaml) { |
| 196 _processEmbedderYaml(libDir, embedderYaml); | 196 _processEmbedderYaml(libDir, embedderYaml); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void refresh(Map<String, List<Folder>> packageMap) { | 199 void refresh(Map<String, List<Folder>> packageMap) { |
| 200 // Clear existing. | 200 // Clear existing. |
| 201 embedderYamls.clear(); | 201 embedderYamls.clear(); |
| 202 if (packageMap == null) { | 202 if (packageMap != null) { |
| 203 return; | 203 _processPackageMap(packageMap); |
| 204 } | 204 } |
| 205 packageMap.forEach(_processPackage); | |
| 206 } | 205 } |
| 207 | 206 |
| 208 /// Given the yaml for an embedder ([embedderYaml]) and a folder | 207 /// Given the yaml for an embedder ([embedderYaml]) and a folder |
| 209 /// ([libDir]), setup the uri mapping. | 208 /// ([libDir]), setup the uri mapping. |
| 210 void _processEmbedderYaml(Folder libDir, String embedderYaml) { | 209 void _processEmbedderYaml(Folder libDir, String embedderYaml) { |
| 211 YamlNode yaml; | 210 YamlNode yaml; |
| 212 try { | 211 try { |
| 213 yaml = loadYaml(embedderYaml); | 212 yaml = loadYaml(embedderYaml); |
| 214 } catch (_) { | 213 } catch (_) { |
| 215 return; | 214 return; |
| 216 } | 215 } |
| 217 if (yaml is! YamlMap) { | 216 if (yaml is! YamlMap) { |
| 218 return; | 217 return; |
| 219 } | 218 } |
| 220 embedderYamls[libDir] = yaml; | 219 embedderYamls[libDir] = yaml; |
| 221 } | 220 } |
| 222 | 221 |
| 223 /// Given a package [name] and a list of folders ([libDirs]), | 222 /// Given a package [name] and a list of folders ([libDirs]), |
| 224 /// add any found `_embedder.yaml` files. | 223 /// add any found `_embedder.yaml` files. |
| 225 void _processPackage(String name, List<Folder> libDirs) { | 224 void _processPackage(String name, List<Folder> libDirs) { |
| 226 for (Folder libDir in libDirs) { | 225 for (Folder libDir in libDirs) { |
| 227 String embedderYaml = _readEmbedderYaml(libDir); | 226 String embedderYaml = _readEmbedderYaml(libDir); |
| 228 if (embedderYaml != null) { | 227 if (embedderYaml != null) { |
| 229 _processEmbedderYaml(libDir, embedderYaml); | 228 _processEmbedderYaml(libDir, embedderYaml); |
| 230 } | 229 } |
| 231 } | 230 } |
| 232 } | 231 } |
| 233 | 232 |
| 233 void _processPackageMap(Map<String, List<Folder>> packageMap) { |
| 234 packageMap.forEach(_processPackage); |
| 235 } |
| 236 |
| 234 /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string. | 237 /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string. |
| 235 /// Returns null if the file doesn't exist. | 238 /// Returns null if the file doesn't exist. |
| 236 String _readEmbedderYaml(Folder libDir) { | 239 String _readEmbedderYaml(Folder libDir) { |
| 237 File file = libDir.getChild(EMBEDDER_FILE_NAME); | 240 File file = libDir.getChild(EMBEDDER_FILE_NAME); |
| 238 try { | 241 try { |
| 239 return file.readAsStringSync(); | 242 return file.readAsStringSync(); |
| 240 } on FileSystemException { | 243 } on FileSystemException { |
| 241 // File can't be read. | 244 // File can't be read. |
| 242 return null; | 245 return null; |
| 243 } | 246 } |
| 244 } | 247 } |
| 245 } | 248 } |
| OLD | NEW |