From e972dd331cd2cefe4443be0f904d32692e7747bd Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Mon, 10 Nov 2025 00:12:00 -0500 Subject: [PATCH] feat: Creates run-handler script so that using find works properly for runs. --- run | 172 +++++++---------------------------------- runs/after/clipse | 4 +- runs/before/dev | 4 +- runs/utils/run-handler | 85 ++++++++++++++++++++ 4 files changed, 116 insertions(+), 149 deletions(-) create mode 100755 runs/utils/run-handler diff --git a/run b/run index ca212f1..b009adb 100755 --- a/run +++ b/run @@ -2,10 +2,10 @@ # Adapted from https://github.com/ThePrimeagen/dev/blob/master/run # -# Runs scripts in the `./runs` directory, which will install or uninstall -# packages. It either runs a single script given an argument (filename in runs) -# or all scripts in the runs directory. - +# Installs packages declared in the 'runs' directory, will run scripts matching the +# file name in the 'runs/before' and 'runs/after', respectively, to handle setting up or +# tearing down based on the run mode. +# set -e set -o nounset set -o pipefail @@ -14,109 +14,11 @@ THIS_FILE=${BASH_SOURCE[0]} LOG_LABEL=$(basename $THIS_FILE) LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} -declare dry_run grep uninstall +declare dry_run grepstr mode log() { - logging log --source "$THIS_FILE" "$@" -} - -# TODO: Remove. -run() { - local script=$1 - local flag=$2 - log "running script: $script $flag" - - local actual_flags="-S --noconfirm" - - if [[ $flag == "--uninstall" ]]; then - actual_flags="-Rns" - fi - log "ok, here's the actual script:: $s $actual_flags" - - if [[ $dry_run == "0" ]]; then - $script $actual_flags - fi -} - -after() { - local after - local file=${1:-""} - local arg=${2:-""} - - # Check for after script, and run it if found. - after=$(find "$DEV_ENV/runs/after" -mindepth 1 -maxdepth 1 -executable -name "$(basename "$file")" | head -1) - if [[ -n "$after" ]]; then - log --echo " running after script: '$after'" - [[ $dry_run == "0" ]] && source "$after" "$arg" - fi -} - -before() { - local before - local file=${1:-""} - local arg=${2:-""} - - before=$(find "$DEV_ENV/runs/before" -mindepth 1 -maxdepth 1 -executable -name "$(basename "$file")" | head -1) - if [[ -n "$before" ]]; then - log --echo " running before script: '$before'" - [[ $dry_run == "0" ]] && source "$before" "$arg" - fi -} - -install() { - local file line pkg after before - - # Ensure yay is installed before proceeding. - [[ $dry_run == "0" ]] && . "$DEV_ENV/runs/before/yay" - - file=${1:-""} - # Early out if the file is not readable. - [[ ! -r $file ]] && exit 1 - log --echo "Installing packages from: $file" - - before "$file" install - - # Loop over lines in the file and install the packages. - while read -r line; do - # Skip lines that begin with '#' (comments) - if [[ ! $line =~ ^# ]]; then - # Remove any inline comments. - pkg=${line%% \#*} - if [[ -n $pkg ]]; then - log --echo " pkg: '$pkg'" - [[ $dry_run == "0" ]] && yay -S --noconfirm --needed "$pkg" - fi - fi - done <"$file" - - after "$file" install -} - -uninstall() { - local file line pkg - - file=${1:-""} - # Early out if the file is not readable. - [[ ! -r $file ]] && exit 1 - - log --echo "Uninstalling packages from: $file" - - before "$file" uninstall - - # Loop over lines in the file and uninstall the packages. - while read -r line; do - # Skip lines that begin with '#' (comments) - if [[ ! $line =~ ^# ]]; then - # Remove any inline comments. - pkg=${line%% \#*} - log --echo " pkg: '$pkg'" - [[ $dry_run == "0" ]] && yay -Rns "$pkg" - fi - done <"$file" - - after "$file" uninstall - -} + logging log --source "$THIS_FILE" --echo "$@" +} && export -f log ############################## MAIN ############################## @@ -124,57 +26,35 @@ uninstall() { source "$SCRIPTS/hypr/logging" setup-logging "$LOG_FILE" "$LOG_LABEL" -if [ -z "$DEV_ENV" ]; then - log --error "env var DEV_ENV needs to be present" && exit 1 -fi +[ -z "$DEV_ENV" ] && + log --error "env var DEV_ENV needs to be present" && + exit 1 -# if i just did DEV_ENV=$(pwd) ./run then this is needed for the rest of the -# scripts -export DEV_ENV="$DEV_ENV" - -grep="" +grepstr="" dry_run="0" -uninstall="0" +mode="install" while [[ $# -gt 0 ]]; do - echo "ARG: \"$1\"" - - # TODO: Fix some of these arguments, should we add a '--grep' option, should '[un]install' be args / not options? - # Handle a --dry or --dry-run argument if [[ "$1" =~ ^--dry ]]; then dry_run="1" - # Handle an --uninstall argument - elif [[ "$1" =~ ^--u ]]; then - uninstall="1" - # Handle an --install argument (default) - elif [[ ! "$1" =~ ^--i ]]; then - grep="$1" - else - grep="$1" + elif [[ "$1" == "uninstall" ]]; then + mode="uninstall" + yayflags=("-Rns") + elif [[ ! "$1" == "install" ]]; then + grepstr="$1" fi shift done +export DEV_ENV="$DEV_ENV" export LOG_ENABLE_DRY_RUN="$dry_run" +export RUN_GREP_STR="$grepstr" +export RUN_MODE="$mode" -log --echo "RUN: -- grep: '$grep'" -runs_dir=$(find $DEV_ENV/runs -mindepth 1 -maxdepth 1 -type f) # TODO: keep +log "RUN: -- grep: '$grepstr'" -#runs_dir=$(find $DEV_ENV/runs -mindepth 1 -maxdepth 1 -executable) # TODO: remove. - -for s in $runs_dir; do - if basename $s | grep -vq "$grep"; then - log "grep \"$grep\" filtered out $s" - continue - fi - - if [[ $uninstall == "1" ]]; then - # TODO: Use function instead. - # run $s --uninstall - uninstall "$s" - else - # TODO: Use function instead. - # run $s --install - install "$s" - fi -done +find "$DEV_ENV/runs" \ + -mindepth 1 \ + -maxdepth 1 \ + -type f \ + -exec bash -xc "$DEV_ENV/runs/utils/run-handler {}" \; diff --git a/runs/after/clipse b/runs/after/clipse index ee177a5..6a51121 100755 --- a/runs/after/clipse +++ b/runs/after/clipse @@ -3,11 +3,13 @@ XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share} install() { - mkdir -p "$XDG_DATA_HOME/clipse" + log " Ensure clipse data directory exists, and start clipse." + mkdir -p "$XDG_DATA_HOME/clipse" &>/dev/null setsid uwsm app -- clipse -listen } uninstall() { + log " Removing clipse data directory." rm -rf "$XDG_DATA_HOME/clipse" } diff --git a/runs/before/dev b/runs/before/dev index e1e6380..ef8c3d7 100755 --- a/runs/before/dev +++ b/runs/before/dev @@ -1,4 +1,4 @@ #!/usr/bin/env bash -yay -S --noconfirm --needed rustup -rustup default stable +# yay -S --noconfirm --needed rustup +# rustup default stable diff --git a/runs/utils/run-handler b/runs/utils/run-handler new file mode 100755 index 0000000..196eb19 --- /dev/null +++ b/runs/utils/run-handler @@ -0,0 +1,85 @@ +#!/usr/bin/env bash + +set -e +set -o nounset +set -o pipefail + +declare file +declare -a yayflags + +THIS_FILE=${BASH_SOURCE[0]} +LOG_LABEL=${LOG_LABEL:-$(basename $THIS_FILE)} +LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} +DEV_ENV=${DEV_ENV:-""} + +MODE=${RUN_MODE:-"install"} +DRY_RUN=${LOG_ENABLE_DRY_RUN:-"0"} +GREP_STR=${RUN_GREP_STR:-""} + +log() { + logging log --source "$THIS_FILE" --echo "$@" +} && export -f log + +run_before_or_after() { + local dir file run + dir=${1:-""} + run=${2:-""} + + file=$(find "$dir" -mindepth 1 -maxdepth 1 -executable -name "$(basename "$run")" | head -1) + if [[ -n "$file" ]]; then + log " running $(basename "$dir") script: '$file'" + [[ $DRY_RUN == "0" ]] && source "$file" "$mode" + fi +} + +main() { + local line pkg + + log "RUN: file: $file" + + if [[ -z $GREP_STR ]] || [[ $file =~ $GREP_STR ]]; then + + log "${MODE}ing packages from: $file" + + run_before_or_after "$DEV_ENV/runs/before" "$file" + + # Loop over lines in the file and install the packages. + while IFS= read -r line; do + # Skip lines that begin with '#' (comments) + if [[ ! $line =~ ^# ]]; then + # Remove any inline comments. + pkg=${line%% \#*} + if [[ -n $pkg ]]; then + log " pkg: '$pkg'" + [[ $DRY_RUN == "0" ]] && yay "${yayflags[@]}" "$pkg" + fi + fi + done <"$file" + + run_before_or_after "$DEV_ENV/runs/after" "$file" + else + log "Grep filtered out: '$file'" + fi +} + +############################## MAIN ############################## + +# Setup logging file and label. +source "$SCRIPTS/hypr/logging" +setup-logging "$LOG_FILE" "$LOG_LABEL" + +file=${1:-""} +yayflags=("-S" "--noconfirm" "--needed") + +[[ -z "$DEV_ENV" ]] && + log --error "env var DEV_ENV needs to be present" && exit 1 +[[ ! -r $file ]] && + log --error "File not readable." && exit 1 + +# Change yay flags if mode is 'uninstall' +[[ $MODE == "uninstall" ]] && yayflags=("-Rns") + +# Ensure yay is installed before proceeding. +[[ $DRY_RUN == "0" ]] && . "$DEV_ENV/runs/before/yay" + +main