/* * 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 com.google.common.base.Objects; import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.google.common.io.Files; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Set; import java.util.logging.Logger; /** * Super class for all modules. */ public abstract class Module { private static final Logger logger = Logger.getLogger(Module.class.getName()); /** * All possible attributes for the make file. */ protected enum Key { LOCAL_STATIC_JAVA_LIBRARIES, LOCAL_JAVA_LIBRARIES, LOCAL_SRC_FILES } ModuleCache moduleCache = ModuleCache.getInstance(); private File imlFile; private Set allDependencies = Sets.newHashSet(); // direct + indirect private Set allDependentImlFiles = Sets.newHashSet(); protected abstract void build() throws IOException; protected abstract String getName(); protected abstract File getDir(); protected abstract boolean isAndroidModule(); protected abstract List getIntermediatesDirs(); public abstract Set getDirectDependencies(); protected abstract ImmutableList getSourceDirs(); protected abstract ImmutableList getExcludeDirs(); public abstract File getRepoRoot(); public void buildImlFile() throws IOException { String imlTemplate = Files.toString(new File(getRepoRoot(), Constants.REL_IML_TEMPLATE), Constants.CHARSET); String facetXml = ""; if (isAndroidModule()) { facetXml = buildAndroidFacet(); } imlTemplate = imlTemplate.replace("@FACETS@", facetXml); String moduleDir = getDir().getAbsolutePath(); StringBuilder sourceDirectories = new StringBuilder(); sourceDirectories.append(" \n"); ImmutableList srcDirs = getSourceDirs(); for (File src : srcDirs) { String relative = src.getAbsolutePath().substring(moduleDir.length()); sourceDirectories.append(" \n"); } ImmutableList excludeDirs = getExcludeDirs(); for (File src : excludeDirs) { String relative = src.getAbsolutePath().substring(moduleDir.length()); sourceDirectories.append(" \n"); } sourceDirectories.append(" \n"); // Intermediates. sourceDirectories.append(buildIntermediates()); imlTemplate = imlTemplate.replace("@SOURCES@", sourceDirectories.toString()); StringBuilder moduleDependencies = new StringBuilder(); for (String dependency : getDirectDependencies()) { moduleDependencies.append(" \n"); } imlTemplate = imlTemplate.replace("@MODULE_DEPENDENCIES@", moduleDependencies.toString()); imlFile = new File(moduleDir, getName() + ".iml"); logger.info("Creating " + imlFile.getAbsolutePath()); Files.write(imlTemplate, imlFile, Constants.CHARSET); } protected String buildIntermediates() { StringBuilder sb = new StringBuilder(); for (File intermediatesDir : getIntermediatesDirs()) { sb.append(" \n"); sb.append(" \n"); sb.append(" \n"); } return sb.toString(); } protected void buildDependentModules() throws IOException { Set directDependencies = getDirectDependencies(); String[] copy = directDependencies.toArray(new String[directDependencies.size()]); for (String dependency : copy) { Module child = moduleCache.getAndCache(dependency); if (child == null) { directDependencies.remove(dependency); } else { addAllDependencies(dependency); addAllDependencies(child.getAllDependencies()); //logger.info("Adding iml " + child.getName() + " " + child.getImlFile()); allDependentImlFiles.add(child.getImlFile()); allDependentImlFiles.addAll(child.getAllDependentImlFiles()); } } } public File getImlFile() { return imlFile; } public Set getAllDependencies() { return allDependencies; } public void addAllDependencies(String dependency) { this.allDependencies.add(dependency); } public void addAllDependencies(Set dependencies) { this.allDependencies.addAll(dependencies); } public Set getAllDependentImlFiles() { return allDependentImlFiles; } private String buildAndroidFacet() { // Not sure how to handle android facet for multi-module since there could be more than // one intermediates directory. String dir = getIntermediatesDirs().get(0).getAbsolutePath(); String xml = "" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n"; return xml; } @Override public String toString() { return Objects.toStringHelper(Module.class) .add("name", getName()) .add("allDependencies", allDependencies) .add("iml files", allDependentImlFiles) .add("imlFile", imlFile) .toString(); } }