Add support for distribution folder.

Changed template directory to be dynamically looked up.

Change-Id: I89b96581e842574e1c7ad36e8633d3e787f488c2
This commit is contained in:
Chiao Cheng
2012-09-21 16:58:32 -07:00
parent 6ad53901b8
commit 6818901ea0
6 changed files with 114 additions and 72 deletions

View File

@@ -1,46 +0,0 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.idegen;
import java.nio.charset.Charset;
/**
* Constants
*/
public class Constants {
public static final Charset CHARSET = Charset.forName("UTF-8");
public static final String REL_TEMPLATE_DIR = "development/tools/idegen/templates";
public static final String REL_MODULES_TEMPLATE = REL_TEMPLATE_DIR + "/idea/modules.xml";
public static final String REL_VCS_TEMPLATE = REL_TEMPLATE_DIR + "/idea/vcs.xml";
public static final String REL_IML_TEMPLATE = REL_TEMPLATE_DIR + "/module-template.iml";
public static final String REL_OUT_APP_DIR = "out/target/common/obj/APPS";
public static final String FRAMEWORK_MODULE = "framework";
public static final String[] AUTO_DEPENDENCIES = new String[]{
FRAMEWORK_MODULE, "libcore"
};
public static final String[] DIRS_WITH_AUTO_DEPENDENCIES = new String[] {
"packages", "vendor", "frameworks/ex", "frameworks/opt", "frameworks/support"
};
// Framework needs a special constant for it's intermediates because it does not follow
// normal conventions.
public static final String FRAMEWORK_INTERMEDIATES = "framework-res_intermediates";
}

View File

