| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.deps; |
| 18 |
|
| 19 |
import com.google.common.base.Function; |
| 20 |
import com.google.common.base.Joiner; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Maps; |
| 23 |
import com.google.common.collect.Multimap; |
| 24 |
import com.google.common.collect.Multimaps; |
| 25 |
import com.google.common.collect.Sets; |
| 26 |
import com.google.javascript.jscomp.CheckLevel; |
| 27 |
import com.google.javascript.jscomp.DiagnosticType; |
| 28 |
import com.google.javascript.jscomp.ErrorManager; |
| 29 |
import com.google.javascript.jscomp.JSError; |
| 30 |
import com.google.javascript.jscomp.SourceFile; |
| 31 |
|
| 32 |
import java.io.ByteArrayOutputStream; |
| 33 |
import java.io.File; |
| 34 |
import java.io.IOException; |
| 35 |
import java.io.PrintStream; |
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.Collection; |
| 38 |
import java.util.List; |
| 39 |
import java.util.Map; |
| 40 |
import java.util.Set; |
| 41 |
import java.util.logging.Logger; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class DepsGenerator { |
| 50 |
|
| 51 |
public static enum InclusionStrategy { |
| 52 |
ALWAYS, |
| 53 |
WHEN_IN_SRCS, |
| 54 |
DO_NOT_DUPLICATE |
| 55 |
} |
| 56 |
|
| 57 |
private static Logger logger = |
| 58 |
Logger.getLogger(DepsGenerator.class.getName()); |
| 59 |
|
| 60 |
|
| 61 |
private final Collection<SourceFile> srcs; |
| 62 |
private final Collection<SourceFile> deps; |
| 63 |
private final String closurePathAbs; |
| 64 |
private final InclusionStrategy mergeStrategy; |
| 65 |
final ErrorManager errorManager; |
| 66 |
|
| 67 |
static final DiagnosticType SAME_FILE_WARNING = DiagnosticType.warning( |
| 68 |
"DEPS_SAME_FILE", |
| 69 |
"Namespace \"{0}\" is both required and provided in the same file."); |
| 70 |
|
| 71 |
static final DiagnosticType NEVER_PROVIDED_ERROR = DiagnosticType.error( |
| 72 |
"DEPS_NEVER_PROVIDED", |
| 73 |
"Namespace \"{0}\" is required but never provided."); |
| 74 |
|
| 75 |
static final DiagnosticType DUPE_PROVIDES_WARNING = DiagnosticType.warning( |
| 76 |
"DEPS_DUPE_PROVIDES", |
| 77 |
"Multiple calls to goog.provide(\"{0}\")"); |
| 78 |
|
| 79 |
static final DiagnosticType MULTIPLE_PROVIDES_ERROR = DiagnosticType.error( |
| 80 |
"DEPS_DUPE_PROVIDES", |
| 81 |
"Namespace \"{0}\" is already provided in other file {1}"); |
| 82 |
|
| 83 |
static final DiagnosticType DUPE_REQUIRE_WARNING = DiagnosticType.warning( |
| 84 |
"DEPS_DUPE_REQUIRES", |
| 85 |
"Namespace \"{0}\" is required multiple times"); |
| 86 |
|
| 87 |
static final DiagnosticType NO_DEPS_WARNING = DiagnosticType.warning( |
| 88 |
"DEPS_NO_DEPS", |
| 89 |
"No dependencies found in file"); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public DepsGenerator( |
| 95 |
Collection<SourceFile> deps, |
| 96 |
Collection<SourceFile> srcs, |
| 97 |
InclusionStrategy mergeStrategy, |
| 98 |
String closurePathAbs, |
| 99 |
ErrorManager errorManager) { |
| 100 |
this.deps = deps; |
| 101 |
this.srcs = srcs; |
| 102 |
this.mergeStrategy = mergeStrategy; |
| 103 |
this.closurePathAbs = closurePathAbs; |
| 104 |
this.errorManager = errorManager; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public String computeDependencyCalls() throws IOException { |
| 114 |
|
| 115 |
Map<String, DependencyInfo> depsFiles = parseDepsFiles(); |
| 116 |
logger.fine("preparsedFiles: " + depsFiles); |
| 117 |
|
| 118 |
|
| 119 |
Map<String, DependencyInfo> jsFiles = parseSources(depsFiles.keySet()); |
| 120 |
|
| 121 |
|
| 122 |
if (errorManager.getErrorCount() > 0) { |
| 123 |
return null; |
| 124 |
} |
| 125 |
|
| 126 |
cleanUpDuplicatedFiles(depsFiles, jsFiles); |
| 127 |
|
| 128 |
|
| 129 |
validateDependencies(depsFiles.values(), jsFiles.values()); |
| 130 |
|
| 131 |
if (errorManager.getErrorCount() > 0) { |
| 132 |
return null; |
| 133 |
} |
| 134 |
|
| 135 |
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 136 |
writeDepsContent(depsFiles, jsFiles, new PrintStream(output)); |
| 137 |
return new String(output.toByteArray()); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected void cleanUpDuplicatedFiles(Map<String, DependencyInfo> depsFiles, |
| 148 |
Map<String, DependencyInfo> jsFiles) { |
| 149 |
Set<String> depsPathsCopy = Sets.newHashSet(depsFiles.keySet()); |
| 150 |
for (String path : depsPathsCopy) { |
| 151 |
if (mergeStrategy != InclusionStrategy.WHEN_IN_SRCS) { |
| 152 |
jsFiles.remove(path); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
for (String path : jsFiles.keySet()) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
depsFiles.remove(path); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private void validateDependencies(Iterable<DependencyInfo> preparsedFileDepedencies, |
| 176 |
Iterable<DependencyInfo> parsedFileDependencies) { |
| 177 |
|
| 178 |
|
| 179 |
Map<String, DependencyInfo> providesMap = Maps.newHashMap(); |
| 180 |
addToProvideMap(preparsedFileDepedencies, providesMap); |
| 181 |
addToProvideMap(parsedFileDependencies, providesMap); |
| 182 |
|
| 183 |
for (DependencyInfo depInfo : parsedFileDependencies) { |
| 184 |
List<String> requires = Lists.newArrayList(depInfo.getRequires()); |
| 185 |
for (int i = 0, l = requires.size(); i < l; ++i) { |
| 186 |
String namespace = requires.get(i); |
| 187 |
|
| 188 |
if (requires.subList(i + 1, l).contains(namespace)) { |
| 189 |
reportDuplicateRequire(namespace, depInfo); |
| 190 |
} |
| 191 |
|
| 192 |
DependencyInfo provider = providesMap.get(namespace); |
| 193 |
if (provider == null) { |
| 194 |
reportUndefinedNamespace(namespace, depInfo); |
| 195 |
} else if (provider == depInfo) { |
| 196 |
reportSameFile(namespace, depInfo); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private void reportSameFile(String namespace, DependencyInfo depInfo) { |
| 203 |
errorManager.report(CheckLevel.WARNING, |
| 204 |
JSError.make(depInfo.getName(), -1, -1, |
| 205 |
SAME_FILE_WARNING, namespace)); |
| 206 |
} |
| 207 |
|
| 208 |
private void reportUndefinedNamespace( |
| 209 |
String namespace, DependencyInfo depInfo) { |
| 210 |
errorManager.report(CheckLevel.ERROR, |
| 211 |
JSError.make(depInfo.getName(), -1, -1, |
| 212 |
NEVER_PROVIDED_ERROR, namespace)); |
| 213 |
} |
| 214 |
|
| 215 |
private void reportDuplicateProvide(String namespace, DependencyInfo firstDep, |
| 216 |
DependencyInfo secondDep) { |
| 217 |
if (firstDep == secondDep) { |
| 218 |
errorManager.report(CheckLevel.WARNING, |
| 219 |
JSError.make(firstDep.getName(), -1, -1, |
| 220 |
DUPE_PROVIDES_WARNING, namespace)); |
| 221 |
} else { |
| 222 |
errorManager.report(CheckLevel.ERROR, |
| 223 |
JSError.make(secondDep.getName(), -1, -1, |
| 224 |
MULTIPLE_PROVIDES_ERROR, namespace, firstDep.getName())); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
private void reportDuplicateRequire( |
| 229 |
String namespace, DependencyInfo depInfo) { |
| 230 |
errorManager.report(CheckLevel.WARNING, |
| 231 |
JSError.make(depInfo.getName(), -1, -1, |
| 232 |
DUPE_REQUIRE_WARNING, namespace)); |
| 233 |
} |
| 234 |
|
| 235 |
private void reportNoDepsInDepsFile(String filePath) { |
| 236 |
errorManager.report(CheckLevel.WARNING, |
| 237 |
JSError.make(filePath, -1, -1, NO_DEPS_WARNING)); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private void addToProvideMap(Iterable<DependencyInfo> depInfos, |
| 245 |
Map<String, DependencyInfo> providesMap) { |
| 246 |
for (DependencyInfo depInfo : depInfos) { |
| 247 |
for (String provide : depInfo.getProvides()) { |
| 248 |
DependencyInfo prevValue = providesMap.put(provide, depInfo); |
| 249 |
|
| 250 |
if (prevValue != null) { |
| 251 |
reportDuplicateProvide(provide, prevValue, depInfo); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
protected DepsFileParser createDepsFileParser() { |
| 258 |
DepsFileParser depsParser = new DepsFileParser(errorManager); |
| 259 |
depsParser.setShortcutMode(true); |
| 260 |
return depsParser; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
protected boolean shouldSkipDepsFile(SourceFile file) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private Map<String, DependencyInfo> parseDepsFiles() throws IOException { |
| 275 |
DepsFileParser depsParser = createDepsFileParser(); |
| 276 |
Map<String, DependencyInfo> depsFiles = Maps.newHashMap(); |
| 277 |
for (SourceFile file : deps) { |
| 278 |
if (!shouldSkipDepsFile(file)) { |
| 279 |
List<DependencyInfo> |
| 280 |
depInfos = depsParser.parseFileReader( |
| 281 |
file.getName(), file.getCodeReader()); |
| 282 |
if (depInfos.isEmpty()) { |
| 283 |
reportNoDepsInDepsFile(file.getName()); |
| 284 |
} else { |
| 285 |
for (DependencyInfo info : depInfos) { |
| 286 |
depsFiles.put(info.getPathRelativeToClosureBase(), info); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
for (SourceFile src : srcs) { |
| 296 |
if ((new File(src.getName())).exists() && |
| 297 |
!shouldSkipDepsFile(src)) { |
| 298 |
List<DependencyInfo> srcInfos = |
| 299 |
depsParser.parseFileReader(src.getName(), src.getCodeReader()); |
| 300 |
for (DependencyInfo info : srcInfos) { |
| 301 |
depsFiles.put(info.getPathRelativeToClosureBase(), info); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
return depsFiles; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
private Map<String, DependencyInfo> parseSources( |
| 318 |
Set<String> preparsedFiles) throws IOException { |
| 319 |
Map<String, DependencyInfo> parsedFiles = Maps.newHashMap(); |
| 320 |
JsFileParser jsParser = new JsFileParser(errorManager); |
| 321 |
|
| 322 |
for (SourceFile file : srcs) { |
| 323 |
String closureRelativePath = |
| 324 |
PathUtil.makeRelative( |
| 325 |
closurePathAbs, PathUtil.makeAbsolute(file.getName())); |
| 326 |
logger.fine("Closure-relative path: " + closureRelativePath); |
| 327 |
|
| 328 |
if (InclusionStrategy.WHEN_IN_SRCS == mergeStrategy || |
| 329 |
!preparsedFiles.contains(closureRelativePath)) { |
| 330 |
DependencyInfo depInfo = |
| 331 |
jsParser.parseFile( |
| 332 |
file.getName(), closureRelativePath, |
| 333 |
file.getCode()); |
| 334 |
|
| 335 |
|
| 336 |
file.clearCachedSource(); |
| 337 |
parsedFiles.put(closureRelativePath, depInfo); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
return parsedFiles; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
private void writeDepsContent(Map<String, DependencyInfo> depsFiles, |
| 351 |
Map<String, DependencyInfo> jsFiles, PrintStream out) |
| 352 |
throws IOException { |
| 353 |
|
| 354 |
writeDepInfos(out, jsFiles.values()); |
| 355 |
|
| 356 |
|
| 357 |
if (mergeStrategy == InclusionStrategy.ALWAYS) { |
| 358 |
|
| 359 |
|
| 360 |
Multimap<String, DependencyInfo> infosIndex = Multimaps.index( |
| 361 |
depsFiles.values(), |
| 362 |
new Function<DependencyInfo, String>() { |
| 363 |
@Override |
| 364 |
public String apply(DependencyInfo from) { |
| 365 |
return from.getName(); |
| 366 |
} |
| 367 |
}); |
| 368 |
|
| 369 |
for (String depsPath : infosIndex.keySet()) { |
| 370 |
String path = formatPathToDepsFile(depsPath); |
| 371 |
out.println("\n// Included from: " + path); |
| 372 |
writeDepInfos(out, infosIndex.get(depsPath)); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
protected String formatPathToDepsFile(String path) { |
| 381 |
return path; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
private void writeDepInfos(PrintStream out, |
| 389 |
Collection<DependencyInfo> depInfos |
| 390 |
) throws IOException { |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
for (DependencyInfo depInfo : depInfos) { |
| 396 |
Collection<String> provides = depInfo.getProvides(); |
| 397 |
Collection<String> requires = depInfo.getRequires(); |
| 398 |
|
| 399 |
out.print("goog.addDependency('" + |
| 400 |
depInfo.getPathRelativeToClosureBase() + "', "); |
| 401 |
writeJsArray(out, provides); |
| 402 |
out.print(", "); |
| 403 |
writeJsArray(out, requires); |
| 404 |
out.println(");"); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
private static void writeJsArray(PrintStream out, Collection<String> values) { |
| 413 |
if (values.isEmpty()) { |
| 414 |
out.print("[]"); |
| 415 |
} else { |
| 416 |
out.print("['"); |
| 417 |
out.print(Joiner.on("', '").join(values)); |
| 418 |
out.print("']"); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static List<SourceFile> createSourceFilesFromPaths( |
| 423 |
Collection<String> paths) { |
| 424 |
List<SourceFile> files = Lists.newArrayList(); |
| 425 |
for (String path : paths) { |
| 426 |
files.add(SourceFile.fromFile(path)); |
| 427 |
} |
| 428 |
return files; |
| 429 |
} |
| 430 |
|
| 431 |
static List<SourceFile> createSourceFilesFromPaths( |
| 432 |
String ... paths) { |
| 433 |
return createSourceFilesFromPaths(Arrays.asList(paths)); |
| 434 |
} |
| 435 |
} |