From 7fa1faf7bf22c8d49593172b336e96d2ec2018fe Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 17 Apr 2020 02:40:52 -0700 Subject: [PATCH] bat-modules: Add working but work-in-progress code --- build.sh | 3 ++- lib/constants.sh | 1 + src/bat-modules.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index e538554..f45f31d 100755 --- a/build.sh +++ b/build.sh @@ -275,9 +275,10 @@ pp_consolidate__do() { # Inlines constants: # EXECUTABLE_BAT +# EXECUTABLE_GIT # PROGRAM_* pp_inline_constants() { - local constants=("EXECUTABLE_BAT" "PROGRAM") + local constants=("EXECUTABLE_BAT" "EXECUTABLE_GIT" "PROGRAM") # Determine the PROGRAM_ constants. local nf_constants="$( ( set -o posix ; set) | grep '^PROGRAM_' | cut -d'=' -f1)" diff --git a/lib/constants.sh b/lib/constants.sh index fa0c3b5..a5abd0f 100644 --- a/lib/constants.sh +++ b/lib/constants.sh @@ -6,6 +6,7 @@ # Issues: https://github.com/eth-p/bat-extras/issues # ----------------------------------------------------------------------------- EXECUTABLE_BAT="bat" +EXECUTABLE_GIT="git" PROGRAM="$(basename "$0" .sh)" PROGRAM_HOMEPAGE="https://github.com/eth-p/bat-extras" PROGRAM_COPYRIGHT="Copyright (C) 2019-2020 eth-p | MIT License" diff --git a/src/bat-modules.sh b/src/bat-modules.sh index 63c9231..c9ac32c 100755 --- a/src/bat-modules.sh +++ b/src/bat-modules.sh @@ -22,7 +22,10 @@ hook_version # ----------------------------------------------------------------------------- COMMON_URL_GITHUB="https://github.com/%s.git" COMMON_URL_GITLAB="https://gitlab.com/%s.git" -MODULES_FILE="$(bat --config-dir)/modules.txt" +CONFIG_DIR="$(bat --config-dir)" +SYNTAX_DIR="${CONFIG_DIR}/syntaxes" +THEME_DIR="${CONFIG_DIR}/themes" +MODULES_FILE="${CONFIG_DIR}/modules.txt" # ----------------------------------------------------------------------------- # Options: # ----------------------------------------------------------------------------- @@ -52,6 +55,8 @@ done # Ensures that the modules file at $MODULES_FILE exists. # If it doesn't, this will print a friendly warning and exit with exit code 1. +# +# This will also make sure the syntaxes and themes directories exist. ensure_setup() { if ! [[ -f "$MODULES_FILE" ]]; then printc "%{YELLOW}The bat-modules modules file wasn't found.%{CLEAR}\n" @@ -59,6 +64,9 @@ ensure_setup() { printc "%{YELLOW}read the documentation at %{CLEAR}%s%{YELLOW} for more info.%{CLEAR}\n" "${PROGRAM_HOMEPAGE}" exit 1 fi + + mkdir -p "${SYNTAX_DIR}" &>/dev/null || true + mkdir -p "${THEME_DIR}" &>/dev/null || true } # Prints an error message that parsing @@ -183,13 +191,50 @@ action:clear() { } action:update() { + CHANGES=false + dsl_on_command_commit() { - echo "$BM_SOURCE" + case "$BM_TYPE" in + syntax) cd "$SYNTAX_DIR" ;; + theme) cd "$THEME_DIR" ;; + esac + + local hash + local name="$(parse_source_name "$BM_SOURCE")" + printc "%{BLUE}----- %s: %s -----%{CLEAR}\n" "$BM_TYPE" "$name" + + # If it isn't cloned, clone it. + if ! [[ -d "$name" ]]; then + printc "%{YELLOW}Cloning...%{CLEAR}\n" + "$EXECUTABLE_GIT" clone "$BM_SOURCE" "$name" + CHANGES=true + fi + + # If it is cloned, fetch/checkout. + printc "%{YELLOW}Updating...%{CLEAR}\n" + cd "$name" + hash="$("$EXECUTABLE_GIT" rev-parse HEAD)" + "$EXECUTABLE_GIT" fetch origin --quiet + "$EXECUTABLE_GIT" checkout "$BM_OPT_CHECKOUT" --quiet + hash_new="$("$EXECUTABLE_GIT" rev-parse HEAD)" + + if [[ "$hash" != "$hash_new" ]]; then + printc "%{YELLOW}Updated to %s.%{CLEAR}\n" "$hash_new" + CHANGES=true + fi } # Parse the DSL. ensure_setup dsl_parse_file "$MODULES_FILE" + + # If there are changes, update. + printc "%{BLUE}----- bat-modules -----%{CLEAR}\n" "$BM_TYPE" "$name" + printc "%{YELLOW}Done.%{CLEAR}\n" + if "$CHANGES"; then + printc "%{YELLOW}Rebuilding cache...%{CLEAR}\n" + "$EXECUTABLE_BAT" cache --build + fi } # -----------------------------------------------------------------------------