Skip to content

Commit

Permalink
refactor: use regex to match options; change makepkg flags
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Jun 16, 2021
1 parent dfc83a6 commit 55373b1
Showing 1 changed file with 81 additions and 55 deletions.
136 changes: 81 additions & 55 deletions source/yeet
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function join_by {
}
# Check if we are root, else use sudo
function require_root {
if [ "$(id -u)" != "0" ]; then
if [ -f "$(command -v $SUDO_BIN)" ]; then
if [[ "$(id -u)" != "0" ]]; then
if [[ -f "$(command -v $SUDO_BIN)" ]]; then
$SUDO_BIN $*
else
printr "ERR Command $SUDO_BIN not found, cannot run command \`$*\` without root; aborting..."
Expand Down Expand Up @@ -81,7 +81,7 @@ CONFIG_FILE_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/yeet/yeet.conf"
# Make sure the yeet dir exists
mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/yeet/"
# Now check if the user already has a config file in that dir
if [ -f "$CONFIG_FILE_PATH" ]; then
if [[ -f "$CONFIG_FILE_PATH" ]]; then
# If the file exists, set all the variables from it
source "$CONFIG_FILE_PATH"
else
Expand Down Expand Up @@ -113,7 +113,7 @@ else
echo "PRINT_LOGO=true" >> "$CONFIG_FILE_PATH"

# If there is no config yet and the operation is not 'help', definitely print the logo
if [ "$1" != "-h" ] && [ "$1" != "--help" ] && ! [ -z "$1" ]; then
if [[ "$1" != "-h" ]] && [[ "$1" != "--help" ]] && ! [[ -z "$1" ]]; then
print_logo
fi
# Then turn it off, we don't want to print again
Expand All @@ -135,53 +135,69 @@ fi

## UI
# Print out the logo, unless specified otherwise in the config (generated using command `figlet -f pagga YEET`)
if [ "$PRINT_LOGO" == "true" ]; then
if [[ "$PRINT_LOGO" == "true" ]]; then
# Make sure the operation is not 'help', else we will be printing the logo twice
if [ "$1" != "-h" ] && [ "$1" != "--help" ] && ! [ -z "$1" ]; then
if [[ "$1" != "-h" ]] && [[ "$1" != "--help" ]] && ! [[ -z "$1" ]]; then
print_logo
fi
fi

## OPTION parsing
# Usage:
# Regex for the options:
# yeet -h - Print this help text
# yeet -U - Update all packages
# yeet -s <package search terms> - Search for a package
help_regex="(\-h|\-\-help)"
# yeet -U OR yeet -Syyu - Update all packages
# yeet -S <package name> - Install the specified package(s)
updateinstall_regex_1="\-S(U)*(y)*(u)*"
updateinstall_regex_2="\-U"
install_regex="\-S"
# yeet -s <package search terms> - Search for a package
search_regex="\-s"
# yeet -R <package name> - Remove the specified package(s)
remove_regex="\-Rs*"
# yeet -B <path to package dir> - Build an AUR package
build_regex="\-B"
# yeet -Q [options] - Run pacman -Q
pacman_regex="\-Q.*"
# yeet <package search terms> - Search for a package and install it
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ -z "$1" ]; then
operation="help"
elif [ "$1" == "-U" ] || [ "$1" == "-Syyu" ] || [ "$1" == "-Syu" ] || [ "$1" == "-SyyU" ] || [ "$1" == "-SyU" ] || [ "$1" == "-Syy" ] || [ "$1" == "-Sy" ] || [ "$1" == "-Suu" ] || [ "$1" == "-Su" ] || [ "$1" == "-SUU" ] || [ "$1" == "-SU" ]; then
# Update all packages
operation="updateinstall"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [ "$1" == "-s" ]; then
# Search for a package
operation="search"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [ "$1" == "-S" ]; then
# Install package(s)
operation="install"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [ "$1" == "-R" ]; then
# Remove package(s)
operation="remove"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [ "$1" == "-B" ]; then
# Build package
operation="build"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [ "$1" == "-Q" ] || [ "$1" == "-Q"* ]; then
# If the first option is -Q and the like (e.g., -Ql), then let pacman handle it
operation="pacman"

