Files
martial-master/scripts/ensure-martial-tool-installed.sh
T

94 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
TOOL_DIR="${TOOL_DIR:-$PROJECT_DIR/../martial-tool}"
CACHE_DIR="$PROJECT_DIR/.build-cache"
STATE_FILE="$CACHE_DIR/martial-tool.state"
log() {
printf '[martial-tool] %s\n' "$*"
}
fail() {
printf '[martial-tool] ERROR: %s\n' "$*" >&2
exit 1
}
[[ -d "$TOOL_DIR" ]] || fail "martial-tool directory not found: $TOOL_DIR"
[[ -f "$TOOL_DIR/pom.xml" ]] || fail "pom.xml not found in $TOOL_DIR"
command -v mvn >/dev/null 2>&1 || fail "mvn command not found"
revision="$(sed -n 's:.*<revision>\([^<]*\)</revision>.*:\1:p' "$TOOL_DIR/pom.xml" | head -n 1)"
[[ -n "$revision" ]] || fail "Unable to parse <revision> from $TOOL_DIR/pom.xml"
mapfile -t modules < <(sed -n 's:.*<module>\([^<]*\)</module>.*:\1:p' "$TOOL_DIR/pom.xml")
(( ${#modules[@]} > 0 )) || fail "No modules found in $TOOL_DIR/pom.xml"
missing=()
for artifact in "${modules[@]}"; do
if [[ "$artifact" == "blade-bom" ]]; then
group_path="org/springblade/platform"
else
group_path="org/springblade"
fi
version_dir="$HOME/.m2/repository/$group_path/$artifact/$revision"
if [[ ! -d "$version_dir" ]]; then
missing+=("$artifact")
continue
fi
if ! find "$version_dir" -maxdepth 1 -type f \( -name "$artifact-$revision.jar" -o -name "$artifact-$revision.pom" \) | grep -q .; then
missing+=("$artifact")
fi
done
if git -C "$TOOL_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git_head="$(git -C "$TOOL_DIR" rev-parse HEAD)"
git_status="$(git -C "$TOOL_DIR" status --porcelain)"
if [[ -n "$git_status" ]]; then
if command -v sha256sum >/dev/null 2>&1; then
dirty_hash="$(printf '%s' "$git_status" | sha256sum | awk '{print $1}')"
else
dirty_hash="$(printf '%s' "$git_status" | shasum -a 256 | awk '{print $1}')"
fi
tool_state="${git_head}-dirty-${dirty_hash}"
else
tool_state="$git_head"
fi
else
pom_hash="$(find "$TOOL_DIR" -name pom.xml -type f -print0 | sort -z | xargs -0 cat | sha256sum | awk '{print $1}')"
tool_state="nogit-${pom_hash}"
fi
mkdir -p "$CACHE_DIR"
previous_state=""
if [[ -f "$STATE_FILE" ]]; then
previous_state="$(cat "$STATE_FILE")"
fi
if [[ ${#missing[@]} -eq 0 && "$tool_state" == "$previous_state" ]]; then
log "Artifacts already installed for revision $revision and source state unchanged, skip install."
exit 0
fi
if [[ ${#missing[@]} -gt 0 ]]; then
log "Missing artifacts detected (${#missing[@]}): ${missing[*]}"
else
log "martial-tool source changed, reinstall required."
log "cached state: ${previous_state:-<none>}"
log "current state: $tool_state"
fi
log "Installing martial-tool from $TOOL_DIR ..."
(
cd "$TOOL_DIR"
mvn -T 1C -DskipTests install
)
printf '%s\n' "$tool_state" > "$STATE_FILE"
log "Install complete. State cache updated: $STATE_FILE"