From a06d5865c772139c1742247aa694c11f9a568d8a Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Fri, 27 Dec 2019 19:38:18 +0100
Subject: [PATCH] [enh] Add scripts directory with android scripts

---
 scripts/build-android.sh           |  20 +++++
 scripts/docker-start.sh            |  15 ++++
 scripts/emulate-android.sh         |  19 +++++
 scripts/env-android.sh             |  49 +++++++++++++
 scripts/env-clean.sh               |  25 +++++++
 scripts/env-global.sh              | 113 +++++++++++++++++++++++++++++
 scripts/ionic-update.sh            |  31 ++++++++
 scripts/release-android.sh         |  72 ++++++++++++++++++
 scripts/run-android.sh             |  23 ++++++
 scripts/signing-release-android.sh |  68 +++++++++++++++++
 10 files changed, 435 insertions(+)
 create mode 100755 scripts/build-android.sh
 create mode 100755 scripts/docker-start.sh
 create mode 100755 scripts/emulate-android.sh
 create mode 100755 scripts/env-android.sh
 create mode 100755 scripts/env-clean.sh
 create mode 100755 scripts/env-global.sh
 create mode 100755 scripts/ionic-update.sh
 create mode 100755 scripts/release-android.sh
 create mode 100755 scripts/run-android.sh
 create mode 100755 scripts/signing-release-android.sh

