| 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.CharMatcher; |
| 20 |
import com.google.common.base.Function; |
| 21 |
import com.google.common.base.Functions; |
| 22 |
import com.google.common.collect.Lists; |
| 23 |
import com.google.javascript.jscomp.ErrorManager; |
| 24 |
|
| 25 |
import java.io.FileReader; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.Reader; |
| 28 |
import java.io.StringReader; |
| 29 |
import java.util.List; |
| 30 |
import java.util.logging.Level; |
| 31 |
import java.util.logging.Logger; |
| 32 |
import java.util.regex.Matcher; |
| 33 |
import java.util.regex.Pattern; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class DepsFileParser extends JsFileLineParser { |
| 43 |
|
| 44 |
private static Logger logger = Logger.getLogger(DepsFileParser.class.getName()); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private final Matcher depMatcher = |
| 51 |
Pattern.compile("\\s*goog.addDependency\\((.*)\\);?\\s*").matcher(""); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private final Matcher depArgsMatch = |
| 58 |
Pattern.compile("\\s*([^,]*), (\\[[^\\]]*\\]), (\\[[^\\]]*\\])\\s*").matcher(""); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private List<DependencyInfo> depInfos; |
| 64 |
|
| 65 |
|
| 66 |
private final Function<String, String> pathTranslator; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public DepsFileParser(ErrorManager errorManager) { |
| 74 |
this(Functions.<String>identity(), errorManager); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public DepsFileParser(Function<String, String> pathTranslator, |
| 82 |
ErrorManager errorManager) { |
| 83 |
super(errorManager); |
| 84 |
this.pathTranslator = pathTranslator; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public List<DependencyInfo> parseFile(String filePath) throws IOException { |
| 96 |
return parseFileReader(filePath, new FileReader(filePath)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public List<DependencyInfo> parseFile(String filePath, String fileContents) { |
| 109 |
return parseFileReader(filePath, new StringReader(fileContents)); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public List<DependencyInfo> parseFileReader(String filePath, Reader reader) { |
| 122 |
depInfos = Lists.newArrayList(); |
| 123 |
logger.fine("Parsing Dep: " + filePath); |
| 124 |
doParse(filePath, reader); |
| 125 |
return depInfos; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@Override |
| 137 |
protected boolean parseLine(String line) throws ParseException { |
| 138 |
boolean hasDependencies = false; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if (line.indexOf("addDependency") != -1) { |
| 143 |
depMatcher.reset(line); |
| 144 |
|
| 145 |
if (depMatcher.matches()) { |
| 146 |
hasDependencies = true; |
| 147 |
String addDependencyParams = depMatcher.group(1); |
| 148 |
depArgsMatch.reset(addDependencyParams); |
| 149 |
|
| 150 |
if (!depArgsMatch.matches()) { |
| 151 |
|
| 152 |
|
| 153 |
throw new ParseException("Invalid arguments to goog.addDependency(). Found: " |
| 154 |
+ addDependencyParams, true); |
| 155 |
} |
| 156 |
|
| 157 |
String path = pathTranslator.apply(parseJsString(depArgsMatch.group(1))); |
| 158 |
DependencyInfo depInfo = new SimpleDependencyInfo(path, filePath, |
| 159 |
|
| 160 |
parseJsStringArray(depArgsMatch.group(2)), |
| 161 |
|
| 162 |
parseJsStringArray(depArgsMatch.group(3))); |
| 163 |
|
| 164 |
if (logger.isLoggable(Level.FINE)) { |
| 165 |
logger.fine("Found dep: " + depInfo); |
| 166 |
} |
| 167 |
depInfos.add(depInfo); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return !shortcutMode || hasDependencies || |
| 172 |
CharMatcher.WHITESPACE.matchesAllOf(line); |
| 173 |
} |
| 174 |
} |