@@ -21,8 +21,12 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -31,6 +35,8 @@ import java.util.regex.Pattern;
*/
public class DirectorySearch {
private static final Logger logger = Logger.getLogger(DirectorySearch.class.getName());
private static final HashSet<String> SOURCE_DIRS = Sets.newHashSet();
static {
SOURCE_DIRS.add("src");
@@ -40,6 +46,9 @@ public class DirectorySearch {
private static final Pattern EXCLUDE_PATTERN = Pattern.compile("values-..(-.*)*");
private static File repoRoot = null;
public static final String REL_TEMPLATE_DIR = "templates";
public static final String REL_TEMPLATE_PATH_FROM_ROOT = "development/tools/idegen/"
+ REL_TEMPLATE_DIR;
/**
* Find the repo root. This is the root branch directory of a full repo checkout.
@@ -138,4 +147,38 @@ public class DirectorySearch {
return builder.build();
}
private static File templateDirCurrent = null;
private static File templateDirRoot = null;
public static File findTemplateDir() throws FileNotFoundException {
// Cache optimization.
if (templateDirCurrent != null && templateDirCurrent.exists()) return templateDirCurrent;
if (templateDirRoot != null && templateDirRoot.exists()) return templateDirRoot;
File currentDir = null;
try {
currentDir = new File(IntellijProject.class.getProtectionDomain().getCodeSource()
.getLocation().toURI().getPath()).getParentFile();
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Could not get jar location.", e);
return null;
}
// First check relative to current run directory.
templateDirCurrent = new File(currentDir, REL_TEMPLATE_DIR);
if (templateDirCurrent.exists()) {
return templateDirCurrent;
} else {
// Then check relative to root directory.
templateDirRoot = new File(repoRoot, REL_TEMPLATE_PATH_FROM_ROOT);
if (templateDirRoot.exists()) {
return templateDirRoot;
}
}
throw new FileNotFoundException(
"Unable to find template dir. Tried the following locations:\n" +
templateDirCurrent.getAbsolutePath() + "\n" +
templateDirRoot.getAbsolutePath());
}
}

View File

@@ -25,15 +25,19 @@ import java.io.File;
*/
public class FrameworkModule extends StandardModule {
// Framework needs a special constant for it's intermediates because it does not follow
// normal conventions.
private static final String FRAMEWORK_INTERMEDIATES = "framework-res_intermediates";
public FrameworkModule(String moduleName, String makeFile) {
super(Constants.FRAMEWORK_MODULE, makeFile, true);
super(IntellijProject.FRAMEWORK_MODULE, makeFile, true);
}
@Override
protected String buildIntermediates() {
StringBuilder sb = new StringBuilder();
File intermediates = new File(repoRoot,
Constants.REL_OUT_APP_DIR + File.separator + Constants.FRAMEWORK_INTERMEDIATES);
REL_OUT_APP_DIR + File.separator + FRAMEWORK_INTERMEDIATES);
ImmutableList<File> intermediateSrcDirs = DirectorySearch.findSourceDirs(intermediates);
sb.append(" <content url=\"file://").append(intermediates).append("\">\n");
for (File src : intermediateSrcDirs) {

View File

@@ -22,6 +22,7 @@ import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Set;
import java.util.logging.Logger;
@@ -31,9 +32,16 @@ import java.util.logging.Logger;
*/
public class IntellijProject {
public static final String FRAMEWORK_MODULE = "framework";
public static final Charset CHARSET = Charset.forName("UTF-8");
private static final Logger logger = Logger.getLogger(IntellijProject.class.getName());
private static final String MODULES_TEMPLATE_FILE_NAME = "modules.xml";
private static final String VCS_TEMPLATE_FILE_NAME = "vcs.xml";
ModuleCache cache = ModuleCache.getInstance();
File indexFile;
File repoRoot;
File projectIdeaDir;
@@ -109,19 +117,27 @@ public class IntellijProject {
/**
* Framework module needs special handling due to one off resource path:
* frameworks/base/Android.mk
* frameworks/base/Android.mk
*/
private void buildFrameWorkModule() throws IOException {
String makeFile = cache.getMakeFile(Constants.FRAMEWORK_MODULE);
logger.info("makefile: " + makeFile);
StandardModule frameworkModule = new FrameworkModule(Constants.FRAMEWORK_MODULE, makeFile);
frameworkModule.build();
cache.put(frameworkModule);
String makeFile = cache.getMakeFile(FRAMEWORK_MODULE);
if (makeFile == null) {
logger.warning("Unable to find framework module: " + FRAMEWORK_MODULE +
". Skipping.");
} else {
logger.info("makefile: " + makeFile);
StandardModule frameworkModule = new FrameworkModule(FRAMEWORK_MODULE,
makeFile);
frameworkModule.build();
cache.put(frameworkModule);
}
}
private void createModulesFile(Module module) throws IOException {
String modulesContent = Files.toString(new File(repoRoot, Constants.REL_MODULES_TEMPLATE),
Constants.CHARSET);
String modulesContent = Files.toString(
new File(DirectorySearch.findTemplateDir(),
"idea" + File.separator + MODULES_TEMPLATE_FILE_NAME),
CHARSET);
StringBuilder sb = new StringBuilder();
File moduleIml = module.getImlFile();
sb.append(" <module fileurl=\"file://").append(moduleIml.getAbsolutePath())
@@ -136,12 +152,14 @@ public class IntellijProject {
File out = new File(projectIdeaDir, "modules.xml");
logger.info("Creating " + out.getAbsolutePath());
Files.write(modulesContent, out, Constants.CHARSET);
Files.write(modulesContent, out, CHARSET);
}
private void createVcsFile(Module module) throws IOException {
String vcsTemplate = Files.toString(new File(module.getRepoRoot(),
Constants.REL_VCS_TEMPLATE), Constants.CHARSET);
String vcsTemplate = Files.toString(
new File(DirectorySearch.findTemplateDir(),
"idea" + File.separator + VCS_TEMPLATE_FILE_NAME),
CHARSET);
StringBuilder sb = new StringBuilder();
for (String name : module.getAllDependencies()) {
@@ -154,18 +172,17 @@ public class IntellijProject {
}
}
vcsTemplate = vcsTemplate.replace("@VCS@", sb.toString());
Files.write(vcsTemplate, new File(projectIdeaDir, "vcs.xml"), Constants.CHARSET);
Files.write(vcsTemplate, new File(projectIdeaDir, "vcs.xml"), CHARSET);
}
private void createNameFile(String name) throws IOException {
File out = new File(projectIdeaDir, ".name");
Files.write(name, out, Constants.CHARSET);
File out = new File(projectIdeaDir, ".name");
Files.write(name, out, CHARSET);
}
private void copyTemplates() throws IOException {
File templateDir = new File(repoRoot,
Constants.REL_TEMPLATE_DIR + File.separatorChar + "idea");
copyTemplates(templateDir, projectIdeaDir);
File templateDir = DirectorySearch.findTemplateDir();
copyTemplates(new File(templateDir, "idea"), projectIdeaDir);
}
private void copyTemplates(File fromDir, File toDir) throws IOException {

View File

@@ -34,6 +34,8 @@ public abstract class Module {
private static final Logger logger = Logger.getLogger(Module.class.getName());
private static final String IML_TEMPLATE_FILE_NAME = "module-template.iml";
/**
* All possible attributes for the make file.
*/
@@ -44,23 +46,35 @@ public abstract class Module {
}
ModuleCache moduleCache = ModuleCache.getInstance();
private File imlFile;
private Set<String> allDependencies = Sets.newHashSet(); // direct + indirect
private Set<File> allDependentImlFiles = Sets.newHashSet();
protected abstract void build() throws IOException;
protected abstract String getName();
protected abstract File getDir();
protected abstract boolean isAndroidModule();
protected abstract List<File> getIntermediatesDirs();
public abstract Set<String> getDirectDependencies();
protected abstract ImmutableList<File> getSourceDirs();
protected abstract ImmutableList<File> getExcludeDirs();
public abstract File getRepoRoot();
public void buildImlFile() throws IOException {
String imlTemplate = Files.toString(new File(getRepoRoot(), Constants.REL_IML_TEMPLATE),
Constants.CHARSET);
String imlTemplate = Files.toString(
new File(DirectorySearch.findTemplateDir(), IML_TEMPLATE_FILE_NAME),
IntellijProject.CHARSET);
String facetXml = "";
if (isAndroidModule()) {
@@ -100,7 +114,7 @@ public abstract class Module {
imlFile = new File(moduleDir, getName() + ".iml");
logger.info("Creating " + imlFile.getAbsolutePath());
Files.write(imlTemplate, imlFile, Constants.CHARSET);
Files.write(imlTemplate, imlFile, IntellijProject.CHARSET);
}
protected String buildIntermediates() {

View File

@@ -17,6 +17,7 @@
package com.android.idegen;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@@ -45,10 +46,18 @@ import java.util.regex.Pattern;
*/
public class StandardModule extends Module {
static final String REL_OUT_APP_DIR = "out/target/common/obj/APPS";
private static final Logger logger = Logger.getLogger(StandardModule.class.getName());
private static final Pattern SRC_PATTERN = Pattern.compile(
".*\\(call all-java-files-under, (.*)\\)");
private static final String[] AUTO_DEPENDENCIES = new String[]{
IntellijProject.FRAMEWORK_MODULE, "libcore"
};
private static final String[] DIRS_WITH_AUTO_DEPENDENCIES = new String[]{
"packages", "vendor", "frameworks/ex", "frameworks/opt", "frameworks/support"
};
String moduleName;
File makeFile;
@@ -66,7 +75,8 @@ public class StandardModule extends Module {
}
public StandardModule(String moduleName, String makeFile, boolean searchForSrc) {
this(moduleName, new File(makeFile), searchForSrc);
this(Preconditions.checkNotNull(moduleName), new File(Preconditions.checkNotNull(makeFile)),
searchForSrc);
}
public StandardModule(String moduleName, File makeFile, boolean searchForSrc) {
@@ -75,16 +85,16 @@ public class StandardModule extends Module {
this.moduleRoot = makeFile.getParentFile();
this.repoRoot = DirectorySearch.findRepoRoot(makeFile);
this.intermediatesDir = new File(repoRoot.getAbsolutePath() + File.separator +
Constants.REL_OUT_APP_DIR + File.separator + getName() + "_intermediates" +
REL_OUT_APP_DIR + File.separator + getName() + "_intermediates" +
File.separator + "src");
this.searchForSrc = searchForSrc;
// TODO: auto-detect when framework dependency is needed instead of using coded list.
for (String dir : Constants.DIRS_WITH_AUTO_DEPENDENCIES) {
for (String dir : DIRS_WITH_AUTO_DEPENDENCIES) {
// length + 2 to account for slash
boolean isDir = makeFile.getAbsolutePath().startsWith(repoRoot + "/" + dir);
if (isDir) {
for (String dependency : Constants.AUTO_DEPENDENCIES) {
for (String dependency : AUTO_DEPENDENCIES) {
this.directDependencies.add(dependency);
}
}