diff --git a/Makefile b/Makefile index fa40213..a2d68d1 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,7 @@ install: scripts/make install uninstall: - scripts/make uninstall \ No newline at end of file + scripts/make uninstall + +fix: + scripts/make fix \ No newline at end of file diff --git a/docs/installation.md b/docs/installation.md index dd23230..738f589 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -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 diff --git a/scripts/brew b/scripts/brew deleted file mode 100755 index ca570b0..0000000 --- a/scripts/brew +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -##? brew formula - -export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" -source "${NAVI_HOME}/scripts/install" - -gen_formula() { - version="$(latest_version_released)" - header "version: ${version}" - - header "sha_for x86_64-unknown-linux-musl..." - sha_linux="$(sha_for_asset_on_github "$version" "x86_64-unknown-linux-musl")" - header "$sha_linux" - - header "sha_for x86_64-apple-darwin..." - sha_osx="$(sha_for_asset_on_github "$version" "x86_64-apple-darwin")" - header "$sha_osx" - - header "rb..." - curl -s https://raw.githubusercontent.com/denisidoro/homebrew-tools/master/navi.rb \ - | sed -E "s/version ['\"].*/version '${version}'/" \ - | awk '!x{x=sub("sha256","sha_osx")}7' \ - | awk '!x{x=sub("sha256","sha_linux")}7' \ - | sed -E "s/sha_osx.*/sha256 \"${sha_osx}\"/" \ - | sed -E "s/sha_linux.*/sha256 \"${sha_linux}\"/" -} - -gen_sha() { - version="${1:-$(latest_version_released)}" - header "version: ${version}" - - header "sha_for source..." - sha_for_asset_on_github "$version" -} - -cmd="$1" -shift - -case "$cmd" in - "formula") gen_formula "$@" ;; - "sha") gen_sha "$@" ;; -esac diff --git a/scripts/docker b/scripts/docker index 7b7d882..aac503f 100755 --- a/scripts/docker +++ b/scripts/docker @@ -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 "$@" \ No newline at end of file diff --git a/scripts/fix b/scripts/fix index d5d4c1a..02b6a7a 100755 --- a/scripts/fix +++ b/scripts/fix @@ -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 diff --git a/scripts/install b/scripts/install index 67988d0..e857a23 100755 --- a/scripts/install +++ b/scripts/install @@ -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"}" # ===================== @@ -78,7 +59,7 @@ log::ansi() { _log() { local template="$1" shift - echoerr -e "$(printf "$template" "$@")" + echoerr "$(printf "$template" "$@")" } _header() { @@ -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}" } @@ -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 } @@ -166,15 +147,14 @@ 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() { @@ -182,6 +162,11 @@ sha_for_asset_on_github() { 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 @@ -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 @@ -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 } diff --git a/scripts/make b/scripts/make index 787fa84..cf4a036 100755 --- a/scripts/make +++ b/scripts/make @@ -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 diff --git a/scripts/release b/scripts/release index 0fdb242..62e304e 100755 --- a/scripts/release +++ b/scripts/release @@ -14,7 +14,7 @@ is_windows() { get_env_target() { eval $(rustc --print cfg | grep target) local r raw="${target_arch}-${target_vendor}-${target_os}-${target_env}" - echoerr "env_raw: $raw" + log::note "env target raw: $raw" if echo "$raw" | grep -q "x86_64-apple-macos"; then echo "x86_64-apple-darwin" else @@ -23,21 +23,21 @@ get_env_target() { } _tap() { - echoerr "$@" + log::note "$@" "$@" } release() { - local -r cross_target="${1:-}" - echoerr "cross_target: $cross_target" + local -r env_target="$(get_env_target)" + log::note "env target: $env_target" + + local -r cross_target="${1:-"$env_target"}" + log::note "desired target: $cross_target" TAR_DIR="${NAVI_HOME}/target/tar" local use_zip=false local cross=true - local -r env_target="$(get_env_target)" - echoerr "env_target: $env_target" - if [[ $cross_target == $env_target ]]; then cross=false fi @@ -83,11 +83,10 @@ release() { _ls "${bin_path}" _ls "${TAR_DIR}" - } _ls() { - echoerr "contents from $@:" + log::note "contents from $@:" ls -la "$@" || true } diff --git a/scripts/run b/scripts/run index 93198d4..53f1238 100755 --- a/scripts/run +++ b/scripts/run @@ -4,6 +4,13 @@ set -euo pipefail export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" cd "$NAVI_HOME" -rm -rf "target/release/navi" 2> /dev/null || true -rm -rf "target/debug/navi" 2> /dev/null || true -cargo run -- "$@" + +if command_exists navi; then + navi "$@" +elif [ -f "./target/release/navi" ]; then + "./target/release/navi" "$@" +elif [ -f "./target/debug/navi" ]; then + "./target/debug/navi" "$@" +else + cargo run -- "$@" +fi diff --git a/scripts/tag b/scripts/tag index 1285df7..678da5b 100755 --- a/scripts/tag +++ b/scripts/tag @@ -5,7 +5,7 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" source "${NAVI_HOME}/scripts/install" version="${1:-$(version_from_toml)}" -echo "version: $version..." +log::note "version: $version..." sleep 2 git tag -a "v${version}" diff --git a/src/shell.rs b/src/shell.rs index 4f42316..5752f9c 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -37,9 +37,8 @@ impl ShellSpawnError { pub fn out() -> Command { let shell = CONFIG.shell(); let mut cmd = Command::new(&shell); - if shell != "cmd.exe" { - cmd.arg("-c"); - } + let arg = if shell == "cmd.exe" { "/c" } else { "-c" }; + cmd.arg(arg); cmd } diff --git a/tests/run b/tests/run index b582b71..3ab3819 100755 --- a/tests/run +++ b/tests/run @@ -91,27 +91,27 @@ _integration() { rm "$log_file" 2>/dev/null || true mv "$cheats_path" "$bak_cheats_path" 2>/dev/null || true - echoerr "Starting sessions..." + log::note "Starting sessions..." tmux new-session -d -s ci "export NAVI_TEST_PATH='${cheats_path}'; ${NAVI_HOME}/tests/run _navi |& tee '${log_file}'" sleep 5 _assert_tmux "$log_file" - echoerr "Downloading default cheatsheets..." + log::note "Downloading default cheatsheets..." tmux send-key -t ci "download default"; tmux send-key -t ci "Enter" sleep 1 _assert_tmux "$log_file" - echoerr "Confirming import..." + log::note "Confirming import..." tmux send-key -t ci "y"; tmux send-key -t ci "Enter" sleep 6 _assert_tmux "$log_file" - echoerr "Running snippet..." + log::note "Running snippet..." tmux send-key -t ci "pwd" sleep 1 tmux send-key -t ci "Enter" - echoerr "Checking paths..." + log::note "Checking paths..." sleep 2 local -r downloaded_path="$(cat "$log_file" | grep 'They are now located at' | sed 's/They are now located at //')" ls "$downloaded_path" | grep -q '^pkg_mgr__brew.cheat$'