1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 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.badlogic.gdx.setup; 18 19 import com.badlogic.gdx.setup.DependencyBank.ProjectType; 20 21 import java.io.BufferedWriter; 22 import java.io.File; 23 import java.io.FileWriter; 24 import java.io.IOException; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 public class ProjectBuilder { 29 30 DependencyBank bank; 31 List<ProjectType> modules = new ArrayList<ProjectType>(); 32 List<Dependency> dependencies = new ArrayList<Dependency>(); 33 File settingsFile; 34 File buildFile; 35 36 public ProjectBuilder(DependencyBank bank) { 37 this.bank = bank; 38 } 39 40 public List<String> buildProject(List<ProjectType> projects, List<Dependency> dependencies) { 41 List<String> incompatibilities = new ArrayList<String>(); 42 for (Dependency dep : dependencies) { 43 for (ProjectType type : projects) { 44 dep.getDependencies(type); 45 incompatibilities.addAll(dep.getIncompatibilities(type)); 46 } 47 } 48 this.modules = projects; 49 this.dependencies = dependencies; 50 return incompatibilities; 51 } 52 53 public boolean build() throws IOException { 54 settingsFile = File.createTempFile("libgdx-setup-settings", ".gradle"); 55 buildFile = File.createTempFile("libgdx-setup-build", ".gradle"); 56 if (!settingsFile.exists()) { 57 settingsFile.createNewFile(); 58 } 59 if (!buildFile.exists()) { 60 buildFile.createNewFile(); 61 } 62 settingsFile.setWritable(true); 63 buildFile.setWritable(true); 64 try { 65 FileWriter settingsWriter = new FileWriter(settingsFile.getAbsoluteFile()); 66 BufferedWriter settingsBw = new BufferedWriter(settingsWriter); 67 String settingsContents = "include "; 68 for (ProjectType module : modules) { 69 settingsContents += "'" + module.getName() + "'"; 70 if (modules.indexOf(module) != modules.size() - 1) { 71 settingsContents += ", "; 72 } 73 } 74 settingsBw.write(settingsContents); 75 settingsBw.close(); 76 settingsWriter.close(); 77 78 FileWriter buildWriter = new FileWriter(buildFile.getAbsoluteFile()); 79 BufferedWriter buildBw = new BufferedWriter(buildWriter); 80 81 BuildScriptHelper.addBuildScript(modules, buildBw); 82 BuildScriptHelper.addAllProjects(buildBw); 83 for (ProjectType module : modules) { 84 BuildScriptHelper.addProject(module, dependencies, buildBw); 85 } 86 87 //Add task here for now 88 buildBw.write("\n"); 89 buildBw.write("tasks.eclipse.doLast {\n"); 90 buildBw.write(" delete \".project\"\n"); 91 buildBw.write("}"); 92 93 buildBw.close(); 94 buildWriter.close(); 95 return true; 96 } catch (IOException e) { 97 e.printStackTrace(); 98 return false; 99 } 100 } 101 102 public void cleanUp() { 103 settingsFile.deleteOnExit(); 104 buildFile.deleteOnExit(); 105 } 106 107 } 108