Skip to content

Commit 855fe0e

Browse files
committed
Improve workspace validation in Android port build
1 parent 75fd2f9 commit 855fe0e

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

scripts/build-android-port.sh

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,96 @@ log "The DOWNLOAD_DIR is ${DOWNLOAD_DIR}"
1818
ENV_DIR="$DOWNLOAD_DIR/tools"
1919
ENV_FILE="$ENV_DIR/env.sh"
2020

21-
log "Loading workspace environment from $ENV_FILE"
22-
if [ -f "$ENV_FILE" ]; then
21+
load_environment() {
22+
if [ ! -f "$ENV_FILE" ]; then
23+
return 1
24+
fi
25+
2326
log "Workspace environment file metadata"
2427
ls -l "$ENV_FILE" | while IFS= read -r line; do log "$line"; done
2528
log "Workspace environment file contents"
2629
sed 's/^/[build-android-port] ENV: /' "$ENV_FILE"
2730
# shellcheck disable=SC1090
2831
source "$ENV_FILE"
29-
else
30-
log "Workspace tools not found. Running setup-workspace.sh"
31-
./scripts/setup-workspace.sh -q -DskipTests
32-
if [ -f "$ENV_FILE" ]; then
33-
log "Workspace environment file metadata after setup"
34-
ls -l "$ENV_FILE" | while IFS= read -r line; do log "$line"; done
35-
log "Workspace environment file contents after setup"
36-
sed 's/^/[build-android-port] ENV: /' "$ENV_FILE"
37-
# shellcheck disable=SC1090
38-
source "$ENV_FILE"
32+
}
33+
34+
check_java_home() {
35+
local name="$1" home="$2" version_pattern="$3"
36+
37+
if [ -z "$home" ]; then
38+
log "$name is not set"
39+
return 1
40+
fi
41+
42+
if [ ! -x "$home/bin/java" ]; then
43+
log "$name does not contain a java binary: $home/bin/java"
44+
return 1
45+
fi
46+
47+
local version_output
48+
version_output="$("$home/bin/java" -version 2>&1 || true)"
49+
if ! grep -q "$version_pattern" <<<"$version_output"; then
50+
log "$name java -version output did not match $version_pattern"
51+
log "$version_output"
52+
return 1
53+
fi
54+
55+
log "$name detected at $home (${version_output%%$'\n'*})"
56+
return 0
57+
}
58+
59+
validate_workspace() {
60+
local missing=0
61+
62+
if ! check_java_home "JAVA_HOME" "${JAVA_HOME:-}" '1\\.8'; then
63+
missing=1
64+
fi
65+
66+
if ! check_java_home "JAVA_HOME_17" "${JAVA_HOME_17:-}" '17\\.'; then
67+
missing=1
68+
fi
69+
70+
if [ -z "${MAVEN_HOME:-}" ]; then
71+
log "MAVEN_HOME is not set"
72+
missing=1
73+
elif [ ! -x "$MAVEN_HOME/bin/mvn" ]; then
74+
log "Maven binary missing at $MAVEN_HOME/bin/mvn"
75+
missing=1
3976
else
77+
log "Maven detected at $MAVEN_HOME"
78+
fi
79+
80+
return $missing
81+
}
82+
83+
log "Loading workspace environment from $ENV_FILE"
84+
if ! load_environment; then
85+
log "Workspace tools not found. Running setup-workspace.sh"
86+
./scripts/setup-workspace.sh -q -DskipTests "$@"
87+
if ! load_environment; then
4088
log "Failed to create workspace environment at $ENV_FILE" >&2
4189
exit 1
4290
fi
4391
fi
4492

45-
log "Loaded environment: JAVA_HOME=${JAVA_HOME:-<unset>} JAVA_HOME_17=${JAVA_HOME_17:-<unset>} MAVEN_HOME=${MAVEN_HOME:-<unset>}"
46-
47-
if [ -z "${JAVA_HOME_17:-}" ] || ! "${JAVA_HOME_17:-}/bin/java" -version 2>&1 | grep -q '17\\.0'; then
48-
echo "Failed to provision JDK 17" >&2
49-
exit 1
93+
if validate_workspace; then
94+
log "Workspace environment validated"
95+
else
96+
log "Workspace validation failed. Re-provisioning tools via setup-workspace.sh"
97+
./scripts/setup-workspace.sh -q -DskipTests "$@"
98+
if ! load_environment; then
99+
log "Failed to create workspace environment at $ENV_FILE" >&2
100+
exit 1
101+
fi
102+
if ! validate_workspace; then
103+
echo "Failed to provision required JDK or Maven binaries" >&2
104+
exit 1
105+
fi
106+
log "Workspace environment validated"
50107
fi
51108

109+
log "Loaded environment: JAVA_HOME=${JAVA_HOME:-<unset>} JAVA_HOME_17=${JAVA_HOME_17:-<unset>} MAVEN_HOME=${MAVEN_HOME:-<unset>}"
110+
52111
export PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH"
53112
"$JAVA_HOME/bin/java" -version
54113
"$JAVA_HOME_17/bin/java" -version

0 commit comments

Comments
 (0)