-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathsetup-workspace.sh
More file actions
executable file
·285 lines (251 loc) · 10.2 KB
/
setup-workspace.sh
File metadata and controls
executable file
·285 lines (251 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
###
# Prepare Codename One workspace by installing Maven, provisioning JDK 8 and 17,
# building core modules, and installing Maven archetypes.
# IMPORTANT: Run this script from the project root!
###
set -euo pipefail
[ "${DEBUG:-0}" = "1" ] && set -x
log() {
echo "[setup-workspace] $1"
}
# Normalize TMPDIR and compose paths without duplicate slashes
TMPDIR="${TMPDIR:-/tmp}"
TMPDIR="${TMPDIR%/}"
# Place downloaded tools outside the repository so it isn't filled with binaries
# Strip any trailing slash again at the join to be extra safe.
DOWNLOAD_DIR="${TMPDIR%/}/codenameone-tools"
ENV_DIR="$DOWNLOAD_DIR/tools"
mkdir -p "$DOWNLOAD_DIR"
mkdir -p "$ENV_DIR"
CN1_BINARIES_PARENT="$(cd .. && pwd -P)"
CN1_BINARIES="${CN1_BINARIES_PARENT%/}/cn1-binaries"
mkdir -p "$CN1_BINARIES_PARENT"
ENV_FILE="$ENV_DIR/env.sh"
log "The DOWNLOAD_DIR is ${DOWNLOAD_DIR}"
mkdir -p ~/.codenameone
cp maven/CodeNameOneBuildClient.jar ~/.codenameone
# Reuse previously saved environment if present (so we can skip downloads)
if [ -f "$ENV_FILE" ]; then
log "Found existing workspace environment at $ENV_FILE"
ls -l "$ENV_FILE" | while IFS= read -r line; do log "$line"; done
log "Existing workspace environment file contents"
sed 's/^/[setup-workspace] ENV: /' "$ENV_FILE"
# shellcheck disable=SC1090
source "$ENV_FILE"
fi
JAVA_HOME="${JAVA_HOME:-}"
JAVA17_HOME="${JAVA17_HOME:-}"
MAVEN_HOME="${MAVEN_HOME:-}"
log "Detecting host platform"
os_name=$(uname -s)
arch_name=$(uname -m)
case "$os_name" in
Linux) os="linux" ;;
Darwin) os="mac" ;;
*) echo "Unsupported OS: $os_name" >&2; exit 1 ;;
esac
case "$arch_name" in
x86_64|amd64) arch="x64" ;;
arm64|aarch64) arch="aarch64" ;;
*) echo "Unsupported architecture: $arch_name" >&2; exit 1 ;;
esac
# Determine platform-specific JDK download URLs
arch_jdk8="$arch"
if [ "$os" = "mac" ] && [ "$arch" = "aarch64" ]; then
arch_jdk8="x64"
fi
JDK8_URL="https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u462-b08/OpenJDK8U-jdk_${arch_jdk8}_${os}_hotspot_8u462b08.tar.gz"
JDK17_URL="https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.16%2B8/OpenJDK17U-jdk_${arch}_${os}_hotspot_17.0.16_8.tar.gz"
MAVEN_URL="https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.tar.gz"
install_jdk() {
local url="$1" dest_var="$2"
local archive="$DOWNLOAD_DIR/$(basename "$url")"
if [ -f "$archive" ]; then
log "Using cached JDK archive $(basename "$archive")"
else
log "Downloading JDK from $url"
curl -fL --retry 3 --retry-all-errors "$url" -o "$archive"
fi
local top
top=$(tar -tzf "$archive" 2>/dev/null | head -1 | cut -d/ -f1 || true)
if [ -z "$top" ]; then
log "Unable to determine extracted directory from $(basename "$archive")" >&2
exit 1
fi
local extracted="$DOWNLOAD_DIR/$top"
if [ -d "$extracted" ]; then
log "JDK already extracted at $extracted"
else
log "Extracting JDK to $DOWNLOAD_DIR"
tar -xzf "$archive" -C "$DOWNLOAD_DIR"
fi
local home="$extracted"
if [ -d "$home/Contents/Home" ]; then
home="$home/Contents/Home"
fi
printf -v "$dest_var" '%s' "$home"
}
CN1_VERSION=$(awk -F'[<>]' '/<version>/{print $3; exit}' maven/pom.xml)
log "Detected Codename One snapshot version ${CN1_VERSION}"
log "Ensuring JDK 8 is available"
if [ -z "${JAVA_HOME:-}" ] || [ ! -x "$JAVA_HOME/bin/java" ] || ! "$JAVA_HOME/bin/java" -version 2>&1 | grep -q '8\.0'; then
log "Provisioning JDK 8..."
install_jdk "$JDK8_URL" JAVA_HOME
else
log "Using existing JDK 8 at $JAVA_HOME"
fi
log "Ensuring JDK 17 is available"
if [ -z "${JAVA17_HOME:-}" ] || [ ! -x "$JAVA17_HOME/bin/java" ] || ! "$JAVA17_HOME/bin/java" -version 2>&1 | grep -q '17\.0'; then
log "Provisioning JDK 17..."
install_jdk "$JDK17_URL" JAVA17_HOME
else
log "Using existing JDK 17 at $JAVA17_HOME"
fi
log "Ensuring Maven is available"
if [ -z "${MAVEN_HOME:-}" ] || ! [ -x "$MAVEN_HOME/bin/mvn" ]; then
mvn_archive="$DOWNLOAD_DIR/$(basename "$MAVEN_URL")"
if [ -f "$mvn_archive" ]; then
log "Using cached Maven archive $(basename "$mvn_archive")"
else
log "Downloading Maven from $MAVEN_URL"
curl -fL --retry 3 --retry-all-errors "$MAVEN_URL" -o "$mvn_archive"
fi
mvn_top=$(tar -tzf "$mvn_archive" 2>/dev/null | head -1 | cut -d/ -f1 || true)
if [ -z "$mvn_top" ]; then
log "Unable to determine extracted directory from $(basename "$mvn_archive")" >&2
exit 1
fi
if [ -n "$mvn_top" ] && [ -d "$DOWNLOAD_DIR/$mvn_top" ]; then
log "Maven already extracted at $DOWNLOAD_DIR/$mvn_top"
else
log "Extracting Maven to $DOWNLOAD_DIR"
tar -xzf "$mvn_archive" -C "$DOWNLOAD_DIR"
fi
MAVEN_HOME="$DOWNLOAD_DIR/$mvn_top"
else
log "Using existing Maven at $MAVEN_HOME"
fi
ARCHETYPE_PLUGIN_COORD="org.apache.maven.plugins:maven-archetype-plugin:3.2.1"
log "Preloading Maven archetype plugin ($ARCHETYPE_PLUGIN_COORD) for offline project generation"
if "$MAVEN_HOME/bin/mvn" -B -N "$ARCHETYPE_PLUGIN_COORD:help" -Ddetail -Dgoal=generate >/dev/null 2>&1; then
log "Maven archetype plugin cached locally"
else
log "Failed to preload $ARCHETYPE_PLUGIN_COORD; archetype generation may download dependencies" >&2
fi
log "Writing environment to $ENV_FILE"
cat > "$ENV_FILE" <<ENV
export JAVA_HOME="$JAVA_HOME"
export JAVA17_HOME="$JAVA17_HOME"
export MAVEN_HOME="$MAVEN_HOME"
export PATH="\$JAVA_HOME/bin:\$MAVEN_HOME/bin:\$PATH"
ENV
log "Workspace environment file metadata"
if [ -f "$ENV_FILE" ]; then
ls -l "$ENV_FILE" | while IFS= read -r line; do log "$line"; done
log "Workspace environment file contents"
sed 's/^/[setup-workspace] ENV: /' "$ENV_FILE"
else
log "Environment file was not created at $ENV_FILE" >&2
fi
# shellcheck disable=SC1090
source "$ENV_FILE"
log "JDK 8 version:"; "$JAVA_HOME/bin/java" -version
log "JDK 17 version:"; "$JAVA17_HOME/bin/java" -version
log "Maven version:"; "$MAVEN_HOME/bin/mvn" -version
PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH"
log "Preparing cn1-binaries checkout"
if [ -d "$CN1_BINARIES/.git" ]; then
log "Found existing cn1-binaries repository at $CN1_BINARIES"
if git -C "$CN1_BINARIES" remote get-url origin >/dev/null 2>&1; then
if git -C "$CN1_BINARIES" fetch --depth=1 origin; then
remote_head=$(git -C "$CN1_BINARIES" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true)
if [ -z "$remote_head" ]; then
current_branch=$(git -C "$CN1_BINARIES" rev-parse --abbrev-ref HEAD 2>/dev/null || true)
if [ -n "$current_branch" ] && [ "$current_branch" != "HEAD" ]; then
remote_head="origin/$current_branch"
else
remote_head="origin/master"
fi
fi
if ! git -C "$CN1_BINARIES" rev-parse --verify "$remote_head" >/dev/null 2>&1; then
if git -C "$CN1_BINARIES" rev-parse --verify origin/main >/dev/null 2>&1; then
remote_head="origin/main"
elif git -C "$CN1_BINARIES" rev-parse --verify origin/master >/dev/null 2>&1; then
remote_head="origin/master"
else
log "Unable to determine remote head for cached cn1-binaries; removing checkout"
rm -rf "$CN1_BINARIES"
fi
fi
if [ -d "$CN1_BINARIES/.git" ]; then
log "Updating cn1-binaries to $remote_head"
git -C "$CN1_BINARIES" reset --hard "$remote_head"
fi
else
log "Failed to fetch updates for cached cn1-binaries; removing checkout"
rm -rf "$CN1_BINARIES"
fi
else
log "Cached cn1-binaries checkout missing origin remote; removing"
rm -rf "$CN1_BINARIES"
fi
fi
if [ ! -d "$CN1_BINARIES/.git" ]; then
log "Cloning cn1-binaries"
git clone --depth=1 --filter=blob:none https://github.com/codenameone/cn1-binaries "$CN1_BINARIES"
fi
log "Building Codename One core modules"
"$MAVEN_HOME/bin/mvn" -f maven/pom.xml -T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true -DskipTests -Djava.awt.headless=true -Dcn1.binaries="$CN1_BINARIES" -Dcodename1.platform=javase -P local-dev-javase,compile-android,!download-cn1-binaries install "$@"
log "Building Codename One Maven plugin"
"$MAVEN_HOME/bin/mvn" -f maven/pom.xml \
-pl codenameone-maven-plugin -am \
-T 1C -Dmaven.javadoc.skip=true -Dmaven.source.skip=true \
-DskipTests -Djava.awt.headless=true \
-Dcn1.binaries="$CN1_BINARIES" \
-P !download-cn1-binaries \
install "$@"
BUILD_CLIENT="$HOME/.codenameone/CodeNameOneBuildClient.jar"
log "Ensuring CodeNameOneBuildClient.jar is installed"
if [ ! -f "$BUILD_CLIENT" ]; then
if ! "$MAVEN_HOME/bin/mvn" -f maven/pom.xml -Dcn1.binaries="$CN1_BINARIES" -P !download-cn1-binaries cn1:install-codenameone "$@"; then
log "Falling back to copying CodeNameOneBuildClient.jar"
mkdir -p "$(dirname "$BUILD_CLIENT")"
cp maven/CodeNameOneBuildClient.jar "$BUILD_CLIENT" || true
fi
fi
log "Installing cn1-maven-archetypes"
if [ "${SKIP_CN1_ARCHETYPES:-0}" = "1" ]; then
log "Skipping cn1-maven-archetypes install because SKIP_CN1_ARCHETYPES=1"
skip_archetypes=1
else
set +e # don't let a transient git failure abort the whole build
if [ -d cn1-maven-archetypes/.git ]; then
log "Updating existing cn1-maven-archetypes checkout"
if ! git -C cn1-maven-archetypes fetch --all --tags; then
log "git fetch failed (exit 128?). Leaving existing copy as-is."
else
git -C cn1-maven-archetypes reset --hard origin/master || \
log "git reset failed; keeping local state."
fi
else
if ! git clone https://github.com/shannah/cn1-maven-archetypes cn1-maven-archetypes; then
log "git clone failed (likely exit 128). Skipping archetype install."
skip_archetypes=1
fi
fi
set -e
fi
if [ "${skip_archetypes:-0}" -eq 0 ]; then
(
cd cn1-maven-archetypes
current_version=$("$MAVEN_HOME/bin/mvn" -q -DforceStdout help:evaluate -Dexpression=project.version | tr -d '\r' | tail -n 1)
if [ -z "$current_version" ]; then
log "Unable to determine cn1-maven-archetypes version; proceeding with defaults"
elif [ "$current_version" != "$CN1_VERSION" ]; then
log "Updating cn1-maven-archetypes version from $current_version to $CN1_VERSION to match local snapshot"
"$MAVEN_HOME/bin/mvn" -q -B versions:set -DnewVersion="$CN1_VERSION" -DgenerateBackupPoms=false
fi
"$MAVEN_HOME/bin/mvn" -T 1C -DskipTests -DskipITs=true -Dinvoker.skip=true install
) || log "Archetype mvn install failed; continuing."
fi