-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsetup
More file actions
executable file
·213 lines (178 loc) · 7.21 KB
/
setup
File metadata and controls
executable file
·213 lines (178 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env bash
# vim: ft=bash
#
# Runs setup.
#
# Run "bin/setup help" for usage.
#
# —————————————————————————————————————————————————————————————————————————————————————
# NOTE: These setup scripts rely on an open source BASH framework BashMatic.
# https://github.com/kigster/bashmatic
#
# The framework is pretty light-weight, and is installed in your $HOME/.bashmatic folder.
# You can safely remove that folder after the setup if you wish, although re-running the
# setup will re-install it.
# —————————————————————————————————————————————————————————————————————————————————————
source "bin/deps"
#—————————————————————————————————————————————————————————————————————————————————————————————————————————————
# Private Functions
#—————————————————————————————————————————————————————————————————————————————————————————————————————————————
__setup.actions() {
local sep="${1}"
printf "$(array.join "${sep}" $(util.functions-matching "setup." | sed 's/ main//g'))"
}
#—————————————————————————————————————————————————————————————————————————————————————————————————————————————
# Public Functions
#—————————————————————————————————————————————————————————————————————————————————————————————————————————————
setup.rbenv() {
local rbenv_home="${HOME}/.rbenv"
local ruby_version="$(cat .ruby-version | tr -d '\n')"
if [[ -n $(command -v ruby) ]]; then
local installed_version="$(ruby -e 'puts RUBY_VERSION' | tr -d '\n')"
if [[ ${installed_version} == ${ruby_version} ]]; then
info "RUBY already installed, current version: ${bldylw}${ruby_version}"
return 0
fi
fi
[[ -d "${rbenv_home}" && -f "${rbenv_home}/bin/rbenv" ]] || {
if [[ -z ${CI} ]]; then
warning "You are on a local system without any RBENV..."
run.ui.ask "Are you sure you want to wipe your local ~/.rbenv?"
fi
warning "Recreating RBENV home, downloading rbenv..."
run "rm -rf ${rbenv_home}"
run "git clone https://github.com/rbenv/rbenv.git ${rbenv_home}"
export PATH="${rbenv_home}/bin:${rbenv_home}/shims:${PATH}"
}
export PATH="${rbenv_home}/bin:${rbenv_home}/shims:${PATH}"
[[ $(which rbenv) == "${rbenv_home}/bin/rbenv" ]] || {
error "Can't find rbenv in the PATH — which rbenv returns: $(which rbenv)"
error "PATH: ${PATH}"
return 1
}
# Set our current ruby version to the desired one, even if it's not yet there.
# This allows the next block to auto-detect it and skip the remainder.
run "rbenv global ${ruby_version} || true"
# see if we even need to install anything:
local ruby_sdk_marker="$(rbenv versions | grep "${ruby_version}" | cut -d ' ' -f 1)"
if [[ ${ruby_sdk_marker} == "*" ]]; then
info "Ruby Version ${ruby_version} is already present, and is RBENV default."
info "Skipping the rest of RBENV setup."
return 0
fi
eval "$(rbenv init -)"
run "mkdir -p ${rbenv_home}/plugins"
run "git clone https://github.com/rbenv/ruby-build.git ${rbenv_home}/plugins/ruby-build"
run "rbenv rehash"
hash -r
run "rbenv install -s ${ruby_version}"
run "rbenv global ${ruby_version}"
}
setup.gems() {
for gem in rubocop relaxed-rubocop; do
gem.install "${gem}"
done
}
setup.remove-git-hook() {
set -e
[[ -L .git/hooks/pre-commit ]] && {
info 'Removing git commit hook...'
run "rm -f .git/hooks/pre-commit"
echo
}
set +e
}
setup.git-hook() {
set -e
if [[ ! -L .git/hooks/pre-commit ]]; then
info 'Installing git pre-commit hook'
run "cd .git/hooks && ln -nfs ../../bin/linter pre-commit && cd -"
else
info: "git pre-commit hook is already installed."
fi
set +e
}
setup.os-specific() {
local os="$(uname -s | tr '[:upper:]' '[:lower:]')"
local setup_script
setup_script="./bin/setup-${os}"
if [[ -x "${setup_script}" ]]; then
set -e
source "${setup_script}"
# run it's main function
eval "setup.${os} \"$*\""
echo
if [[ -z ${CI} ]]; then
info "Please note — to print your Bazel environment run "
info "❯ ${bldylw}bin/show-env"
else
/usr/bin/env bash bin/show-env || true
fi
else
error "Operating system ${os} is not currently supported." >&2
return 1
fi
echo
return 0
}
setup.main() {
local action="$1"
local is_help=0
local code=0
[[ "${action}" == "-h" || ${action} == "--help" ]] && {
action="help"
is_help=1
}
local func="setup.${action}"
if [[ -n ${action} ]]; then
if util.is-a-function "${func}"; then
((is_help)) || h2 "Executing partial setup for action ${bldylw}${action}"
shift
${func} "$@"
code=$?
[[ ${code} -eq 0 ]] && {
((is_help)) || success "Setup for ${action} was successful"
}
[[ ${code} -ne 0 ]] && error "Error setting up ${action}"
else
error "Invalid action provided — '${action}'"
info "Valid actions are: [ ${bldylw}${action_list}$(txt-info) ]"
echo
code=1
fi
else
set +e
h2 "Installing required development dependencies for working with rules_ruby and Bazel."
setup.rbenv
setup.gems
[[ -z ${CI} ]] && setup.git-hook
setup.os-specific "$@"
fi
((is_help)) || bin/show-env
exit ${code}
}
# shellcheck disable=SC2059,SC2155,SC2154
setup.help() {
trap 'exit 0' INT
printf "
$(help-header USAGE)
${bldblk}# without any arguments runs a complete setup.${clr}
$(help-usage "bin/setup")
${bldblk}# alternatively, a sub-setup function name can be passed:${clr}
$(help-usage "bin/setup [ ${action_list} ]")
$(help-header DESCRIPTION:)
Runs full setup without any arguments.
Accepts one optional argument — one of the actions that typically run
as part of setup, with one exception — ${bldylw}remove-git-hook${clr}.
This action removes the git commit hook installed by the setup.
$(help-header EXAMPLES:)
$(help-example "bin/setup")
Or, to run only one of the sub-functions (actions), pass
it as an argument:
$(help-example "bin/setup help")
$(help-example "bin/setup remove-git-hook")
"
}
action_list="$(__setup.actions " | " | sed 's/setup\.//g')"
export action_list
bashmatic.detect-subshell || setup.main "$@"