Fix semantic version comaprison in self-update

This commit is contained in:
2024-11-04 09:19:25 -05:00
parent c2af8fef1b
commit a2ed872f15

View File

@@ -1549,10 +1549,6 @@ uninstall() {
# @description Checks for installJRMC update and re-executes, if necessary # @description Checks for installJRMC update and re-executes, if necessary
update() { update() {
debug "Running: ${FUNCNAME[0]} $*" debug "Running: ${FUNCNAME[0]} $*"
local script_url="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC"
local tmp; tmp=$(mktemp)
debug "Checking for installJRMC update" debug "Checking for installJRMC update"
# Function to extract and normalize version from a script # Function to extract and normalize version from a script
@@ -1566,28 +1562,33 @@ update() {
echo "$version_line" echo "$version_line"
} }
# Helper function for version comparison
version_greater() {
# Returns true if version $1 is greater than version $2
[[ "$(echo -e "$1\n$2" | sort -V | head -n 1)" != "$1" ]]
}
# Check if we're in a git directory and if it's the installJRMC repository # Check if we're in a git directory and if it's the installJRMC repository
if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null \ if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null \
&& [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" == *"bryan/installJRMC"* ]]; then && [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" == *"installJRMC"* ]]; then
debug "installJRMC git repository detected. Running git pull..." debug "installJRMC git repository detected. Running git pull..."
# Get the current commit hash # Get the current commit hash
local before_pull_hash local before_pull_hash after_pull_hash
before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD) before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
if git -C "$SCRIPT_DIR" pull | grep -qv "Already up to date"; then if git -C "$SCRIPT_DIR" pull | grep -qv "Already up to date"; then
return 0 return 0
fi fi
local after_pull_hash
after_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD) after_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
if [[ "$before_pull_hash" != "$after_pull_hash" ]]; then if [[ "$before_pull_hash" != "$after_pull_hash" ]]; then
echo "installJRMC repository updated. Restarting script..." echo "installJRMC repository updated. Restarting script..."
exec "$SCRIPT_PATH" "$@" "--no-update" exec "$SCRIPT_PATH" "$@" "--no-update"
fi fi
fi else # Download the latest version of the script
local script_url="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC"
local tmp; tmp=$(mktemp)
# Download the latest version of the script # Acquire latest version
install_package --silent wget install_package --silent wget
if command -v wget &>/dev/null; then if command -v wget &>/dev/null; then
execute wget -q -O "$tmp" "$script_url" execute wget -q -O "$tmp" "$script_url"
@@ -1603,14 +1604,14 @@ update() {
[[ -z $remote_version ]] && { rm -f "$tmp"; return 1; } [[ -z $remote_version ]] && { rm -f "$tmp"; return 1; }
# Compare versions and update if necessary # Compare versions and update if necessary
if [[ $SCRIPT_VERSION < $remote_version ]]; then if version_greater "$remote_version" "$SCRIPT_VERSION"; then
echo "Updating installJRMC $SCRIPT_VERSION to $remote_version" echo "Updating installJRMC $SCRIPT_VERSION to $remote_version"
execute mv "$tmp" "$SCRIPT_PATH" execute mv "$tmp" "$SCRIPT_PATH"
execute chmod +x "$SCRIPT_PATH" execute chmod +x "$SCRIPT_PATH"
rm -f "$tmp"
exec "$SCRIPT_PATH" "$@" "--no-update" exec "$SCRIPT_PATH" "$@" "--no-update"
fi fi
fi
rm -f "$tmp"
} }
# @description installJRMC main function # @description installJRMC main function