• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         StringBuilder jarsXml = new StringBuilder();
64         for (File jar : c.jarFiles) {
65             jarsXml.append("<orderEntry type=\"module-library\">"
66                     + "<library><CLASSES><root url=\"jar://$MODULE_DIR$/")
67                 .append(jar.getPath())
68             .append("!/\"/></CLASSES><JAVADOC/><SOURCES/></library>"
69                     + "</orderEntry>\n");
70         }
71 
72         iml = iml.replace("SOURCE_FOLDERS",
73                 sourceRootsXml.toString() + excludeXml.toString());
74         iml = iml.replace("JAR_ENTRIES", jarsXml.toString());
75 
76         Files.toFile(iml, new File(IDEA_IML));
77     }
78 
isTests(File file)79     private static boolean isTests(File file) {
80         String path = file.getPath();
81 
82         // test-runner is testing infrastructure, not test code.
83         if (path.contains("test-runner")) {
84             return false;
85         }
86 
87         return path.toUpperCase().contains("TEST");
88     }
89 }