Update zrep initialization to include global settings and reminder to source zshrc. Check for 'zini' path in fpath before adding. Enhance update logic to handle specific package updates. Improve update messaging and handle no updates found case. Refactor update package function for better readability. Add conditional help message for 'init' command if config file doesn't exist.

This commit is contained in:
Stig-Ørjan Smelror 2024-03-12 14:58:13 +01:00
parent 849d4e46a4
commit 4d249d2bf1

52
zrep
View File

@ -95,18 +95,25 @@ function zrep_init() {
install_dir=${install_dir:-"${HOME}/.zrep"} install_dir=${install_dir:-"${HOME}/.zrep"}
# Write to .zreprc # Write to .zreprc
echo "[main]" > ${ZREP_CONFIG} cat > "${ZREP_CONFIG}" <<EOF
echo "zrep_install_dir='${install_dir}'" >> "${ZREP_CONFIG}" [main]
zrep_install_dir = ${install_dir}
[global]
repo_url = https://kekepower.com/zrep
EOF
else else
echo "Loading configuration from ${ZREP_CONFIG}" echo "Loading configuration from ${ZREP_CONFIG}"
zini ${ZREP_CONFIG} zini ${ZREP_CONFIG}
install_dir=${config[main_zrep_install_dir]} install_dir=${config[main_zrep_install_dir]}
fi fi
# Ensure zrep_install_dir exists
mkdir -p "${install_dir}/functions/zini" mkdir -p "${install_dir}/functions/zini"
curl -s https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini" curl -s https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"
if ! grep -q "zini" "${zshrc_file}"; then
echo "Adding 'zini' path to fpath in ${zshrc_file}"
echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file} echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file}
fi
# Check if .zshrc already sources .zrep_addons, if not, add it # Check if .zshrc already sources .zrep_addons, if not, add it
if ! grep -q "source ${addons_file}" "${zshrc_file}"; then if ! grep -q "source ${addons_file}" "${zshrc_file}"; then
@ -139,6 +146,7 @@ EOF
fi fi
echo "zrep initialization complete." echo "zrep initialization complete."
echo "Remember to 'source ${zshrc_file}' to load the 'zrep' settings."
} }
# Function to parse remote JSON data and extract author, script, and version # Function to parse remote JSON data and extract author, script, and version
@ -346,7 +354,7 @@ function zrep_check_if_installed() {
installed_version=$(jq -r --arg author "$author_name" --arg script "$script_name" \ installed_version=$(jq -r --arg author "$author_name" --arg script "$script_name" \
'.[$author][] | select(.script == $script) | .version' "$installedJson") '.[$author][] | select(.script == $script) | .version' "$installedJson")
if [[ -n "$installed_version" && "$installed_version" != "null" ]]; then if [[ -n "${installed_version}" && "${installed_version}" != "null" ]]; then
# Package is installed, and version is stored in installed_version # Package is installed, and version is stored in installed_version
# echo "Package $package is installed with version $installed_version." # echo "Package $package is installed with version $installed_version."
return 0 # Package is installed return 0 # Package is installed
@ -360,7 +368,7 @@ typeset -A updatesAvailable
function zrep_check_for_updates() { function zrep_check_for_updates() {
local remoteFile="${config[global_repo_url]}/packages.json" local remoteFile="${config[global_repo_url]}/packages.json"
local localFile="${config[main_zrep_install_dir]}/installed.json" local localFile="${config[main_zrep_install_dir]}/installed.json"
local remotePackages=$(curl -s "$remoteFile") local remotePackages=$(curl -s "${remoteFile}")
# Reset global variables # Reset global variables
updatesAvailable=() updatesAvailable=()
@ -372,52 +380,56 @@ function zrep_check_for_updates() {
for entry in ${(f)authorsScripts}; do for entry in ${(f)authorsScripts}; do
local author="${entry%%/*}" local author="${entry%%/*}"
local rest="${entry#*/}" local rest="${entry#*/}"
local script_name="${rest%%:*}" local script="${rest%%:*}"
local installed_version="${rest##*:}" local installed_version="${rest##*:}"
local remote_version=$(jq -r --arg author "$author" --arg script "$script_name" \ local remote_version=$(jq -r --arg author "$author" --arg script "$script" \
'.authors[] | select(.name==$author) | .scripts[] | select(.name==$script) | .version' <<<"$remotePackages") '.authors[] | select(.name==$author) | .scripts[] | select(.name==$script) | .version' <<<"$remotePackages")
if [[ "$remote_version" > "$installed_version" ]]; then if [[ "${remote_version}" > "${installed_version}" ]]; then
updatesAvailable[$author/$script_name]="$remote_version" updatesAvailable[${author}/${script}]="${remote_version}"
zrep_msg info "\n$author/$script_name can be updated from $installed_version to $remote_version" zrep_msg info "\n${author}/${script} can be updated from ${installed_version} to ${remote_version}"
updates=true # Mark that updates are available updates=true # Mark that updates are available
fi fi
done done
} }
function zrep_update_package() { function zrep_update_package() {
local specificPackage=${1} local specificPackage=${1}
zrep_check_for_updates zrep_check_for_updates
# Check if in update mode or specific package update # Check if in update mode or specific package update
if [[ -n "$specificPackage" ]]; then if [[ -n "${specificPackage}" ]]; then
# Logic for updating a specific package # Logic for updating a specific package
# Assuming specificPackage format is "author/script" # Assuming specificPackage format is "author/script"
local version=${updatesAvailable[$specificPackage]} local version=${updatesAvailable[${specificPackage}]}
if [[ -n "$version" ]]; then if [[ -n "$version" ]]; then
local author="${specificPackage%%/*}" local author="${specificPackage%%/*}"
local script="${specificPackage#*/}" local script="${specificPackage#*/}"
local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}" local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
# echo "Updating $specificPackage to version $version..." # echo "Updating $specificPackage to version $version..."
zrep_install_package u $author/$script local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg}
else else
echo "No update available for $specificPackage." echo "No update available for ${specificPackage}."
fi fi
else else
if [[ ${updates} == "true" ]]; then
# General update mode: update all packages listed in updatesAvailable # General update mode: update all packages listed in updatesAvailable
for package in ${(k)updatesAvailable}; do for package in ${(k)updatesAvailable}; do
local author=${package%%/*} local author=${package%%/*}
local script=${package#*/} local script=${package#*/}
local version=${updatesAvailable[$package]} local version=${updatesAvailable[${package}]}
# local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}" # local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
# echo "Preparing to update $package to version $version..." # echo "Preparing to update $package to version $version..."
local install_pkg="$author/$script" local install_pkg="${author}/${script}"
zrep_install_package u $install_pkg zrep_install_package u ${install_pkg}
done done
else
zrep_msg info "\nNo updates found."
fi
fi fi
} }
@ -432,7 +444,7 @@ function zrep_install_package() {
updates=false updates=false
local package="${1}" local package="${1}"
# Call zrep_check_if_installed to check if the package is already installed # Call zrep_check_if_installed to check if the package is already installed
if zrep_check_if_installed "$package"; then if zrep_check_if_installed "${package}"; then
zrep_msg info "Package ${package} is already installed." zrep_msg info "Package ${package} is already installed."
zrep_msg info "Use 'zrep list' to see installed packages." zrep_msg info "Use 'zrep list' to see installed packages."
return 0 return 0
@ -598,7 +610,9 @@ function zrep_help() {
zrep_msg sub "\nUsage: zrep <command> [arguments]" zrep_msg sub "\nUsage: zrep <command> [arguments]"
zrep_msg info "Available commands:" zrep_msg info "Available commands:"
if [[ ! -f ${ZREP_CONFIG} ]]; then
zrep_msg info " init: Initialize zrep" zrep_msg info " init: Initialize zrep"
fi
zrep_msg info " check: Check for updates" zrep_msg info " check: Check for updates"
zrep_msg info " install (i) <author/package>: Install a package" zrep_msg info " install (i) <author/package>: Install a package"
zrep_msg info " remove (rm, delete) <author/package>: Remove a package" zrep_msg info " remove (rm, delete) <author/package>: Remove a package"