Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active August 17, 2023 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RichardBronosky/15a67d9f5d34f002162de2a73b543bb8 to your computer and use it in GitHub Desktop.
Save RichardBronosky/15a67d9f5d34f002162de2a73b543bb8 to your computer and use it in GitHub Desktop.
A bash plugin to manage plugins

bash-bashplug

A bash plugin to manage plugins

Installation

Set up bash_plugins folder

(Skip this step if you already have a bash_plugins folder.)

{
# simply copy-pasta this whole block, or customize these vars and copy-pasta the rest
bash_plugins_dir=~/.bash_plugins
bash_startup_file=~/.bash_profile

if ! [[ -d $bash_plugins_dir ]]; then
  mkdir $bash_plugins_dir
fi
if ! grep -q '\*\.plugin\.sh' $bash_startup_file; then
  cat >> $bash_startup_file <<BASH

export BASH_PLUGINS_DIR="$bash_plugins_dir"
for plugin in \$(find \$BASH_PLUGINS_DIR -name '*.plugin.sh'); do source \$plugin; done
BASH
  export BASH_PLUGINS_DIR="$bash_plugins_dir"
fi
}

Set up this plugin

{
# simply copy-pasta this whole block
plugin_url=https://gist.github.com/15a67d9f5d34f002162de2a73b543bb8

if bashplug -vq >/dev/null 2>&1; then
  bashplug add "$plugin_url"
elif [[ -n "$BASH_PLUGINS_DIR" ]]; then
  (
    set -e
    cd "$BASH_PLUGINS_DIR"
    git clone $plugin_url
    dir_name="$(ls -rtd1 $(find * -maxdepth 0 -type d) | tail -n1)"
    plugin_name="$(basename $(find $dir_name -name '*.plugin.sh') .plugin.sh)"
    if ! [[ "$dir_name" == "$plugin_name" ]] && ! [[ -d "$plugin_name" ]]; then
      mv "$dir_name" "$plugin_name"
      dir_name="$plugin_name"
    fi
    for plugin in $(find $dir_name -name '*.plugin.sh'); do source $plugin; done
  )
else
  cat<<EOF

Could not find your BASH_PLUGINS_DIR. Did not install plugin.
Have you followed the *Set up bash_plugins folder* steps yet?

EOF
fi
}
function init_via_source(){
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local script=$(basename ${BASH_SOURCE[0]} | sed 's/\.plugin//')
source $script_dir/$script
}
function init_via_wrapper(){
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local script=$(basename $BASH_SOURCE | sed 's/\.plugin//')
local wrapper=$(basename $script .sh | sed 's/^bash-//')
alias $wrapper="$script_dir/$script"
}
init_via_wrapper
# vim: set ts=2 sts=2 sw=2 et:
#!/bin/bash -eu
namespace=_bashplug_
function _bashplug_usage(){
cat <<USAGE
$(basename $0) usage
Call with one of the functions:
$(${namespace}function_list)
USAGE
}
function _bashplug_add(){
plugin_url="$1"
if [[ -z "$plugin_url" ]]; then
cat<<ERROR
You must provide a url or path to the [plugin] repo you want to clone.
ERROR
return 1
fi
if [[ -n "$BASH_PLUGINS_DIR" ]]; then
(
set -e
cd "$BASH_PLUGINS_DIR"
git clone $plugin_url
dir_name="$(ls -rtd1 $(find * -maxdepth 0 -type d) | tail -n1)"
entrypoint="$(find $dir_name -name '*.plugin.sh')"
if [[ -z $entrypoint ]]; then
cat <<ERROR
This plugin does not seem to have an entrypoint.
Looking for a file that matches the pattern *.plugin.sh
ERROR
exit
fi
plugin_name="$(basename $entrypoint .plugin.sh)"
if ! [[ "$dir_name" == "$plugin_name" ]] && ! [[ -d "$plugin_name" ]]; then
mv "$dir_name" "$plugin_name"
dir_name="$plugin_name"
fi
for plugin in $(find $dir_name -name '*.plugin.sh'); do source $plugin; done
)
else
cat<<ERROR
Could not find your BASH_PLUGINS_DIR. Did not install plugin.
Have you followed the *Set up bash_plugins folder* steps yet?
ERROR
fi
}
function _bashplug_function_list(){
declare -F | awk '$NF ~ /'$namespace'/ {sub("'$namespace'", ""); print $NF}'
}
function main(){
func="${1:-}"
shift || true
if declare -f ${namespace}${func}>/dev/null; then
${namespace}${func} "$@"
else
${namespace}usage
fi
}
main "$@"
# vim: set ts=2 sts=2 sw=2 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment