Home | History | Annotate | Download | only in setup
      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 
     20 import com.badlogic.gdx.setup.DependencyBank.ProjectType;
     21 
     22 import java.io.BufferedWriter;
     23 import java.io.IOException;
     24 import java.util.List;
     25 
     26 public class BuildScriptHelper {
     27 
     28 	private static int indent = 0;
     29 
     30 	public static void addBuildScript(List<ProjectType> projects, BufferedWriter wr) throws IOException {
     31 		write(wr, "buildscript {");
     32 		//repos
     33 		write(wr, "repositories {");
     34 		write(wr, DependencyBank.mavenLocal);
     35 		write(wr, DependencyBank.mavenCentral);
     36 		write(wr, "maven { url \"" + DependencyBank.libGDXSnapshotsUrl + "\" }");
     37 		if (projects.contains(ProjectType.HTML)) {
     38 			write(wr, DependencyBank.jCenter);
     39 		}
     40 		if (projects.contains(ProjectType.IOSMOE)) {
     41 			write(wr, "maven { url " + DependencyBank.moeLocalUrl + " }");
     42 		}
     43 		write(wr, "}");
     44 		//dependencies
     45 		write(wr, "dependencies {");
     46 		if (projects.contains(ProjectType.HTML)) {
     47 			write(wr, "classpath '" + DependencyBank.gwtPluginImport + "'");
     48 		}
     49 		if (projects.contains(ProjectType.ANDROID)) {
     50 			write(wr, "classpath '" + DependencyBank.androidPluginImport + "'");
     51 		}
     52 		if (projects.contains(ProjectType.IOS)) {
     53 			write(wr, "classpath '" + DependencyBank.roboVMPluginImport + "'");
     54 		}
     55 		if (projects.contains(ProjectType.IOSMOE)) {
     56 			write(wr, "classpath '" + DependencyBank.moePluginImport + "'");
     57 		}
     58 		write(wr, "}");
     59 		write(wr, "}");
     60 		space(wr);
     61 	}
     62 
     63 	public static void addAllProjects(BufferedWriter wr) throws IOException {
     64 		write(wr, "allprojects {");
     65 		write(wr, "apply plugin: \"eclipse\"");
     66 		write(wr, "apply plugin: \"idea\"");
     67 		space(wr);
     68 		write(wr, "version = '1.0'");
     69 		write(wr, "ext {");
     70 		write(wr, "appName = \"%APP_NAME%\"");
     71 		write(wr, "gdxVersion = '" + DependencyBank.libgdxVersion + "'");
     72 		write(wr, "roboVMVersion = '" + DependencyBank.roboVMVersion + "'");
     73 		write(wr, "box2DLightsVersion = '" + DependencyBank.box2DLightsVersion + "'");
     74 		write(wr, "ashleyVersion = '" + DependencyBank.ashleyVersion + "'");
     75 		write(wr, "aiVersion = '" + DependencyBank.aiVersion + "'");
     76 		write(wr, "}");
     77 		space(wr);
     78 		write(wr, "repositories {");
     79 		write(wr, DependencyBank.mavenLocal);
     80 		write(wr, DependencyBank.mavenCentral);
     81 		write(wr, "maven { url \"" + DependencyBank.libGDXSnapshotsUrl + "\" }");
     82 		write(wr, "maven { url \"" + DependencyBank.libGDXReleaseUrl + "\" }");
     83 		write(wr, "}");
     84 		write(wr, "}");
     85 	}
     86 
     87 	public static void addProject(ProjectType project, List<Dependency> dependencies, BufferedWriter wr) throws IOException {
     88 		space(wr);
     89 		write(wr, "project(\":" + project.getName() + "\") {");
     90 		for (String plugin : project.getPlugins()) {
     91 			write(wr, "apply plugin: \"" + plugin + "\"");
     92 		}
     93 		space(wr);
     94 		addConfigurations(project, wr);
     95 		space(wr);
     96 		addDependencies(project, dependencies, wr);
     97 		write(wr, "}");
     98 	}
     99 
    100 	private static void addDependencies(ProjectType project, List<Dependency> dependencyList, BufferedWriter wr) throws IOException {
    101 		write(wr, "dependencies {");
    102 		if (!project.equals(ProjectType.CORE)) {
    103 			write(wr, "compile project(\":" + ProjectType.CORE.getName() + "\")");
    104 		}
    105 		for (Dependency dep : dependencyList) {
    106 			if (dep.getDependencies(project) == null) continue;
    107 			for (String moduleDependency : dep.getDependencies(project)) {
    108 				if (moduleDependency == null) continue;
    109 				if ((project.equals(ProjectType.ANDROID) || project.equals(ProjectType.IOSMOE)) && moduleDependency.contains("native")) {
    110 					write(wr, "natives \"" + moduleDependency + "\"");
    111 				} else {
    112 					write(wr, "compile \"" + moduleDependency + "\"");
    113 				}
    114 			}
    115 		}
    116 		write(wr, "}");
    117 	}
    118 
    119 	private static void addConfigurations(ProjectType project, BufferedWriter wr) throws IOException {
    120 		if (project.equals(ProjectType.ANDROID) || project.equals(ProjectType.IOSMOE)) {
    121 			write(wr, "configurations { natives }");
    122 		}
    123 	}
    124 
    125 	private static void write(BufferedWriter wr, String input) throws IOException {
    126 		int delta = StringUtils.countMatches(input, '{') - StringUtils.countMatches(input, '}');
    127 		indent += delta *= 4;
    128 		indent = clamp(indent);
    129 		if (delta > 0) {
    130 			wr.write(StringUtils.repeat(" ", clamp(indent - 4)) + input + "\n");
    131 		} else if (delta < 0) {
    132 			wr.write(StringUtils.repeat(" ", clamp(indent)) + input + "\n");
    133 		} else {
    134 			wr.write(StringUtils.repeat(" ", indent) + input + "\n");
    135 		}
    136 	}
    137 
    138 	private static void space(BufferedWriter wr) throws IOException {
    139 		wr.write("\n");
    140 	}
    141 
    142 	private static int clamp(int indent) {
    143 		if (indent < 0) {
    144 			return 0;
    145 		}
    146 		return indent;
    147 	}
    148 
    149 	static class StringUtils {
    150 
    151 		public static int countMatches(String input, char match) {
    152 			int count = 0;
    153 			for (int i = 0; i < input.length(); i++) {
    154 				if (input.charAt(i) == match) {
    155 					count++;
    156 				}
    157 			}
    158 			return count;
    159 		}
    160 
    161 		public static String repeat(String toRepeat, int count) {
    162 			String repeat = "";
    163 			for (int i = 0; i < count; i++) {
    164 				repeat += toRepeat;
    165 			}
    166 			return repeat;
    167 		}
    168 	}
    169 
    170 }
    171