diff --git a/scripts/build-android.sh b/scripts/build-android.sh
new file mode 100755
index 00000000..c5f10e83
--- /dev/null
+++ b/scripts/build-android.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-android.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+# Run the build
+echo "Running cordova build..."
+ionic cordova build android --warning-mode=none --color
+#ionic cordova build android --warning-mode=none --color --verbose
diff --git a/scripts/docker-start.sh b/scripts/docker-start.sh
new file mode 100755
index 00000000..d9a1d171
--- /dev/null
+++ b/scripts/docker-start.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+cd ${PROJECT_DIR}
+
+PWD=$(pwd)
+CMD="sudo docker run -ti --rm -p 8100:8100 -p 35729:35729 -v $PWD:/cesium:rw cesium:release"
+echo "Executing: $CMD"
+$CMD
diff --git a/scripts/emulate-android.sh b/scripts/emulate-android.sh
new file mode 100755
index 00000000..fd924a90
--- /dev/null
+++ b/scripts/emulate-android.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  cd ..
+  PROJECT_DIR=`pwd`
+  export PROJECT_DIR
+fi;
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-android.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+# Run the build
+echo "Running Android emulator..."
+ionic cordova emulate android --warning-mode=none --color
diff --git a/scripts/env-android.sh b/scripts/env-android.sh
new file mode 100755
index 00000000..2949ceba
--- /dev/null
+++ b/scripts/env-android.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Preparing environment
+. ${PROJECT_DIR}/scripts/env-global.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+if [[ "_" == "_${CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL}" ]]; then
+  echo "Missing Gradle distribution URL - please export env variable 'CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'"
+  exit 1
+fi
+
+echo "Preparing Android environment:"
+echo " - using Android SDK: ${ANDROID_SDK_ROOT}"
+echo " - using Android SDK tools: ${ANDROID_SDK_TOOLS_ROOT}"
+echo " - using Gradle: ${CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL}"
+echo " - using Java: ${JAVA_HOME}"
+echo " - project dir: ${PROJECT_DIR}"
+
+cd ${PROJECT_DIR}
+
+# Prepare Android platform
+if [[ ! -d "${PROJECT_DIR}/platforms/android" ]]; then
+  echo "Adding Cordova Android platform..."
+  ionic cordova prepare android --color --verbose
+  if [[ $? -ne 0 ]]; then
+    exit 1
+  fi
+fi
+
+# Copy local files
+if [[ -d "${PROJECT_DIR}/.local/android" ]]; then
+  echo "Copying files from directory '${PROJECT_DIR}/.local/android' into '${PROJECT_DIR}/platforms/android'..."
+  cp -rf ${PROJECT_DIR}/.local/android/* ${PROJECT_DIR}/platforms/android
+  if [[ $? -ne 0 ]]; then
+    exit 1
+  fi
+else
+  echo "No directory '${PROJECT_DIR}/.local/android' found. Please create it, with a file 'release-signing.properties' for release signing"
+fi
+
diff --git a/scripts/env-clean.sh b/scripts/env-clean.sh
new file mode 100755
index 00000000..888bb378
--- /dev/null
+++ b/scripts/env-clean.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+if [[ ! -f "${PROJECT_DIR}/package.json" ]]; then
+  echo "Invalid project dir: file 'package.json' not found in ${PROJECT_DIR}"
+  echo "-> Make sur to run the script 'prepare_env.sh' from the project directory, or export env variable 'PROJECT_DIR'"
+  exit 1
+fi;
+
+cd ${PROJECT_DIR}
+
+#echo "Cleaning project dependencies..."
+#rm -rf node_modules
+
+echo "Cleaning Cordova plugins..."
+rm -rf plugins
+
+echo "Cleaning Android platform..."
+#rm -rf platforms/android
diff --git a/scripts/env-global.sh b/scripts/env-global.sh
new file mode 100755
index 00000000..28e43597
--- /dev/null
+++ b/scripts/env-global.sh
@@ -0,0 +1,113 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+if [[ ! -f "${PROJECT_DIR}/package.json" ]]; then
+  echo "Invalid project dir: file 'package.json' not found in ${PROJECT_DIR}"
+  echo "-> Make sure to run the script inside his directory, or export env variable 'PROJECT_DIR'"
+  exit 1
+fi;
+
+echo "Preparing project environment.."
+NODEJS_VERSION=10
+
+ANDROID_NDK_VERSION=r19c
+ANDROID_SDK_VERSION=r29.0.0
+ANDROID_SDK_TOOLS_VERSION=4333796
+ANDROID_SDK_ROOT=/usr/lib/android-sdk
+ANDROID_SDK_TOOLS_ROOT=${ANDROID_SDK_ROOT}/build-tools
+
+#JAVA_HOME=
+
+GRADLE_VERSION=4.10.3
+CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=https\://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-all.zip
+
+# Override with a local file, if any
+if [[ -f "${PROJECT_DIR}/.local/env.sh" ]]; then
+  echo "Loading environment variables from: '.local/env.sh'"
+  source ${PROJECT_DIR}/.local/env.sh
+  if [[ $? -ne 0 ]]; then
+    exit 1
+  fi
+else
+  echo "No file '${PROJECT_DIR}/.local/env.sh' found. Will use defaults"
+fi
+
+# Checking Java installed
+if [[ "_" == "_${JAVA_HOME}" ]]; then
+  JAVA_CMD=`which java`
+  if [[ "_" == "_${JAVA_CMD}" ]]; then
+    echo "No Java installed. Please install java, or set env variable JAVA_HOME "
+    exit 1
+  fi
+fi
+
+# Checking Android SDK
+if [[ "_" == "_${ANDROID_SDK_ROOT}" ]]; then
+  echo "Please set env variable ANDROID_SDK_ROOT "
+  exit 1
+fi
+if [[ ! -d "${ANDROID_SDK_ROOT}" ]]; then
+  echo "Invalid path for ANDROID_SDK_ROOT: ${ANDROID_SDK_ROOT} is not a directory"
+  exit 1
+fi
+
+# Export useful variables
+export PROJECT_DIR \
+  JAVA_HOME \
+  ANDROID_SDK_ROOT \
+  CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL
+
+# Node JS
+export NVM_DIR="$HOME/.nvm"
+if [[ -d "${NVM_DIR}" ]]; then
+
+    # Load NVM
+    . ${NVM_DIR}/nvm.sh
+
+    # Switch to expected version
+    nvm use ${NODEJS_VERSION}
+
+    # Or install it
+    if [[ $? -ne 0 ]]; then
+        nvm install ${NODEJS_VERSION}
+        if [[ $? -ne 0 ]]; then
+            exit 1;
+        fi
+    fi
+else
+    echo "nvm (Node version manager) not found (directory ${NVM_DIR} not found). Please install, and retry"
+    exit -1
+fi
+
+# Install global dependencies
+IONIC_PATH=`which ionic`
+CORDOVA_PATH=`which cordova`
+if [[ "_" == "_${IONIC_PATH}" || "_" == "_${CORDOVA_PATH}" ]]; then
+  echo "Installing global dependencies..."
+  npm install -g cordova ionic native-run yarn
+  if [[ $? -ne 0 ]]; then
+      exit 1
+  fi
+fi
+
+NATIVE_RUN_PATH=`which native-run`
+if [[ "_" == "_${NATIVE_RUN_PATH}" ]]; then
+  echo "Installing global dependencies..."
+  npm install -g native-run
+  if [[ $? -ne 0 ]]; then
+      exit 1
+  fi
+fi
+
+# Install project dependencies
+if [[ ! -d "${PROJECT_DIR}/node_modules" ]]; then
+    echo "Installing project dependencies..."
+    cd ${PROJECT_DIR}
+    yarn
+fi
diff --git a/scripts/ionic-update.sh b/scripts/ionic-update.sh
new file mode 100755
index 00000000..08a3df0d
--- /dev/null
+++ b/scripts/ionic-update.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-global.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+echo "Updating Ionic..."
+npm update -g ionic@latest
+
+echo "Updating Cordova..."
+npm update -g cordova@latest
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+echo "Updating Cordova plugins..."
+ionic cordova platform update android --save
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
diff --git a/scripts/release-android.sh b/scripts/release-android.sh
new file mode 100755
index 00000000..b1d17958
--- /dev/null
+++ b/scripts/release-android.sh
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-android.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+# Run the build
+echo "Running cordova build..."
+
+ionic cordova build android --warning-mode=none --color --prod --release
+# TODO: try this :
+#ionic cordova build android --warning-mode=none --color --prod --release --optimizejs --minifyjs --minifycss
+
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+# Signature
+KEYSTORE_FILE=${PROJECT_DIR}/.local/Cesium.keystore
+KEY_ALIAS=Cesium
+KEY_PWD=
+APK_DIR=${PROJECT_DIR}/platforms/android/build/outputs/apk/release
+APK_UNSIGNED_FILE=${APK_DIR}/android-release.apk
+BUILD_TOOLS_DIR="${ANDROID_SDK_ROOT}/build-tools/28.*/"
+
+if [[ ! -f "${APK_UNSIGNED_FILE}" ]]; then
+  echo "APK file not found at: ${APK_UNSIGNED_FILE}"
+  exit 1
+fi
+
+# Check if signed
+cd ${BUILD_TOOLS_DIR}
+./apksigner verify ${APK_UNSIGNED_FILE}
+
+# Not signed ? Do it !
+if [[ $? -ne 0 ]]; then
+  echo "It seems that the APK file ${APK_UNSIGNED_FILE} is not signed !"
+  if [[ ! -f "${KEYSTORE_FILE}" ]]; then
+    echo "ERROR: Unable to sign: no keystore file found at ${KEYSTORE_FILE} !"
+    exit 1
+  fi
+
+  echo "Signing APK file ${APK_UNSIGNED_FILE}..."
+  APK_SIGNED_FILE=${APK_DIR}/android-release-signed.apk
+
+  jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ${KEYSTORE_FILE} ${APK_UNSIGNED_FILE} Cesium
+
+  BUILD_TOOLS_DIR="${ANDROID_SDK_ROOT}/build-tools/28.*/"
+  cd ${BUILD_TOOLS_DIR}
+  ./zipalign -v 4 ${APK_UNSIGNED_FILE} ${APK_SIGNED_FILE}
+
+  ./apksigner verify ${APK_SIGNED_FILE}
+  if [[ $? -ne 0 ]]; then
+    echo "Signing failed !"
+    exit 1
+  fi
+
+  # Do file replacement
+  rm ${APK_UNSIGNED_FILE}
+  mv ${APK_SIGNED_FILE} ${APK_UNSIGNED_FILE}
+fi
diff --git a/scripts/run-android.sh b/scripts/run-android.sh
new file mode 100755
index 00000000..f645cd8c
--- /dev/null
+++ b/scripts/run-android.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-android.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+# Run the build
+echo "Building Android application..."
+ionic cordova build android --warning-mode=none --color --device
+
+echo "Running Android application..."
+native-run android --app ${PROJECT_DIR}/platforms/android/build/outputs/apk/debug/android-debug.apk --device
diff --git a/scripts/signing-release-android.sh b/scripts/signing-release-android.sh
new file mode 100755
index 00000000..0bbd5bef
--- /dev/null
+++ b/scripts/signing-release-android.sh
@@ -0,0 +1,68 @@
+#!/bin/bash
+
+# Get to the root project
+if [[ "_" == "_${PROJECT_DIR}" ]]; then
+  SCRIPT_DIR=$(dirname $0)
+  PROJECT_DIR=$(cd ${SCRIPT_DIR}/.. && pwd)
+  export PROJECT_DIR
+fi;
+
+# Default env (can be override in file <PROJECT>/.local/env.sh)
+KEYSTORE_FILE=${PROJECT_DIR}/.local/android/Cesium.keystore
+KEY_ALIAS=Cesium
+KEYSTORE_PWD=
+APK_RELEASE_DIR=${PROJECT_DIR}/platforms/android/build/outputs/apk/release
+APK_UNSIGNED_FILE=${APK_RELEASE_DIR}/android-release-unsigned.apk
+APK_SIGNED_FILE=${APK_RELEASE_DIR}/android-release-signed.apk
+
+
+# Preparing Android environment
+. ${PROJECT_DIR}/scripts/env-android.sh
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+
+cd ${PROJECT_DIR}
+
+# Sign files
+echo "Signing APK file..."
+if [[ ! -f "${APK_UNSIGNED_FILE}" ]]; then
+  echo "APK file not found: ${APK_UNSIGNED_FILE}"
+  exit 1
+fi
+if [[ ! -f "${KEYSTORE_FILE}" ]]; then
+  echo "Keystore file not found: ${KEYSTORE_FILE}"
+  exit 1
+fi
+
+# Remove previous version
+if [[ -f "${APK_SIGNED_FILE}" ]]; then
+  echo "Delete previous signed APK file: ${APK_SIGNED_FILE}"
+  rm -f ${APK_SIGNED_FILE}
+fi
+
+echo "Executing jarsigner..."
+jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ${KEYSTORE_FILE} ${APK_UNSIGNED_FILE} Cesium
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+echo "Executing jarsigner [OK]"
+
+BUILD_TOOLS_DIR="${ANDROID_SDK_ROOT}/build-tools/28.*/"
+cd ${BUILD_TOOLS_DIR}
+
+echo "Executing zipalign..."
+./zipalign -v 4 ${APK_UNSIGNED_FILE} ${APK_SIGNED_FILE}
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+echo "Executing zipalign [OK]"
+
+echo "Verify APK signature..."
+./apksigner verify ${APK_SIGNED_FILE}
+if [[ $? -ne 0 ]]; then
+  exit 1
+fi
+echo "Verify APK signature [OK]"
+
+echo "Successfully generated signed APK at: ${APK_SIGNED_FILE}"
-- 
GitLab