Skip to content

Commit

Permalink
better build imp
Browse files Browse the repository at this point in the history
  • Loading branch information
wysaid committed Jan 18, 2023
1 parent 1803aae commit 67d69b4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
88 changes: 88 additions & 0 deletions utils/platform_utils.sh
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 16 additions & 27 deletions vscode_tasks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 67d69b4

Please sign in to comment.