# First check if the first param is an option
if [[ "$1" =~ $option_regex ]]; then
# Then test the regex
if [[ "$1" =~ $help_regex ]] || [[ -z "$1" ]]; then
operation="help"
elif [[ "$1" =~ $updateinstall_regex_1 ]] || [[ "$1" =~ $updateinstall_regex_2 ]]; then
# Update all packages
operation="updateinstall"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [[ "$1" =~ $install_regex ]]; then
# Install package(s)
operation="install"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [[ "$1" =~ $search_regex ]]; then
# Search for a package
operation="search"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [[ "$1" =~ $remove_regex ]]; then
# Remove package(s)
operation="remove"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [[ "$1" =~ $build_regex ]]; then
# Build package
operation="build"
# Shift the params so the second one is the first, the third is the second and so on
shift
elif [[ "$1" =~ $pacman_regex ]]; then
# If the first option is -Q and the like (e.g., -Ql), then let pacman handle it
operation="pacman"
else
# Invalid option, show help
operation="help"
fi
else
# Search for a package and install it
operation="searchinstall"
Expand Down Expand Up @@ -209,7 +225,7 @@ function update_packages {
# First update packages from sync repos
printb "==> Updating packages from sync repos using pacman..."
# Check that the pacman binary exists
if ! [ -f "$(command -v $PACMAN_BIN)" ] ; then
if ! [[ -f "$(command -v $PACMAN_BIN)" ]] ; then
printr "ERR Command $PACMAN_BIN not found, cannot use pacman. Edit the PACMAN_BIN variable in $CONFIG_FILE_PATH and change it to the path to pacman"
exit 2
else
Expand All @@ -222,7 +238,7 @@ function update_packages {
printb "==> Updating packages from AUR..."
printb "==> Searching for packages to update..."
aur_packages_to_update=$(package-query --rsort=p -uA || echo "")
if [ -z "$aur_packages_to_update" ]; then
if [[ -z "$aur_packages_to_update" ]]; then
printw "==> Nothing to do"
else
echo "$aur_packages_to_update"
Expand Down Expand Up @@ -266,9 +282,9 @@ function install_packages {
printg "==> Installing $(package-query --rsort=p -iSA "$package")"

# If the package is in the sync repos, use pacman to install it
if [ "$use_pacman" == "1" ]; then
if [[ "$use_pacman" == "1" ]]; then
# Check that the pacman binary exists
if ! [ -f "$(command -v $PACMAN_BIN)" ] ; then
if ! [[ -f "$(command -v $PACMAN_BIN)" ]] ; then
printr "ERR Command $PACMAN_BIN not found, cannot use pacman. Edit the PACMAN_BIN variable in $CONFIG_FILE_PATH and change it to the path to pacman"
exit 2
else
Expand All @@ -282,20 +298,30 @@ function install_packages {
mkdir -p "$YEET_CACHE_DIR/build/"
cd "$YEET_CACHE_DIR/build/"
# Check if the package is already built
if [ -d "$package" ]; then
if [[ -d "$package" ]]; then
# Ask the user if they want to rebuild the package
read -p $'\e[1;33m ==> Package is already built, rebuild?\e[0m [\e[1;32my\e[0m/\e[1;31mN\e[0m] ' -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]; then
# If yes, delete the package dir and clone it again
printb "\n==> Rebuilding $package..."
rm -rf "$package"
git clone --quiet "$AUR_URL/$package.git" "$package"
else
# If not, install it with pacman -U and exit
cd "$package"
require_root $PACMAN_BIN -U *.pkg.tar.zst
printg "==> Installed $package successfully"
return $?
fi
else
# If it is not already built, clone it
git clone --quiet "$AUR_URL/$package.git" "$package"
fi

# If the package exists, the directory exists, else it does not exist
if ! [ -d "$package" ]; then
# Ideally this should not happen
if ! [[ -d "$package" ]]; then
printr "ERR Package $package not found in AUR!"
exit 1
fi
Expand All @@ -306,7 +332,7 @@ function install_packages {

if [[ $REPLY =~ ^[Yy]$ ]]; then
printb "\n==> Editing build files for $package..."
if ! [ -f "$(command -v $FILE_MANAGER)" ] ; then
if ! [[ -f "$(command -v $FILE_MANAGER)" ]] ; then
printw "WARN Command $FILE_MANAGER not found, cannot open file manager to edit build files. Edit the FILE_MANAGER variable in $CONFIG_FILE_PATH and change it to your preferred file manager"
else
$FILE_MANAGER .
Expand All @@ -315,8 +341,8 @@ function install_packages {

printg "==> Building $package..."

# Build it
makepkg -sic
# Build, install and clean up
makepkg -sfCci
fi

printg "==> Installed $package successfully"
Expand All @@ -328,28 +354,28 @@ function updateinstall_packages {
update_packages

# Then install package(s)
if ! [ -z "$*" ]; then
if ! [[ -z "$*" ]]; then
install_packages $*
fi
}
# Remove package(s)
function remove_packages {
printb "==> Removing $(join_by ', ' $*)..."
require_root pacman -Rs $*
require_root $PACMAN_BIN -Rs $*
printg "==> Removed $(join_by ', ' $*) successfully"
}
# Build a package
function build_packages {
printb "==> Building AUR package from contents of directory $1..."
# First check if the directory exists
if ! [ -d "$1" ]; then
if ! [[ -d "$1" ]]; then
printr "ERR Directory $1 does not exist!"
exit 1
fi

printb "==> Searching for PKGBUILD in directory $1..."
# Then check if there is a PKGBUILD in that directory
if ! [ -f "$1/PKGBUILD" ]; then
if ! [[ -f "$1/PKGBUILD" ]]; then
printr "ERR Could not find PKGBUILD in $1!"
exit 1
fi
Expand All @@ -365,11 +391,11 @@ function build_packages {

# Ask the user if they want to install the package
read -p $'\e[1;33m ==> Install package?\e[0m [\e[1;32my\e[0m/\e[1;31mN\e[0m] ' -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]] || [ -z "$REPLY"]; then
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z "$REPLY" ]]; then
printb "==> Installing..."

# Install the package using pacman
require_root pacman -U *.pkg.tar.zst
require_root $PACMAN_BIN -U *.pkg.tar.zst

printg "==> Package installed successfully!"
else
Expand All @@ -384,7 +410,7 @@ function searchinstall_packages {
# Ask the user what packages they want to install
read -p $'\e[1;33m==> Enter names of the packages to install, separated by spaces:\e[0m ' -r
printb
if [ -z "$REPLY" ]; then
if [[ -z "$REPLY" ]]; then
printr "==> No packages selected, aborting!"
exit 127
fi
Expand Down

0 comments on commit 55373b1

Please sign in to comment.