1 /*
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 
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.SortedSet;
20 
21 /**
22  * Generates an IntelliJ project.
23  */
24 public class IntelliJ {
25 
26     private static final String IDEA_IML = "android.iml";
27     private static final String IDEA_IPR = "android.ipr";
28 
29     /**
30      * Generates IntelliJ configuration files from the given configuration.
31      */
generateFrom(Configuration c)32     public static void generateFrom(Configuration c) throws IOException {
33         File templatesDirectory = new File(c.toolDirectory, "templates");
34         String ipr = Files.toString(new File(templatesDirectory, IDEA_IPR));
35         Files.toFile(ipr, new File(IDEA_IPR));
36 
37         String iml = Files.toString(new File(templatesDirectory, IDEA_IML));
38 
39         StringBuilder sourceRootsXml = new StringBuilder();
40         for (File sourceRoot : c.sourceRoots) {
41             sourceRootsXml.append("<sourceFolder url=\"file://$MODULE_DIR$/")
42                 .append(sourceRoot.getPath())
43                 .append("\" isTestSource=\"").append(isTests(sourceRoot))
44                 .append("\"/>\n");
45         }
46 
47         /*
48          * IntelliJ excludes are module-wide. We explicitly exclude directories
49          * under source roots but leave the rest in so you can still pull
50          * up random non-Java files.
51          */
52         StringBuilder excludeXml = new StringBuilder();
53         for (File excludedDir : c.excludesUnderSourceRoots()) {
54             sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/")
55                 .append(excludedDir.getPath())
56                 .append("\"/>\n");
57         }
58 
59         // Exclude Eclipse's output directory.
60         sourceRootsXml.append("<excludeFolder "
61                 + "url=\"file://$MODULE_DIR$/out/eclipse\"/>\n");
62 
63         // Exclude some other directories that take a long time to scan.
64         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/.repo\"/>\n");
65         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/external/bluetooth\"/>\n");
66         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/external/chromium\"/>\n");
67         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/external/icu4c\"/>\n");
68         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/external/webkit\"/>\n");
69         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/frameworks/base/docs\"/>\n");
70         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/out/host\"/>\n");
71         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/out/target/common/docs\"/>\n");
72         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates\"/>\n");
73         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/out/target/product\"/>\n");
74         sourceRootsXml.append("<excludeFolder url=\"file://$MODULE_DIR$/prebuilt\"/>\n");
75 
76         StringBuilder jarsXml = new StringBuilder();
77         for (File jar : c.jarFiles) {
78             jarsXml.append("<orderEntry type=\"module-library\">"
79                     + "<library><CLASSES><root url=\"jar://$MODULE_DIR$/")
80                 .append(jar.getPath())
81             .append("!/\"/></CLASSES><JAVADOC/><SOURCES/></library>"
82                     + "</orderEntry>\n");
83         }
84 
85         iml = iml.replace("SOURCE_FOLDERS",
86                 sourceRootsXml.toString() + excludeXml.toString());
87         iml = iml.replace("JAR_ENTRIES", jarsXml.toString());
88 
89         Files.toFile(iml, new File(IDEA_IML));
90     }
91 
isTests(File file)92     private static boolean isTests(File file) {
93         String path = file.getPath();
94 
95         // test-runner is testing infrastructure, not test code.
96         if (path.contains("test-runner")) {
97             return false;
98         }
99 
100         return path.toUpperCase().contains("TEST");
101     }
102 }
103