add ripgrep
[webi-installers/.git] / rg / rg.bash
1 # title: Ripgrep
2 # homepage: https://github.com/BurntSushi/ripgrep
3 # tagline: a modern drop-in grep replacement
4 # alias: rg
5 # description: |
6 #   'rg' is a drop-in replacement for 'grep', that respects '.gitignore' and '.ignore', has all of the sensible default options you want (colors, numbers, etc) turned on by default, is written in Rust, and simply outperforms grep in every imaginable way. R.I.P. grep.
7 # examples: |
8 #
9 #   ```bash
10 #   rg <search-term> # searches recursively, ignoing .git, node_modules, etc
11 #   ```
12 #
13 #   ```bash
14 #   rg 'function doStuff'
15 #   ```
16 #
17 #   ```bash
18 #   rg 'doStuff\(.*\)'
19 #   ```
20
21 set -e
22 set -u
23 set -o pipefail
24
25 # Use the script's first argument or the supplied WEBI_VERSION or ''
26 WEBI_VERSION=${1:-${WEBI_VERSION:-}}
27
28 # Set a temporary directory, if not already set
29 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-ripgrep.XXXXXXXX)"}
30
31 ###################
32 #  Get WEBI vars  #
33 ###################
34
35 # The WEBI bootstrap will define these
36 # but each script should be testable in its own right
37
38 if [ -z "${WEBI_PKG_URL}" ]; then
39   release_tab="${WEBI_HOST}/api/releases/ripgrep@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1"
40   WEBI_CSV=$(curl -fsSL "$release_tab" -H "User-Agent: $(uname -a)")
41   WEBI_CHANNEL=$(echo $WEBI_TAB | cut -d ',' -f 3)
42   if [ "error" == "$WEBI_CHANNEL" ]; then
43      echo "could not find release for ripgrep v${WEBI_VERSION}"
44      exit 1
45   fi
46   WEBI_VERSION=$(echo $WEBI_TAB | cut -d ',' -f 1)
47   WEBI_PKG_URL=$(echo $WEBI_TAB | cut -d ',' -f 9)
48   WEBI_PKG_FILE="$WEBI_TMP/$(echo $WEBI_PKG_URL | sed s:.*/::)"
49 fi
50
51 ###################
52 # Install ripgrep #
53 ###################
54
55 new_rg="${HOME}/.local/bin/rg"
56
57 # Test for existing version 
58 set +e
59 cur_rg="$(command -v rg)"
60 set -e
61 if [ -n "$cur_rg" ]; then
62   cur_ver=$(rg --version | head -n 1 | cut -d ' ' -f 2)
63   if [ "$cur_ver" == "$WEBI_VERSION" ]; then
64     echo "ripgrep v$WEBI_VERSION already installed at $cur_rg"
65     exit 0
66   elif [ "$cur_rg" != "$new_rg" ]; then
67     echo "WARN: possible conflict with ripgrep v$WEBI_VERSION at $cur_rg"
68   fi
69 fi
70
71 # TODO move download to the webi bootstrap
72 echo Downloading ripgrep v"${WEBI_VERSION}" from "${WEBI_PKG_URL}"
73 curl -fsSL "${WEBI_PKG_URL}" -o "${WEBI_PKG_FILE}"
74 pushd "${WEBI_TMP}" 2>&1 >/dev/null
75         echo Installing ripgrep v${WEBI_VERSION} as "$new_rg" 
76         tar xf "${WEBI_PKG_FILE}"
77         rm "${WEBI_PKG_FILE}"
78         mv ./ripgrep-*/rg "${HOME}/.local/bin/"
79 popd 2>&1 >/dev/null
80
81 ###################
82 #   Update PATH   #
83 ###################
84
85 # TODO get better output from pathman / output the path to add as return to webi bootstrap
86 pathman add "$HOME/.local/bin/"
87
88 echo "Installed 'rg'"
89 echo ""