From ae6becb2ccfdcb102b386acd62407552ecdca178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Fri, 5 Apr 2024 00:36:24 +0200 Subject: [PATCH] Updated zrep script to version 0.0.7 with new features and improvements. - Updated version to 0.0.7 - Added URL-encoding function for search queries - Implemented search function to query and process JSON response - Refactored package removal function - Added check for 'jq' installation - Improved package installation process - Enhanced update package functionality - Added search command to search for authors, packages, or descriptions --- t | 34 +++++++++++++ zrep | 163 ++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 145 insertions(+), 52 deletions(-) create mode 100755 t diff --git a/t b/t new file mode 100755 index 0000000..21e110a --- /dev/null +++ b/t @@ -0,0 +1,34 @@ +#!/usr/bin/zsh + +function zrep_fpath_2() { + local base_dir="${1}" + + # Ensure globbing finds dotfiles and nullglob avoids empty directory issues + setopt local_options dotglob nullglob + + # Check if the base directory exists + if [[ ! -d "${base_dir}" ]]; then + echo "Error: Base directory '${base_dir}' does not exist." + return 1 + fi + + # Iterate over directories within $base_dir with exactly one character + for one_char_dir in ${base_dir}/?; do + # Check if it's indeed a directory + [[ -d "${one_char_dir}" ]] || continue + + # Recursively find all final directories under one_char_dir with the pattern first_letter/author/script + for script_dir in ${one_char_dir}/*/*(/); do + local script_name=$(basename "${script_dir}") + local matching_files=("${script_dir}/${script_name}") + + # Check if there's at least one file matching the script directory's name + if (( ${#matching_files} )); then + echo "${script_dir}" + fi + done + done +} + +zrep_fpath_2 /home/stig/.zrep + diff --git a/zrep b/zrep index 2f22818..efe798d 100755 --- a/zrep +++ b/zrep @@ -2,13 +2,17 @@ setopt extendedglob -VERSION="0.0.6" # Wed-2024-04-03 +VERSION="0.0.7" # Fri-2024-04-05 ZREP="Zsh Repository Tool" # Define the default path to .zreprc ZREP_CONFIG="${HOME}/.zreprc" function zrep_fpath() { local base_dir="${1}" + local mode="${2:-generic}" # Default mode is 'generic' + + # Ensure globbing finds dotfiles and nullglob avoids empty directory issues + setopt local_options dotglob nullglob # Check if the base directory exists if [[ ! -d "${base_dir}" ]]; then @@ -16,12 +20,28 @@ function zrep_fpath() { return 1 fi - # Add directories containing at least one file to fpath - for dir in ${base_dir}/**/*(/N); do - if [[ -n $(ls -A "${dir}/") ]]; then - fpath=(${dir} $fpath) - fi - done + if [[ "${mode}" == "zrep_load" ]]; then + # Specific mode for first_letter/author/script structure + for one_char_dir in ${base_dir}/?; do + [[ -d "${one_char_dir}" ]] || continue + + for script_dir in ${one_char_dir}/*/*(/); do + local script_name=$(basename "${script_dir}") + local matching_files=("${script_dir}/${script_name}".*) + + if (( ${#matching_files} )); then + fpath+=("${script_dir}") + fi + done + done + else + # Generic mode for any directory containing at least one file + for dir in ${base_dir}/**/*(/N); do + if [[ -n $(ls -A "${dir}/") ]]; then + fpath+=("${dir}") + fi + done + fi } if [[ ${1} != "init" ]]; then @@ -115,6 +135,65 @@ function zrep_msg() { printf "%b\n" "${color}${message}${base_colors[end]}" } +################################################################################################# +# Function to URL-encode strings in Zsh +zrep_search_url_encode() { + local string="${1}" + local strlen=${#string} + local encoded="" + + for (( pos=0 ; pos /dev/null; then + if [[ ! -x $(which jq) ]]; then echo "Error: 'jq' is not installed. Please install jq to continue." exit 1 fi if [[ -f "${ZREP_CONFIG}" ]]; then zini "${ZREP_CONFIG}" - zrep_fpath ${config[main_zrep_install_dir]} + zrep_fpath ${config[main_zrep_install_dir]} zrep_load else - if [[ "${1}" == "init" ]]; then - echo "\nWelcome to zrep. Looks like this is the first time you are" - echo "running me." - read "response?Are you ready to set up zrep? (y/n): " - if [[ "${response}" =~ ^[Yy]$ ]]; then - zrep_init - else - echo "Setup aborted. Try again later." - fi - else - # echo "${ZREP_CONFIG} not found." - # Ask the user if they want to run 'zrep init' - echo "\nWelcome to zrep. I was unable to find the config file." - read "response?Would you like to run 'zrep init' to set up? (y/n): " - if [[ "${response}" =~ ^[Yy]$ ]]; then - zrep_init - else - echo "Initialization canceled. Please run 'zrep init' manually to set up." - exit 1 - fi - fi + echo "${ZREP_CONFIG} not found. Run 'zrep init' to set up." + exit 1 fi } @@ -414,10 +470,11 @@ function zrep_remove_package() { local first_letter="${author:0:1:l}" local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}" - zrep_msg info "\nFound installed package: $package_name, version: $installed_version" + zrep_msg std "\nFound installed package: $package_name, version: $installed_version" # Ask user for confirmation with default response "Y" - read "REPLY?Are you sure you want to remove this package? (y/n) [Y]: " + zrep_msg info "Are you sure you want to remove this package? (y/n) [Y]: \c" + read REPLY REPLY=${REPLY:-Y} echo @@ -425,7 +482,6 @@ function zrep_remove_package() { # Remove the package directory from disk if [[ -d "${package_dir}" ]]; then rm -rf "${package_dir}" - zrep_msg sub "Package directory '${package_dir}' removed successfully." else zrep_msg debug "Warning: Package directory '${package_dir}' not found." fi @@ -436,7 +492,7 @@ function zrep_remove_package() { if .[$author] == [] then del(.[$author]) else . end' \ "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json" - zrep_msg sub "Package '${package_name}' removed successfully from installed.json." + zrep_msg sub "Package '${package_name}' successfully removed." else zrep_msg info "Removal canceled." fi @@ -458,10 +514,8 @@ function zrep_check_if_installed() { if [[ -n "${installed_version}" && "${installed_version}" != "null" ]]; then # Package is installed, and version is stored in installed_version - # echo "Package $package is installed with version $installed_version." return 0 # Package is installed else - # echo "Package $package is not installed." return 1 # Package is not installed fi } @@ -482,13 +536,16 @@ function zrep_global_downloader() { exit ;; esac + eval ${dloader} "${1}" } typeset -A updatesAvailable function zrep_check_for_updates() { - local remoteFile="${config[global_repo_url]}/packages.json\?$(date +%s)" + # Now using a zrep API to fetch the JSON for checking for updates. This _may_ change in the + # future if the load on the DB gets too high. + local remoteFile="${config[global_repo_url]}/getver.php" # local localFile="${config[main_zrep_install_dir]}/installed.json" zrep_installed_json local remotePackages=$(zrep_global_downloader "${remoteFile}") @@ -533,8 +590,6 @@ function zrep_update_package() { if [[ -n "$version" ]]; then local author="${specificPackage%%/*}" local script="${specificPackage#*/}" - # local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}" - # echo "Updating $specificPackage to version $version..." local install_pkg="${author}/${script}" zrep_install_package u ${install_pkg} else @@ -557,9 +612,6 @@ function zrep_update_package() { local author=${package%%/*} local script=${package#*/} local version=${updatesAvailable[${package}]} - # local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}" - - # echo "Preparing to update $package to version $version..." local install_pkg="${author}/${script}" zrep_install_package u ${install_pkg} done @@ -662,13 +714,15 @@ function zrep_install_package() { fi # If not installed, proceed with fetching the package information - zrep_parse_remote "${config[global_repo_url]}/packages.json\?$(date +%s)" "${package}" + # Using the new zrep API to get the package version + zrep_parse_remote "${config[global_repo_url]}/getver.php\?p\=${package}" ${package} local tmpDir="${config[main_zrep_install_dir]}/tmp" mkdir -p "${tmpDir}" local zipFile="${tmpDir}/${author}-${script}-${version}.zip" + dlurl="${config[global_repo_url]}/download/${package}/${version}" zrep_download_package "${zipFile}" "${dlurl}" unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}" @@ -806,9 +860,10 @@ function zrep_help() { zrep_msg info " update (u) :\t\t\tUpdate zrep package" zrep_msg info " enable :\t\t\tEnable zrep package" zrep_msg info " disable :\t\t\tDisable zrep package" + zrep_msg info " search 'search term':\t\t\t\tSearch for authors, packages or description" zrep_msg info " version:\t\t\t\t\tDisplay zrep version" zrep_msg info " list:\t\t\t\t\t\tList installed packages" - zrep_msg info " help:\t\t\tDisplay help for pacakage (if available)" + zrep_msg info " help:\t\t\tDisplay help for package (if available)" } @@ -861,6 +916,7 @@ function zrep_read_usage() { function main() { zrep_main_version_string + if [[ ${1} != "init" ]]; then zrep_load_config ${1} zrep_load_theme ${config[global_theme]} @@ -879,6 +935,9 @@ function main() { zrep_check_for_deps exit ;; + search | s | find) + zrep_search "${@:2}" + ;; install | i) zrep_install_package ${2} ;;