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
|
2024-03-10 13:31:19 +01:00
|
|
|
ZREP_CONFIG="${HOME}/.zreprc"
|
2024-02-27 16:50:07 +01:00
|
|
|
|
|
|
|
function zrep_fpath() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local base_dir="${1}"
|
2024-02-27 16:50:07 +01:00
|
|
|
|
|
|
|
# Check if the base directory exists
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -d "${base_dir}" ]]; then
|
|
|
|
echo "Error: Base directory '${base_dir}' does not exist."
|
2024-02-27 16:50:07 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add directories containing at least one file to fpath
|
2024-03-10 13:31:19 +01:00
|
|
|
for dir in ${base_dir}/**/*(/N); do
|
|
|
|
if [[ -n $(ls -A "${dir}/") ]]; then
|
|
|
|
fpath=(${dir} $fpath)
|
2024-02-27 16:50:07 +01:00
|
|
|
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
|
2024-03-06 18:33:14 +01:00
|
|
|
typeset -A colors
|
|
|
|
colors=(
|
|
|
|
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"
|
|
|
|
)
|
2024-02-28 23:41:09 +01:00
|
|
|
|
2024-03-04 22:36:49 +01:00
|
|
|
function zrep_main_version_string() {
|
2024-03-10 09:49:34 +01:00
|
|
|
echo "${colors[bold_black]}${colors[white_bg]} ${ZREP} ${colors[end]}${colors[bold_white]}${colors[black_bg]} ${VERSION} ${colors[end]}"
|
2024-03-04 22:36:49 +01:00
|
|
|
}
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
function zrep_version() {
|
2024-03-06 18:33:14 +01:00
|
|
|
zrep_msg info "\nCreated by kekePower - 2024"
|
|
|
|
zrep_msg info "License: MIT"
|
|
|
|
zrep_msg info "https://git.kekepower.com/kekePower/zrep/"
|
|
|
|
zrep_msg info "Please see '${colors[green]}${ZSH_SCRIPT:t} help${colors[end]}'${colors[yellow]} for more info"
|
2024-02-28 23:41:09 +01:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_msg() {
|
2024-03-06 18:33:14 +01:00
|
|
|
local color=${colors[end]} # Default to no color if type is unrecognized
|
2024-02-28 23:41:09 +01:00
|
|
|
case ${1} in
|
2024-03-06 18:33:14 +01:00
|
|
|
std) color=${colors[green]} ;;
|
|
|
|
info) color=${colors[yellow]} ;;
|
|
|
|
debug) color=${colors[red]} ;;
|
|
|
|
other) color=${colors[bold_yellow]} ;;
|
|
|
|
sub) color=${colors[magenta]} ;;
|
|
|
|
main) color="${colors[white]} ${colors[green_bg]}" ;;
|
2024-02-28 23:41:09 +01:00
|
|
|
esac
|
2024-03-06 18:33:14 +01:00
|
|
|
printf "${color}%b${colors[end]}\n" "${2}"
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_init() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local zshrc_file="${HOME}/.zshrc"
|
|
|
|
local addons_file="${HOME}/.zrep_addons"
|
2024-02-28 23:41:09 +01:00
|
|
|
local install_dir
|
|
|
|
|
|
|
|
# Check if .zreprc exists
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f ${ZREP_CONFIG} ]]; then
|
|
|
|
echo "${ZREP_CONFIG} not found. Creating it..."
|
2024-02-28 23:41:09 +01:00
|
|
|
# Prompt user for install directory
|
2024-03-10 13:31:19 +01:00
|
|
|
read "install_dir?Enter zrep installation directory [${HOME}/.zrep]: "
|
|
|
|
install_dir=${install_dir:-"${HOME}/.zrep"}
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
# Write to .zreprc
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "[main]" > ${ZREP_CONFIG}
|
|
|
|
echo "zrep_install_dir='${install_dir}'" >> "${ZREP_CONFIG}"
|
2024-02-28 23:41:09 +01:00
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "Loading configuration from ${ZREP_CONFIG}"
|
|
|
|
zini ${ZREP_CONFIG}
|
2024-03-09 00:36:09 +01:00
|
|
|
install_dir=${config[main_zrep_install_dir]}
|
2024-02-28 23:41:09 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Ensure zrep_install_dir exists
|
2024-03-10 13:31:19 +01:00
|
|
|
mkdir -p "${install_dir}/functions/zini"
|
|
|
|
curl -s https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"
|
|
|
|
echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file}
|
2024-02-28 23:41:09 +01:00
|
|
|
|
2024-03-09 00:36:09 +01:00
|
|
|
# Check if .zshrc already sources .zrep_addons, if not, add it
|
2024-03-10 13:31:19 +01:00
|
|
|
if ! grep -q "source ${addons_file}" "${zshrc_file}"; then
|
2024-03-09 00:36:09 +01:00
|
|
|
echo "Adding source command for .zrep_addons to .zshrc..."
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "source ${addons_file}" >> "${zshrc_file}"
|
2024-03-09 00:36:09 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create or update the .zrep_addons file
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f ${addons_file} ]]; then
|
|
|
|
echo "Creating file ${addons_file}..."
|
|
|
|
cat > "${addons_file}" <<EOF
|
2024-03-09 00:36:09 +01:00
|
|
|
# Source the .addons file from the zrep installation directory
|
|
|
|
source "${install_dir}/.addons"
|
|
|
|
|
|
|
|
# If addons array is defined and not empty, add its elements to fpath
|
|
|
|
if [[ -n \${addons[@]} ]]; then
|
|
|
|
for addon in "\${addons[@]}"; do
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -d \${addon} ]] && [[ ! " \${fpath[*]} " =~ " \${addon} " ]]; then
|
|
|
|
fpath=(\${addon} "\${fpath[@]}") # Prepend the new addon to fpath
|
2024-02-28 23:41:09 +01:00
|
|
|
fi
|
2024-03-10 09:49:34 +01:00
|
|
|
autoload -Uz $(basename ${addon})
|
2024-03-10 10:02:08 +01:00
|
|
|
done
|
2024-03-09 00:36:09 +01:00
|
|
|
else
|
|
|
|
echo "zrep: No addons enabled."
|
|
|
|
fi
|
|
|
|
EOF
|
|
|
|
echo "File .zrep_addons created and configured."
|
2024-02-28 23:41:09 +01:00
|
|
|
else
|
2024-03-09 00:36:09 +01:00
|
|
|
echo "File .zrep_addons already exists. Review manually if update is needed."
|
2024-02-28 23:41:09 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "zrep initialization complete."
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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-03-10 13:31:19 +01:00
|
|
|
local url="${1}"
|
2024-02-28 23:41:09 +01:00
|
|
|
local json_data
|
|
|
|
|
|
|
|
# Fetch JSON data from the URL
|
2024-03-10 13:31:19 +01:00
|
|
|
json_data=$(curl -s "${url}")
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
# Extract author, script, and version using jq
|
2024-03-10 13:31:19 +01:00
|
|
|
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')
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
# Set the variables as global
|
|
|
|
export author
|
|
|
|
export script
|
|
|
|
export version
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to write to installed.json after successful install
|
|
|
|
function zrep_update_installed_json() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local author="${1}"
|
|
|
|
local script="${2}"
|
|
|
|
local version="${3}"
|
2024-02-28 23:41:09 +01:00
|
|
|
local json_file="${config[main_zrep_install_dir]}/installed.json"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Check if the JSON file exists and create it if not
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f "${json_file}" ]]; then
|
|
|
|
echo "{}" > "${json_file}" # Initialize with an empty object
|
2024-03-04 23:40:02 +01:00
|
|
|
fi
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Proper jq command to update the JSON
|
2024-03-10 13:31:19 +01:00
|
|
|
jq --arg author "${author}" --arg script "${script}" --arg version "${version}" \
|
|
|
|
'if .[${author}] then
|
|
|
|
.[${author}] += [{"script": ${script}, "version": ${version}}]
|
2024-03-04 23:40:02 +01:00
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
.[${author}] = [{"script": ${script}, "version": ${version}}]
|
|
|
|
end' "${json_file}" > "${json_file}.tmp" && mv "${json_file}.tmp" "${json_file}"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info "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
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f "${installed_json}" ]]; then
|
2024-03-06 18:33:14 +01:00
|
|
|
zrep_msg debug "No installed packages found."
|
2024-02-28 23:41:09 +01:00
|
|
|
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:"
|
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Iterate through each author and their packages
|
2024-03-10 13:31:19 +01:00
|
|
|
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}" | cut -d ' ' -f1) # Extract package name before the version
|
2024-03-10 09:49:34 +01:00
|
|
|
local is_active="${colors[white]}(${colors[end]}${colors[bold_red]}Inactive${colors[end]}${colors[white]})${colors[end]}" # Set default to Inactive
|
2024-03-04 20:20:33 +01:00
|
|
|
|
|
|
|
# Check if the package is active (only modify if active)
|
2024-03-10 13:31:19 +01:00
|
|
|
if grep -q "${package_name}" ${config[main_zrep_install_dir]}/.addons; then
|
2024-03-06 18:33:14 +01:00
|
|
|
is_active="${colors[white]}(${colors[end]}${colors[bold_green]}Active${colors[end]}${colors[white]})${colors[end]}"
|
2024-03-04 20:20:33 +01:00
|
|
|
fi
|
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info " - ${package_info} ${is_active}"
|
2024-03-04 23:40:02 +01:00
|
|
|
done
|
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() {
|
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
local package_names=""
|
2024-03-04 23:40:02 +01:00
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
# Check if installed.json exists
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f "${installed_json}" ]]; then
|
2024-02-29 16:53:22 +01:00
|
|
|
echo "No installed packages found."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Parse installed.json and concatenate package names
|
2024-03-10 13:31:19 +01:00
|
|
|
jq -r 'to_entries[] | .key as ${author} | .value[] | "\(${author})/\(.script) (\(.version))"' "${installed_json}" | while IFS= read -r package_info; do
|
|
|
|
package_names+="${package_info} "
|
2024-03-04 23:40:02 +01:00
|
|
|
done
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Assuming you want to print out the concatenated package names
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -n "${package_names}" ]]; then
|
|
|
|
echo "Installed packages: ${package_names}"
|
2024-03-04 23:40:02 +01:00
|
|
|
else
|
|
|
|
echo "No packages found."
|
|
|
|
fi
|
2024-02-29 16:53:22 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
# Function to load configuration
|
2024-03-09 00:36:09 +01:00
|
|
|
function zrep_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-03-10 13:31:19 +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
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ "${1}" == "init" ]]; then
|
|
|
|
echo "${ZREP_CONFIG} not found. Proceeding with 'zrep init'..."
|
2024-02-27 16:50:07 +01:00
|
|
|
zrep_init
|
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "${ZREP_CONFIG} not found."
|
2024-02-27 16:50:07 +01:00
|
|
|
# Ask the user if they want to run 'zrep init'
|
|
|
|
read "response?Would you like to run 'zrep init' to set up? (y/n): "
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ "${response}" =~ ^[Yy]$ ]]; then
|
2024-02-27 16:50:07 +01:00
|
|
|
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-10 13:31:19 +01:00
|
|
|
local package_name="${1}"
|
2024-02-28 23:41:09 +01:00
|
|
|
local installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
|
|
|
|
# Check if installed.json exists
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ ! -f "${installed_json}" ]]; then
|
2024-02-28 23:41:09 +01:00
|
|
|
echo "Error: installed.json not found."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get package information from installed.json
|
|
|
|
local package_info
|
2024-03-10 13:31:19 +01:00
|
|
|
package_info=$(zrep_parse_installed_json | jq ".[] | select(.author + \"/\" + .script == \"${package_name}\")")
|
2024-02-28 23:41:09 +01:00
|
|
|
|
|
|
|
# Check if the package is installed
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -z "${package_info}" ]]; then
|
|
|
|
echo "Error: Package '${package_name}' is not installed."
|
2024-02-28 23:41:09 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
local author=$(echo "${package_info}" | jq -r '.author')
|
|
|
|
local script=$(echo "${package_info}" | jq -r '.script')
|
|
|
|
local first_letter=$(echo "${author}" | cut -c 1 | tr '[:upper:]' '[:lower:]')
|
|
|
|
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
echo "Package information:"
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "${package_info}" | jq .
|
2024-02-28 23:41:09 +01:00
|
|
|
|
2024-02-29 16:53:22 +01:00
|
|
|
# Ask user for confirmation with default response "Y"
|
2024-03-04 22:36:49 +01:00
|
|
|
read "REPLY?Are you sure you want to remove this package? (y/n) [Y]: "
|
|
|
|
REPLY=${REPLY:-Y}
|
2024-02-28 23:41:09 +01:00
|
|
|
echo
|
2024-02-29 16:53:22 +01:00
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
|
2024-02-29 16:53:22 +01:00
|
|
|
# Remove the package directory from disk
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -d "${package_dir}" ]]; then
|
|
|
|
rm -rf "${package_dir}"
|
|
|
|
echo "Package directory '${package_dir}' removed successfully."
|
2024-02-29 16:53:22 +01:00
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "Warning: Package directory '${package_dir}' not found."
|
2024-02-29 16:53:22 +01:00
|
|
|
fi
|
|
|
|
|
2024-03-04 22:36:49 +01:00
|
|
|
# Safely check and remove author and first letter directories if empty
|
2024-03-10 13:31:19 +01:00
|
|
|
local author_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}"
|
|
|
|
if [[ -d "${author_dir}" && ! "$(ls -A "${author_dir}")" ]]; then
|
|
|
|
rmdir "${author_dir}"
|
|
|
|
echo "Author directory '${author_dir}' removed successfully."
|
2024-03-04 22:36:49 +01:00
|
|
|
fi
|
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -d "${config[main_zrep_install_dir]}/${first_letter}" && ! "$(ls -A "${config[main_zrep_install_dir]}/${first_letter}")" ]]; then
|
|
|
|
rmdir "${config[main_zrep_install_dir]}/${first_letter}"
|
|
|
|
echo "First letter directory '${config[main_zrep_install_dir]}/${first_letter}' removed successfully."
|
2024-03-04 22:36:49 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove the package from installed.json
|
2024-03-10 13:31:19 +01:00
|
|
|
jq "del(.[] | select(.author == \"${author}\" and .script == \"${script}\"))" "${installed_json}" > "${installed_json}.tmp" && mv "${installed_json}.tmp" "${installed_json}"
|
|
|
|
echo "Package '${package_name}' removed successfully from installed.json."
|
2024-02-28 23:41:09 +01:00
|
|
|
else
|
|
|
|
echo "Removal canceled."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
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}"
|
|
|
|
|
|
|
|
# Get the base directory where the package will be installed
|
|
|
|
local baseDir="${config[main_zrep_install_dir]}/"
|
2024-03-04 23:40:02 +01:00
|
|
|
local tmpDir="${baseDir}/tmp"
|
2024-03-04 20:20:33 +01:00
|
|
|
|
|
|
|
# Create the directory if it doesn't exist
|
2024-03-10 13:31:19 +01:00
|
|
|
mkdir -p "${tmpDir}"
|
2024-03-04 20:20:33 +01:00
|
|
|
|
|
|
|
# Download the package zip file
|
2024-03-04 23:40:02 +01:00
|
|
|
local zipFile="${tmpDir}/${author}_${package}_${version}.zip"
|
2024-03-10 13:31:19 +01:00
|
|
|
curl -s -o "${zipFile}" "${dlurl}"
|
2024-03-04 20:20:33 +01:00
|
|
|
|
|
|
|
# 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
|
2024-03-10 13:31:19 +01:00
|
|
|
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
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_update_installed_json "${author}" "${script}" "${version}"
|
2024-03-04 20:20:33 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Clean up: Remove the downloaded zip file
|
2024-03-10 13:31:19 +01:00
|
|
|
rm "${zipFile}"
|
2024-03-04 20:20:33 +01:00
|
|
|
|
|
|
|
}
|
2024-03-04 23:40:02 +01:00
|
|
|
|
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"
|
2024-03-10 13:31:19 +01:00
|
|
|
jq -c '.' "${installed_json}"
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
function zrep_parse_package_name() {
|
2024-03-09 00:36:09 +01:00
|
|
|
# echo "Looking for package ${1}"
|
2024-03-10 13:31:19 +01:00
|
|
|
package_name="${1}"
|
2024-03-04 23:40:02 +01:00
|
|
|
installed_json="${config[main_zrep_install_dir]}/installed.json"
|
|
|
|
author="${package_name%/*}"
|
|
|
|
script="${package_name#*/}"
|
2024-03-10 13:31:19 +01:00
|
|
|
first_letter=$(echo "${author}" | cut -c 1 | tr '[:upper:]' '[:lower:]')
|
|
|
|
addon_path="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
|
|
|
# Check if the package is installed
|
2024-03-10 13:31:19 +01:00
|
|
|
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."
|
2024-03-04 22:17:21 +01:00
|
|
|
return 1
|
|
|
|
fi
|
2024-03-04 23:40:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_enable() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local package_name="${1}"
|
|
|
|
zrep_parse_package_name "${package_name}"
|
2024-03-04 23:40:02 +01:00
|
|
|
|
|
|
|
# Assuming zrep_parse_package_name sets 'addon_path' correctly
|
|
|
|
# and 'package_name' is in 'author/script' format
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-09 00:36:09 +01:00
|
|
|
# Initialize addons array if .zrep_addons does not exist
|
|
|
|
if [ ! -f "${config[main_zrep_install_dir]}/.addons" ]; then
|
|
|
|
addons=()
|
|
|
|
else
|
|
|
|
# Load existing addons from ${config[main_zrep_install_dir]}/.addons
|
|
|
|
source "${config[main_zrep_install_dir]}/.addons"
|
|
|
|
fi
|
2024-03-04 22:17:21 +01:00
|
|
|
|
|
|
|
# Check if the addon is already enabled
|
|
|
|
local addon_exists=0
|
|
|
|
for addon in "${addons[@]}"; do
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ "${addon}" == "${addon_path}" ]]; then
|
2024-03-04 22:17:21 +01:00
|
|
|
addon_exists=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if ((addon_exists)); then
|
2024-03-10 13:31:19 +01:00
|
|
|
echo "Package '${package_name}' is already enabled."
|
2024-03-04 22:17:21 +01:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add addon path to the array
|
2024-03-10 13:31:19 +01:00
|
|
|
addons+=("${addon_path}")
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Reconstruct .zrep_addons file with the updated addons array
|
2024-03-04 22:17:21 +01:00
|
|
|
{
|
|
|
|
echo "addons=("
|
|
|
|
for addon in "${addons[@]}"; do
|
2024-03-10 13:31:19 +01:00
|
|
|
echo " '${addon}'"
|
2024-03-04 22:17:21 +01:00
|
|
|
done
|
|
|
|
echo ")"
|
2024-03-09 00:36:09 +01:00
|
|
|
} > "${config[main_zrep_install_dir]}/.addons"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Source the updated .zrep_addons to apply changes
|
2024-03-09 00:36:09 +01:00
|
|
|
source "${HOME}/.zrep_addons"
|
|
|
|
autoload -Uz "${script}"
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info "\nPackage '${package_name}' has been enabled and added to fpath."
|
2024-03-09 00:36:09 +01:00
|
|
|
zrep_msg info "You may have to run 'source ~/.zrep_addons' to get access to it."
|
2024-03-04 22:17:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_disable() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local package_name="${1}"
|
|
|
|
zrep_parse_package_name "${package_name}"
|
2024-03-04 23:40:02 +01:00
|
|
|
|
|
|
|
# Assuming zrep_parse_package_name sets 'addon_path' correctly
|
|
|
|
# and 'package_name' is in 'author/script' format
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-09 00:36:09 +01:00
|
|
|
# Initialize addons array if .zrep_addons does not exist
|
|
|
|
if [ ! -f "${config[main_zrep_install_dir]}/.addons" ]; then
|
|
|
|
addons=()
|
|
|
|
else
|
|
|
|
# Load existing addons from ${config[main_zrep_install_dir]}/.addons
|
|
|
|
source "${config[main_zrep_install_dir]}/.addons"
|
|
|
|
fi
|
2024-03-04 22:17:21 +01:00
|
|
|
|
|
|
|
# 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
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ "${addon}" == "${addon_path}" ]]; then
|
2024-03-04 22:17:21 +01:00
|
|
|
found=1
|
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
new_addons+=("${addon}")
|
2024-03-04 22:17:21 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if ((found == 0)); then
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg debug "\nPackage '${package_name}' is not currently enabled."
|
2024-03-04 22:17:21 +01:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Reconstruct .zrep_addons file with the new addons array
|
2024-03-04 22:17:21 +01:00
|
|
|
{
|
|
|
|
echo "addons=("
|
2024-03-04 23:40:02 +01:00
|
|
|
for addon in "${new_addons[@]}"; do
|
2024-03-10 13:31:19 +01:00
|
|
|
echo " '${addon}'"
|
2024-03-04 22:17:21 +01:00
|
|
|
done
|
|
|
|
echo ")"
|
2024-03-09 00:36:09 +01:00
|
|
|
} > ${config[main_zrep_install_dir]}/.addons
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-04 23:40:02 +01:00
|
|
|
# Source the updated .zrep_addons to apply changes
|
|
|
|
source ${HOME}/.zrep_addons
|
2024-03-09 00:36:09 +01:00
|
|
|
unfunction ${script} 2>/dev/null || true
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info "\nPackage '${package_name} (${script})' has been disabled and removed from fpath."
|
2024-03-09 00:36:09 +01:00
|
|
|
zrep_msg info "You may have to run 'source ~/.zrep_addons' to remove it from your shell."
|
2024-03-04 22:17:21 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-03-06 18:33:14 +01:00
|
|
|
zrep_msg sub "\nUsage: zrep <command> [arguments]"
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info "Available commands:"
|
|
|
|
zrep_msg info " init: Initialize zrep"
|
|
|
|
zrep_msg info " install (i) <author/package>: Install a package"
|
|
|
|
zrep_msg info " remove (rm, delete) <author/package>: Remove a package"
|
|
|
|
zrep_msg info " update (u) <author/package>: Update zrep package"
|
|
|
|
zrep_msg info " enable <author/package>: Enable zrep package"
|
|
|
|
zrep_msg info " disable <author/package>: Disable zrep package"
|
|
|
|
zrep_msg info " version: Display zrep version"
|
|
|
|
zrep_msg info " list: List installed packages"
|
|
|
|
zrep_msg info " <author/package> help: Display help for pacakage (if available)"
|
2024-03-06 18:33:14 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function zrep_read_usage() {
|
2024-03-10 13:31:19 +01:00
|
|
|
local package_name="${1}"
|
2024-03-06 18:33:14 +01:00
|
|
|
# Parse the package name to extract author and script
|
|
|
|
local author="${package_name%/*}"
|
|
|
|
local script="${package_name#*/}"
|
2024-03-10 13:31:19 +01:00
|
|
|
local first_letter=$(echo "${author}" | cut -c 1 | tr '[:upper:]' '[:lower:]')
|
2024-03-01 12:22:39 +01:00
|
|
|
|
2024-03-06 18:33:14 +01:00
|
|
|
# Construct the path to the USAGE file
|
2024-03-10 13:31:19 +01:00
|
|
|
local usage_file="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}/USAGE"
|
2024-03-06 18:33:14 +01:00
|
|
|
|
|
|
|
# Check if the USAGE file exists
|
2024-03-10 13:31:19 +01:00
|
|
|
if [[ -f "${usage_file}" ]]; then
|
2024-03-06 18:33:14 +01:00
|
|
|
# Display the content of the USAGE file
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg sub "\n${package_name}:"
|
2024-03-06 18:33:14 +01:00
|
|
|
local usage_buffer=$(<${usage_file})
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg info "${usage_buffer}"
|
2024-03-06 18:33:14 +01:00
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_msg debug "No USAGE file found for package '${package_name}'."
|
2024-03-06 18:33:14 +01:00
|
|
|
fi
|
2024-02-28 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
|
2024-03-04 22:36:49 +01:00
|
|
|
zrep_main_version_string
|
2024-03-04 22:17:21 +01:00
|
|
|
|
2024-03-09 00:36:09 +01:00
|
|
|
zrep_load_config
|
2024-02-27 16:50:07 +01:00
|
|
|
|
2024-03-06 18:33:14 +01:00
|
|
|
# Check if the second argument is "help" and the first argument is not empty
|
|
|
|
if [[ "${2}" == "help" && -n "${1}" ]]; then
|
|
|
|
zrep_read_usage "${1}"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2024-02-27 16:50:07 +01:00
|
|
|
# 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-03-10 13:31:19 +01:00
|
|
|
if [[ -z "${zrep_remove_package_name}" ]]; then
|
|
|
|
echo "Usage: zrep remove author/package"
|
2024-02-28 23:41:09 +01:00
|
|
|
else
|
2024-03-10 13:31:19 +01:00
|
|
|
zrep_remove_package "${zrep_remove_package_name}"
|
2024-02-28 23:41:09 +01:00
|
|
|
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}
|
2024-03-09 00:36:09 +01:00
|
|
|
source "${HOME}/.zrep_addons"
|
2024-03-04 22:17:21 +01:00
|
|
|
;;
|
|
|
|
disable)
|
|
|
|
zrep_disable ${2}
|
2024-03-09 00:36:09 +01:00
|
|
|
source "${HOME}/.zrep_addons"
|
2024-03-04 22:17:21 +01:00
|
|
|
;;
|
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 "$@"
|