| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.base.Throwables; |
| 21 |
import com.google.common.collect.ImmutableList; |
| 22 |
import com.google.common.collect.Iterables; |
| 23 |
import com.google.common.collect.LinkedHashMultimap; |
| 24 |
import com.google.common.collect.LinkedListMultimap; |
| 25 |
import com.google.common.collect.ListMultimap; |
| 26 |
import com.google.common.collect.Lists; |
| 27 |
import com.google.common.collect.Maps; |
| 28 |
import com.google.common.collect.Multimap; |
| 29 |
import com.google.common.collect.Sets; |
| 30 |
import com.google.javascript.jscomp.deps.SortedDependencies; |
| 31 |
import com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException; |
| 32 |
import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException; |
| 33 |
import com.google.javascript.jscomp.graph.LinkedDirectedGraph; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Collection; |
| 37 |
import java.util.Comparator; |
| 38 |
import java.util.Iterator; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Map; |
| 41 |
import java.util.Set; |
| 42 |
import java.util.TreeSet; |
| 43 |
|
| 44 |
import org.json.JSONArray; |
| 45 |
import org.json.JSONException; |
| 46 |
import org.json.JSONObject; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class JSModuleGraph { |
| 56 |
|
| 57 |
private List<JSModule> modules; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private List<List<JSModule>> modulesByDepth; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private Map<JSModule, Set<JSModule>> dependencyMap = Maps.newHashMap(); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public JSModuleGraph(JSModule[] modulesInDepOrder) { |
| 83 |
this(ImmutableList.copyOf(modulesInDepOrder)); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public JSModuleGraph(List<JSModule> modulesInDepOrder) { |
| 90 |
Preconditions.checkState( |
| 91 |
modulesInDepOrder.size() == Sets.newHashSet(modulesInDepOrder).size(), |
| 92 |
"Found duplicate modules"); |
| 93 |
modules = ImmutableList.copyOf(modulesInDepOrder); |
| 94 |
modulesByDepth = Lists.newArrayList(); |
| 95 |
|
| 96 |
for (JSModule module : modulesInDepOrder) { |
| 97 |
int depth = 0; |
| 98 |
for (JSModule dep : module.getDependencies()) { |
| 99 |
int depDepth = dep.getDepth(); |
| 100 |
if (depDepth < 0) { |
| 101 |
throw new ModuleDependenceException(String.format( |
| 102 |
"Modules not in dependency order: %s preceded %s", |
| 103 |
module.getName(), dep.getName()), |
| 104 |
module, dep); |
| 105 |
} |
| 106 |
depth = Math.max(depth, depDepth + 1); |
| 107 |
} |
| 108 |
|
| 109 |
module.setDepth(depth); |
| 110 |
if (depth == modulesByDepth.size()) { |
| 111 |
modulesByDepth.add(new ArrayList<JSModule>()); |
| 112 |
} |
| 113 |
modulesByDepth.get(depth).add(module); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
Iterable<JSModule> getAllModules() { |
| 121 |
return modules; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
Map<String, JSModule> getModulesByName() { |
| 128 |
Map<String, JSModule> result = Maps.newHashMap(); |
| 129 |
for (JSModule m : modules) { |
| 130 |
result.put(m.getName(), m); |
| 131 |
} |
| 132 |
return result; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
int getModuleCount() { |
| 139 |
return modules.size(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
JSModule getRootModule() { |
| 146 |
return Iterables.getOnlyElement(modulesByDepth.get(0)); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
JSONArray toJson() { |
| 159 |
JSONArray modules = new JSONArray(); |
| 160 |
for (JSModule module : getAllModules()) { |
| 161 |
JSONObject node = new JSONObject(); |
| 162 |
try { |
| 163 |
node.put("name", module.getName()); |
| 164 |
JSONArray deps = new JSONArray(); |
| 165 |
node.put("dependencies", deps); |
| 166 |
for (JSModule m : module.getDependencies()) { |
| 167 |
deps.put(m.getName()); |
| 168 |
} |
| 169 |
JSONArray transitiveDeps = new JSONArray(); |
| 170 |
node.put("transitive-dependencies", transitiveDeps); |
| 171 |
for (JSModule m : getTransitiveDepsDeepestFirst(module)) { |
| 172 |
transitiveDeps.put(m.getName()); |
| 173 |
} |
| 174 |
JSONArray inputs = new JSONArray(); |
| 175 |
node.put("inputs", inputs); |
| 176 |
for (CompilerInput input : module.getInputs()) { |
| 177 |
inputs.put(input.getSourceFile().getOriginalPath()); |
| 178 |
} |
| 179 |
modules.put(node); |
| 180 |
} catch (JSONException e) { |
| 181 |
Throwables.propagate(e); |
| 182 |
} |
| 183 |
} |
| 184 |
return modules; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public boolean dependsOn(JSModule src, JSModule m) { |
| 192 |
Set<JSModule> deps = dependencyMap.get(src); |
| 193 |
if (deps == null) { |
| 194 |
deps = getTransitiveDepsDeepestFirst(src); |
| 195 |
dependencyMap.put(src, deps); |
| 196 |
} |
| 197 |
|
| 198 |
return deps.contains(m); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
JSModule getDeepestCommonDependency(JSModule m1, JSModule m2) { |
| 211 |
int m1Depth = m1.getDepth(); |
| 212 |
int m2Depth = m2.getDepth(); |
| 213 |
|
| 214 |
|
| 215 |
for (int depth = Math.min(m1Depth, m2Depth) - 1; depth >= 0; depth--) { |
| 216 |
List<JSModule> modulesAtDepth = modulesByDepth.get(depth); |
| 217 |
|
| 218 |
|
| 219 |
for (int i = modulesAtDepth.size() - 1; i >= 0; i--) { |
| 220 |
JSModule m = modulesAtDepth.get(i); |
| 221 |
if (dependsOn(m1, m) && dependsOn(m2, m)) { |
| 222 |
return m; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
return null; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public JSModule getDeepestCommonDependencyInclusive( |
| 239 |
JSModule m1, JSModule m2) { |
| 240 |
if (m2 == m1 || dependsOn(m2, m1)) { |
| 241 |
return m1; |
| 242 |
} else if (dependsOn(m1, m2)) { |
| 243 |
return m2; |
| 244 |
} |
| 245 |
|
| 246 |
return getDeepestCommonDependency(m1, m2); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
public JSModule getDeepestCommonDependencyInclusive( |
| 251 |
Collection<JSModule> modules) { |
| 252 |
Iterator<JSModule> iter = modules.iterator(); |
| 253 |
JSModule dep = iter.next(); |
| 254 |
while (iter.hasNext()) { |
| 255 |
dep = getDeepestCommonDependencyInclusive(dep, iter.next()); |
| 256 |
} |
| 257 |
return dep; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
Set<JSModule> getTransitiveDepsDeepestFirst(JSModule m) { |
| 269 |
Set<JSModule> deps = dependencyMap.get(m); |
| 270 |
if (deps != null) { |
| 271 |
return deps; |
| 272 |
} |
| 273 |
deps = new TreeSet<JSModule>(new InverseDepthComparator()); |
| 274 |
addDeps(deps, m); |
| 275 |
dependencyMap.put(m, deps); |
| 276 |
return deps; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
private void addDeps(Set<JSModule> deps, JSModule m) { |
| 283 |
for (JSModule dep : m.getDependencies()) { |
| 284 |
deps.add(dep); |
| 285 |
addDeps(deps, dep); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public void coalesceDuplicateFiles() { |
| 298 |
Multimap<String, JSModule> fileRefs = LinkedHashMultimap.create(); |
| 299 |
for (JSModule module : modules) { |
| 300 |
for (CompilerInput jsFile : module.getInputs()) { |
| 301 |
fileRefs.put(jsFile.getName(), module); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
for (String path : fileRefs.keySet()) { |
| 306 |
Collection<JSModule> refModules = fileRefs.get(path); |
| 307 |
if (refModules.size() > 1) { |
| 308 |
JSModule depModule = getDeepestCommonDependencyInclusive(refModules); |
| 309 |
CompilerInput file = refModules.iterator().next().getByName(path); |
| 310 |
for (JSModule module : refModules) { |
| 311 |
if (module != depModule) { |
| 312 |
module.removeByName(path); |
| 313 |
} |
| 314 |
} |
| 315 |
if (!refModules.contains(depModule)) { |
| 316 |
depModule.add(file); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public List<CompilerInput> manageDependencies( |
| 342 |
List<String> entryPoints, |
| 343 |
List<CompilerInput> inputs) |
| 344 |
throws CircularDependencyException, |
| 345 |
MissingModuleException, |
| 346 |
MissingProvideException { |
| 347 |
DependencyOptions depOptions = new DependencyOptions(); |
| 348 |
depOptions.setDependencySorting(true); |
| 349 |
depOptions.setDependencyPruning(true); |
| 350 |
depOptions.setEntryPoints(entryPoints); |
| 351 |
return manageDependencies(depOptions, inputs); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public List<CompilerInput> manageDependencies( |
| 368 |
DependencyOptions depOptions, |
| 369 |
List<CompilerInput> inputs) |
| 370 |
throws CircularDependencyException, MissingProvideException, |
| 371 |
MissingModuleException { |
| 372 |
|
| 373 |
SortedDependencies<CompilerInput> sorter = |
| 374 |
new SortedDependencies<CompilerInput>(inputs); |
| 375 |
Iterable<CompilerInput> entryPointInputs = createEntryPointInputs( |
| 376 |
depOptions, inputs, sorter); |
| 377 |
|
| 378 |
|
| 379 |
List<CompilerInput> absoluteOrder = |
| 380 |
sorter.getDependenciesOf(inputs, depOptions.shouldSortDependencies()); |
| 381 |
|
| 382 |
|
| 383 |
ListMultimap<JSModule, CompilerInput> entryPointInputsPerModule = |
| 384 |
LinkedListMultimap.create(); |
| 385 |
for (CompilerInput input : entryPointInputs) { |
| 386 |
JSModule module = input.getModule(); |
| 387 |
Preconditions.checkNotNull(module); |
| 388 |
entryPointInputsPerModule.put(module, input); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
for (JSModule module : getAllModules()) { |
| 394 |
module.removeAll(); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
for (JSModule module : entryPointInputsPerModule.keySet()) { |
| 400 |
List<CompilerInput> transitiveClosure = |
| 401 |
sorter.getDependenciesOf( |
| 402 |
entryPointInputsPerModule.get(module), |
| 403 |
depOptions.shouldSortDependencies()); |
| 404 |
for (CompilerInput input : transitiveClosure) { |
| 405 |
JSModule oldModule = input.getModule(); |
| 406 |
if (oldModule == null) { |
| 407 |
input.setModule(module); |
| 408 |
} else { |
| 409 |
input.setModule(null); |
| 410 |
input.setModule( |
| 411 |
getDeepestCommonDependencyInclusive(oldModule, module)); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
for (CompilerInput input : absoluteOrder) { |
| 419 |
JSModule module = input.getModule(); |
| 420 |
if (module != null) { |
| 421 |
module.add(input); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
List<CompilerInput> result = Lists.newArrayList(); |
| 427 |
for (JSModule module : getAllModules()) { |
| 428 |
result.addAll(module.getInputs()); |
| 429 |
} |
| 430 |
|
| 431 |
return result; |
| 432 |
} |
| 433 |
|
| 434 |
private Collection<CompilerInput> createEntryPointInputs( |
| 435 |
DependencyOptions depOptions, |
| 436 |
List<CompilerInput> inputs, |
| 437 |
SortedDependencies<CompilerInput> sorter) |
| 438 |
throws MissingModuleException, MissingProvideException { |
| 439 |
Set<CompilerInput> entryPointInputs = Sets.newLinkedHashSet(); |
| 440 |
Map<String, JSModule> modulesByName = getModulesByName(); |
| 441 |
|
| 442 |
if (depOptions.shouldPruneDependencies()) { |
| 443 |
if (!depOptions.shouldDropMoochers()) { |
| 444 |
entryPointInputs.addAll(sorter.getInputsWithoutProvides()); |
| 445 |
} |
| 446 |
|
| 447 |
for (String entryPoint : depOptions.getEntryPoints()) { |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
String inputName = entryPoint; |
| 452 |
int splitPoint = entryPoint.indexOf(':'); |
| 453 |
CompilerInput entryPointInput = null; |
| 454 |
if (splitPoint != -1) { |
| 455 |
String moduleName = entryPoint.substring(0, splitPoint); |
| 456 |
inputName = entryPoint.substring( |
| 457 |
Math.min(splitPoint + 1, entryPoint.length() - 1)); |
| 458 |
JSModule module = modulesByName.get(moduleName); |
| 459 |
if (module == null) { |
| 460 |
throw new MissingModuleException(moduleName); |
| 461 |
} else { |
| 462 |
entryPointInput = sorter.getInputProviding(inputName); |
| 463 |
entryPointInput.overrideModule(module); |
| 464 |
} |
| 465 |
} else { |
| 466 |
entryPointInput = sorter.getInputProviding(inputName); |
| 467 |
} |
| 468 |
|
| 469 |
entryPointInputs.add(entryPointInput); |
| 470 |
} |
| 471 |
|
| 472 |
CompilerInput baseJs = sorter.maybeGetInputProviding("goog"); |
| 473 |
if (baseJs != null) { |
| 474 |
entryPointInputs.add(baseJs); |
| 475 |
} |
| 476 |
} else { |
| 477 |
entryPointInputs.addAll(inputs); |
| 478 |
} |
| 479 |
return entryPointInputs; |
| 480 |
} |
| 481 |
|
| 482 |
LinkedDirectedGraph<JSModule, String> toGraphvizGraph() { |
| 483 |
LinkedDirectedGraph<JSModule, String> graphViz = |
| 484 |
LinkedDirectedGraph.create(); |
| 485 |
for (JSModule module : getAllModules()) { |
| 486 |
graphViz.createNode(module); |
| 487 |
for (JSModule dep : module.getDependencies()) { |
| 488 |
graphViz.createNode(dep); |
| 489 |
graphViz.connect(module, "->", dep); |
| 490 |
} |
| 491 |
} |
| 492 |
return graphViz; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
private class InverseDepthComparator implements Comparator<JSModule> { |
| 500 |
@Override |
| 501 |
public int compare(JSModule m1, JSModule m2) { |
| 502 |
return depthCompare(m2, m1); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
private int depthCompare(JSModule m1, JSModule m2) { |
| 507 |
if (m1 == m2) { |
| 508 |
return 0; |
| 509 |
} |
| 510 |
int d1 = m1.getDepth(); |
| 511 |
int d2 = m2.getDepth(); |
| 512 |
return d1 < d2 ? -1 : d2 == d1 ? m1.getName().compareTo(m2.getName()) : 1; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
protected static class ModuleDependenceException |
| 522 |
extends IllegalArgumentException { |
| 523 |
private static final long serialVersionUID = 1; |
| 524 |
|
| 525 |
private final JSModule module; |
| 526 |
private final JSModule dependentModule; |
| 527 |
|
| 528 |
protected ModuleDependenceException(String message, |
| 529 |
JSModule module, JSModule dependentModule) { |
| 530 |
super(message); |
| 531 |
this.module = module; |
| 532 |
this.dependentModule = dependentModule; |
| 533 |
} |
| 534 |
|
| 535 |
public JSModule getModule() { |
| 536 |
return module; |
| 537 |
} |
| 538 |
|
| 539 |
public JSModule getDependentModule() { |
| 540 |
return dependentModule; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
public static class MissingModuleException extends Exception { |
| 545 |
MissingModuleException(String moduleName) { |
| 546 |
super(moduleName); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
} |