2024-02-27 18:39:09 +01:00
|
|
|
#!/usr/bin/zsh
|
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
setopt extendedglob
|
|
|
|
|
2024-02-27 18:39:09 +01:00
|
|
|
VERSION="0.0.1" # Sat-2024-02-24
|
|
|
|
ZREP="Zsh Repository Tool"
|
2024-02-28 23:41:09 +01:00
|
|
|
# Define the path to .zreprc
|
|
|
|
ZREP_CONFIG="$HOME/.zreprc"
|
|
|
|
|
|
|
|
# echo "START: $fpath"
|
2024-02-27 16:50:07 +01:00
|
|
|
|
|
|
|
function zrep_fpath() {
|
|
|
|
local base_dir="$1"
|
|
|
|
|
|
|
|
# Check if the base directory exists
|
|
|
|
if [[ ! -d "$base_dir" ]]; then
|
|
|
|
echo "Error: Base directory '$base_dir' does not exist."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add directories containing at least one file to fpath
|
|
|
|
for dir in $base_dir/**/*(/N); do
|
2024-02-28 23:41:09 +01:00
|
|
|
if [[ -n $(ls -A "$dir/") ]]; then
|
2024-02-27 16:50:07 +01:00
|
|
|
fpath=($dir $fpath)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_fpath ${HOME}/.zrep/functions
|
2024-02-27 16:50:07 +01:00
|
|
|
autoload -Uz zini
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# List of colors available
|
|
|
|
black="\033[0;30m"
|
|
|
|
red="\033[0;31m"
|
|
|
|
green="\033[0;32m"
|
|
|
|
yellow="\033[0;33m"
|
|
|
|
blue="\033[0;34m"
|
|
|
|
magenta="\033[0;35m"
|
|
|
|
cyan="\033[0;36m"
|
|
|
|
white="\033[0;37m"
|
|
|
|
bold_black="\033[1;30m"
|
|
|
|
bold_red="\033[1;31m"
|
|
|
|
bold_green="\033[1;32m"
|
|
|
|
bold_yellow="\033[1;33m"
|
|
|
|
bold_blue="\033[1;34m"
|
|
|
|
bold_magenta="\033[1;35m"
|
|
|
|
bold_cyan="\033[1;36m"
|
|
|
|
bold_white="\033[1;37m"
|
|
|
|
black_bg="\033[40m"
|
|
|
|
red_bg="\033[41m"
|
|
|
|
green_bg="\033[42m"
|
|
|
|
yellow_bg="\033[43m"
|
|
|
|
blue_bg="\033[44m"
|
|
|
|
magenta_bg="\033[45m"
|
|
|
|
cyan_bg="\033[46m"
|
|
|
|
white_bg="\033[47m"
|
|
|
|
end="\033[0m"
|
|
|
|
|
|
|
|
|
|
|
|
function zrep_version() {
|
|
|
|
zrep_msg info "Created by kekePower - 2024"
|
|
|
|
zrep_msg info "- https://git.kekepower.com/kekePower/zrep/${end}"
|
|
|
|
zrep_msg info "Please see '${ZSH_SCRIPT:t} help' for more info"
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_msg() {
|
|
|
|
|
|
|
|
# Determine the color based on the type
|
|
|
|
# local color="${end}" # Default to no color if type is unrecognized
|
|
|
|
case ${1} in
|
|
|
|
std) color="${green}" ;;
|
|
|
|
info) color="${yellow}" ;;
|
|
|
|
debug) color="${red}" ;;
|
|
|
|
other) color="${bold_yellow}" ;;
|
|
|
|
sub) color="${magenta}" ;;
|
|
|
|
main) color="${white} ${green_bg}" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# echo "1=$1\n2=$2\ncolor=$color"
|
|
|
|
printf "${color}%b${end}\n" "${2}"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_init() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
local config_file="$HOME/.zreprc"
|
|
|
|
local zshrc_file="$HOME/.zshrc"
|
|
|
|
local install_dir
|
|
|
|
|
|
|
|
# Check if .zreprc exists
|
|
|
|
if [[ ! -f $config_file ]]; then
|
|
|
|
echo "$config_file not found. Creating it..."
|
|
|
|
# Prompt user for install directory
|
|
|
|
read "?Enter zrep installation directory [$HOME/.zrep]: " install_dir
|
|
|
|
install_dir=${install_dir:-$HOME/.zrep}
|
|
|
|
|
|
|
|
# Write to .zreprc
|
|
|
|
echo "[main]" > $config_file
|
|
|
|
echo "zrep_install_dir = $install_dir" >> $config_file
|
|
|
|
else
|
|
|
|
echo "Running zini $config_file"
|
|
|
|
zini $config_file
|
|
|
|
echo "Setting install_dir"
|
|
|
|
install_dir=${config[zrep_install_dir]}
|
|
|
|
echo "install_dir=${install_dir}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Ensure zrep_install_dir exists
|
|
|
|
mkdir -p "$install_dir"
|
|
|
|
|
|
|
|
# Update or add fpath in .zshrc, ensuring no duplicate or empty entries
|
|
|
|
if ! grep -q "fpath=(.*$install_dir)" "$zshrc_file"; then
|
|
|
|
echo "Adding zrep installation directory to fpath in .zshrc..."
|
|
|
|
echo "fpath=('$install_dir' \$fpath)" >> "$zshrc_file"
|
|
|
|
if ! grep -q "^export fpath" "$zshrc_file"; then
|
|
|
|
echo "export fpath" >> "$zshrc_file"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "zrep installation directory ($install_dir) is already included in fpath."
|
|
|
|
fi
|
|
|
|
|
|
|
|
source ${zshrc_file}
|
|
|
|
echo "zrep initialization complete."
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to parse remote JSON data and extract author, script, and version
|
|
|
|
function zrep_parse_remote() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
local url="$1"
|
|
|
|
local json_data
|
|
|
|
|
|
|
|
# Fetch JSON data from the URL
|
|
|
|
json_data=$(curl -s "$url")
|
|
|
|
|
|
|
|
# Extract author, script, and version using jq
|
|
|
|
author=$(echo "$json_data" | jq -r '.authors[0].name')
|
|
|
|
script=$(echo "$json_data" | jq -r '.authors[0].scripts[0].name')
|
|
|
|
version=$(echo "$json_data" | jq -r '.authors[0].scripts[0].version')
|
|
|
|
|
|
|
|
# Set the variables as global
|
|
|
|
export author
|
|
|
|
export script
|
|
|
|
export version
|
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}"
|
|
|
|
echo "zrep_parse_remote: ${dlurl}"
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to write to installed.json after successful install
|
|
|
|
function zrep_update_installed_json() {
|
|
|
|
local author="$1"
|
|
|
|
local script="$2"
|
|
|
|
local version="$3"
|
|
|
|
local json_file="${config[main_zrep_install_dir]}/installed.json"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# Create JSON object
|
2024-03-04 22:17:21 +01:00
|
|
|
local json_object='{"script":"'$script'", "version":"'$version'"}'
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
# Use jq to update installed.json (nested structure)
|
|
|
|
jq --argjson obj "$json_object" '
|
|
|
|
.[$obj.author] |= .[$obj.author] + [$obj] # Add to authors array
|
|
|
|
| . + {($obj.author): []} # Create author entry if it does not exist
|
|
|
|
' "$json_file" > "$json_file.tmp" && mv "$json_file.tmp" "$json_file"
|
|
|
|
|
|
|
|
echo "Package '$script' by '$author' version $version installed successfully."
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to list installed packages from installed.json
|
|
|
|
function zrep_list_installed_packages() {
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
2024-03-04 20:20:33 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# Check if installed.json exists
|
|
|
|
if [[ ! -f "$installed_json" ]]; then
|
|
|
|
echo "No installed packages found."
|
|
|
|
return 0
|
|
|
|
fi
|
2024-03-04 20:20:33 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# Parse installed.json and list packages
|
2024-03-04 20:20:33 +01:00
|
|
|
zrep_msg sub "\nInstalled packages:"
|
|
|
|
|
|
|
|
# Iterate through each package (read lines from jq output)
|
|
|
|
while IFS= read -r package_info; do
|
|
|
|
local package_name=$(echo "$package_info" | awk '{print $1}') # Extract package name
|
|
|
|
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)
|
|
|
|
if grep -q "$package_name" ~/.zrep_addons; then
|
|
|
|
is_active="${white}(${end}${bold_green}Active${end}${white})${end}"
|
|
|
|
# echo "DEBUG 2: is_active=${is_active}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# echo "$package_info $is_active"
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-03-04 20:20:33 +01:00
|
|
|
zrep_msg other " - $package_info $is_active"
|
|
|
|
done < <(jq -r '.[] | "\(.author)/\(.script) (\(.version))"' "$installed_json")
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
2024-02-27 16:50:07 +01:00
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
function zrep_list_package() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
local package_names=""
|
|
|
|
|
|
|
|
# Check if installed.json exists
|
|
|
|
if [[ ! -f "$installed_json" ]]; then
|
|
|
|
echo "No installed packages found."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Parse installed.json and concatenate package names
|
|
|
|
while IFS= read -r package_info; do
|
|
|
|
local author=$(jq -r '.author' <<< "$package_info")
|
|
|
|
local script=$(jq -r '.script' <<< "$package_info")
|
|
|
|
local version=$(jq -r '.version' <<< "$package_info")
|
|
|
|
package_names+="$author/$script ($version) "
|
|
|
|
done < <(jq -c '.[]' "$installed_json")
|
|
|
|
|
|
|
|
package_name="$author/$script (${version})"
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
# Function to load configuration
|
2024-03-04 22:17:21 +01:00
|
|
|
# shellcheck disable=SC2120
|
2024-02-27 16:50:07 +01:00
|
|
|
function load_config() {
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
# Check if jq is available
|
2024-02-29 16:53:22 +01:00
|
|
|
if ! command -v jq &> /dev/null; then
|
|
|
|
echo "Error: 'jq' is not installed. Please install jq to continue."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
if [[ -f "$ZREP_CONFIG" ]]; then
|
|
|
|
zini "$ZREP_CONFIG"
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_fpath ${config[main_zrep_install_dir]}
|
2024-02-27 16:50:07 +01:00
|
|
|
else
|
|
|
|
if [[ "$1" == "init" ]]; then
|
|
|
|
echo "$ZREP_CONFIG not found. Proceeding with 'zrep init'..."
|
|
|
|
zrep_init
|
|
|
|
else
|
|
|
|
echo "$ZREP_CONFIG not found."
|
|
|
|
# Ask the user if they want to run 'zrep init'
|
|
|
|
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
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
}
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
function zrep_remove_package() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
local package_name="$1"
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
|
|
|
|
# Check if installed.json exists
|
|
|
|
if [[ ! -f "$installed_json" ]]; then
|
|
|
|
echo "Error: installed.json not found."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get package information from installed.json
|
|
|
|
local package_info
|
|
|
|
package_info=$(zrep_parse_installed_json | jq ".[] | select(.author + \"/\" + .script == \"$package_name\")")
|
|
|
|
|
|
|
|
# Check if the package is installed
|
|
|
|
if [[ -z "$package_info" ]]; then
|
|
|
|
echo "Error: Package '$package_name' is not installed."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
# Extract author and script from package_name
|
|
|
|
local author script
|
|
|
|
author=$(echo "$package_name" | cut -d '/' -f1)
|
|
|
|
script=$(echo "$package_name" | cut -d '/' -f2)
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# Display package information
|
|
|
|
echo "Package information:"
|
|
|
|
echo "$package_info" | jq .
|
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
# Ask user for confirmation with default response "Y"
|
|
|
|
read -q -r "REPLY?Are you sure you want to remove this package? (y/n) " && REPLY="${REPLY:-Y}"
|
2024-02-28 23:41:09 +01:00
|
|
|
echo
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
|
2024-02-29 16:53:22 +01:00
|
|
|
|
|
|
|
# Extract first letter of package_name
|
|
|
|
local first_letter=${package_name:0:1}
|
|
|
|
|
|
|
|
# Remove the package directory from disk
|
|
|
|
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${package_name}"
|
|
|
|
if [[ -d "$package_dir" ]]; then
|
|
|
|
rm -rf "$package_dir"
|
|
|
|
echo "Package directory '$package_dir' removed successfully."
|
|
|
|
|
|
|
|
# Check if the author directory is empty and delete it if so
|
|
|
|
local author_dir="${config[main_zrep_install_dir]}/${first_letter}/$author"
|
2024-03-04 20:20:33 +01:00
|
|
|
if [[ -d "$author_dir" && ! -f "$author_dir/*" ]]; then
|
2024-02-29 16:53:22 +01:00
|
|
|
rm -rf "$author_dir"
|
|
|
|
echo "Author directory '$author_dir' removed successfully."
|
|
|
|
|
|
|
|
# Check if the first letter directory is empty and delete it if so
|
2024-03-04 22:17:21 +01:00
|
|
|
if [[ -d "${config[main_zrep_install_dir]}/$first_letter" && -f "${config[main_zrep_install_dir]}/$first_letter/.[!.]*" ]]; then
|
2024-02-29 16:53:22 +01:00
|
|
|
rm -rf "${config[main_zrep_install_dir]}/$first_letter"
|
|
|
|
echo "First letter directory '${config[main_zrep_install_dir]}/$first_letter' removed successfully."
|
|
|
|
fi
|
|
|
|
fi
|
2024-03-04 20:20:33 +01:00
|
|
|
# Remove the package from installed.json (if everything else was successful)
|
|
|
|
jq "map(select(.author + \"/\" + .script != \"$package_name\"))" "$installed_json" > "$installed_json.tmp"
|
|
|
|
mv "$installed_json.tmp" "$installed_json"
|
|
|
|
echo "Package '$package_name' removed successfully."
|
2024-02-29 16:53:22 +01:00
|
|
|
else
|
|
|
|
echo "Warning: Package directory '$package_dir' not found."
|
|
|
|
fi
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
echo "Package '$package_name' removed successfully."
|
|
|
|
else
|
|
|
|
echo "Removal canceled."
|
|
|
|
fi
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:20:33 +01:00
|
|
|
# Function to install a package by unzipping it to ${config[main_zrep_install_dir]}
|
|
|
|
function zrep_install_package() {
|
|
|
|
|
|
|
|
zrep_parse_remote "https://kekepower.com/zrep/packages.json"
|
|
|
|
|
|
|
|
# Construct the download URL
|
|
|
|
local dlurl="https://kekepower.com/zrep/download.php?a=${author}&s=${script}&v=${version}"
|
|
|
|
echo "${dlurl}"
|
|
|
|
# exit
|
|
|
|
|
|
|
|
# Get the base directory where the package will be installed
|
|
|
|
local baseDir="${config[main_zrep_install_dir]}/"
|
|
|
|
|
|
|
|
# Create the directory if it doesn't exist
|
|
|
|
mkdir -p "$baseDir"
|
|
|
|
|
|
|
|
# Download the package zip file
|
|
|
|
local zipFile="/tmp/${author}_${package}_${version}.zip"
|
|
|
|
curl -s -o "$zipFile" "$dlurl"
|
|
|
|
|
|
|
|
# Check if the download was successful
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "Error: Failed to download the package."
|
|
|
|
return 1
|
|
|
|
fi
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-03-04 20:20:33 +01:00
|
|
|
# Unzip the package to the installation directory
|
|
|
|
unzip -q "$zipFile" -d "$baseDir"
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-03-04 20:20:33 +01:00
|
|
|
# Check if the unzip operation was successful
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "Error: Failed to unzip the package."
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
zrep_update_installed_json "$author" "$script" "$version"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Clean up: Remove the downloaded zip file
|
|
|
|
rm "$zipFile"
|
|
|
|
|
|
|
|
}
|
2024-02-28 23:41:09 +01:00
|
|
|
# Function to parse installed.json
|
|
|
|
function zrep_parse_installed_json() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
jq -c '.' "$installed_json"
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
function zrep_enable() {
|
|
|
|
local package_name="$1"
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
local author="${package_name%/*}"
|
|
|
|
local script="${package_name#*/}"
|
|
|
|
local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]')
|
|
|
|
local addon_path="${config[main_zrep_install_dir]}/$first_letter/$author/$script"
|
|
|
|
|
|
|
|
# Check if the package is installed
|
|
|
|
if [[ ! -f "$installed_json" ]] || ! jq -e ".[] | select(.author == \"$author\" and .script == \"$script\")" "$installed_json" &>/dev/null; then
|
|
|
|
echo "Error: Package '$package_name' is not installed."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Load existing addons from .zrep_addons
|
|
|
|
source ~/.zrep_addons
|
|
|
|
|
|
|
|
# Check if the addon is already enabled
|
|
|
|
local addon_exists=0
|
|
|
|
for addon in "${addons[@]}"; do
|
|
|
|
if [[ "$addon" == "$addon_path" ]]; then
|
|
|
|
addon_exists=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if ((addon_exists)); then
|
|
|
|
echo "Package '$package_name' is already enabled."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add addon path to the array
|
|
|
|
addons+=("$addon_path")
|
|
|
|
|
|
|
|
# Update .zrep_addons file
|
|
|
|
{
|
|
|
|
echo "addons=("
|
|
|
|
for addon in "${addons[@]}"; do
|
|
|
|
echo " '$addon'"
|
|
|
|
done
|
|
|
|
echo ")"
|
|
|
|
echo
|
|
|
|
echo 'if [[ -n ${addons[@]} ]]; then'
|
|
|
|
echo ' local addon_paths'
|
|
|
|
echo ' for addon in "${addons[@]}"; do'
|
|
|
|
echo ' addon_paths+="$addon "'
|
|
|
|
echo ' done'
|
|
|
|
echo ' fpath=($addon_paths $fpath)'
|
|
|
|
echo ' export fpath'
|
|
|
|
echo ' # echo "fpath updated with enabled addons."'
|
|
|
|
echo 'else'
|
|
|
|
echo ' echo "zrep: No addons enabled."'
|
|
|
|
echo 'fi'
|
|
|
|
} > ${HOME}/.zrep_addons
|
|
|
|
|
|
|
|
source ${HOME}/.zshrc
|
|
|
|
|
|
|
|
echo "Package '$package_name' has been enabled and added to fpath."
|
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_disable() {
|
|
|
|
local package_name="$1"
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
local author="${package_name%/*}"
|
|
|
|
local script="${package_name#*/}"
|
|
|
|
local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]')
|
|
|
|
local addon_path="${config[main_zrep_install_dir]}/$first_letter/$author/$script"
|
|
|
|
|
|
|
|
# Load existing addons from .zrep_addons
|
|
|
|
source ${HOME}/.zrep_addons
|
|
|
|
|
|
|
|
# Initialize a new array for addons
|
|
|
|
local new_addons=()
|
|
|
|
|
|
|
|
# Flag to check if addon was found and removed
|
|
|
|
local found=0
|
|
|
|
|
|
|
|
# Iterate through existing addons
|
|
|
|
for addon in "${addons[@]}"; do
|
|
|
|
if [[ "$addon" == "$addon_path" ]]; then
|
|
|
|
found=1
|
|
|
|
else
|
|
|
|
new_addons+=("$addon")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if ((found == 0)); then
|
|
|
|
echo "Package '$package_name' is not currently enabled."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Update the addons array
|
|
|
|
addons=("${new_addons[@]}")
|
|
|
|
|
|
|
|
# Update .zrep_addons file with the new array
|
|
|
|
{
|
|
|
|
echo "addons=("
|
|
|
|
for addon in "${addons[@]}"; do
|
|
|
|
echo " '$addon'"
|
|
|
|
done
|
|
|
|
echo ")"
|
|
|
|
echo
|
|
|
|
echo 'if [[ -n ${addons[@]} ]]; then'
|
|
|
|
echo ' local addon_paths'
|
|
|
|
echo ' for addon in "${addons[@]}"; do'
|
|
|
|
echo ' addon_paths+="$addon "'
|
|
|
|
echo ' done'
|
|
|
|
echo ' fpath=($addon_paths $fpath)'
|
|
|
|
echo ' export fpath'
|
|
|
|
echo ' # echo "fpath updated with enabled addons."'
|
|
|
|
echo 'else'
|
|
|
|
echo ' echo "zrep: No addons enabled."'
|
|
|
|
echo 'fi'
|
|
|
|
} > ${HOME}/.zrep_addons
|
|
|
|
|
|
|
|
source ${HOME}/.zshrc
|
|
|
|
|
|
|
|
# Remove the addon path from $fpath
|
|
|
|
fpath=(${fpath[@]/#$addon_path})
|
|
|
|
export fpath
|
|
|
|
|
|
|
|
echo "Package '$package_name' has been disabled and removed from fpath."
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
# Help function to display available options
|
|
|
|
function zrep_help() {
|
2024-03-01 12:22:39 +01:00
|
|
|
|
|
|
|
zrep_msg sub "Usage: zrep <command> [arguments]"
|
|
|
|
zrep_msg other "Available commands:"
|
|
|
|
zrep_msg other " init: Initialize zrep"
|
2024-03-04 22:17:21 +01:00
|
|
|
zrep_msg other " install (i) <author/package>: Install a package"
|
|
|
|
zrep_msg other " remove (rm, delete) <author/package>: Remove a package"
|
|
|
|
zrep_msg other " update (u) <author/package>: Update zrep package"
|
2024-03-01 12:22:39 +01:00
|
|
|
zrep_msg other " version: Display zrep version"
|
|
|
|
zrep_msg other " list: List installed packages"
|
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
|
2024-03-04 22:17:21 +01:00
|
|
|
echo "${magenta}${blue_bg} ${ZREP} ${end}${bold_white}${blue_bg}${VERSION} ${end}"
|
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
load_config
|
|
|
|
|
|
|
|
# Example command handling structure
|
2024-02-29 16:53:22 +01:00
|
|
|
case "${1}" in
|
2024-02-27 16:50:07 +01:00
|
|
|
init)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_init
|
|
|
|
zrep_fpath ${config[main_zrep_install_dir]}
|
|
|
|
exit
|
2024-02-27 16:50:07 +01:00
|
|
|
;;
|
2024-02-29 16:53:22 +01:00
|
|
|
install | i)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_msg info "Install function here"
|
|
|
|
zrep_install_package ${1}
|
|
|
|
;;
|
2024-02-29 16:53:22 +01:00
|
|
|
remove | delete | rm)
|
2024-02-28 23:41:09 +01:00
|
|
|
# Parse the command argument to extract the package name
|
2024-02-29 16:53:22 +01:00
|
|
|
zrep_remove_package_name="${2:-}"
|
2024-02-28 23:41:09 +01:00
|
|
|
if [[ -z "$zrep_remove_package_name" ]]; then
|
|
|
|
echo "Usage: zrep remove package_name"
|
|
|
|
else
|
|
|
|
zrep_remove_package "$zrep_remove_package_name"
|
|
|
|
fi
|
2024-02-27 16:50:07 +01:00
|
|
|
;;
|
2024-02-29 16:53:22 +01:00
|
|
|
update | u)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_msg info "Update function here"
|
|
|
|
;;
|
2024-02-29 16:53:22 +01:00
|
|
|
version | -v | --version)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_version
|
|
|
|
;;
|
|
|
|
list)
|
|
|
|
zrep_list_installed_packages
|
|
|
|
;;
|
2024-02-29 16:53:22 +01:00
|
|
|
help | -h | --help)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_help
|
2024-02-27 16:50:07 +01:00
|
|
|
;;
|
2024-03-04 22:17:21 +01:00
|
|
|
enable)
|
|
|
|
zrep_enable ${2}
|
|
|
|
;;
|
|
|
|
disable)
|
|
|
|
zrep_disable ${2}
|
|
|
|
;;
|
2024-02-27 16:50:07 +01:00
|
|
|
*)
|
2024-02-28 23:41:09 +01:00
|
|
|
zrep_help
|
2024-02-27 16:50:07 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# Call main with all passed arguments
|
|
|
|
main "$@"
|