fix: improve initialization process and error handling

- Add dependency checking for jq and downloaders
- Improve directory creation and error handling
- Better zini installation and configuration
- Enhanced user feedback during setup
- Version bump to 0.1.0
This commit is contained in:
Stig-Ørjan Smelror 2025-05-18 13:07:54 +02:00
parent 36b81d6657
commit 58438a5fdf

195
zpi
View File

@ -2,7 +2,7 @@
setopt extendedglob setopt extendedglob
VERSION="0.0.9" # Sat-2024-04-06 VERSION="0.1.0" # Sat-2025-05-18
ZREP="Zrep Package Installer" ZREP="Zrep Package Installer"
# Define the default path to .zreprc # Define the default path to .zreprc
ZREP_CONFIG="${HOME}/.zreprc" ZREP_CONFIG="${HOME}/.zreprc"
@ -223,6 +223,31 @@ function zrep_check_for_deps() {
done done
} }
# Function to check for required dependencies
function zrep_check_deps() {
local missing_deps=()
# Check for jq
if ! command -v jq &> /dev/null; then
missing_deps+=("jq")
fi
# Check for downloader (curl/wget)
if ! command -v curl &> /dev/null && ! command -v wget &> /dev/null; then
missing_deps+=("curl or wget")
fi
if [[ ${#missing_deps[@]} -ne 0 ]]; then
echo "The following dependencies are required but not installed:"
for dep in "${missing_deps[@]}"; do
echo " - ${dep}"
done
echo "Please install them and try again."
return 1
fi
return 0
}
# The first step after downloading zrep. # The first step after downloading zrep.
# Here we create the default file and directory structure # Here we create the default file and directory structure
function zrep_init() { function zrep_init() {
@ -230,23 +255,32 @@ function zrep_init() {
local zrep_addons="${HOME}/.zrep_addons" local zrep_addons="${HOME}/.zrep_addons"
local install_dir local install_dir
# Check for required dependencies
if ! zrep_check_deps; then
return 1
fi
# Check if .zreprc exists # Check if .zreprc exists
if [[ ! -f ${ZREP_CONFIG} ]]; then if [[ ! -f ${ZREP_CONFIG} ]]; then
echo "${ZREP_CONFIG} not found. Creating it..." echo "${ZREP_CONFIG} not found. Creating it..."
# Prompt user for install directory # Prompt user for install directory
read "install_dir?Enter zpi installation directory [${HOME}/.zrep]: " read "install_dir?Enter zpi installation directory [${HOME}/.zrep]: "
install_dir=${install_dir:-"${HOME}/.zrep"} install_dir=${install_dir:-"${HOME}/.zrep"}
mkdir -p ${install_dir} mkdir -p "${install_dir}" || {
read "downloader?Choose command to download packages [curl, wget, wget2]: " echo "Failed to create directory: ${install_dir}"
if [[ ${downloader} != curl && ${downloader} != wget && ${downloader} != wget2 ]]; then return 1
echo "Invalid choice: '$downloader'. Try again." }
read "downloader?Choose command to download packages [curl, wget, wget2] "
if [[ ${downloader} != curl && ${downloader} != wget && ${downloader} != wget2 ]]; then # Choose downloader
echo "Invalid choice: '$downloader'. Exiting." local downloader=""
exit if command -v curl &> /dev/null; then
fi downloader="curl"
elif command -v wget &> /dev/null; then
downloader="wget"
fi fi
echo "Using ${downloader} for downloads"
# Write to .zreprc # Write to .zreprc
cat > "${ZREP_CONFIG}" <<EOF cat > "${ZREP_CONFIG}" <<EOF
[main] [main]
@ -257,33 +291,86 @@ repo_url = https://zrep.kekepower.com
theme = classic theme = classic
downloader = ${downloader} downloader = ${downloader}
EOF EOF
echo "The file '${ZREP_CONFIG}' has been created." echo "The file '${ZREP_CONFIG}' has been created."
else else
zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini
echo "Loading configuration from ${ZREP_CONFIG}" echo "Loading configuration from ${ZREP_CONFIG}"
zini ${ZREP_CONFIG} # Ensure the install directory exists
# install_dir=${config[main_zrep_install_dir]} install_dir=${HOME}/.zrep # Default value
if [[ -f ${ZREP_CONFIG} ]]; then
# Extract the install directory from the config file if it exists
local config_install_dir=$(grep -m 1 '^zrep_install_dir' "${ZREP_CONFIG}" | cut -d'=' -f2 | tr -d '[:space:]')
if [[ -n "${config_install_dir}" ]]; then
install_dir="${config_install_dir}"
fi
mkdir -p "${install_dir}" || {
echo "Failed to create directory: ${install_dir}"
return 1
}
fi
# Load zini if available
if [[ -f "${install_dir}/functions/zini/zini" ]]; then
fpath=("${install_dir}/functions/zini" $fpath)
autoload -Uz zini
zini ${ZREP_CONFIG}
fi
fi fi
if [[ $(zrep_find_string zini) -eq 0 ]]; then # Create necessary directories
mkdir -p "${install_dir}/functions/zini" local dirs=(
zrep_global_downloader https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini" "${install_dir}/functions"
echo "Adding 'zini' path to fpath in ${zshrc_file}" "${install_dir}/themes"
echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file} "${install_dir}/packages"
)
for dir in "${dirs[@]}"; do
if [[ ! -d "${dir}" ]]; then
echo "Creating directory: ${dir}"
mkdir -p "${dir}" || {
echo "Failed to create directory: ${dir}"
return 1
}
fi
done
# Install zini if not found
if [[ ! -f "${install_dir}/functions/zini/zini" ]]; then
echo "Installing zini..."
mkdir -p "${install_dir}/functions/zini" || {
echo "Failed to create directory: ${install_dir}/functions/zini"
return 1
}
if ! zrep_global_downloader https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"; then
echo "Failed to download zini"
return 1
fi
# Add zini to fpath in .zshrc if not already present
if ! grep -q "${install_dir}/functions/zini" "${zshrc_file}" 2>/dev/null; then
echo "Adding 'zini' path to fpath in ${zshrc_file}"
echo "fpath=('${install_dir}/functions/zini' \$fpath)" >> "${zshrc_file}"
fi
# Ensure zini is loaded
fpath=("${install_dir}/functions/zini" $fpath)
autoload -Uz zini autoload -Uz zini
fi
# Check if .zshrc already sources .zrep_addons, if not, add it # Load the config
if [[ $(zrep_find_string "source ${zrep_addons}") -eq 0 ]]; then if [[ -f "${ZREP_CONFIG}" ]]; then
echo "Adding source command for .zrep_addons to .zshrc..." zini "${ZREP_CONFIG}"
echo "source ${zrep_addons}" >> "${zshrc_file}" fi
fi fi
# Create or update the .zrep_addons file # Create or update the .zrep_addons file
if [[ ! -f ${zrep_addons} ]]; then echo "Configuring zrep addons..."
if [[ ! -f "${zrep_addons}" ]]; then
echo "Creating file ${zrep_addons}..." echo "Creating file ${zrep_addons}..."
touch ${install_dir}/.addons touch "${install_dir}/.addons" || {
echo "Failed to create: ${install_dir}/.addons"
return 1
}
cat > "${zrep_addons}" <<EOF cat > "${zrep_addons}" <<EOF
# Source the .addons file from the zrep installation directory # Source the .addons file from the zrep installation directory
source "${install_dir}/.addons" source "${install_dir}/.addons"
@ -291,29 +378,55 @@ source "${install_dir}/.addons"
# If addons array is defined and not empty, add its elements to fpath # If addons array is defined and not empty, add its elements to fpath
if [[ -n \${addons[@]} ]]; then if [[ -n \${addons[@]} ]]; then
for addon in "\${addons[@]}"; do for addon in "\${addons[@]}"; do
if [[ -d \${addon} ]] && [[ ! " \${fpath[*]} " =~ " \${addon} " ]]; then if [[ -d "\${addon}" ]] && [[ ! " \${fpath[*]} " =~ " \${addon} " ]]; then
fpath=(\${addon} "\${fpath[@]}") # Prepend the new addon to fpath fpath=("\${addon}" "\${fpath[@]}") # Prepend the new addon to fpath
fi fi
autoload -Uz \$(basename \${addon}) autoload -Uz \$(basename "\${addon}")
done done
else else
echo "zpi: No addons enabled." echo "zpi: No addons enabled. Use 'zpi enable <package>' to enable addons."
fi fi
EOF EOF
echo "File .zrep_addons created and configured." echo "File ${zrep_addons} created and configured."
else else
echo "File .zrep_addons already exists. Review manually if update is needed." echo "File ${zrep_addons} already exists. Review manually if update is needed."
fi fi
if [[ ! -d ${install_dir}/themes ]]; then # Add source to .zshrc if not present
echo "Installing the Classic theme to ${install_dir}/themes" if ! grep -q "source \"${zrep_addons}\"" "${zshrc_file}" 2>/dev/null; then
mkdir -p ${install_dir}/themes echo "Adding source command for ${zrep_addons} to ${zshrc_file}..."
zrep_global_downloader https://git.kekepower.com/kekePower/zpi/raw/branch/main/themes/classic -o ${install_dir}/themes/classic echo -e "\n# Source zrep addons\nsource \"${zrep_addons}\"" >> "${zshrc_file}"
fi fi
echo "zpi initialization complete."
echo "You should copy 'zpi' to a path in you 'PATH' so that it's accessible." # Install default theme if not present
echo "For example '${HOME}/bin'" if [[ ! -f "${install_dir}/themes/classic" ]]; then
echo "Remember to 'source ${zshrc_file}' to load the 'zpi' settings." echo "Installing the Classic theme to ${install_dir}/themes"
if ! zrep_global_downloader https://git.kekepower.com/kekePower/zpi/raw/branch/main/themes/classic -o "${install_dir}/themes/classic"; then
echo "Warning: Failed to download classic theme"
fi
fi
# Create a symlink if not in PATH
local bin_dir="${HOME}/bin"
if [[ ! -d "${bin_dir}" ]]; then
mkdir -p "${bin_dir}" && chmod 755 "${bin_dir}" || {
echo "Warning: Could not create ${bin_dir}"
bin_dir=""
}
fi
if [[ -n "${bin_dir}" && ! -f "${bin_dir}/zpi" ]]; then
echo "Creating symlink in ${bin_dir}"
ln -sf "$(pwd)/zpi" "${bin_dir}/zpi"
fi
echo -e "\n${base_colors[bold_green]}zpi initialization complete!${base_colors[end]}"
if [[ "${PATH}" != *"${bin_dir}"* ]]; then
echo -e "\n${base_colors[bold_yellow]}Note:${base_colors[end]} Add ${bin_dir} to your PATH by adding this to your ~/.zshrc:"
echo " export PATH=\"${bin_dir}:\$PATH\""
fi
echo -e "\nTo start using zpi, run: ${base_colors[bold_white]}source ${zshrc_file}${base_colors[end]}"
echo "Then try: ${base_colors[bold_white]}zpi help${base_colors[end]} to see available commands"
} }
function zrep_installed_json() { function zrep_installed_json() {