| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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.src.generated.gn; | 5 library analyzer.src.generated.gn; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:core'; | 8 import 'dart:core'; |
| 9 | 9 |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 Packages _packages; | 58 Packages _packages; |
| 59 | 59 |
| 60 GnWorkspace._(this.provider, this.root, this._packagesFilePaths); | 60 GnWorkspace._(this.provider, this.root, this._packagesFilePaths); |
| 61 | 61 |
| 62 @override | 62 @override |
| 63 Map<String, List<Folder>> get packageMap => | 63 Map<String, List<Folder>> get packageMap => |
| 64 _packageMap ??= _convertPackagesToMap(packages); | 64 _packageMap ??= _convertPackagesToMap(packages); |
| 65 | 65 |
| 66 Packages get packages => _packages ??= _createPackages(); | 66 Packages get packages => _packages ??= _createPackages(); |
| 67 | 67 |
| 68 @override |
| 69 UriResolver get packageUriResolver => |
| 70 new PackageMapUriResolver(provider, packageMap); |
| 71 |
| 72 @override |
| 73 SourceFactory createSourceFactory(DartSdk sdk) { |
| 74 List<UriResolver> resolvers = <UriResolver>[]; |
| 75 if (sdk != null) { |
| 76 resolvers.add(new DartUriResolver(sdk)); |
| 77 } |
| 78 resolvers.add(packageUriResolver); |
| 79 resolvers.add(new ResourceUriResolver(provider)); |
| 80 return new SourceFactory(resolvers, packages, provider); |
| 81 } |
| 82 |
| 83 /** |
| 84 * Return the file with the given [absolutePath]. |
| 85 * |
| 86 * Return `null` if the given [absolutePath] is not in the workspace [root]. |
| 87 */ |
| 88 File findFile(String absolutePath) { |
| 89 try { |
| 90 File writableFile = provider.getFile(absolutePath); |
| 91 if (writableFile.exists) { |
| 92 return writableFile; |
| 93 } |
| 94 } catch (_) {} |
| 95 return null; |
| 96 } |
| 97 |
| 68 /** | 98 /** |
| 69 * Creates an alternate representation for available packages. | 99 * Creates an alternate representation for available packages. |
| 70 */ | 100 */ |
| 71 Map<String, List<Folder>> _convertPackagesToMap(Packages packages) { | 101 Map<String, List<Folder>> _convertPackagesToMap(Packages packages) { |
| 72 Map<String, List<Folder>> folderMap = new HashMap<String, List<Folder>>(); | 102 Map<String, List<Folder>> folderMap = new HashMap<String, List<Folder>>(); |
| 73 if (packages != null && packages != Packages.noPackages) { | 103 if (packages != null && packages != Packages.noPackages) { |
| 74 packages.asMap().forEach((String packageName, Uri uri) { | 104 packages.asMap().forEach((String packageName, Uri uri) { |
| 75 String path = provider.pathContext.fromUri(uri); | 105 String path = provider.pathContext.fromUri(uri); |
| 76 folderMap[packageName] = [provider.getFolder(path)]; | 106 folderMap[packageName] = [provider.getFolder(path)]; |
| 77 }); | 107 }); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 for (String packageName in map.keys) { | 145 for (String packageName in map.keys) { |
| 116 Folder folder = provider.getFolder(pathContext.fromUri(map[packageName])); | 146 Folder folder = provider.getFolder(pathContext.fromUri(map[packageName])); |
| 117 String folderPath = _resolveSymbolicLink(folder); | 147 String folderPath = _resolveSymbolicLink(folder); |
| 118 // Add a '.' so that the URI is suitable for resolving relative URI's | 148 // Add a '.' so that the URI is suitable for resolving relative URI's |
| 119 // against it. | 149 // against it. |
| 120 String uriPath = pathContext.join(folderPath, '.'); | 150 String uriPath = pathContext.join(folderPath, '.'); |
| 121 map[packageName] = pathContext.toUri(uriPath); | 151 map[packageName] = pathContext.toUri(uriPath); |
| 122 } | 152 } |
| 123 } | 153 } |
| 124 | 154 |
| 125 @override | 155 /** |
| 126 UriResolver get packageUriResolver => | 156 * Find the GN workspace that contains the given [path]. |
| 127 new PackageMapUriResolver(provider, packageMap); | 157 * |
| 158 * Return `null` if a workspace could not be found. |
| 159 */ |
| 160 static GnWorkspace find(ResourceProvider provider, String path) { |
| 161 Context context = provider.pathContext; |
| 128 | 162 |
| 129 @override | 163 // Ensure that the path is absolute and normalized. |
| 130 SourceFactory createSourceFactory(DartSdk sdk) { | 164 if (!context.isAbsolute(path)) { |
| 131 List<UriResolver> resolvers = <UriResolver>[]; | 165 throw new ArgumentError('Not an absolute path: $path'); |
| 132 if (sdk != null) { | |
| 133 resolvers.add(new DartUriResolver(sdk)); | |
| 134 } | 166 } |
| 135 resolvers.add(packageUriResolver); | 167 path = context.normalize(path); |
| 136 resolvers.add(new ResourceUriResolver(provider)); | 168 |
| 137 return new SourceFactory(resolvers, packages, provider); | 169 Folder folder = provider.getFolder(path); |
| 170 while (true) { |
| 171 Folder parent = folder.parent; |
| 172 if (parent == null) { |
| 173 return null; |
| 174 } |
| 175 |
| 176 // Found the .jiri_root file, must be a non-git workspace. |
| 177 if (folder.getChildAssumingFolder(_jiriRootName).exists) { |
| 178 String root = folder.path; |
| 179 List<String> packagesFiles = |
| 180 _findPackagesFile(provider, root, path, forHost: false); |
| 181 if (packagesFiles.isEmpty) { |
| 182 packagesFiles = |
| 183 _findPackagesFile(provider, root, path, forHost: true); |
| 184 } |
| 185 if (packagesFiles.isEmpty) { |
| 186 return null; |
| 187 } |
| 188 return new GnWorkspace._(provider, path, packagesFiles); |
| 189 } |
| 190 |
| 191 // Go up the folder. |
| 192 folder = parent; |
| 193 } |
| 138 } | 194 } |
| 139 | 195 |
| 140 /** | 196 /** |
| 141 * Return the file with the given [absolutePath]. | |
| 142 * | |
| 143 * Return `null` if the given [absolutePath] is not in the workspace [root]. | |
| 144 */ | |
| 145 File findFile(String absolutePath) { | |
| 146 try { | |
| 147 File writableFile = provider.getFile(absolutePath); | |
| 148 if (writableFile.exists) { | |
| 149 return writableFile; | |
| 150 } | |
| 151 } catch (_) {} | |
| 152 return null; | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * For a source at `$root/foo/bar`, the packages files are generated in | 197 * For a source at `$root/foo/bar`, the packages files are generated in |
| 157 * `$root/out/<debug|release>-XYZ/[hostABC/]gen/foo/bar`. | 198 * `$root/out/<debug|release>-XYZ/[hostABC/]gen/foo/bar`. |
| 158 * | 199 * |
| 159 * Note that in some cases multiple .packages files can be found at that | 200 * Note that in some cases multiple .packages files can be found at that |
| 160 * location, for example if the package contains both a library and a binary | 201 * location, for example if the package contains both a library and a binary |
| 161 * target. | 202 * target. |
| 162 */ | 203 */ |
| 163 static List<String> _findPackagesFile( | 204 static List<String> _findPackagesFile( |
| 164 ResourceProvider provider, String root, String path, | 205 ResourceProvider provider, String root, String path, |
| 165 {forHost: false}) { | 206 {forHost: false}) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 return const <String>[]; | 241 return const <String>[]; |
| 201 } | 242 } |
| 202 return genDir | 243 return genDir |
| 203 .getChildren() | 244 .getChildren() |
| 204 .where((resource) => resource is File) | 245 .where((resource) => resource is File) |
| 205 .map((resource) => resource as File) | 246 .map((resource) => resource as File) |
| 206 .where((File file) => pathContext.extension(file.path) == '.packages') | 247 .where((File file) => pathContext.extension(file.path) == '.packages') |
| 207 .map((File file) => file.path) | 248 .map((File file) => file.path) |
| 208 .toList(); | 249 .toList(); |
| 209 } | 250 } |
| 210 | |
| 211 /** | |
| 212 * Find the GN workspace that contains the given [path]. | |
| 213 * | |
| 214 * Return `null` if a workspace could not be found. | |
| 215 */ | |
| 216 static GnWorkspace find(ResourceProvider provider, String path) { | |
| 217 Context context = provider.pathContext; | |
| 218 | |
| 219 // Ensure that the path is absolute and normalized. | |
| 220 if (!context.isAbsolute(path)) { | |
| 221 throw new ArgumentError('Not an absolute path: $path'); | |
| 222 } | |
| 223 path = context.normalize(path); | |
| 224 | |
| 225 Folder folder = provider.getFolder(path); | |
| 226 while (true) { | |
| 227 Folder parent = folder.parent; | |
| 228 if (parent == null) { | |
| 229 return null; | |
| 230 } | |
| 231 | |
| 232 // Found the .jiri_root file, must be a non-git workspace. | |
| 233 if (folder.getChildAssumingFolder(_jiriRootName).exists) { | |
| 234 String root = folder.path; | |
| 235 List<String> packagesFiles = | |
| 236 _findPackagesFile(provider, root, path, forHost: false); | |
| 237 if (packagesFiles.isEmpty) { | |
| 238 packagesFiles = | |
| 239 _findPackagesFile(provider, root, path, forHost: true); | |
| 240 } | |
| 241 if (packagesFiles.isEmpty) { | |
| 242 return null; | |
| 243 } | |
| 244 return new GnWorkspace._(provider, path, packagesFiles); | |
| 245 } | |
| 246 | |
| 247 // Go up the folder. | |
| 248 folder = parent; | |
| 249 } | |
| 250 } | |
| 251 } | 251 } |
| OLD | NEW |