| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 /** | 114 /** |
| 115 * The absolute path to the `bazel-bin` folder. | 115 * The absolute path to the `bazel-bin` folder. |
| 116 */ | 116 */ |
| 117 final String bin; | 117 final String bin; |
| 118 | 118 |
| 119 /** | 119 /** |
| 120 * The absolute path to the `bazel-genfiles` folder. | 120 * The absolute path to the `bazel-genfiles` folder. |
| 121 */ | 121 */ |
| 122 final String genfiles; | 122 final String genfiles; |
| 123 | 123 |
| 124 /** | |
| 125 * Create a new Bazel workspace that contains the given [path]. | |
| 126 * | |
| 127 * The [symlinkPrefix] is the prefix for names of symlinks like `bazel-bin`, | |
| 128 * `bazel-genfiles`, etc. | |
| 129 */ | |
| 130 factory BazelWorkspace(ResourceProvider provider, String path, | |
| 131 {String symlinkPrefix: 'bazel', String readonlySuffix}) { | |
| 132 Context context = provider.pathContext; | |
| 133 | |
| 134 // Ensure that the path is absolute and normalized. | |
| 135 if (!context.isAbsolute(path)) { | |
| 136 throw new ArgumentError('not absolute: $path'); | |
| 137 } | |
| 138 path = context.normalize(path); | |
| 139 | |
| 140 Folder folder = provider.getFolder(path); | |
| 141 while (true) { | |
| 142 Folder parent = folder.parent; | |
| 143 | |
| 144 // Found the READONLY folder, must be a git-based workspace. | |
| 145 if (readonlySuffix != null && parent != null) { | |
| 146 Folder readonlyFolder = parent.getChildAssumingFolder(_READONLY); | |
| 147 if (readonlyFolder.exists) { | |
| 148 String root = folder.path; | |
| 149 String readonly = readonlyFolder.path; | |
| 150 return new BazelWorkspace._( | |
| 151 provider, | |
| 152 root, | |
| 153 context.join(readonly, readonlySuffix), | |
| 154 context.join(root, '$symlinkPrefix-bin'), | |
| 155 context.join(root, '$symlinkPrefix-genfiles')); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 // Found the WORKSPACE file, must be a non-git workspace. | |
| 160 if (folder.getChildAssumingFile(_WORKSPACE).exists) { | |
| 161 String root = folder.path; | |
| 162 return new BazelWorkspace._( | |
| 163 provider, | |
| 164 root, | |
| 165 null, | |
| 166 context.join(root, '$symlinkPrefix-bin'), | |
| 167 context.join(root, '$symlinkPrefix-genfiles')); | |
| 168 } | |
| 169 | |
| 170 // Go up the folder. | |
| 171 folder = parent; | |
| 172 if (folder == null) { | |
| 173 return new BazelWorkspace._(provider, path, null, null, null); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 BazelWorkspace._( | 124 BazelWorkspace._( |
| 179 this.provider, this.root, this.readonly, this.bin, this.genfiles); | 125 this.provider, this.root, this.readonly, this.bin, this.genfiles); |
| 180 | 126 |
| 181 /** | 127 /** |
| 182 * Return the file with the given [absolutePath], looking first into | 128 * Return the file with the given [absolutePath], looking first into |
| 183 * directories for generated files: `bazel-genfiles` and `bazel-bin`, and | 129 * directories for generated files: `bazel-bin` and `bazel-genfiles`, and |
| 184 * then into the workspace root. The file in the workspace root is returned | 130 * then into the workspace root. The file in the workspace root is returned |
| 185 * even if it does not exist. Return `null` if the given [absolutePath] is | 131 * even if it does not exist. Return `null` if the given [absolutePath] is |
| 186 * not in the workspace [root]. | 132 * not in the workspace [root]. |
| 187 */ | 133 */ |
| 188 File findFile(String absolutePath) { | 134 File findFile(String absolutePath) { |
| 189 Context context = provider.pathContext; | 135 Context context = provider.pathContext; |
| 190 try { | 136 try { |
| 191 String relative = context.relative(absolutePath, from: root); | 137 String relative = context.relative(absolutePath, from: root); |
| 192 // genfiles | 138 // genfiles |
| 193 if (genfiles != null) { | 139 if (genfiles != null) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 217 } | 163 } |
| 218 } | 164 } |
| 219 | 165 |
| 220 /** | 166 /** |
| 221 * Return the file for the given [pathInWorkspace]. The file is returned even | 167 * Return the file for the given [pathInWorkspace]. The file is returned even |
| 222 * if it does not exist. | 168 * if it does not exist. |
| 223 */ | 169 */ |
| 224 File getFile(String pathInWorkspace) { | 170 File getFile(String pathInWorkspace) { |
| 225 return provider.getFile(provider.pathContext.join(root, pathInWorkspace)); | 171 return provider.getFile(provider.pathContext.join(root, pathInWorkspace)); |
| 226 } | 172 } |
| 173 |
| 174 /** |
| 175 * Find the Bazel workspace that contains the given [path]. |
| 176 * |
| 177 * Return `null` if a workspace markers, such as the `WORKSPACE` file, or |
| 178 * the sibling `READONLY` folder cannot be found. |
| 179 * |
| 180 * The [symlinkPrefix] is the prefix for names of symlinks like `bazel-bin`, |
| 181 * `bazel-genfiles`, etc. |
| 182 */ |
| 183 static BazelWorkspace find(ResourceProvider provider, String path, |
| 184 {String symlinkPrefix: 'bazel', String readonlySuffix}) { |
| 185 Context context = provider.pathContext; |
| 186 |
| 187 // Ensure that the path is absolute and normalized. |
| 188 if (!context.isAbsolute(path)) { |
| 189 throw new ArgumentError('not absolute: $path'); |
| 190 } |
| 191 path = context.normalize(path); |
| 192 |
| 193 Folder folder = provider.getFolder(path); |
| 194 while (true) { |
| 195 Folder parent = folder.parent; |
| 196 if (parent == null) { |
| 197 return null; |
| 198 } |
| 199 |
| 200 // Found the READONLY folder, must be a git-based workspace. |
| 201 if (readonlySuffix != null) { |
| 202 Folder readonlyFolder = parent.getChildAssumingFolder(_READONLY); |
| 203 if (readonlyFolder.exists) { |
| 204 String root = folder.path; |
| 205 String readonly = readonlyFolder.path; |
| 206 return new BazelWorkspace._( |
| 207 provider, |
| 208 root, |
| 209 context.join(readonly, readonlySuffix), |
| 210 context.join(root, '$symlinkPrefix-bin'), |
| 211 context.join(root, '$symlinkPrefix-genfiles')); |
| 212 } |
| 213 } |
| 214 |
| 215 // Found the WORKSPACE file, must be a non-git workspace. |
| 216 if (folder.getChildAssumingFile(_WORKSPACE).exists) { |
| 217 String root = folder.path; |
| 218 return new BazelWorkspace._( |
| 219 provider, |
| 220 root, |
| 221 null, |
| 222 context.join(root, '$symlinkPrefix-bin'), |
| 223 context.join(root, '$symlinkPrefix-genfiles')); |
| 224 } |
| 225 |
| 226 // Go up the folder. |
| 227 folder = parent; |
| 228 } |
| 229 } |
| 227 } | 230 } |
| OLD | NEW |