blob: e92b58eb2db0a25632cd7b68ceda5d09d88bc2e5 [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.io.File;
18import java.io.IOException;
19import java.io.BufferedReader;
20import java.io.FileReader;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.List;
25import java.util.Set;
26import java.util.TreeSet;
27import java.util.SortedSet;
28import java.util.regex.Pattern;
29
30/**
31 * Immutable representation of an IDE configuration. Assumes that the current
32 * directory is the project's root directory.
33 */
34public class Configuration {
35
36 /** Java source tree roots. */
37 public final SortedSet<File> sourceRoots;
38
39 /** Found .jar files (that weren't excluded). */
40 public final List<File> jarFiles;
41
42 /** Excluded directories which may or may not be under a source root. */
43 public final SortedSet<File> excludedDirs;
44
45 /** The root directory for this tool. */
46 public final File toolDirectory;
47
48 /** File name used for excluded path files. */
49 private static final String EXCLUDED_PATHS = "excluded-paths";
50
51 /**
52 * Constructs a Configuration by traversing the directory tree, looking
53 * for .java and .jar files and identifying source roots.
54 */
55 public Configuration() throws IOException {
56 this.toolDirectory = new File("development/tools/idegen");
57 if (!toolDirectory.isDirectory()) {
58 // The wrapper script should have already verified this.
59 throw new AssertionError("Not in root directory.");
60 }
61
62 Stopwatch stopwatch = new Stopwatch();
63
64 Excludes excludes = readExcludes();
65
66 stopwatch.reset("Read excludes");
67
68 List<File> jarFiles = new ArrayList<File>(500);
69 SortedSet<File> excludedDirs = new TreeSet<File>();
70 SortedSet<File> sourceRoots = new TreeSet<File>();
71
72 traverse(new File("."), sourceRoots, jarFiles, excludedDirs, excludes);
73
74 stopwatch.reset("Traversed tree");
75
76 Log.debug(sourceRoots.size() + " source roots");
77 Log.debug(jarFiles.size() + " jar files");
78 Log.debug(excludedDirs.size() + " excluded dirs");
79
80 this.sourceRoots = Collections.unmodifiableSortedSet(sourceRoots);
81 this.jarFiles = Collections.unmodifiableList(jarFiles);
82 this.excludedDirs = Collections.unmodifiableSortedSet(excludedDirs);
83 }
84
85 /**
86 * Reads excluded path files.
87 */
88 private Excludes readExcludes() throws IOException {
89 List<Pattern> patterns = new ArrayList<Pattern>();
90
91 File globalExcludes = new File(toolDirectory, EXCLUDED_PATHS);
92 parseFile(globalExcludes, patterns);
93
94 // Look for Google-specific excludes.
95 // TODO: Traverse all vendor-specific directories.
96 File googleExcludes = new File("./vendor/google/" + EXCLUDED_PATHS);
97 if (googleExcludes.exists()) {
98 parseFile(googleExcludes, patterns);
99 }
100
101 // Look for user-specific excluded-paths file in current directory.
102 File localExcludes = new File(EXCLUDED_PATHS);
103 if (localExcludes.exists()) {
104 parseFile(localExcludes, patterns);
105 }
106
107 return new Excludes(patterns);
108 }
109
110 /**
111 * Recursively finds .java source roots, .jar files, and excluded
112 * directories.
113 */
114 private static void traverse(File directory, Set<File> sourceRoots,
115 Collection<File> jarFiles, Collection<File> excludedDirs,
116 Excludes excludes) throws IOException {
117 /*
118 * Note it would be faster to stop traversing a source root as soon as
119 * we encounter the first .java file, but it appears we have nested
120 * source roots in our generated source directory (specifically,
121 * R.java files and aidl .java files don't share the same source
122 * root).
123 */
124
125 boolean firstJavaFile = true;
Paul Crowley7fdedf02014-10-21 13:02:46 +0100126 File[] files = directory.listFiles();
127 if (files == null) {
128 return;
129 }
Debajit Ghosh5f2ff002009-09-01 22:20:31 -0700130 for (File file : files) {
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800131 // Trim preceding "./" from path.
132 String path = file.getPath().substring(2);
133
Ying Wang8ee57972015-11-02 17:07:04 -0800134 // Skip nonexistent files/diretories, e.g. broken symlinks.
135 if (!file.exists()) {
136 Log.debug("Skipped nonexistent: " + path);
137 continue;
138 }
139
Paul Crowley7fdedf02014-10-21 13:02:46 +0100140 if (file.isDirectory()) {
141 // Traverse nested directories.
142 if (excludes.exclude(path)) {
143 // Don't recurse into excluded dirs.
144 Log.debug("Excluding: " + path);
145 excludedDirs.add(file);
146 } else {
147 traverse(file, sourceRoots, jarFiles, excludedDirs,
148 excludes);
149 }
150 } else if (path.endsWith(".java")) {
151 // Keep track of source roots for .java files.
152 // Do not check excludes in this branch.
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800153 if (firstJavaFile) {
154 // Only parse one .java file per directory.
155 firstJavaFile = false;
156
157 File sourceRoot = rootOf(file);
158 if (sourceRoot != null) {
159 sourceRoots.add(sourceRoot);
160 }
161 }
Paul Crowley7fdedf02014-10-21 13:02:46 +0100162 } else if (path.endsWith(".jar")) {
163 // Keep track of .jar files.
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800164 if (excludes.exclude(path)) {
Paul Crowley7fdedf02014-10-21 13:02:46 +0100165 Log.debug("Skipped: " + file);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800166 } else {
Paul Crowley7fdedf02014-10-21 13:02:46 +0100167 jarFiles.add(file);
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800168 }
169 }
170 }
171 }
172
173 /**
174 * Determines the source root for a given .java file. Returns null
175 * if the file doesn't have a package or if the file isn't in the
176 * correct directory structure.
177 */
178 private static File rootOf(File javaFile) throws IOException {
179 String packageName = parsePackageName(javaFile);
180 if (packageName == null) {
181 // No package.
182 // TODO: Treat this as a source root?
183 return null;
184 }
185
186 String packagePath = packageName.replace('.', File.separatorChar);
187 File parent = javaFile.getParentFile();
188 String parentPath = parent.getPath();
189 if (!parentPath.endsWith(packagePath)) {
190 // Bad dir structure.
191 return null;
192 }
193
194 return new File(parentPath.substring(
195 0, parentPath.length() - packagePath.length()));
196 }
197
198 /**
199 * Reads a Java file and parses out the package name. Returns null if none
200 * found.
201 */
202 private static String parsePackageName(File file) throws IOException {
203 BufferedReader in = new BufferedReader(new FileReader(file));
204 try {
205 String line;
206 while ((line = in.readLine()) != null) {
207 String trimmed = line.trim();
208 if (trimmed.startsWith("package")) {
209 // TODO: Make this more robust.
210 // Assumes there's only once space after "package" and the
211 // line ends in a ";".
212 return trimmed.substring(8, trimmed.length() - 1);
213 }
214 }
215
216 return null;
217 } finally {
218 in.close();
219 }
220 }
221
222 /**
223 * Picks out excluded directories that are under source roots.
224 */
225 public SortedSet<File> excludesUnderSourceRoots() {
226 // TODO: Refactor this to share the similar logic in
227 // Eclipse.constructExcluding().
228 SortedSet<File> picked = new TreeSet<File>();
229 for (File sourceRoot : sourceRoots) {
230 String sourcePath = sourceRoot.getPath() + "/";
231 SortedSet<File> tailSet = excludedDirs.tailSet(sourceRoot);
232 for (File file : tailSet) {
233 if (file.getPath().startsWith(sourcePath)) {
234 picked.add(file);
235 } else {
236 break;
237 }
238 }
239 }
240 return picked;
241 }
242
243 /**
244 * Reads a list of regular expressions from a file, one per line, and adds
245 * the compiled patterns to the given collection. Ignores lines starting
246 * with '#'.
247 *
248 * @param file containing regular expressions, one per line
249 * @param patterns collection to add compiled patterns from file to
250 */
251 public static void parseFile(File file, Collection<Pattern> patterns)
252 throws IOException {
253 BufferedReader in = new BufferedReader(new FileReader(file));
254 try {
255 String line;
256 while ((line = in.readLine()) != null) {
257 String trimmed = line.trim();
258 if (trimmed.length() > 0 && !trimmed.startsWith("#")) {
259 patterns.add(Pattern.compile(trimmed));
260 }
261 }
262 } finally {
263 in.close();
264 }
265 }
266}