From 57fdd108c02fa6e6467ccb816e01bac347b8240f Mon Sep 17 00:00:00 2001 From: MRColor Date: Thu, 26 Oct 2023 19:06:50 +0200 Subject: [PATCH] ADD - script update checker function, new app EARNFM, script version patch typo fix --- .env.template | 2 +- runme.ps1 | 41 ++++++++++++++++++++++++++++++++++------- runme.sh | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/.env.template b/.env.template index 1dbe8b9..6dc256e 100644 --- a/.env.template +++ b/.env.template @@ -12,7 +12,7 @@ # ###################################################################### ## PROJECT_VERSION -PROJECT_VERSION=2.5.4 +PROJECT_VERSION=2.5.1 ## PROJECT NAME COMPOSE_PROJECT_NAME=money4band DS_PROJECT_SERVER_URL=https://discord.com/invite/Fq8eeazBAD diff --git a/runme.ps1 b/runme.ps1 index 2566022..c8d9dbb 100644 --- a/runme.ps1 +++ b/runme.ps1 @@ -196,18 +196,45 @@ function fn_fail($text) { # Function to check if there are any updates available # function check_project_updates { # Get the current script version from the local .env file - $SCRIPT_VERSION = (Get-Content .\$ENV_FILENAME | Select-String -Pattern "PROJECT_VERSION=" -SimpleMatch).ToString().Split("=")[1] + $SCRIPT_VERSION_MATCH = (Get-Content .\$ENV_FILENAME | Select-String -Pattern "PROJECT_VERSION=(\d+\.\d+\.\d+)").Matches + if ($SCRIPT_VERSION_MATCH.Count -eq 0) { + errorprint_and_log "Failed to get the script version from the local .env file." + return + } + $SCRIPT_VERSION = $SCRIPT_VERSION_MATCH[0].Groups[1].Value # Get the latest script version from the .env.template file on GitHub - $webClient = New-Object System.Net.WebClient - $templateContent = $webClient.DownloadString("$PROJECT_URL/$ENV_TEMPLATE_FILENAME") - $LATEST_SCRIPT_VERSION = ($templateContent | Select-String -Pattern "PROJECT_VERSION=" -SimpleMatch).ToString().Split("=")[1] + try { + $webClient = New-Object System.Net.WebClient + $templateContent = $webClient.DownloadString("$PROJECT_URL/$ENV_TEMPLATE_FILENAME") + $LATEST_SCRIPT_VERSION_MATCH = ($templateContent | Select-String -Pattern "PROJECT_VERSION=(\d+\.\d+\.\d+)").Matches + if ($LATEST_SCRIPT_VERSION_MATCH.Count -eq 0) { + errorprint_and_log "Failed to get the latest script version from GitHub." + return + } + $LATEST_SCRIPT_VERSION = $LATEST_SCRIPT_VERSION_MATCH[0].Groups[1].Value + } catch { + errorprint_and_log "Failed to fetch the .env.template file from GitHub: $_" + return + } + + # Split the versions into major, minor, and patch numbers + $SCRIPT_VERSION_SPLIT = $SCRIPT_VERSION.Split(".") + $LATEST_SCRIPT_VERSION_SPLIT = $LATEST_SCRIPT_VERSION.Split(".") # Compare the versions and print a message if a newer version is available - if ($SCRIPT_VERSION -lt $LATEST_SCRIPT_VERSION) { - print_and_log "GREEN" "A newer version of the script is available. Please consider updating." + for ($i=0; $i -lt 3; $i++) { + if ([int]$SCRIPT_VERSION_SPLIT[$i] -lt [int]$LATEST_SCRIPT_VERSION_SPLIT[$i]) { + print_and_log "Yellow" "A newer version of the script is available. Please consider updating." + return + } + elseif ([int]$SCRIPT_VERSION_SPLIT[$i] -gt [int]$LATEST_SCRIPT_VERSION_SPLIT[$i]) { + return + } } - Read-Host -Prompt "Press Enter to continue" + + # If the loop completes without finding a newer version, print a message indicating that the script is up to date + print_and_log "BLUE" "Script is up to date." } # Function to detect OS diff --git a/runme.sh b/runme.sh index fb46a53..87a2d39 100644 --- a/runme.sh +++ b/runme.sh @@ -37,7 +37,7 @@ fi #read -r -p "Press Enter to continue" # Script version getting it from ${ENV_FILENAME} file # -readonly SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_FILENAME}) +SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_FILENAME}) # Script name # readonly SCRIPT_NAME=$(basename "$0") # save the script name in a variable not the full path @@ -197,17 +197,42 @@ fn_fail() { # Function to check if there are any updates available # check_project_updates() { # Get the current script version from the local .env file - SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K.*' "./\$ENV_FILENAME") + SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' "./${ENV_FILENAME}") + if [[ -z $SCRIPT_VERSION ]]; then + errorprint_and_log "Failed to get the script version from the local .env file." + return 1 + fi # Get the latest script version from the .env.template file on GitHub - LATEST_SCRIPT_VERSION=$(curl -s "$PROJECT_URL/$ENV_TEMPLATE_FILENAME" | grep -oP 'PROJECT_VERSION=\K.*') + LATEST_SCRIPT_VERSION=$(curl -fs "$PROJECT_URL/$ENV_TEMPLATE_FILENAME" | grep -oP 'PROJECT_VERSION=\K[^#\r]+') + if [[ -z $LATEST_SCRIPT_VERSION ]]; then + errorprint_and_log "Failed to get the latest script version from GitHub." + return 1 + fi + + # Split the versions into major, minor, and patch numbers + IFS='.' read -ra SCRIPT_VERSION_SPLIT <<< "$SCRIPT_VERSION" + IFS='.' read -ra LATEST_SCRIPT_VERSION_SPLIT <<< "$LATEST_SCRIPT_VERSION" # Compare the versions and print a message if a newer version is available - if [[ "$SCRIPT_VERSION" < "$LATEST_SCRIPT_VERSION" ]]; then - print_and_log "GREEN" "A newer version of the script is available. Please consider updating." - fi + for i in "${!SCRIPT_VERSION_SPLIT[@]}"; do + if (( ${SCRIPT_VERSION_SPLIT[i]} < ${LATEST_SCRIPT_VERSION_SPLIT[i]} )); then + print_and_log "YELLOW" "A newer version of the script is available. Please consider updating." + return 0 # Return here to exit the function as soon as a newer version is found + elif (( ${SCRIPT_VERSION_SPLIT[i]} > ${LATEST_SCRIPT_VERSION_SPLIT[i]} )); then + # If any part of the local version is greater, it's not an older version + return 0 + fi + done + + # If the loop completes without finding a newer version, you're up to date + print_and_log "BLUE" "Script is up to date." } + + + + # Function to detect OS detect_os() { toLog_ifDebug -l "[DEBUG]" -m "Detecting OS..."