From d8a6744aab3f9ed032ddfd3a4abc8d24b8f635d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Latner?= <matyas.latner@nic.cz>
Date: Wed, 18 Feb 2015 10:22:51 +0100
Subject: [PATCH] #1 Build variants for desktop app

 * build variant connected to application settings
---
 .../nic/tablexia/android/AndroidLauncher.java |  2 +-
 build.gradle                                  | 27 +++----
 core/src/cz/nic/tablexia/Tablexia.java        | 14 ++--
 .../cz/nic/tablexia/TablexiaApplication.java  |  6 +-
 .../src/cz/nic/tablexia/TablexiaSettings.java | 73 +++++++++++++++++--
 core/src/cz/nic/tablexia/menu/MainMenu.java   |  4 +-
 .../screen/statistics/StatisticsScreen.java   |  2 +-
 desktop/build.gradle                          | 26 ++++++-
 .../nic/tablexia/desktop/DesktopLauncher.java | 15 +++-
 ios/src/cz/nic/tablexia/IOSLauncher.java      |  2 +-
 10 files changed, 135 insertions(+), 36 deletions(-)

diff --git a/android/src/cz/nic/tablexia/android/AndroidLauncher.java b/android/src/cz/nic/tablexia/android/AndroidLauncher.java
index 28d4fdf69..0f26cb80a 100644
--- a/android/src/cz/nic/tablexia/android/AndroidLauncher.java
+++ b/android/src/cz/nic/tablexia/android/AndroidLauncher.java
@@ -11,6 +11,6 @@ public class AndroidLauncher extends AndroidApplication {
 	protected void onCreate (Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
-		initialize(new Tablexia(), config);
+		initialize(new Tablexia(BuildConfig.DEBUG), config);
 	}
 }
diff --git a/build.gradle b/build.gradle
index cb017cdb4..434b081d0 100644
--- a/build.gradle
+++ b/build.gradle
@@ -19,7 +19,7 @@ allprojects {
         appName = 'Tablexia-libGDX'
         gdxVersion = '1.5.2'
 		gdxUtilsVersion = '0.9.1'
-	mbassadorVersion = '1.2.0'
+	    mbassadorVersion = '1.2.0'
         roboVMVersion = '1.0.0-beta-01'
         box2DLightsVersion = '1.3'
         ashleyVersion = '1.3.1'
@@ -42,8 +42,9 @@ project(":desktop") {
         compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
         compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
         compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
-	compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
-	compile "net.engio:mbassador:$mbassadorVersion"
+	    compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
+	    compile "net.engio:mbassador:$mbassadorVersion"
+        compile "com.jcabi:jcabi-manifests:1.1"
     }
 }
 
