From 67d69b4ad2bf6fb3f747153c5f156e13869cbc18 Mon Sep 17 00:00:00 2001 From: wysaid Date: Wed, 18 Jan 2023 12:01:10 +0800 Subject: [PATCH] better build imp --- README.md | 2 +- utils/platform_utils.sh | 88 +++++++++++++++++++++++++++++++++++++++++ vscode_tasks.sh | 43 ++++++++------------ 3 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 utils/platform_utils.sh diff --git a/README.md b/README.md index 5353c466..b7812e3a 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ dependencies { * Done. * Using `Visual Studio Code`: (Simple) - * Setup your Android SDK with ENV variable. (names `ANDROID_HOME` / `ANDROID_SDK_HOME` / `ANDROID_SDK_ROOT` are all OK). + * Setup ENV variable `ANDROID_HOME` to your Android SDK installation directory. * Open the repo with `Visual Studio Code` * Press `⌘ + shift + B` (Mac) or `ctrl + shift + B` (Win/Linux), choose the option `Enable CMake And Build Project With CMake`. * Done. diff --git a/utils/platform_utils.sh b/utils/platform_utils.sh new file mode 100644 index 00000000..539342f2 --- /dev/null +++ b/utils/platform_utils.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash + +if ! command -v realpath &>/dev/null; then + function realpath() { + if [[ $# -ne 0 ]]; then + pushd . >/dev/null + cd $@ && pwd + popd >/dev/null + fi + } +fi + +REPO_DIR=$(git rev-parse --show-toplevel) + +function isWsl() { + [[ -d "/mnt/c" ]] || command -v wslpath &>/dev/null +} + +function isMingW() { + [[ -d "/c" ]] +} + +function isCygwin() { + [[ -d "/cygdrive/c" ]] +} + +function isWindows() { + # check mingw and cygwin + isWsl || isMingW || isCygwin +} + +function isMacOSX() { + [[ "$(uname -s)" == "Darwin" ]] +} + +function isLinux() { + [[ "$(uname -s)" == "Linux" ]] +} + +function getEnvironmentVariable() { + VAR_NAME="$@" + RET_VALUE= + if isWindows && command -v cmd &>/dev/null; then + # Detect native var first + RET_VALUE=$(cmd /C set "$VAR_NAME" | sed 's/.*=//') + fi + + if [[ -z "$RET_VALUE" ]]; then + RET_VALUE=${VAR_NAME} + fi + + if [[ -n "$RET_VALUE" ]]; then + echo $RET_VALUE + fi +} + +if isWindows && ! command -v cmd &>/dev/null; then + if [[ -f "/mnt/c/Windows/system32/cmd.exe" ]]; then + function cmd() { + /mnt/c/Windows/system32/cmd.exe $@ + } + elif [[ -f "/c/Windows/system32/cmd.exe" ]]; then + function cmd() { + /c/Windows/system32/cmd.exe $@ + } + elif [[ -f "/cygdrive/c/Windows/system32/cmd.exe" ]]; then + function cmd() { + /cygdrive/c/Windows/system32/cmd.exe $@ + } + fi +fi + +function runGradleCommand() { + + if isWindows && [[ -f "$REPO_DIR/local.properties" ]] && [[ -n "$(grep -i "sdk.dir=" "$REPO_DIR/local.properties" | grep -E ':(\\|/)')" ]]; then + # Run with Windows Native + echo "Perform Windows Native Gradle Func: " + cmd /C gradlew $@ && exit 0 + # Go ahead if failed. + echo "Perform Windows Native Gradle Failed, Go with current shell env" + export GRADLE_RUN_WIN_FAILED=true + fi + + if ! ./gradlew $@ && [[ -z "$GRADLE_RUN_WIN_FAILED" ]]; then + # Try again with Windows Native + command -v cmd &>/dev/null && cmd /C gradlew $@ + fi +} diff --git a/vscode_tasks.sh b/vscode_tasks.sh index add23e9a..9bf02e91 100755 --- a/vscode_tasks.sh +++ b/vscode_tasks.sh @@ -9,23 +9,13 @@ THIS_DIR="$(pwd)" PROJECT_DIR="$THIS_DIR" ADB_COMMAND="$PROJECT_DIR/utils/adb_command.sh" +source "$PROJECT_DIR/utils/platform_utils.sh" + export PACKAGE_NAME="org.wysaid.cgeDemo" export LAUNCH_ACTIVITY="MainActivity" export GRADLEW_RUN_TASK="installDebug" export ANDROID_BUILD_TYPE="assembleDebug" -if ! command -v cmd &>/dev/null && [[ -f "/mnt/c/Windows/system32/cmd.exe" ]]; then - function cmd() { - /mnt/c/Windows/system32/cmd.exe $@ - } -fi - -function runGradleCommand() { - if ! ./gradlew $@; then - command -v cmd &>/dev/null && cmd /C gradlew $@ - fi -} - function setupProject() { if [[ -f "$PROJECT_DIR/local.properties" ]] && grep -E '^usingCMakeCompile=true' "$PROJECT_DIR/local.properties"; then echo "Using cmake, skip ndk build..." @@ -87,21 +77,20 @@ function buildProject() { fi } -if [[ ! -f "local.properties" ]]; then - if [[ -n "$ANDROID_HOME" ]]; then - echo "sdk.dir=$ANDROID_HOME" >>local.properties - elif [[ -n "$ANDROID_SDK_ROOT" ]]; then - echo "sdk.dir=$ANDROID_SDK_ROOT" >>local.properties - elif [[ -n "$ANDROID_SDK_HOME" ]]; then - if [[ -d "$ANDROID_SDK_HOME/platform-tools" ]]; then - echo "sdk.dir=$ANDROID_SDK_HOME" >>local.properties - elif [[ -d "$ANDROID_SDK_HOME/../platform-tools" ]]; then - echo "sdk.dir=$(realpath $ANDROID_SDK_HOME/../platform-tools)" >>local.properties - fi - elif [[ -n "$ANDROID_SDK" ]]; then - echo "sdk.dir=$ANDROID_SDK" >>local.properties - else - echo "Can't find ANDROID_SDK, Please setup 'local.properties'" >&2 +function patchAndroidSDKLocation() { + VAR_ENV_VALUE=$(getEnvironmentVariable $@) + if [[ -n "$VAR_ENV_VALUE" ]]; then + VAR_ENV_VALUE=$(echo $VAR_ENV_VALUE | tr '\\' '/') + echo "sdk.dir=$VAR_ENV_VALUE" >>local.properties + return 0 + fi + return 1 +} + +if [[ ! -f "local.properties" ]] && ! grep "sdk.dir=" local.properties &>/dev/null; then + if ! (patchAndroidSDKLocation ANDROID_HOME || + patchAndroidSDKLocation ANDROID_SDK_ROOT); then + echo "sdk.dir is missing in 'local.properties' and env var ANDROID_HOME is not defined." >&2 fi fi