OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
11 * or implied. See the License for the specific language governing permissions a
nd limitations under | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 | 14 |
15 package com.google.dart.engine.source; | 15 package com.google.dart.engine.source; |
16 | 16 |
| 17 import com.google.common.annotations.VisibleForTesting; |
17 import com.google.dart.engine.AnalysisEngine; | 18 import com.google.dart.engine.AnalysisEngine; |
18 import com.google.dart.engine.sdk.DirectoryBasedDartSdk; | 19 import com.google.dart.engine.sdk.DirectoryBasedDartSdk; |
19 import com.google.dart.engine.utilities.io.ProcessRunner; | 20 import com.google.dart.engine.utilities.io.ProcessRunner; |
20 | 21 |
21 import org.json.JSONArray; | 22 import org.json.JSONArray; |
22 import org.json.JSONException; | 23 import org.json.JSONException; |
23 import org.json.JSONObject; | 24 import org.json.JSONObject; |
24 | 25 |
25 import java.io.File; | 26 import java.io.File; |
26 import java.io.IOException; | 27 import java.io.IOException; |
27 import java.net.URI; | 28 import java.net.URI; |
28 import java.util.ArrayList; | 29 import java.util.ArrayList; |
29 import java.util.HashMap; | 30 import java.util.HashMap; |
30 import java.util.Iterator; | 31 import java.util.Iterator; |
31 import java.util.List; | 32 import java.util.List; |
32 import java.util.Map; | 33 import java.util.Map; |
33 | 34 |
34 /** | 35 /** |
35 * An explicit package: resolver. This UriResolver shells out to pub, calling it
's list-package-dirs | 36 * An explicit package: resolver. This UriResolver shells out to pub, calling it
's list-package-dirs |
36 * command. It parses the resulting json map, which maps symbolic package refere
nces to their | 37 * command. It parses the resulting json map, which maps symbolic package refere
nces to their |
37 * concrete locations on disk. | 38 * concrete locations on disk. |
38 * | 39 * |
39 * <pre> | 40 * <pre> |
40 * { | 41 *{ |
41 * "packages": { | 42 *"packages": { |
42 * "foo": "path/to/foo", | 43 *"foo": "path/to/foo", |
43 * "bar": "path/to/bar" | 44 *"bar": "path/to/bar" |
44 * }, | 45 *}, |
45 * "input_files": [ | 46 *"input_files": [ |
46 * ... | 47 *... |
47 * ] | 48 *] |
48 * }, | 49 *}, |
49 * </pre> | 50 *</pre> |
50 */ | 51 */ |
51 public class ExplicitPackageUriResolver extends UriResolver { | 52 public class ExplicitPackageUriResolver extends UriResolver { |
52 /** | 53 /** |
53 * The name of the {@code package} scheme. | 54 * The name of the {@code package} scheme. |
54 */ | 55 */ |
55 public static final String PACKAGE_SCHEME = "package"; | 56 public static final String PACKAGE_SCHEME = "package"; |
56 | 57 |
57 private static final String PUB_LIST_COMMAND = "list-package-dirs"; | 58 private static final String PUB_LIST_COMMAND = "list-package-dirs"; |
58 | 59 |
59 /** | 60 /** |
60 * Return {@code true} if the given URI is a {@code package} URI. | 61 * Return {@code true} if the given URI is a {@code package} URI. |
61 * | 62 * |
62 * @param uri the URI being tested | 63 * @param uri the URI being tested |
63 * @return {@code true} if the given URI is a {@code package} URI | 64 * @return {@code true} if the given URI is a {@code package} URI |
64 */ | 65 */ |
65 public static boolean isPackageUri(URI uri) { | 66 public static boolean isPackageUri(URI uri) { |
66 return PACKAGE_SCHEME.equals(uri.getScheme()); | 67 return PACKAGE_SCHEME.equals(uri.getScheme()); |
67 } | 68 } |
68 | 69 |
69 private File rootDir; | 70 private File rootDir; |
70 private DirectoryBasedDartSdk sdk; | 71 private DirectoryBasedDartSdk sdk; |
71 private Map<String, List<File>> packageMap; | 72 @VisibleForTesting |
| 73 protected Map<String, List<File>> packageMap; |
72 | 74 |
73 // TODO: For now, this takes a DirectoryBasedDartSdk. We may want to abstract
this out into | 75 // TODO: For now, this takes a DirectoryBasedDartSdk. We may want to abstract
this out into |
74 // something that can return a package map. | 76 // something that can return a package map. |
75 | 77 |
76 /** | 78 /** |
77 * Create a new ExplicitPackageUriResolver. | 79 * Create a new ExplicitPackageUriResolver. |
78 * | 80 * |
79 * @param sdk the sdk; this is used to locate the pub command to run | 81 * @param sdk the sdk; this is used to locate the pub command to run |
80 * @param rootDir the directory for which we'll be resolving package informati
on | 82 * @param rootDir the directory for which we'll be resolving package informati
on |
81 */ | 83 */ |
82 public ExplicitPackageUriResolver(DirectoryBasedDartSdk sdk, File rootDir) { | 84 public ExplicitPackageUriResolver(DirectoryBasedDartSdk sdk, File rootDir) { |
83 if (rootDir == null) { | 85 if (rootDir == null) { |
84 throw new IllegalArgumentException("the root dir must not be null"); | 86 throw new IllegalArgumentException("the root dir must not be null"); |
85 } | 87 } |
86 | 88 |
87 this.sdk = sdk; | 89 this.sdk = sdk; |
88 this.rootDir = rootDir; | 90 this.rootDir = rootDir; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 144 } |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
146 // Return a FileBasedSource that doesn't exist. This helps provide more mean
ingful error | 148 // Return a FileBasedSource that doesn't exist. This helps provide more mean
ingful error |
147 // messages to users (a missing file error, as opposed to an invalid uri err
or). | 149 // messages to users (a missing file error, as opposed to an invalid uri err
or). |
148 | 150 |
149 String fullPackagePath = pkgName + "/" + relPath; | 151 String fullPackagePath = pkgName + "/" + relPath; |
150 | 152 |
151 return new FileBasedSource( // | 153 return new FileBasedSource( // |
152 contentCache, | 154 contentCache, new File(rootDir, fullPackagePath.replace('/', File.separa
torChar)), UriKind.PACKAGE_URI); |
153 new File(rootDir, fullPackagePath.replace('/', File.separatorChar)), | 155 } |
154 UriKind.PACKAGE_URI); | 156 |
| 157 public String resolvePathToPackage(String path) { |
| 158 if (packageMap == null) { |
| 159 return null; |
| 160 } |
| 161 |
| 162 for (String key : packageMap.keySet()) { |
| 163 List<File> files = packageMap.get(key); |
| 164 for (File file : files) { |
| 165 try { |
| 166 if (file.getCanonicalPath().endsWith(path)) { |
| 167 return key; |
| 168 } |
| 169 } catch (IOException e) { |
| 170 |
| 171 } |
| 172 } |
| 173 } |
| 174 return null; |
155 } | 175 } |
156 | 176 |
157 @Override | 177 @Override |
158 public URI restoreAbsolute(Source source) { | 178 public URI restoreAbsolute(Source source) { |
159 if (packageMap == null) { | 179 if (packageMap == null) { |
160 return null; | 180 return null; |
161 } | 181 } |
162 | 182 |
163 if (source instanceof FileBasedSource) { | 183 if (source instanceof FileBasedSource) { |
164 String sourcePath = ((FileBasedSource) source).getFile().getPath(); | 184 String sourcePath = ((FileBasedSource) source).getFile().getPath(); |
(...skipping 24 matching lines...) Expand all Loading... |
189 ProcessRunner runner = new ProcessRunner(builder); | 209 ProcessRunner runner = new ProcessRunner(builder); |
190 | 210 |
191 try { | 211 try { |
192 if (runner.runSync(0) == 0) { | 212 if (runner.runSync(0) == 0) { |
193 return parsePackageMap(runner.getStdOut()); | 213 return parsePackageMap(runner.getStdOut()); |
194 } else { | 214 } else { |
195 AnalysisEngine.getInstance().getLogger().logInformation( | 215 AnalysisEngine.getInstance().getLogger().logInformation( |
196 "pub " + PUB_LIST_COMMAND + " failed: exit code " + runner.getExitCo
de()); | 216 "pub " + PUB_LIST_COMMAND + " failed: exit code " + runner.getExitCo
de()); |
197 } | 217 } |
198 } catch (IOException ioe) { | 218 } catch (IOException ioe) { |
199 AnalysisEngine.getInstance().getLogger().logInformation( | 219 AnalysisEngine.getInstance() |
200 "error running pub " + PUB_LIST_COMMAND, | 220 .getLogger().logInformation("error running pub " + PUB_LIST_COMMAND, i
oe); |
201 ioe); | |
202 } catch (JSONException e) { | 221 } catch (JSONException e) { |
203 AnalysisEngine.getInstance().getLogger().logError( | 222 AnalysisEngine.getInstance() |
204 "malformed json from pub " + PUB_LIST_COMMAND, | 223 .getLogger().logError("malformed json from pub " + PUB_LIST_COMMAND, e
); |
205 e); | |
206 } | 224 } |
207 | 225 |
208 return new HashMap<String, List<File>>(); | 226 return new HashMap<String, List<File>>(); |
209 } | 227 } |
210 | 228 |
211 protected Map<String, List<File>> parsePackageMap(String jsonText) throws JSON
Exception { | 229 protected Map<String, List<File>> parsePackageMap(String jsonText) throws JSON
Exception { |
212 Map<String, List<File>> map = new HashMap<String, List<File>>(); | 230 Map<String, List<File>> map = new HashMap<String, List<File>>(); |
213 | 231 |
214 // Json format: | 232 // Json format: |
215 // { | 233 // { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 } | 273 } |
256 } | 274 } |
257 } | 275 } |
258 } | 276 } |
259 } | 277 } |
260 | 278 |
261 return map; | 279 return map; |
262 } | 280 } |
263 | 281 |
264 } | 282 } |
OLD | NEW |