@@ -55,11 +56,11 @@ project(":android") {
     dependencies {
         compile project(":core")
         compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
-	compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
-	compile "net.engio:mbassador:$mbassadorVersion"
-	natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
-	natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
-	natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
+	    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
+	    compile "net.engio:mbassador:$mbassadorVersion"
+	    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
+	    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
+	    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
         natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
         natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
         natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
@@ -76,10 +77,10 @@ project(":ios") {
         compile project(":core")
         compile "org.robovm:robovm-rt:${roboVMVersion}"
         compile "org.robovm:robovm-cocoatouch:${roboVMVersion}"
-	compile "net.engio:mbassador:$mbassadorVersion"
+	    compile "net.engio:mbassador:$mbassadorVersion"
         compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
-	compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
-	natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
+	    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
+	    natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
         natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
     }
 }
@@ -109,8 +110,8 @@ project(":core") {
     dependencies {
         compile "com.badlogicgames.gdx:gdx:$gdxVersion"
 		compile "net.dermetfan.libgdx-utils:libgdx-utils:$gdxUtilsVersion"
-	compile "net.engio:mbassador:$mbassadorVersion"
-	compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
+	    compile "net.engio:mbassador:$mbassadorVersion"
+	    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
 
 		testCompile "junit:junit:4.11"
 		testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"               
diff --git a/core/src/cz/nic/tablexia/Tablexia.java b/core/src/cz/nic/tablexia/Tablexia.java
index a70dd82b9..3f6670c58 100644
--- a/core/src/cz/nic/tablexia/Tablexia.java
+++ b/core/src/cz/nic/tablexia/Tablexia.java
@@ -29,11 +29,15 @@ public class Tablexia extends TablexiaApplication {
 	public 	static final String 	LOADER_BACKGOURND		= "application/gfx/intro/screen_loader_background.png";
 	private static final float 		MAIN_MWNU_WIDTH_RATIO 	= 2f/7;
 	
-	private boolean					loadingComplete;
-	
-	public Tablexia() {
-		loadingComplete = false;
-	}
+	private boolean					loadingComplete         = false;
+
+    public Tablexia(boolean debug) {
+        TablexiaSettings.init(debug);
+    }
+
+    public Tablexia(String buildVariantKey) {
+        TablexiaSettings.init(buildVariantKey);
+    }
 	
 	private void loadingComplete() {
 		if (!loadingComplete) {
diff --git a/core/src/cz/nic/tablexia/TablexiaApplication.java b/core/src/cz/nic/tablexia/TablexiaApplication.java
index 0c43f3ea9..85ab84bb8 100644
--- a/core/src/cz/nic/tablexia/TablexiaApplication.java
+++ b/core/src/cz/nic/tablexia/TablexiaApplication.java
@@ -52,7 +52,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
 	@Override
 	public void dispose () {
 		inputMultiplexer.removeProcessor(stage);
-		if (TablexiaSettings.isDebug()) {			
+		if (TablexiaSettings.getInstance().isDebug()) {
 			debugInfo.dispose();
 		}
 		stage.dispose();
@@ -92,7 +92,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
 		Gdx.gl.glClearColor(BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, BACKGROUND_COLOR.a);
 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
 		
-		if (TablexiaSettings.isDebug()) {			
+		if (TablexiaSettings.getInstance().isDebug()) {
 			debugInfo.update();
 		}
 		float deltaTime = Gdx.graphics.getDeltaTime();
@@ -114,7 +114,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
 //////////////////////////// DEBUG INFO
 	
 	private void prepareDebugInfo() {
-		if (TablexiaSettings.isDebug()) {			
+		if (TablexiaSettings.getInstance().isDebug()) {
 			debugInfo = new DebugInfo(getStage().getWidth(), getStage().getHeight());
 			getStage().addActor(debugInfo);
 		}
diff --git a/core/src/cz/nic/tablexia/TablexiaSettings.java b/core/src/cz/nic/tablexia/TablexiaSettings.java
index 567378cf3..9cb32ac26 100644
--- a/core/src/cz/nic/tablexia/TablexiaSettings.java
+++ b/core/src/cz/nic/tablexia/TablexiaSettings.java
@@ -1,16 +1,79 @@
 package cz.nic.tablexia;
 
+import cz.nic.tablexia.util.Log;
+
 public class TablexiaSettings {
+
+    public static enum BuildVariant {
+
+        RELEASE("release", false),
+        DEBUG("debug", true);
+
+        private final static BuildVariant   FALLBACK_VARIANT = BuildVariant.DEBUG;
+
+        private final String                key;
+        private final boolean               isDebug;
+
+        private BuildVariant(String key, boolean isDebug) {
+            this.key = key;
+            this.isDebug = isDebug;
+        }
+
+        public boolean isDebug() {
+            return isDebug;
+        }
+
+        public static BuildVariant getBuildVariantForKey(String key) {
+            for(BuildVariant buildVariant: BuildVariant.values()) {
+                if (buildVariant.key.equals(key)) {
+                    return buildVariant;
+                }
+            }
+            return FALLBACK_VARIANT;
+        }
+    }
 	
-	private static final boolean  	DEBUG = true;
+	private final BuildVariant      BUILD_VARIANT;
 	private static final boolean 	DEBUG_SHOW_BOUNDING_BOXES = true;
+
+    private static TablexiaSettings instance;
+
+    private TablexiaSettings(BuildVariant buildVariant) {
+        BUILD_VARIANT = buildVariant;
+    }
+
+    public static TablexiaSettings getInstance() {
+        if (instance == null) {
+            String exceptionMessage = "Tablexia setting is not initialized!";
+            Log.err(TablexiaSettings.class, exceptionMessage);
+            throw new IllegalStateException(exceptionMessage);
+        }
+        return instance;
+    }
+
+    public static void init(boolean debug) {
+        TablexiaSettings.init(debug ? TablexiaSettings.BuildVariant.DEBUG : TablexiaSettings.BuildVariant.RELEASE);
+    }
+
+    public static void init(String buildVariantKey) {
+        TablexiaSettings.init(BuildVariant.getBuildVariantForKey(buildVariantKey));
+    }
+
+    public static void init(BuildVariant buildVariant) {
+        if (instance != null) {
+            String exceptionMessage = "Tablexia setting already initialized!";
+            Log.err(TablexiaSettings.class, exceptionMessage);
+            throw new IllegalStateException(exceptionMessage);
+        }
+        instance = new TablexiaSettings(buildVariant);
+    }
 	
-	public static boolean isDebug() {
-		return DEBUG;
+	public boolean isDebug() {
+		return BUILD_VARIANT.isDebug();
 	}
 	
-	public static boolean isShowBoudingBoxes() {
-		return DEBUG && DEBUG_SHOW_BOUNDING_BOXES;
+	public boolean isShowBoudingBoxes() {
+		return isDebug() && DEBUG_SHOW_BOUNDING_BOXES;
 	}
 	
 	
diff --git a/core/src/cz/nic/tablexia/menu/MainMenu.java b/core/src/cz/nic/tablexia/menu/MainMenu.java
index f5bd0bd55..037b682b2 100644
--- a/core/src/cz/nic/tablexia/menu/MainMenu.java
+++ b/core/src/cz/nic/tablexia/menu/MainMenu.java
@@ -49,7 +49,7 @@ public class MainMenu extends Stack {
 
 	public MainMenu(float width, float height) {
 		setBounds(0, 0, width, height);
-		setDebug(TablexiaSettings.isShowBoudingBoxes());
+		setDebug(TablexiaSettings.getInstance().isShowBoudingBoxes());
 		Texture background = ApplicationTextureManager.getInstance().get(ApplicationTextureManager.MAINMENU_BACKGROUND, Texture.class);
 		background.setFilter(TextureFilter.Linear, TextureFilter.Linear);
 		
@@ -91,7 +91,7 @@ public class MainMenu extends Stack {
 	
 	public void initMenuItems() {
 		menuTableLayout.reset();
-		menuTableLayout.setDebug(TablexiaSettings.isShowBoudingBoxes());
+		menuTableLayout.setDebug(TablexiaSettings.getInstance().isShowBoudingBoxes());
 		
 		for (final MainMenuDefinition menuItem : MainMenuDefinition.values()) {
 			addMenuItem(menuItem);
diff --git a/core/src/cz/nic/tablexia/screen/statistics/StatisticsScreen.java b/core/src/cz/nic/tablexia/screen/statistics/StatisticsScreen.java
index 3a564b020..7c6edd3a9 100644
--- a/core/src/cz/nic/tablexia/screen/statistics/StatisticsScreen.java
+++ b/core/src/cz/nic/tablexia/screen/statistics/StatisticsScreen.java
@@ -35,7 +35,7 @@ public class StatisticsScreen extends AbstractTablexiaScreen<Void> {
 		label.setPosition(getStage().getWidth() / 2, getStage().getHeight() / 2);
 		backgroundLayout.setActor(label);
 		
-		backgroundLayout.setDebug(TablexiaSettings.isShowBoudingBoxes());
+		backgroundLayout.setDebug(TablexiaSettings.getInstance().isShowBoudingBoxes());
 		getStage().addActor(backgroundLayout);
 	}
 	
diff --git a/desktop/build.gradle b/desktop/build.gradle
index bf0d55912..bbcaa4b63 100644
--- a/desktop/build.gradle
+++ b/desktop/build.gradle
@@ -14,18 +14,38 @@ task run(dependsOn: classes, type: JavaExec) {
     ignoreExitValue = true
 }
 
-task dist(type: Jar) {
+task debugJar(type: Jar) {
+    baseName = "debug"
     from files(sourceSets.main.output.classesDir)
     from files(sourceSets.main.output.resourcesDir)
     from {configurations.compile.collect {zipTree(it)}}
     from files(project.assetsDir);
- 
+
     manifest {
         attributes 'Main-Class': project.mainClassName
+        attributes 'Build-Variant': 'debug'
     }
 }
 
-dist.dependsOn classes
+task releaseJar(type: Jar) {
+    baseName = "release"
+    from files(sourceSets.main.output.classesDir)
+    from files(sourceSets.main.output.resourcesDir)
+    from {configurations.compile.collect {zipTree(it)}}
+    from files(project.assetsDir);
+
+    manifest {
+        attributes 'Main-Class': project.mainClassName
+        attributes 'Build-Variant': 'release'
+    }
+}
+
+artifacts {
+    archives releaseJar, debugJar
+}
+
+debugJar.dependsOn classes
+releaseJar.dependsOn classes
 
 eclipse {
     project {
diff --git a/desktop/src/cz/nic/tablexia/desktop/DesktopLauncher.java b/desktop/src/cz/nic/tablexia/desktop/DesktopLauncher.java
index f20233f58..7affc3788 100644
--- a/desktop/src/cz/nic/tablexia/desktop/DesktopLauncher.java
+++ b/desktop/src/cz/nic/tablexia/desktop/DesktopLauncher.java
@@ -2,14 +2,25 @@ package cz.nic.tablexia.desktop;
 
 import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
 import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
+import com.jcabi.manifests.Manifests;
+
 import cz.nic.tablexia.Tablexia;
 
 public class DesktopLauncher {
-	public static void main (String[] arg) {
+
+    public static final String BUILD_VARIANT_MANIFEST_ATTRIBUTE = "Build-Variant";
+
+    public static void main (String[] arg) {
+
+        String buildVariant = null;
+        if (Manifests.exists(BUILD_VARIANT_MANIFEST_ATTRIBUTE)) {
+            buildVariant = Manifests.read(BUILD_VARIANT_MANIFEST_ATTRIBUTE);
+        }
+
 		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
 		config.resizable = false;
 		config.width = 1280;
 		config.height = 800;
-		new LwjglApplication(new Tablexia(), config);
+		new LwjglApplication(new Tablexia(buildVariant), config);
 	}
 }
diff --git a/ios/src/cz/nic/tablexia/IOSLauncher.java b/ios/src/cz/nic/tablexia/IOSLauncher.java
index 36232c763..c108dd7ea 100644
--- a/ios/src/cz/nic/tablexia/IOSLauncher.java
+++ b/ios/src/cz/nic/tablexia/IOSLauncher.java
@@ -11,7 +11,7 @@ public class IOSLauncher extends IOSApplication.Delegate {
     @Override
     protected IOSApplication createApplication() {
         IOSApplicationConfiguration config = new IOSApplicationConfiguration();
-        return new IOSApplication(new Tablexia(), config);
+        return new IOSApplication(new Tablexia(null), config);
     }
 
     public static void main(String[] argv) {
-- 
GitLab