Update zrep script:

- Commented out unused variables and echo statements.
- Added check to create installed.json if not exists.
- Improved jq command to update installed.json.
- Refactored zrep_list_installed_packages function.
- Refactored zrep_list_package function to concatenate package names.
- Updated zrep_install_package to use a temporary directory.
- Refactored zrep_enable and zrep_disable functions.
- Added zrep_parse_package_name function for package name parsing. - Mon, 04 Mar 2024 23:40:02 +0100
This commit is contained in:
Stig-Ørjan Smelror 2024-03-04 23:40:02 +01:00
parent d1f2b04d5b
commit e3ee9e49a9

135
zrep
View File

@ -147,8 +147,8 @@ function zrep_parse_remote() {
export script export script
export version export version
local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}" # local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}"
echo "zrep_parse_remote: ${dlurl}" # echo "zrep_parse_remote: ${dlurl}"
} }
@ -159,14 +159,18 @@ function zrep_update_installed_json() {
local version="$3" local version="$3"
local json_file="${config[main_zrep_install_dir]}/installed.json" local json_file="${config[main_zrep_install_dir]}/installed.json"
# Create JSON object # Check if the JSON file exists and create it if not
local json_object='{"script":"'$script'", "version":"'$version'"}' if [[ ! -f "$json_file" ]]; then
echo "{}" > "$json_file" # Initialize with an empty object
fi
# Use jq to update installed.json (nested structure) # Proper jq command to update the JSON
jq --argjson obj "$json_object" ' jq --arg author "$author" --arg script "$script" --arg version "$version" \
.[$obj.author] |= .[$obj.author] + [$obj] # Add to authors array 'if .[$author] then
| . + {($obj.author): []} # Create author entry if it does not exist .[$author] += [{"script": $script, "version": $version}]
' "$json_file" > "$json_file.tmp" && mv "$json_file.tmp" "$json_file" else
.[$author] = [{"script": $script, "version": $version}]
end' "$json_file" > "$json_file.tmp" && mv "$json_file.tmp" "$json_file"
echo "Package '$script' by '$author' version $version installed successfully." echo "Package '$script' by '$author' version $version installed successfully."
} }
@ -184,26 +188,21 @@ function zrep_list_installed_packages() {
# Parse installed.json and list packages # Parse installed.json and list packages
zrep_msg sub "\nInstalled packages:" zrep_msg sub "\nInstalled packages:"
# Iterate through each package (read lines from jq output) # Iterate through each author and their packages
while IFS= read -r package_info; do jq -r 'to_entries | .[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "$installed_json" | while IFS= read -r package_info; do
local package_name=$(echo "$package_info" | awk '{print $1}') # Extract package name local package_name=$(echo "$package_info" | cut -d ' ' -f1) # Extract package name before the version
local is_active="${white}(${end}${red}Inactive${end}${white})${end}" # Set default to Inactive local is_active="${white}(${end}${red}Inactive${end}${white})${end}" # Set default to Inactive
# echo "DEBUG 1: is_active=${is_active}"
# Check if the package is active (only modify if active) # Check if the package is active (only modify if active)
if grep -q "$package_name" ~/.zrep_addons; then if grep -q "$package_name" ~/.zrep_addons; then
is_active="${white}(${end}${bold_green}Active${end}${white})${end}" is_active="${white}(${end}${bold_green}Active${end}${white})${end}"
# echo "DEBUG 2: is_active=${is_active}"
fi fi
# echo "$package_info $is_active"
zrep_msg other " - $package_info $is_active" zrep_msg other " - $package_info $is_active"
done < <(jq -r '.[] | "\(.author)/\(.script) (\(.version))"' "$installed_json") done
} }
function zrep_list_package() { function zrep_list_package() {
local installed_json="${config[main_zrep_install_dir]}/installed.json" local installed_json="${config[main_zrep_install_dir]}/installed.json"
local package_names="" local package_names=""
@ -214,15 +213,16 @@ function zrep_list_package() {
fi fi
# Parse installed.json and concatenate package names # Parse installed.json and concatenate package names
while IFS= read -r package_info; do jq -r 'to_entries[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "$installed_json" | while IFS= read -r package_info; do
local author=$(jq -r '.author' <<< "$package_info") package_names+="$package_info "
local script=$(jq -r '.script' <<< "$package_info") done
local version=$(jq -r '.version' <<< "$package_info")
package_names+="$author/$script ($version) "
done < <(jq -c '.[]' "$installed_json")
package_name="$author/$script (${version})"
# Assuming you want to print out the concatenated package names
if [[ -n "$package_names" ]]; then
echo "Installed packages: $package_names"
else
echo "No packages found."
fi
} }
# Function to load configuration # Function to load configuration
@ -326,17 +326,18 @@ function zrep_install_package() {
# Construct the download URL # Construct the download URL
local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}" local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}"
echo "${dlurl}" # echo "${dlurl}"
# exit # exit
# Get the base directory where the package will be installed # Get the base directory where the package will be installed
local baseDir="${config[main_zrep_install_dir]}/" local baseDir="${config[main_zrep_install_dir]}/"
local tmpDir="${baseDir}/tmp"
# Create the directory if it doesn't exist # Create the directory if it doesn't exist
mkdir -p "$baseDir" mkdir -p "$tmpDir"
# Download the package zip file # Download the package zip file
local zipFile="/tmp/${author}_${package}_${version}.zip" local zipFile="${tmpDir}/${author}_${package}_${version}.zip"
curl -s -o "$zipFile" "$dlurl" curl -s -o "$zipFile" "$dlurl"
# Check if the download was successful # Check if the download was successful
@ -360,6 +361,7 @@ function zrep_install_package() {
rm "$zipFile" rm "$zipFile"
} }
# Function to parse installed.json # Function to parse installed.json
function zrep_parse_installed_json() { function zrep_parse_installed_json() {
@ -368,19 +370,28 @@ function zrep_parse_installed_json() {
} }
function zrep_enable() { function zrep_parse_package_name() {
local package_name="$1" echo "Looking for package ${1}"
local installed_json="${config[main_zrep_install_dir]}/installed.json" package_name="$1"
local author="${package_name%/*}" installed_json="${config[main_zrep_install_dir]}/installed.json"
local script="${package_name#*/}" author="${package_name%/*}"
local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]') script="${package_name#*/}"
local addon_path="${config[main_zrep_install_dir]}/$first_letter/$author/$script" first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]')
addon_path="${config[main_zrep_install_dir]}/$first_letter/$author/$script"
# Check if the package is installed # Check if the package is installed
if [[ ! -f "$installed_json" ]] || ! jq -e ".[] | select(.author == \"$author\" and .script == \"$script\")" "$installed_json" &>/dev/null; then if [[ ! -f "$installed_json" ]] || ! jq -e --arg author "$author" --arg script "$script" '.[$author] | any(.script == $script)' "$installed_json" &>/dev/null; then
echo "Error: Package '$package_name' is not installed." echo "Error: Package '$package_name' is not installed."
return 1 return 1
fi fi
}
function zrep_enable() {
local package_name="$1"
zrep_parse_package_name "$package_name"
# Assuming zrep_parse_package_name sets 'addon_path' correctly
# and 'package_name' is in 'author/script' format
# Load existing addons from .zrep_addons # Load existing addons from .zrep_addons
source ~/.zrep_addons source ~/.zrep_addons
@ -402,7 +413,7 @@ function zrep_enable() {
# Add addon path to the array # Add addon path to the array
addons+=("$addon_path") addons+=("$addon_path")
# Update .zrep_addons file # Reconstruct .zrep_addons file with the updated addons array
{ {
echo "addons=(" echo "addons=("
for addon in "${addons[@]}"; do for addon in "${addons[@]}"; do
@ -411,30 +422,33 @@ function zrep_enable() {
echo ")" echo ")"
echo echo
echo 'if [[ -n ${addons[@]} ]]; then' echo 'if [[ -n ${addons[@]} ]]; then'
echo ' local addon_paths' echo ' local unique_addons=()'
echo ' for addon in "${addons[@]}"; do' echo ' for addon in "${addons[@]}"; do'
echo ' addon_paths+="$addon "' echo ' if [[ ! " ${fpath[@]} " =~ " ${addon} " ]]; then'
echo ' unique_addons+=("$addon")'
echo ' fi'
echo ' done' echo ' done'
echo ' fpath=($addon_paths $fpath)' echo ' if [[ ${#unique_addons[@]} -gt 0 ]]; then'
echo ' fpath=("${unique_addons[@]}" $fpath)'
echo ' fi'
echo ' export fpath' echo ' export fpath'
echo ' # echo "fpath updated with enabled addons."'
echo 'else' echo 'else'
echo ' echo "zrep: No addons enabled."' echo ' echo "zrep: No addons enabled."'
echo 'fi' echo 'fi'
} > ${HOME}/.zrep_addons } > ${HOME}/.zrep_addons
source ${HOME}/.zshrc # Source the updated .zrep_addons to apply changes
source ${HOME}/.zrep_addons
echo "Package '$package_name' has been enabled and added to fpath." echo "Package '$package_name' has been enabled and added to fpath."
} }
function zrep_disable() { function zrep_disable() {
local package_name="$1" local package_name="$1"
local installed_json="${config[main_zrep_install_dir]}/installed.json" zrep_parse_package_name "$package_name"
local author="${package_name%/*}"
local script="${package_name#*/}" # Assuming zrep_parse_package_name sets 'addon_path' correctly
local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]') # and 'package_name' is in 'author/script' format
local addon_path="${config[main_zrep_install_dir]}/$first_letter/$author/$script"
# Load existing addons from .zrep_addons # Load existing addons from .zrep_addons
source ${HOME}/.zrep_addons source ${HOME}/.zrep_addons
@ -459,35 +473,32 @@ function zrep_disable() {
return 0 return 0
fi fi
# Update the addons array # Reconstruct .zrep_addons file with the new addons array
addons=("${new_addons[@]}")
# Update .zrep_addons file with the new array
{ {
echo "addons=(" echo "addons=("
for addon in "${addons[@]}"; do for addon in "${new_addons[@]}"; do
echo " '$addon'" echo " '$addon'"
done done
echo ")" echo ")"
echo echo
echo 'if [[ -n ${addons[@]} ]]; then' echo 'if [[ -n ${addons[@]} ]]; then'
echo ' local addon_paths' echo ' local unique_addons=()'
echo ' for addon in "${addons[@]}"; do' echo ' for addon in "${addons[@]}"; do'
echo ' addon_paths+="$addon "' echo ' if [[ ! " ${fpath[@]} " =~ " ${addon} " ]]; then'
echo ' unique_addons+=("$addon")'
echo ' fi'
echo ' done' echo ' done'
echo ' fpath=($addon_paths $fpath)' echo ' if [[ ${#unique_addons[@]} -gt 0 ]]; then'
echo ' fpath=("${unique_addons[@]}" $fpath)'
echo ' fi'
echo ' export fpath' echo ' export fpath'
echo ' # echo "fpath updated with enabled addons."'
echo 'else' echo 'else'
echo ' echo "zrep: No addons enabled."' echo ' echo "zrep: No addons enabled."'
echo 'fi' echo 'fi'
} > ${HOME}/.zrep_addons } > ${HOME}/.zrep_addons
source ${HOME}/.zshrc # Source the updated .zrep_addons to apply changes
source ${HOME}/.zrep_addons
# Remove the addon path from $fpath
fpath=(${fpath[@]/#$addon_path})
export fpath
echo "Package '$package_name' has been disabled and removed from fpath." echo "Package '$package_name' has been disabled and removed from fpath."
} }