Home | History | Annotate | Download | only in idegen
      1 /*
      2  * Copyright (C) 2012 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 package com.android.idegen;
     18 
     19 import com.google.common.base.Preconditions;
     20 import com.google.common.collect.ImmutableList;
     21 import com.google.common.collect.Iterables;
     22 import com.google.common.collect.Lists;
     23 import com.google.common.collect.Sets;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.util.HashSet;
     28 import java.util.List;
     29 import java.util.Set;
     30 import java.util.logging.Logger;
     31 
     32 /**
     33  * Module while is a composition of many other modules.
     34  * <p>
     35  * This is needed since intellij does not allow two modules to share the same content root.
     36  */
     37 public class AggregatedModule extends Module {
     38 
     39     private static final Logger logger = Logger.getLogger(AggregatedModule.class.getName());
     40 
     41     private String aggregatedModuleName;
     42     private Set<Module> modules;
     43     private HashSet<String> directDependencies = Sets.newHashSet();
     44 
     45     public AggregatedModule(String aggregatedName, Set<Module> modules) {
     46         this.aggregatedModuleName = Preconditions.checkNotNull(aggregatedName);
     47         this.modules = Preconditions.checkNotNull(modules);
     48     }
     49 
     50     public void build() throws IOException {
     51         // Create an iml file that contains all the srcs of modules.
     52         buildDependentModules();
     53         buildDirectDependencies();
     54         //buildImlFile();
     55     }
     56 
     57     @Override
     58     protected File getDir() {
     59         // All modules should be in the same directory so just pull the first.
     60         return modules.iterator().next().getDir();
     61     }
     62 
     63     @Override
     64     protected boolean isAndroidModule() {
     65         for (Module module : modules) {
     66             if (module.isAndroidModule()) {
     67                 return true;
     68             }
     69         }
     70         return false;
     71     }
     72 
     73     @Override
     74     protected List<File> getIntermediatesDirs() {
     75         List<File> result = Lists.newArrayList();
     76         for (Module module : modules) {
     77             Iterables.addAll(result, module.getIntermediatesDirs());
     78         }
     79         return result;
     80     }
     81 
     82     public void buildDirectDependencies() {
     83         for (Module module : modules) {
     84             Set<String> deps = module.getDirectDependencies();
     85             directDependencies.addAll(deps);
     86         }
     87     }
     88 
     89     @Override
     90     public Set<String> getDirectDependencies() {
     91         return directDependencies;
     92     }
     93 
     94     @Override
     95     protected ImmutableList<File> getSourceDirs() {
     96         ImmutableList.Builder<File> builder = ImmutableList.builder();
     97         for (Module module : modules) {
     98             builder.addAll(module.getSourceDirs());
     99         }
    100         return builder.build();
    101     }
    102 
    103 
    104     @Override
    105     protected ImmutableList<File> getExcludeDirs() {
    106         ImmutableList.Builder<File> builder = ImmutableList.builder();
    107         for (Module module : modules) {
    108             builder.addAll(module.getExcludeDirs());
    109         }
    110         return builder.build();
    111     }
    112 
    113     @Override
    114     public Set<File> getAllDependentImlFiles() {
    115         Set<File> result = Sets.newHashSet();
    116         for (Module module : modules) {
    117             result.addAll(module.getAllDependentImlFiles());
    118         }
    119         return result;
    120     }
    121 
    122     @Override
    123     public File getRepoRoot() {
    124         return modules.iterator().next().getRepoRoot();
    125     }
    126 
    127     @Override
    128     public String getName() {
    129         return aggregatedModuleName;
    130     }
    131 
    132 }
    133