refactor: modularize package installation into separate script
This commit is contained in:
131
packages/install.sh
Normal file
131
packages/install.sh
Normal file
@@ -0,0 +1,131 @@
|
||||
#!/bin/bash
|
||||
# packages/install.sh - Module to install package lists
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Usage: $0 <dotfiles_directory>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
dotfiles_dir="$1"
|
||||
# Source shared utilities
|
||||
source "$dotfiles_dir/scripts/utils.sh"
|
||||
|
||||
# Detect package manager
|
||||
detect_package_manager() {
|
||||
if command_exists apt; then
|
||||
echo "apt"
|
||||
elif command_exists yum; then
|
||||
echo "yum"
|
||||
elif command_exists dnf; then
|
||||
echo "dnf"
|
||||
elif command_exists pacman; then
|
||||
echo "pacman"
|
||||
elif command_exists brew; then
|
||||
echo "brew"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
# Install packages with detected package manager
|
||||
install_packages() {
|
||||
local package_manager="$1"
|
||||
shift
|
||||
local packages=("$@")
|
||||
|
||||
if [ ${#packages[@]} -eq 0 ]; then
|
||||
log_info "No packages to install"
|
||||
return 0
|
||||
fi
|
||||
|
||||
case "$package_manager" in
|
||||
apt)
|
||||
log_info "Installing packages with apt..."
|
||||
sudo apt update && sudo apt install -y "${packages[@]}"
|
||||
;;
|
||||
yum)
|
||||
log_info "Installing packages with yum..."
|
||||
sudo yum install -y "${packages[@]}"
|
||||
;;
|
||||
dnf)
|
||||
log_info "Installing packages with dnf..."
|
||||
sudo dnf install -y "${packages[@]}"
|
||||
;;
|
||||
pacman)
|
||||
log_info "Installing packages with pacman..."
|
||||
sudo pacman -S --noconfirm "${packages[@]}"
|
||||
;;
|
||||
brew)
|
||||
log_info "Installing packages with brew..."
|
||||
brew install "${packages[@]}"
|
||||
;;
|
||||
*)
|
||||
log_error "Unknown package manager. Please install packages manually."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Read packages from file
|
||||
read_package_file() {
|
||||
local file="$1"
|
||||
if [ -f "$file" ]; then
|
||||
grep -v '^#' "$file" | grep -v '^[[:space:]]*$' | tr '\n' ' '
|
||||
fi
|
||||
}
|
||||
|
||||
# Loop through package lists and install
|
||||
def install_package_lists() {
|
||||
local dotfiles_dir="$1"
|
||||
local packages_dir="$dotfiles_dir/packages"
|
||||
|
||||
if [ ! -d "$packages_dir" ]; then
|
||||
log_warning "No packages directory found at $packages_dir"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local package_manager
|
||||
package_manager=$(detect_package_manager)
|
||||
|
||||
if [ "$package_manager" = "unknown" ]; then
|
||||
log_error "No supported package manager found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_info "Detected package manager: $package_manager"
|
||||
|
||||
# Define package group order
|
||||
local package_files=("base.txt" "cli-tools.txt" "dev.txt" "gui.txt")
|
||||
|
||||
for package_file in "${package_files[@]}"; do
|
||||
local file_path="$packages_dir/$package_file"
|
||||
if [ -f "$file_path" ]; then
|
||||
# Prompt per group
|
||||
if is_interactive; then
|
||||
read -rp "Install packages from $package_file? [y/N] " answer
|
||||
if [[ ! $answer =~ ^[Yy] ]]; then
|
||||
log_info "Skipping $package_file"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
log_info "Installing packages from $package_file..."
|
||||
local packages
|
||||
packages=$(read_package_file "$file_path")
|
||||
|
||||
if [ -n "$packages" ]; then
|
||||
read -ra package_array <<< "$packages"
|
||||
install_packages "$package_manager" "${package_array[@]}"
|
||||
log_success "Completed installation from $package_file"
|
||||
else
|
||||
log_info "No packages found in $package_file"
|
||||
fi
|
||||
else
|
||||
log_info "Package file $package_file not found, skipping"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Execute when run directly
|
||||
install_package_lists "$dotfiles_dir"
|
||||
@@ -5,121 +5,21 @@ set -euo pipefail
|
||||
|
||||
# Source utilities
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/utils.sh"
|
||||
...existing code...
|
||||
|
||||
# Detect package manager
|
||||
detect_package_manager() {
|
||||
if command_exists apt; then
|
||||
echo "apt"
|
||||
elif command_exists yum; then
|
||||
echo "yum"
|
||||
elif command_exists dnf; then
|
||||
echo "dnf"
|
||||
elif command_exists pacman; then
|
||||
echo "pacman"
|
||||
elif command_exists brew; then
|
||||
echo "brew"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
# Install packages with detected package manager
|
||||
install_packages() {
|
||||
local package_manager="$1"
|
||||
shift
|
||||
local packages=("$@")
|
||||
|
||||
if [ ${#packages[@]} -eq 0 ]; then
|
||||
log_info "No packages to install"
|
||||
return 0
|
||||
fi
|
||||
|
||||
case "$package_manager" in
|
||||
apt)
|
||||
log_info "Installing packages with apt..."
|
||||
sudo apt update && sudo apt install -y "${packages[@]}"
|
||||
;;
|
||||
yum)
|
||||
log_info "Installing packages with yum..."
|
||||
sudo yum install -y "${packages[@]}"
|
||||
;;
|
||||
dnf)
|
||||
log_info "Installing packages with dnf..."
|
||||
sudo dnf install -y "${packages[@]}"
|
||||
;;
|
||||
pacman)
|
||||
log_info "Installing packages with pacman..."
|
||||
sudo pacman -S --noconfirm "${packages[@]}"
|
||||
;;
|
||||
brew)
|
||||
log_info "Installing packages with brew..."
|
||||
brew install "${packages[@]}"
|
||||
;;
|
||||
*)
|
||||
log_error "Unknown package manager. Please install packages manually."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Read packages from file
|
||||
read_package_file() {
|
||||
local file="$1"
|
||||
if [ -f "$file" ]; then
|
||||
# Filter out comments and empty lines
|
||||
grep -v '^#' "$file" | grep -v '^[[:space:]]*$' | tr '\n' ' '
|
||||
fi
|
||||
}
|
||||
|
||||
install_package_lists() {
|
||||
local dotfiles_dir="$1"
|
||||
local packages_dir="$dotfiles_dir/packages"
|
||||
|
||||
if [ ! -d "$packages_dir" ]; then
|
||||
log_warning "No packages directory found at $packages_dir"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local package_manager
|
||||
package_manager=$(detect_package_manager)
|
||||
|
||||
if [ "$package_manager" = "unknown" ]; then
|
||||
log_error "No supported package manager found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_info "Detected package manager: $package_manager"
|
||||
|
||||
# Install package lists in order
|
||||
local package_files=("base.txt" "cli-tools.txt" "dev.txt" "gui.txt")
|
||||
|
||||
for package_file in "${package_files[@]}"; do
|
||||
local file_path="$packages_dir/$package_file"
|
||||
if [ -f "$file_path" ]; then
|
||||
log_info "Installing packages from $package_file..."
|
||||
local packages
|
||||
packages=$(read_package_file "$file_path")
|
||||
|
||||
if [ -n "$packages" ]; then
|
||||
# Convert space-separated string to array
|
||||
read -ra package_array <<< "$packages"
|
||||
install_packages "$package_manager" "${package_array[@]}"
|
||||
log_success "Completed installation from $package_file"
|
||||
else
|
||||
log_info "No packages found in $package_file"
|
||||
fi
|
||||
else
|
||||
log_info "Package file $package_file not found, skipping"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Run if called directly
|
||||
# Run module when called directly
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
if [ $# -ne 1 ]; then
|
||||
log_error "Usage: $0 <dotfiles_directory>"
|
||||
exit 1
|
||||
fi
|
||||
install_package_lists "$1"
|
||||
dotfiles_dir="$1"
|
||||
module_script="$dotfiles_dir/packages/install.sh"
|
||||
|
||||
if [ ! -x "$module_script" ]; then
|
||||
log_error "Module script not found or not executable: $module_script"
|
||||
exit 1
|
||||
fi
|
||||
bash "$module_script" "$dotfiles_dir"
|
||||
fi
|
||||
...existing code...
|
||||
|
||||
Reference in New Issue
Block a user