Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor scripts #527

Merged
merged 15 commits into from
Apr 19, 2021
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ install:
scripts/make install

uninstall:
scripts/make uninstall
scripts/make uninstall

fix:
scripts/make fix
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cargo install navi
bash <(curl -sL https://raw.githubusercontent.com/denisidoro/navi/master/scripts/install)

# (optional) to set directories:
# SOURCE_DIR=/opt/navi BIN_DIR=/usr/local/bin bash <(curl -sL https://raw.githubusercontent.com/denisidoro/navi/master/scripts/install)
# BIN_DIR=/usr/local/bin bash <(curl -sL https://raw.githubusercontent.com/denisidoro/navi/master/scripts/install)
```

#### Downloading pre-compiled binaries
Expand Down
44 changes: 0 additions & 44 deletions scripts/brew

This file was deleted.

33 changes: 25 additions & 8 deletions scripts/docker
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ set -euo pipefail

export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"

cd "$NAVI_HOME"
_start() {
cd "$NAVI_HOME"

./scripts/release x86_64-unknown-linux-musl
./scripts/release x86_64-unknown-linux-musl

docker run \
-e HOMEBREW_NO_AUTO_UPDATE=1 \
-e HOMEBREW_NO_INSTALL_CLEANUP=1 \
-v "$(pwd):/navi" \
-it 'bashell/alpine-bash' \
bash -c 'apk add git; apk add curl; git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && yes | ln -s /navi/target/debug/navi /usr/local/bin/navi; source $HOME/.bashrc; bash'
docker run \
-e HOMEBREW_NO_AUTO_UPDATE=1 \
-e HOMEBREW_NO_INSTALL_CLEANUP=1 \
-v "$(pwd):/navi" \
-it 'bashell/alpine-bash' \
bash -c '/navi/scripts docker setup; exec bash'
}

_setup() {
apk add git
apk add curl
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
ln -s /navi/target/debug/navi /usr/local/bin/navi
}

main() {
local -r fn="$1"
shift || true
"_${fn}" "$@"
}

main "$@"
22 changes: 17 additions & 5 deletions scripts/fix
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ set -euo pipefail
export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/scripts/install"

_commit() {
if [ -n "${DOTFILES:-}" ]; then
git add --all || true
dot git commit am || true
fi
}

cd "$NAVI_HOME"

header "cargo clippy fix..."
_commit
log::note "cargo clippy fix..."
cargo +nightly clippy --fix -Z unstable-options || true

header "cargo fix..."
_commit
log::note "cargo fix..."
cargo fix || true

header "cargo fmt..."
_commit
log::note "cargo fmt..."
cargo fmt || true

header "clippy..."
_commit
log::note "clippy..."
cargo clippy || true

header "dot code beautify..."
_commit
log::note "dot code beautify..."
find scripts -type f | xargs -I% dot code beautify % || true
dot code beautify "${NAVI_HOME}/tests/core.bash" || true
dot code beautify "${NAVI_HOME}/tests/run" || true
119 changes: 56 additions & 63 deletions scripts/install
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail

if ${X_MODE:-false}; then
set -x
fi

# =====================
# paths
# =====================

get_dir() {
local -r first_dir="$1"
local -r second_dir="$2"
local -r useless_folder="${first_dir}/useless"
local folder
mkdir -p "$useless_folder" 2>/dev/null \
&& folder="$first_dir" \
|| folder="$second_dir"
rm -r "$useless_folder" 2>/dev/null
echo "$folder"
}

get_source_dir() {
get_dir "/opt/navi" "${HOME}/.navi/src"
}

get_bin_dir() {
get_dir "/usr/bin" "/usr/local/bin"
}

get_tmp_dir() {
get_dir "/tmp/navi" "${HOME}/.navi/tmp"
}
export BIN_DIR="${BIN_DIR:-"${HOME}/.cargo/bin"}"


# =====================
Expand Down Expand Up @@ -78,7 +59,7 @@ log::ansi() {
_log() {
local template="$1"
shift
echoerr -e "$(printf "$template" "$@")"
echoerr "$(printf "$template" "$@")"
}

_header() {
Expand Down Expand Up @@ -110,19 +91,19 @@ die() {
}

no_binary_warning() {
echoerr "There's no precompiled binary for your platform: $(uname -a)"
log::note "There's no precompiled binary for your platform: $(uname -a)"
}

installation_finish_instructions() {
local -r shell="$(get_shell)"
echoerr -e "Finished. To call navi, restart your shell or reload the config file:\n source ~/.${shell}rc"
log::note -e "Finished. To call navi, restart your shell or reload the config file:\n source ~/.${shell}rc"
local code
if [[ "$shell" == "zsh" ]]; then
code="navi widget ${shell} | source"
else
code='source <(navi widget '"$shell"')'
fi
echoerr -e "\nTo add the Ctrl-G keybinding, add the following to ~/.${shell}rc:\n ${code}"
log::note -e "\nTo add the Ctrl-G keybinding, add the following to ~/.${shell}rc:\n ${code}"
}


Expand All @@ -138,7 +119,7 @@ sha256() {
elif command_exists openssl; then
openssl dgst -sha256
else
echoerr "Unable to calculate sha256!"
log::note "Unable to calculate sha256!"
exit 43
fi
}
Expand Down Expand Up @@ -166,22 +147,26 @@ asset_url() {
}

download_asset() {
local -r tmp_dir="$(mktemp -d -t navi-install-XXXX)"
local -r url="$(asset_url "$@")"
mkdir -p "$TMP_DIR"
cd "$TMP_DIR"
rm -f navi.tar.gz
echoerr "Downloading ${url}..."
log::note "Downloading ${url}..."
cd "$tmp_dir"
curl -L "$url" -o navi.tar.gz
tar xvzf navi.tar.gz
rm -f navi.tar.gz
cp "${TMP_DIR}/navi" "${BIN_DIR}/navi"
mkdir -p "${BIN_DIR}" &>/dev/null || true
mv "./navi" "${BIN_DIR}/navi"
}

sha_for_asset_on_github() {
local -r url="$(asset_url "$@")"
curl -sL "$url" | sha256 | awk '{print $1}'
}

error_installing() {
log::error "Unable to install navi. Please check https://github.com/denisidoro/navi for alternative installation instructions"
exit 33
}


# =====================
# code
Expand All @@ -208,16 +193,13 @@ command_exists() {
get_target() {
local -r unamea="$(uname -a)"
local -r archi="$(uname -sm)"
local is_android

[[ $unamea = *Android* ]] && is_android=true || is_android=false

local target
case "$archi" in
Darwin*) target="x86_64-apple-darwin" ;;
*x86*) $is_android && target="" || target="x86_64-unknown-linux-musl" ;;
*aarch*) $is_android && target="aarch64-linux-android" || target="armv7-unknown-linux-musleabihf" ;;
*arm*) $is_android && target="armv7-linux-androideabi" || target="armv7-unknown-linux-musleabihf" ;;
case "$unamea $archi" in
*arwin*) target="x86_64-apple-darwin" ;;
*inux*x86*) target="x86_64-unknown-linux-musl" ;;
*ndroid*aarch*|*ndroid*arm*) target="aarch64-linux-android" ;;
*inux*aarch*|*inux*arm*) target="armv7-unknown-linux-musleabihf" ;;
*) target="" ;;
esac

Expand All @@ -234,35 +216,46 @@ get_shell() {
# =====================

install_navi() {
export SRC_DIR="${SRC_DIR:-"$(get_source_dir)"}"
export BIN_DIR="${BIN_DIR:-"$(get_bin_dir)"}"
export TMP_DIR="${TMP_DIR:-"$(get_tmp_dir)"}"
echoerr -e "Relevant directories:\n- src: ${SRC_DIR}\n- bin: ${BIN_DIR}\n- tmp: ${TMP_DIR}\n"

local -r target="$(get_target)"

if [[ -n "$target" ]]; then
local -r version="$(latest_version_released)"
download_asset "$version" "$target"
if command_exists navi; then
log::success "navi is already installed"
exit 0

elif command_exists brew; then
brew install navi

elif command_exists cargo; then
no_binary_warning
echoerr "Building sources..."
git clone https://github.com/denisidoro/navi "$SRC_DIR"
cd "$SRC_DIR"
make install
cargo install navi

elif [[ -n "$target" ]]; then
local -r version="$(latest_version_released)"
download_asset "$version" "$target" || error_installing

else
no_binary_warning
echoerr "You don't have the necessary tools to build it"
echoerr "Please open an issue at https://github.com/denisidoro/navi"
echoerr "Aborting..."
exit 33
error_installing

fi

local -r shell="$(get_shell)"
echoerr -e "Finished. To call navi, restart your shell or reload the config file:\n source ~/.${shell}rc"
echoerr -e "\nTo add the Ctrl-G keybinding, add the following to ~/.${shell}rc:\n source \"$(navi widget ${shell})\""
echoerr
log::success "Finished"
log::success "navi is now available at ${BIN_DIR}/navi"
echoerr

if echo "$PATH" | grep -q "$BIN_DIR"; then
:
else
echoerr
log::note 'Please add the following to your .bashrc-like file:'
echo -e ' export PATH="${PATH}:'"$BIN_DIR"'"\n'
fi

log::note "To call navi, restart your shell or reload your .bashrc-like config file"
echo
log::note "Check https://github.com/denisidoro/navi for more info"

export PATH="${PATH}:${BIN_DIR}"

return 0
}

Expand Down
14 changes: 9 additions & 5 deletions scripts/make
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/scripts/install"

install() {
export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}"
export BIN_DIR="${BIN_DIR:-"$(get_bin_dir)"}"
echoerr "The final binary will be available at ${BIN_DIR}/navi..."
"${NAVI_HOME}/scripts/action" release
ln -s "${NAVI_HOME}/target/tar/navi" "${BIN_DIR}/navi"
}

uninstall() {
export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}"
rm -rf "${NAVI_HOME}/target"
rm "$SOURCE_DIR"
rm -f "${BIN_DIR}/navi"
}

fix() {
"${NAVI_HOME}/scripts/fix"
}

cmd="$1"
shift

export X_MODE=true
set -x

case "$cmd" in
"install") install "$@" ;;
"uninstall") uninstall "$@" ;;
"fix") fix "$@" ;;
esac
Loading