Changeset - b4421b2d0743
[Not reviewed]
0 1 0
Branko Majic (branko) - 17 months ago 2024-06-25 00:42:21
branko@majic.rs
[factorio_development.sh] Implemented command for dependency update checks.
1 file changed with 105 insertions and 1 deletions:
0 comments (0 inline, 0 general)
games/factorio_development.sh
Show inline comments
 
@@ -22,7 +22,7 @@
 
set -u
 

	
 
PROGRAM="factorio_development.sh"
 
VERSION="2.0.0"
 
VERSION="2.1.0"
 

	
 
function usage() {
 
    cat <<EOF
 
@@ -31,10 +31,13 @@ $PROGRAM $VERSION, helper tool for development of Factorio mods
 
Usage:
 

	
 
  $PROGRAM [OPTIONS] init [MOD_DIRECTORY_PATH]
 

	
 
  $PROGRAM [OPTIONS] build [MOD_DIRECTORY_PATH]
 
  $PROGRAM [OPTIONS] release MOD_VERSION [MOD_DIRECTORY_PATH]
 
  $PROGRAM [OPTIONS] abort-release [MOD_DIRECTORY_PATH]
 

	
 
  $PROGRAM [OPTIONS] check-updates [MOD_DIRECTORY_PATH]
 

	
 
EOF
 
}
 

	
 
@@ -176,6 +179,16 @@ abort-release [MOD_DIRECTORY_PATH]
 
  - Switch back to main branch.
 
  - Drop the release branch.
 

	
 
check-updates [MOD_DIRECTORY_PATH]
 

	
 
  Arguments:
 

	
 
    MOD_DIRECTORY_PATH (path to base directory)
 

	
 
  Checks for available dependency updates (listed in the info
 
  file). Only dependencies with explicitly listed version are
 
  processed.
 

	
 

	
 
$PROGRAM accepts the following options:
 

	
 
@@ -991,6 +1004,82 @@ function command_abort_release() {
 
}
 

	
 

	
 
#
 
# Checks dependencies for available updates.
 
#
 
# Arguments:
 
#
 
#   $1 (base_dir)
 
#     Base (top-level) directory with the mod files.
 
#
 
# Outputs:
 
#
 
#   List of available dependency updates.
 
#
 
# Returns:
 
#
 
#   0 on success, 1 otherwise.
 
#
 
function command_check_updates() {
 

	
 
    local base_dir="$1"
 

	
 
    local info_file dependency mod_name local_version latest_version mod_info_url mod_info
 

	
 
    local outdated_dependency
 
    local outdated_dependencies=()
 

	
 
    info_file=$(get_info_file "$base_dir") || return 1
 

	
 
    # Process dependencies. Only take into account specifcations that include version.
 
    while read -r dependency; do
 
        # Extracts mod name and version from dependency specification.
 
        mod_name=$(echo "$dependency" | grep -E -o '.*(<|<=|=|>=|>)' | sed -E 's/[[:blank:]]*(<|<=|=|>=|>)//;s/^[[:blank:]]*([!?~]|\([?]\))[[:blank:]]*//')
 
        local_version=$(echo "$dependency" | grep -E -o "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$")
 

	
 
        [[ $mod_name == "base" ]] && continue
 

	
 
        # Fetch the information from public API. Encode the URL
 
        # because some mods have whitespaces in name.
 
        mod_info_url="https://mods.factorio.com/api/mods/${mod_name// /%20}"
 
        if ! mod_info=$(curl --silent --show-error "$mod_info_url"); then
 
            error "Failed to fetch mod information: $mod_name"
 
            error "Request URL: $mod_info_url"
 
            return 1
 
        fi
 

	
 
        latest_version=$(echo "$mod_info" | jq -r '.releases[].version' | sort --version-sort | tail -n1)
 

	
 
        if ! [[ $latest_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
 
            error "Failed to extract latest mod version: $mod_name"
 
            return 1
 
        fi
 
        
 
        # Sort by version, and use the check mode (returns 0 if lines are already sorted).
 
        if ! echo -e "$latest_version\n$local_version" | sort --version-sort --check=silent; then
 
            outdated_dependency=$(printf "%-48s %8s -> %8s" "$mod_name" "$local_version" "$latest_version")
 
            outdated_dependencies+=("$outdated_dependency")
 
        fi
 

	
 
    done < <(jq -r ".dependencies[]" "$info_file" | grep -E "(<|<=|=|>=|>)" | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
 

	
 
    if (( ${#outdated_dependencies[@]} == 0 )); then
 
        success "All listed dependencies are up-to-date."
 
    else
 
        echo -e "Dependencies with available updates:\n"
 

	
 
        for outdated_dependency in "${outdated_dependencies[@]}"; do
 
            echo "  $outdated_dependency"
 
        done
 

	
 
        echo
 
        warning "Some of the listed dependencies are outdated."
 
    fi
 

	
 
    return 0
 
}
 

	
 

	
 
# Set-up colours for message printing if we're not piping and terminal is
 
# capable of outputting the colors.
 
_COLOR_TERMINAL=$(tput colors 2>&1)
 
@@ -1149,6 +1238,21 @@ elif [[ $COMMAND == abort-release ]]; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
elif [[ $COMMAND == check-updates ]]; then
 

	
 
    MOD_DIRECTORY_PATH="${1:-.}"
 
    shift
 

	
 
    # Ensure that passed-in base directory is the repository root.
 
    if [[ ! -d $MOD_DIRECTORY_PATH/.git ]]; then
 
        error "Passed-in path does not point to base directory of the mod (must contain the .git sub-directory)."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if ! command_check_updates "$MOD_DIRECTORY_PATH"; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
else
 

	
 
    error "Unsupported command: $COMMAND"
0 comments (0 inline, 0 general)