|
1 | 1 | #!/usr/bin/env bash |
2 | | - |
3 | | -print_in_color() { |
4 | | - local color="$1" |
5 | | - shift |
6 | | - if [[ -z ${NO_COLOR+x} ]]; then |
7 | | - printf "$color%b\e[0m\n" "$*" |
8 | | - else |
9 | | - printf "%b\n" "$*" |
10 | | - fi |
11 | | -} |
12 | | - |
13 | | -red() { print_in_color "\e[31m" "$*"; } |
14 | | -green() { print_in_color "\e[32m" "$*"; } |
15 | | -green_bold() { print_in_color "\e[1;32m" "$*"; } |
16 | | -blue() { print_in_color "\e[34m" "$*"; } |
17 | | - |
18 | | -section() { |
19 | | - printf "\n=== %s\n" "$(green_bold "$@")" |
20 | | -} |
21 | | - |
22 | | -sudo_copy() { |
23 | | - printf "%s => %s\n" "$(blue "$(printf '%-25s' "$1")")" "$2" |
24 | | - |
25 | | - if [[ -n "$3" ]]; then |
26 | | - $sudo install -m "$3" "$1" "$2" |
27 | | - else |
28 | | - $sudo install "$1" "$2" |
29 | | - fi |
30 | | -} |
31 | | - |
32 | | -copy_executable() { |
33 | | - sudo_copy "$1" "/usr/local/bin/" "755" |
34 | | -} |
35 | | - |
36 | | -copy_man() { |
37 | | - if [[ ! -d "/usr/local/share/man/man1/" ]]; then return; fi |
38 | | - |
39 | | - for file in "$1"/*.1; do |
40 | | - sudo_copy "$file" "/usr/local/share/man/man1/" |
41 | | - done |
42 | | - |
43 | | - if command_exist makewhatis; then |
44 | | - $sudo makewhatis /usr/local/man |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +prepare_installer() { |
| 5 | + echo "Initializing installer..." |
| 6 | + SSI_VERSION="0.1.5" |
| 7 | + |
| 8 | + # Set up tmp dir where all work is done |
| 9 | + tmpdir="$(mktemp -d)" |
| 10 | + trap 'rm -rf "$tmpdir"' EXIT |
| 11 | + cd "$tmpdir" |
| 12 | + export PATH="$tmpdir:$PATH" |
| 13 | + |
| 14 | + # Download ssi if needed |
| 15 | + if ! command -v ssi >/dev/null 2>&1 || [ "$(ssi --version 2>/dev/null)" != "$SSI_VERSION" ]; then |
| 16 | + if command -v curl >/dev/null 2>&1; then |
| 17 | + curl -fSsL https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi -o ssi |
| 18 | + elif command -v wget >/dev/null 2>&1; then |
| 19 | + wget -nv -O ssi https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi |
| 20 | + else |
| 21 | + echo "Error: please install wget or curl, then try again" >&2 |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + chmod +x ssi |
45 | 25 | fi |
46 | 26 | } |
47 | 27 |
|
48 | | -install_completions() { |
49 | | - cmd="$1" |
50 | | - dir="$2" |
51 | | - |
52 | | - if [[ -d "/usr/share/bash-completion/completions" ]]; then |
53 | | - compdir="/usr/share/bash-completion/completions" |
54 | | - elif [[ -d "/usr/local/etc/bash_completion.d" ]]; then |
55 | | - compdir="/usr/local/etc/bash_completion.d" |
56 | | - elif command -v brew &>/dev/null && [[ -d "$(brew --prefix)/etc/bash_completion.d" ]]; then |
57 | | - compdir="$(brew --prefix)/etc/bash_completion.d" |
58 | | - else |
59 | | - compdir='' |
60 | | - fi |
61 | | - |
62 | | - if [[ -n $compdir ]]; then |
63 | | - printf "copying to %s\n" "$compdir" |
64 | | - echo "$cmd" | $sudo tee "${compdir}/${dir}" >/dev/null |
65 | | - else |
66 | | - printf "%s %s\n" "$(red WARN)" "skipping completions installation, compdir not found" |
67 | | - echo " install it manually by adding this to your startup script:" |
68 | | - echo " $cmd" |
69 | | - fi |
70 | | -} |
71 | | - |
72 | | -command_exist() { |
73 | | - [[ -x "$(command -v "$1")" ]] |
74 | | -} |
75 | | - |
76 | | -need() { |
77 | | - command_exist "$1" || fail "Cannot run $1" |
78 | | - printf "%s found\n" "$(blue " $1")" |
79 | | -} |
80 | | - |
81 | | -onerror() { |
82 | | - local exit_status=$? |
83 | | - printf "\n=== %s\n" "$(red "Aborted with exit status $exit_status")" |
84 | | - exit $exit_status |
85 | | -} |
86 | | - |
87 | | -fail() { |
88 | | - printf "$(red 'ERR') %s\n" "$*" |
89 | | - return 1 |
90 | | -} |
91 | | - |
92 | | -initialize() { |
93 | | - set -e |
94 | | - trap 'onerror' ERR |
95 | | - |
96 | | - # Figure out if we need sudo or not |
97 | | - sudo='' |
98 | | - if [[ $EUID -ne 0 ]]; then |
99 | | - sudo='sudo' |
100 | | - fi |
101 | | - |
102 | | - repo="$1" |
103 | | - repo_url="https://github.com/${repo}.git" |
104 | | - pushd "$(mktemp -d)" >/dev/null |
105 | | -} |
106 | | - |
107 | | -section "Initializing" |
108 | | -initialize "DannyBen/rush" |
109 | | - |
110 | | -section "Checking for pre-requisites" |
111 | | -need git |
| 28 | +prepare_installer |
112 | 29 |
|
113 | | -section "Cloning $repo" |
114 | | -git clone --depth 1 "$repo_url" . |
| 30 | +if ! command -v git >/dev/null 2>&1; then |
| 31 | + echo "Error: please install git and try again" >&2 |
| 32 | + exit 1 |
| 33 | +fi |
115 | 34 |
|
116 | | -section "Copying files" |
117 | | -copy_executable 'rush' |
118 | | -copy_man 'doc' |
| 35 | +ssi log info Installing rush |
| 36 | +ssi log debug Cloning github repo |
| 37 | +git clone -q --depth 1 https://github.com/DannyBen/rush.git rush |
| 38 | +ssi install bin rush/rush |
| 39 | +ssi install man rush/doc |
| 40 | +rush completions | ssi install completion - --name rush |
119 | 41 |
|
120 | | -section "Installing bash completions" |
121 | | -install_completions "eval \"\$(rush completions)\"" "rush" |
| 42 | +if command -v rush >/dev/null 2>&1; then |
| 43 | + ssi log info "rush --version : $(rush --version)" |
| 44 | +else |
| 45 | + ssi log warn "rush not found on PATH after install" |
| 46 | +fi |
122 | 47 |
|
123 | | -section "Done" |
124 | | -rush --version |
| 48 | +ssi log debug Installation complete |
0 commit comments