| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.bazel; | 5 library analyzer.src.generated.bazel; |
| 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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 File getFile(String pathInWorkspace) { | 170 File getFile(String pathInWorkspace) { |
| 171 return provider.getFile(provider.pathContext.join(root, pathInWorkspace)); | 171 return provider.getFile(provider.pathContext.join(root, pathInWorkspace)); |
| 172 } | 172 } |
| 173 | 173 |
| 174 /** | 174 /** |
| 175 * Find the Bazel workspace that contains the given [path]. | 175 * Find the Bazel workspace that contains the given [path]. |
| 176 * | 176 * |
| 177 * Return `null` if a workspace markers, such as the `WORKSPACE` file, or | 177 * Return `null` if a workspace markers, such as the `WORKSPACE` file, or |
| 178 * the sibling `READONLY` folder cannot be found. | 178 * the sibling `READONLY` folder cannot be found. |
| 179 * | 179 * |
| 180 * The [symlinkPrefix] is the prefix for names of symlinks like `bazel-bin`, | 180 * Return `null` if the workspace does not have `bazel-genfiles` or |
| 181 * `bazel-genfiles`, etc. | 181 * `blaze-genfiles` folders, so we don't know where to search generated files. |
| 182 */ | 182 */ |
| 183 static BazelWorkspace find(ResourceProvider provider, String path, | 183 static BazelWorkspace find(ResourceProvider provider, String path, |
| 184 {String symlinkPrefix: 'bazel', String readonlySuffix}) { | 184 {String readonlySuffix}) { |
| 185 Context context = provider.pathContext; | 185 Context context = provider.pathContext; |
| 186 | 186 |
| 187 // Ensure that the path is absolute and normalized. | 187 // Ensure that the path is absolute and normalized. |
| 188 if (!context.isAbsolute(path)) { | 188 if (!context.isAbsolute(path)) { |
| 189 throw new ArgumentError('not absolute: $path'); | 189 throw new ArgumentError('not absolute: $path'); |
| 190 } | 190 } |
| 191 path = context.normalize(path); | 191 path = context.normalize(path); |
| 192 | 192 |
| 193 Folder folder = provider.getFolder(path); | 193 Folder folder = provider.getFolder(path); |
| 194 while (true) { | 194 while (true) { |
| 195 Folder parent = folder.parent; | 195 Folder parent = folder.parent; |
| 196 if (parent == null) { | 196 if (parent == null) { |
| 197 return null; | 197 return null; |
| 198 } | 198 } |
| 199 | 199 |
| 200 // Found the READONLY folder, must be a git-based workspace. | 200 // Found the READONLY folder, must be a git-based workspace. |
| 201 if (readonlySuffix != null) { | 201 if (readonlySuffix != null) { |
| 202 Folder readonlyFolder = parent.getChildAssumingFolder(_READONLY); | 202 Folder readonlyFolder = parent.getChildAssumingFolder(_READONLY); |
| 203 if (readonlyFolder.exists) { | 203 if (readonlyFolder.exists) { |
| 204 String root = folder.path; | 204 String root = folder.path; |
| 205 String readonly = readonlyFolder.path; | 205 String readonly = readonlyFolder.path; |
| 206 String symlinkPrefix = _findSymlinkPrefix(provider, root); |
| 207 if (symlinkPrefix == null) { |
| 208 return null; |
| 209 } |
| 206 return new BazelWorkspace._( | 210 return new BazelWorkspace._( |
| 207 provider, | 211 provider, |
| 208 root, | 212 root, |
| 209 context.join(readonly, readonlySuffix), | 213 context.join(readonly, readonlySuffix), |
| 210 context.join(root, '$symlinkPrefix-bin'), | 214 context.join(root, '$symlinkPrefix-bin'), |
| 211 context.join(root, '$symlinkPrefix-genfiles')); | 215 context.join(root, '$symlinkPrefix-genfiles')); |
| 212 } | 216 } |
| 213 } | 217 } |
| 214 | 218 |
| 215 // Found the WORKSPACE file, must be a non-git workspace. | 219 // Found the WORKSPACE file, must be a non-git workspace. |
| 216 if (folder.getChildAssumingFile(_WORKSPACE).exists) { | 220 if (folder.getChildAssumingFile(_WORKSPACE).exists) { |
| 217 String root = folder.path; | 221 String root = folder.path; |
| 222 String symlinkPrefix = _findSymlinkPrefix(provider, root); |
| 223 if (symlinkPrefix == null) { |
| 224 return null; |
| 225 } |
| 218 return new BazelWorkspace._( | 226 return new BazelWorkspace._( |
| 219 provider, | 227 provider, |
| 220 root, | 228 root, |
| 221 null, | 229 null, |
| 222 context.join(root, '$symlinkPrefix-bin'), | 230 context.join(root, '$symlinkPrefix-bin'), |
| 223 context.join(root, '$symlinkPrefix-genfiles')); | 231 context.join(root, '$symlinkPrefix-genfiles')); |
| 224 } | 232 } |
| 225 | 233 |
| 226 // Go up the folder. | 234 // Go up the folder. |
| 227 folder = parent; | 235 folder = parent; |
| 228 } | 236 } |
| 229 } | 237 } |
| 238 |
| 239 /** |
| 240 * Return the symlink prefix for folders `X-bin` or `X-genfiles` by probing |
| 241 * the internal `blaze-genfiles` and `bazel-genfiles`. Return `null` if |
| 242 * neither of the folders exists. |
| 243 */ |
| 244 static String _findSymlinkPrefix(ResourceProvider provider, String root) { |
| 245 Context context = provider.pathContext; |
| 246 if (provider.getFolder(context.join(root, 'blaze-genfiles')).exists) { |
| 247 return 'blaze'; |
| 248 } |
| 249 if (provider.getFolder(context.join(root, 'bazel-genfiles')).exists) { |
| 250 return 'bazel'; |
| 251 } |
| 252 return null; |
| 253 } |
| 230 } | 254 } |
| OLD | NEW |