From 8cbbdc3550be8a5331a0b35c61d11de74519a3a1 Mon Sep 17 00:00:00 2001 From: William Melody Date: Mon, 13 Apr 2020 21:39:39 -0700 Subject: [PATCH] Add scripts, update README.md and package.json. --- etc/README.md | 36 +++++++++++++++------ package.json | 4 ++- scripts/get-bash-completion-path.bash | 45 +++++++++++++++++++++++++++ scripts/install-completion.bash | 42 +++++++++++++++++++++++++ scripts/uninstall-completion.bash | 38 ++++++++++++++++++++++ 5 files changed, 155 insertions(+), 10 deletions(-) create mode 100755 scripts/get-bash-completion-path.bash create mode 100755 scripts/install-completion.bash create mode 100755 scripts/uninstall-completion.bash diff --git a/etc/README.md b/etc/README.md index 6ba4cd4..a6ae224 100644 --- a/etc/README.md +++ b/etc/README.md @@ -2,16 +2,34 @@ ## Homebrew -If you're using Homebrew, just run `brew install xwmx/taps/hosts` and the -completion scripts will be installed automatically. The extra steps to install -`hosts` completion scripts outlined below are *not needed*. +Installing via Homebrew with `brew install xwmx/taps/hosts` will also +install the completion scripts. The extra steps to install `hosts` completion +scripts outlined below are *not needed*. A one-time setup might be needed to [enable completion for all Homebrew programs](https://docs.brew.sh/Shell-Completion). -## bash +## npm -### Linux +Installing via npm should install the completion scripts. If completion +isn't working, try the instructions below. + +## Scripts + +`hosts` includes scripts for installing and uninstalling completions. + +- [install-completion.bash](../scripts/install-completion.bash) +- [uninstall-completion.bash](../scripts/uninstall-completion.bash) + +These scripts will try to determine the completion installation +locations from your environment. If completion doesn't work, you might +need to try installing manually. + +## Manual Installation + +### bash + +#### Linux On a current Linux OS (in a non-minimal installation), bash completion should be available. @@ -22,7 +40,7 @@ Place the completion script in `/etc/bash_completion.d/`: sudo curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.bash -o /etc/bash_completion.d/hosts ``` -### macOS +#### macOS If you aren't installing with homebrew, source the completion script in `.bash_profile`: @@ -34,7 +52,7 @@ then fi ``` -## zsh +### zsh Place the completion script in your `/path/to/zsh/completion` (typically `~/.zsh/completion/`): @@ -43,13 +61,13 @@ Place the completion script in your `/path/to/zsh/completion` (typically $ mkdir -p ~/.zsh/completion $ curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.zsh > ~/.zsh/completion/_hosts ``` -Include the directory in your $fpath by adding in `~/.zshrc`: +Include the directory in your `$fpath` by adding in `~/.zshrc`: ```bash fpath=(~/.zsh/completion $fpath) ``` -Make sure compinit is loaded or do it by adding in `~/.zshrc`: +Make sure `compinit` is loaded or do it by adding in `~/.zshrc`: ```bash autoload -Uz compinit && compinit -i diff --git a/package.json b/package.json index 296890a..0db51eb 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "test": "test" }, "scripts": { - "test": "bats test" + "test": "bats test", + "postinstall": "scripts/install-completion.bash", + "preuninstall": "scripts/uninstall-completion.bash" }, "repository": { "type": "git", diff --git a/scripts/get-bash-completion-path.bash b/scripts/get-bash-completion-path.bash new file mode 100755 index 0000000..9ab95cb --- /dev/null +++ b/scripts/get-bash-completion-path.bash @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +############################################################################### +# uninstall.bash +############################################################################### + +############################################################################### +# Strict Mode +############################################################################### + +set -o nounset +set -o errexit +trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR +set -o errtrace +set -o pipefail +IFS=$'\n\t' + +_get_bash_completion_path() { + local _bash_completion_path= + + if [[ -n "${BASH_COMPLETION_COMPAT_DIR:-}" ]] + then + _bash_completion_path="${BASH_COMPLETION_COMPAT_DIR}" + fi + + if [[ -z "${_bash_completion_path:-}" ]] + then + _bash_completion_path="$( + pkg-config --variable=completionsdir bash-completion 2>/dev/null || true + )" + fi + + if [[ -z "${_bash_completion_path:-}" ]] && + [[ -d "/usr/local/etc/bash_completion.d" ]] + then + _bash_completion_path="/usr/local/etc/bash_completion.d" + fi + + if [[ -z "${_bash_completion_path:-}" ]] && + [[ -d "/etc/bash_completion.d" ]] + then + _bash_completion_path="/etc/bash_completion.d" + fi + + printf "%s\\n" "${_bash_completion_path:-}" +} && _get_bash_completion_path diff --git a/scripts/install-completion.bash b/scripts/install-completion.bash new file mode 100755 index 0000000..92efdd8 --- /dev/null +++ b/scripts/install-completion.bash @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +############################################################################### +# install-completion.bash +############################################################################### + +set -o nounset +set -o errexit +trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR +set -o errtrace +set -o pipefail +IFS=$'\n\t' + +_MY_DIR="$(cd "$(dirname "$0")"; pwd)" +if [[ -z "${_MY_DIR}" ]] && [[ ! -d "${_MY_DIR}" ]] +then + exit 1 +fi + +_install_completion() { + local _bash_completion_path= + _bash_completion_path="$("${_MY_DIR}/get-bash-completion-path.bash")" + + if [[ -n "${_bash_completion_path:-}" ]] && + [[ -d "${_bash_completion_path}" ]] && + [[ ! -e "${_bash_completion_path}/notes-completion.bash" ]] + then + cp \ + "${_MY_DIR}/../etc/notes-completion.bash" \ + "${_bash_completion_path}/notes-completion.bash" + fi + + local _zsh_completion_path="/usr/local/share/zsh/site-functions" + + if [[ -d "${_zsh_completion_path}" ]] && + [[ ! -e "${_zsh_completion_path}/_notes" ]] + then + cp \ + "${_MY_DIR}/../etc/notes-completion.zsh" \ + "${_zsh_completion_path}/_notes" + fi + +} && _install_completion "$@" diff --git a/scripts/uninstall-completion.bash b/scripts/uninstall-completion.bash new file mode 100755 index 0000000..2ba5597 --- /dev/null +++ b/scripts/uninstall-completion.bash @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +############################################################################### +# uninstall-completion.bash +############################################################################### + +set -o nounset +set -o errexit +trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR +set -o errtrace +set -o pipefail +IFS=$'\n\t' + +_MY_DIR="$(cd "$(dirname "$0")"; pwd)" +if [[ -z "${_MY_DIR}" ]] && [[ ! -d "${_MY_DIR}" ]] +then + exit 1 +fi + +_uninstall_completion() { + local _bash_completion_path= + _bash_completion_path="$("${_MY_DIR}/get-bash-completion-path.bash")" + + if [[ -n "${_bash_completion_path:-}" ]] && + [[ -d "${_bash_completion_path}" ]] && + [[ -e "${_bash_completion_path}/notes-completion.bash" ]] + then + rm "${_bash_completion_path}/notes-completion.bash" + fi + + local _zsh_completion_path="/usr/local/share/zsh/site-functions" + + if [[ -d "${_zsh_completion_path}" ]] && + [[ -e "${_zsh_completion_path}/_notes" ]] + then + rm "${_zsh_completion_path}/_notes" + fi + +} && _uninstall_completion "$@"