Update zrep script:

- Changed comment to define default path for .zreprc
- Updated comment to define list of colors available for themes
- Removed clearing of previous theme settings
- Added function to check if a string exists in ~/.zshrc
- Added function to check for required dependencies
- Updated zrep_init function to include checking for dependencies
- Updated zrep_init function to create default file and directory structure
- Updated zrep_init function to check for existing zini path in .zshrc
- Updated zrep_init function to check for existing source command for .zrep_addons in .zshrc
- Updated zrep_init function to create or update .zrep_addons file
- Updated zrep_check_for_updates function to display message if no updates found
- Removed redundant message in zrep_update_package function
- Updated zrep_download_package function to display download attempt message
- Updated zrep_download_package function to display download successful message
- Updated zrep_install_package function to include fetching package information
- Removed commented out line in zrep_install_package function
- Removed commented out line in zrep_parse_package_name function
This commit is contained in:
Stig-Ørjan Smelror 2024-03-13 22:52:47 +01:00
parent a492260ec9
commit 40adc05b9b

87
zrep
View File

@ -4,7 +4,7 @@ setopt extendedglob
VERSION="0.0.2" # Sat-2024-03-13
ZREP="Zsh Repository Tool"
# Define the path to .zreprc
# Define the default path to .zreprc
ZREP_CONFIG="${HOME}/.zreprc"
function zrep_fpath() {
@ -27,7 +27,7 @@ function zrep_fpath() {
zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini
# List of colors available
# Define a list of colors available to use in themes
typeset -A base_colors=(
[green]="\033[0;32m"
[yellow]="\033[1;33m"
@ -71,10 +71,6 @@ zrep_load_theme() {
theme_file="${config[main_zrep_install_dir]}/themes/classic"
fi
# Clear previous theme settings
#unset current_theme
#declare -gA current_theme
# Source the theme file, which should define 'theme_colors'
source "$theme_file"
@ -82,7 +78,6 @@ zrep_load_theme() {
for key value in ${(kv)theme_colors}; do
current_theme[$key]="$value"
done
# print -l ${(kv)current_theme}
}
function zrep_main_version_string() {
@ -97,10 +92,12 @@ function zrep_version() {
exit
}
# This function is used to display messages and use colors
# from the loaded theme.
function zrep_msg() {
local msg_type="$1"
local message="$2"
local color="${base_colors[end]}" # Default color
local color="${base_colors[end]}" # Default color is NONE
# Retrieve the color key from the current theme
local theme_color_key="${current_theme[$msg_type]}"
@ -116,11 +113,44 @@ function zrep_msg() {
printf "%b\n" "${color}${message}${base_colors[end]}"
}
# Function to check if a given string exists in ~/.zshrc
function zrep_find_string() {
local searchString="$1"
local found=0
while IFS= read -r line || [[ -n $line ]]; do
if [[ "$line" == *"$searchString"* ]]; then
found=1
break
fi
done < "${HOME}/.zshrc"
echo $found
}
function zrep_check_for_deps() {
# Array of required external programs
local required_programs=('jq')
# Iterate over the array
for program in "${required_programs[@]}"; do
# Check if the program is available in the system's PATH
if ! command -v "$program" &> /dev/null; then
# Program not found, display a message
echo "Required program not found: $program"
fi
done
}
# The first step after downloading zrep.
# Here we create the default file and directory structure
function zrep_init() {
local zshrc_file="${HOME}/.zshrc"
local addons_file="${HOME}/.zrep_addons"
local zrep_addons="${HOME}/.zrep_addons"
local install_dir
zrep_check_for_deps
# Check if .zreprc exists
if [[ ! -f ${ZREP_CONFIG} ]]; then
echo "${ZREP_CONFIG} not found. Creating it..."
@ -130,11 +160,12 @@ function zrep_init() {
# Write to .zreprc
cat > "${ZREP_CONFIG}" <<EOF
[main]
zrep_install_dir = ${install_dir}
[main]
zrep_install_dir = ${install_dir}
[global]
repo_url = https://kekepower.com/zrep
[global]
repo_url = https://kekepower.com/zrep
theme = classic
EOF
else
echo "Loading configuration from ${ZREP_CONFIG}"
@ -142,23 +173,23 @@ EOF
install_dir=${config[main_zrep_install_dir]}
fi
mkdir -p "${install_dir}/functions/zini"
curl -s https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"
if ! grep -q "zini" "${zshrc_file}"; then
if [[ $(zrep_find_string zini) -eq 0 ]]; then
mkdir -p "${install_dir}/functions/zini"
curl -s https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"
echo "Adding 'zini' path to fpath in ${zshrc_file}"
echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file}
fi
# Check if .zshrc already sources .zrep_addons, if not, add it
if ! grep -q "source ${addons_file}" "${zshrc_file}"; then
if [[ $(zrep_find_string "source ${zrep_addons}") -eq 0 ]]; then
echo "Adding source command for .zrep_addons to .zshrc..."
echo "source ${addons_file}" >> "${zshrc_file}"
echo "source ${zrep_addons}" >> "${zshrc_file}"
fi
# Create or update the .zrep_addons file
if [[ ! -f ${addons_file} ]]; then
echo "Creating file ${addons_file}..."
cat > "${addons_file}" <<EOF
if [[ ! -f ${zrep_addons} ]]; then
echo "Creating file ${zrep_addons}..."
cat > "${zrep_addons}" <<EOF
# Source the .addons file from the zrep installation directory
source "${install_dir}/.addons"
@ -421,6 +452,10 @@ function zrep_check_for_updates() {
fi
done
if [[ ${updates} == "false" ]]; then
zrep_msg info "\nNo updates found."
fi
}
function zrep_update_package() {
@ -455,8 +490,6 @@ function zrep_update_package() {
local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg}
done
else
zrep_msg info "\nNo updates found."
fi
fi
}
@ -469,13 +502,13 @@ function zrep_download_package() {
local attempt=1
while (( attempt <= retries )); do
zrep_msg info "Attempt $attempt of $retries: Downloading..."
zrep_msg sub "Attempt $attempt of $retries: Downloading..."
# Use curl to download the file, capturing HTTP status code
http_status=$(curl -s -w "%{http_code}" -o "${zipFile}" "${dlurl}" -o /dev/null)
# Check if curl command was successful and HTTP status is 200 (OK)
if [[ $? -eq 0 && ${http_status} -eq 200 ]]; then
zrep_msg info "Download successful."
zrep_msg sub "Download successful."
return 0
else
zrep_msg debug "Error: Failed to download the package. HTTP status: ${http_status}."
@ -509,9 +542,7 @@ function zrep_install_package() {
# If not installed, proceed with fetching the package information
zrep_parse_remote "${config[global_repo_url]}/packages.json" "${package}"
# echo "zrep_install_package: $dlurl"
#local baseDir="${config[main_zrep_install_dir]}"
local tmpDir="${config[main_zrep_install_dir]}/tmp"
mkdir -p "${tmpDir}"
@ -540,7 +571,6 @@ function zrep_parse_installed_json() {
}
function zrep_parse_package_name() {
# echo "Looking for package ${1}"
package_name="${1}"
zrep_installed_json
author="${package_name%/*}"
@ -699,7 +729,6 @@ function main() {
case "${1}" in
init)
zrep_init
# zrep_fpath ${config[main_zrep_install_dir]}
exit
;;
check)