6 Commits

Author SHA1 Message Date
aee45ba2ee Added zsh functions for managing zrep packages. Fixed bugs in zrep functions. Updated zrep version to 0.0.7. 2024-04-05 21:43:51 +02:00
ae6becb2cc 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
2024-04-05 00:36:24 +02:00
8ea89b837b Added search function link to zrep website.
Added note about future search function in zrep script.
2024-04-04 13:12:53 +02:00
e37233009c Update README with information about zrep tool and its usage. Added a section explaining what zrep is and how to share scripts. 2024-04-04 13:07:14 +02:00
6399a20ed1 Updated zrep version to 0.0.6
- Updated version to 0.0.6
- Changed repo_url to https://zrep.kekepower.com
- Added reminder to copy 'zrep' to a directory in 'PATH'
- Added reminder to source ${zshrc_file} after installation.
2024-04-03 13:57:43 +02:00
2e0d42c040 Updated zrep version to 0.0.5
- Updated version to 0.0.5
- Added validation for package name format
- Display error message if package name format is incorrect
2024-04-02 21:57:39 +02:00
2 changed files with 145 additions and 64 deletions

View File

@ -4,6 +4,12 @@
zrep is a powerful and user-friendly package manager for the Zsh shell. It allows you to easily install, manage, and share Zsh packages, such as plugins, themes, and scripts, directly from a central repository. zrep is a powerful and user-friendly package manager for the Zsh shell. It allows you to easily install, manage, and share Zsh packages, such as plugins, themes, and scripts, directly from a central repository.
## What is it?
`zrep` in and of itself is the command line tool that you use to install and manage scripts that are placed in `$fpath` used by Zsh. The packages are available on [the zrep website](https://zrep.kekepower.com) where you can, if you want to share your own scripts, register for an account. By sharing your scripts with the community, you are probably making somebody's day easier.
You can read more in the Wiki.
## Key Features ## Key Features
- **Easy Installation**: zrep simplifies the process of installing Zsh packages. With a single command, you can install packages from the zrep repository. - **Easy Installation**: zrep simplifies the process of installing Zsh packages. With a single command, you can install packages from the zrep repository.
@ -20,6 +26,8 @@ zrep is a powerful and user-friendly package manager for the Zsh shell. It allow
To start using zrep, simply run the `zrep init` command to initialize the tool. zrep will guide you through the setup process and create the required configuration files. To start using zrep, simply run the `zrep init` command to initialize the tool. zrep will guide you through the setup process and create the required configuration files.
Use the search function on **[the zrep website](https://zrep.kekepower.com)** to find useful scripts until a search function is added to the `zrep` script some time in the future.
Once initialized, you can explore and install packages using commands like `zrep install <author/package>`, `zrep list`, and `zrep enable <author/package>`. Once initialized, you can explore and install packages using commands like `zrep install <author/package>`, `zrep list`, and `zrep enable <author/package>`.
## Contributing ## Contributing

View File

@ -2,13 +2,17 @@
setopt extendedglob setopt extendedglob
VERSION="0.0.4" # Sun-2024-03-31 VERSION="0.0.8" # Fri-2024-04-05
ZREP="Zsh Repository Tool" ZREP="Zsh Repository Tool"
# Define the default path to .zreprc # Define the default path to .zreprc
ZREP_CONFIG="${HOME}/.zreprc" ZREP_CONFIG="${HOME}/.zreprc"
function zrep_fpath() { function zrep_fpath() {
local base_dir="${1}" 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 # Check if the base directory exists
if [[ ! -d "${base_dir}" ]]; then if [[ ! -d "${base_dir}" ]]; then
@ -16,12 +20,28 @@ function zrep_fpath() {
return 1 return 1
fi fi
# Add directories containing at least one file to fpath if [[ "${mode}" == "zrep_load" ]]; then
for dir in ${base_dir}/**/*(/N); do # Specific mode for first_letter/author/script structure
if [[ -n $(ls -A "${dir}/") ]]; then for one_char_dir in ${base_dir}/?; do
fpath=(${dir} $fpath) [[ -d "${one_char_dir}" ]] || continue
fi
done 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 if [[ ${1} != "init" ]]; then
@ -115,6 +135,65 @@ function zrep_msg() {
printf "%b\n" "${color}${message}${base_colors[end]}" 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<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
# This will output the URL-encoded string
}
# Function to perform a search query and process JSON response
zrep_search() {
local searchTerm="${@}"
local encodedSearch=$(zrep_search_url_encode "${searchTerm}")
# Use the global_repo_url from the config associative array
local response=$(curl -s -A "zrep ${VERSION} (curl)" "${config[global_repo_url]}/find.php?s=${encodedSearch}")
# Determine if the JSON response is an object indicating "No scripts found"
local jsonType=$(echo "$response" | jq -r 'type')
zrep_msg std "\nSearch results:"
if [[ "$jsonType" == "object" ]]; then
# Assuming an object type indicates a message field exists
local message=$(echo "$response" | jq -r '.message')
echo "$message"
return 0
elif [[ "$jsonType" == "array" ]]; then
# It's an array, process each item
echo "$response" | jq -c '.[]' | while IFS= read -r line; do
local script=$(echo "$line" | jq -r '.script')
local description=$(echo "$line" | jq -r '.description')
local url=$(echo "$line" | jq -r '.url')
# You can process these variables further as needed
zrep_msg other " * $script"
zrep_msg sub " - Description: $description"
zrep_msg sub " - $url"
# echo "-------------------------------------"
done
else
echo "Unexpected JSON format."
return 1
fi
}
############################################################################################
# Function to check if a given string exists in ~/.zshrc # Function to check if a given string exists in ~/.zshrc
function zrep_find_string() { function zrep_find_string() {
local searchString="$1" local searchString="$1"
@ -174,7 +253,7 @@ function zrep_init() {
zrep_install_dir = ${install_dir} zrep_install_dir = ${install_dir}
[global] [global]
repo_url = https://kekepower.com/zrep repo_url = https://zrep.kekepower.com
theme = classic theme = classic
downloader = ${downloader} downloader = ${downloader}
EOF EOF
@ -232,6 +311,8 @@ EOF
zrep_global_downloader https://git.kekepower.com/kekePower/zrep/raw/branch/main/themes/classic -o ${install_dir}/themes/classic zrep_global_downloader https://git.kekepower.com/kekePower/zrep/raw/branch/main/themes/classic -o ${install_dir}/themes/classic
fi fi
echo "zrep initialization complete." echo "zrep initialization complete."
echo "You should copy 'zrep' to a path in you 'PATH' so that it's accessible."
echo "For example '${HOME}/bin'"
echo "Remember to 'source ${zshrc_file}' to load the 'zrep' settings." echo "Remember to 'source ${zshrc_file}' to load the 'zrep' settings."
} }
@ -262,17 +343,15 @@ function zrep_parse_remote() {
json_data=$(zrep_global_downloader "${url}") json_data=$(zrep_global_downloader "${url}")
# Directly extract the details based on author_name and script_name # Directly extract the details based on author_name and script_name
dlurl=$(echo "${json_data}" | jq -r --arg author_name "$author_name" --arg script_name "$script_name" '.authors[] | select(.name==$author_name) | .scripts[] | select(.name==$script_name) | .dlurl')
version=$(echo "${json_data}" | jq -r --arg author_name "$author_name" --arg script_name "$script_name" '.authors[] | select(.name==$author_name) | .scripts[] | select(.name==$script_name) | .version') version=$(echo "${json_data}" | jq -r --arg author_name "$author_name" --arg script_name "$script_name" '.authors[] | select(.name==$author_name) | .scripts[] | select(.name==$script_name) | .version')
# Check if the dlurl and version are found # Check if the dlurl and version are found
if [[ -n "$dlurl" && -n "$version" ]]; then if [[ -n "$version" ]]; then
# Set the details as global # Set the details as global
export author="$author_name" #export author="$author_name"
export script="$script_name" #export script="$script_name"
export version export version
export dlurl #export dlurl
else else
zrep_msg debug "\nPackage ${package} not found.\n" zrep_msg debug "\nPackage ${package} not found.\n"
exit 1 exit 1
@ -354,40 +433,19 @@ function zrep_list_package() {
fi fi
} }
# Function to load configuration
function zrep_load_config() { function zrep_load_config() {
# Check if jq is available if [[ ! -x $(which jq) ]]; then
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' is not installed. Please install jq to continue." echo "Error: 'jq' is not installed. Please install jq to continue."
exit 1 exit 1
fi fi
if [[ -f "${ZREP_CONFIG}" ]]; then if [[ -f "${ZREP_CONFIG}" ]]; then
zini "${ZREP_CONFIG}" zini "${ZREP_CONFIG}"
zrep_fpath ${config[main_zrep_install_dir]} zrep_fpath ${config[main_zrep_install_dir]} zrep_load
else else
if [[ "${1}" == "init" ]]; then echo "${ZREP_CONFIG} not found. Run 'zrep init' to set up."
echo "\nWelcome to zrep. Looks like this is the first time you are" exit 1
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
fi fi
} }
@ -412,10 +470,11 @@ function zrep_remove_package() {
local first_letter="${author:0:1:l}" local first_letter="${author:0:1:l}"
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}" 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" # 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} REPLY=${REPLY:-Y}
echo echo
@ -423,7 +482,6 @@ function zrep_remove_package() {
# Remove the package directory from disk # Remove the package directory from disk
if [[ -d "${package_dir}" ]]; then if [[ -d "${package_dir}" ]]; then
rm -rf "${package_dir}" rm -rf "${package_dir}"
zrep_msg sub "Package directory '${package_dir}' removed successfully."
else else
zrep_msg debug "Warning: Package directory '${package_dir}' not found." zrep_msg debug "Warning: Package directory '${package_dir}' not found."
fi fi
@ -434,7 +492,7 @@ function zrep_remove_package() {
if .[$author] == [] then del(.[$author]) else . end' \ if .[$author] == [] then del(.[$author]) else . end' \
"$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json" "$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 else
zrep_msg info "Removal canceled." zrep_msg info "Removal canceled."
fi fi
@ -456,10 +514,8 @@ function zrep_check_if_installed() {
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."
return 0 # Package is installed return 0 # Package is installed
else else
# echo "Package $package is not installed."
return 1 # Package is not installed return 1 # Package is not installed
fi fi
} }
@ -480,13 +536,27 @@ function zrep_global_downloader() {
exit exit
;; ;;
esac esac
eval ${dloader} "${1}" eval ${dloader} "${1}"
} }
function zrep_process_updates() {
for package in ${(k)updatesAvailable}; do
local updateDetails=(${(s/|/)updatesAvailable[${package}]})
local author=${updateDetails[1]}
local script=${updateDetails[2]}
local version=${updateDetails[3]}
echo "Updating ${author}/${script} to version ${version}..."
zrep_install_package u "${author}/${script}" "${version}"
done
}
typeset -A updatesAvailable typeset -A updatesAvailable
function zrep_check_for_updates() { 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" # local localFile="${config[main_zrep_install_dir]}/installed.json"
zrep_installed_json zrep_installed_json
local remotePackages=$(zrep_global_downloader "${remoteFile}") local remotePackages=$(zrep_global_downloader "${remoteFile}")
@ -508,7 +578,9 @@ function zrep_check_for_updates() {
'.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}]="${remote_version}" # updatesAvailable[${author}/${script}]="${remote_version}"
# Store author, script, and version in a single string, separated by "|"
updatesAvailable[${author}/${script}]="${author}|${script}|${remote_version}"
zrep_msg info "\n${author}/${script} 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
@ -531,8 +603,6 @@ function zrep_update_package() {
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}"
# echo "Updating $specificPackage 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}
else else
@ -541,8 +611,8 @@ function zrep_update_package() {
else else
if [[ ${updates} == "true" ]]; then if [[ ${updates} == "true" ]]; then
# Prompt the user only if updates are available # Prompt the user only if updates are available
echo "New updates are available. Do you want to proceed with updating? (Y/n): " zrep_msg sub "New updates are available. Do you want to proceed with updating? (Y/n): \c"
read -q "response?" read "response"
echo # Move to a new line echo # Move to a new line
# Set the default response to 'Y' if no input is entered # Set the default response to 'Y' if no input is entered
@ -550,17 +620,8 @@ function zrep_update_package() {
# Proceed with update if the response is 'Y' or 'y' # Proceed with update if the response is 'Y' or 'y'
if [[ $response =~ ^[Yy]$ ]]; then if [[ $response =~ ^[Yy]$ ]]; then
# General update mode: update all packages listed in updatesAvailable # Call zrep_process_updates to handle all updates.
for package in ${(k)updatesAvailable}; do zrep_process_updates
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
else else
zrep_msg info "Update canceled." zrep_msg info "Update canceled."
fi fi
@ -646,6 +707,11 @@ function zrep_install_package() {
else else
updates=false updates=false
local package="${1}" local package="${1}"
# Ensure package name is in 'author/package' format
if [[ ! "${package}" =~ ^.+/[^/]+$ ]]; then
zrep_msg debug "\nError: Package name must be in 'author/package' format."
return 1
fi
# 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 debug "\nPackage ${package} is already installed." zrep_msg debug "\nPackage ${package} is already installed."
@ -655,13 +721,15 @@ function zrep_install_package() {
fi fi
# If not installed, proceed with fetching the package information # 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" local tmpDir="${config[main_zrep_install_dir]}/tmp"
mkdir -p "${tmpDir}" mkdir -p "${tmpDir}"
local zipFile="${tmpDir}/${author}-${script}-${version}.zip" local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
dlurl="${config[global_repo_url]}/download/${package}/${version}"
zrep_download_package "${zipFile}" "${dlurl}" zrep_download_package "${zipFile}" "${dlurl}"
unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}" unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}"
@ -799,9 +867,10 @@ function zrep_help() {
zrep_msg info " update (u) <author/package>:\t\t\tUpdate zrep package" zrep_msg info " update (u) <author/package>:\t\t\tUpdate zrep package"
zrep_msg info " enable <author/package>:\t\t\tEnable zrep package" zrep_msg info " enable <author/package>:\t\t\tEnable zrep package"
zrep_msg info " disable <author/package>:\t\t\tDisable zrep package" zrep_msg info " disable <author/package>:\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 " version:\t\t\t\t\tDisplay zrep version"
zrep_msg info " list:\t\t\t\t\t\tList installed packages" zrep_msg info " list:\t\t\t\t\t\tList installed packages"
zrep_msg info " <author/package> help:\t\t\tDisplay help for pacakage (if available)" zrep_msg info " <author/package> help:\t\t\tDisplay help for package (if available)"
} }
@ -854,6 +923,7 @@ function zrep_read_usage() {
function main() { function main() {
zrep_main_version_string zrep_main_version_string
if [[ ${1} != "init" ]]; then if [[ ${1} != "init" ]]; then
zrep_load_config ${1} zrep_load_config ${1}
zrep_load_theme ${config[global_theme]} zrep_load_theme ${config[global_theme]}
@ -872,6 +942,9 @@ function main() {
zrep_check_for_deps zrep_check_for_deps
exit exit
;; ;;
search | s | find)
zrep_search "${@:2}"
;;
install | i) install | i)
zrep_install_package ${2} zrep_install_package ${2}
;; ;;