-
Notifications
You must be signed in to change notification settings - Fork 18
/
fix.sh
executable file
·385 lines (326 loc) · 10.7 KB
/
fix.sh
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash -eu
set -o pipefail
apt_upgraded=0
update_apt() {
if [ "${apt_upgraded}" = 0 ]
then
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
apt_upgraded=1
fi
}
install_rbenv() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install rbenv || true
if ! type rbenv 2>/dev/null
then
# https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer
>&2 cat <<EOF
WARNING: seems you still have not added 'rbenv' to the load path.
# Load rbenv automatically by adding
# the following to ~/.bashrc:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
EOF
fi
else
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
fi
}
set_rbenv_env_variables() {
export PATH="${HOME}/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
}
install_ruby_build() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install ruby-build || true
else
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
fi
}
ensure_ruby_build() {
if ! type ruby-build >/dev/null 2>&1 && ! [ -d "${HOME}/.rbenv/plugins/ruby-build" ]
then
install_ruby_build
fi
}
ensure_rbenv() {
if ! type rbenv >/dev/null 2>&1 && ! [ -f "${HOME}/.rbenv/bin/rbenv" ]
then
install_rbenv
fi
set_rbenv_env_variables
ensure_ruby_build
}
latest_ruby_version() {
major_minor=${1}
# Double check that this command doesn't error out under -e
rbenv install --list >/dev/null 2>&1
# not sure why, but 'rbenv install --list' below exits with error code
# 1...after providing the same output the previous line gave when it
# exited with error code 0.
#
# https://github.com/rbenv/rbenv/issues/1441
set +e
rbenv install --list 2>/dev/null | cat | grep "^${major_minor}."
set -e
}
ensure_dev_library() {
header_file_name=${1:?header file name}
homebrew_package=${2:?homebrew package}
apt_package=${3:-${homebrew_package}}
if ! [ -f /usr/include/"${header_file_name}" ] && \
! [ -f /usr/include/x86_64-linux-gnu/"${header_file_name}" ] && \
! [ -f /usr/local/include/"${header_file_name}" ] && \
! [ -f /usr/local/opt/"${homebrew_package}"/include/"${header_file_name}" ]
then
install_package "${homebrew_package}" "${apt_package}"
fi
}
ensure_ruby_build_requirements() {
ensure_dev_library readline/readline.h readline libreadline-dev
ensure_dev_library zlib.h zlib zlib1g-dev
ensure_dev_library openssl/ssl.h openssl libssl-dev
}
ensure_latest_ruby_build_definitions() {
ensure_rbenv
git -C "$(rbenv root)"/plugins/ruby-build pull
}
# You can find out which feature versions are still supported / have
# been release here: https://www.ruby-lang.org/en/downloads/
ensure_ruby_versions() {
ensure_latest_ruby_build_definitions
# You can find out which feature versions are still supported / have
# been release here: https://www.ruby-lang.org/en/downloads/
ruby_versions="$(latest_ruby_version 2.7)"
echo "Latest Ruby versions: ${ruby_versions}"
ensure_ruby_build_requirements
for ver in $ruby_versions
do
# These CFLAGS can be retired once 2.6.7 is no longer needed :
#
# https://github.com/rbenv/ruby-build/issues/1747
# https://github.com/rbenv/ruby-build/issues/1489
# https://bugs.ruby-lang.org/issues/17777
if [ "${ver}" == 2.6.7 ]
then
CFLAGS="-Wno-error=implicit-function-declaration" rbenv install -s "${ver}"
else
rbenv install -s "${ver}"
fi
done
}
ensure_bundle() {
# Not sure why this is needed a second time, but it seems to be?
#
# https://app.circleci.com/pipelines/github/apiology/source_finder/21/workflows/88db659f-a4f4-4751-abc0-46f5929d8e58/jobs/107
set_rbenv_env_variables
bundle --version >/dev/null 2>&1 || gem install --no-document bundler
bundler_version=$(bundle --version | cut -d ' ' -f3)
bundler_version_major=$(cut -d. -f1 <<< "${bundler_version}")
bundler_version_minor=$(cut -d. -f2 <<< "${bundler_version}")
bundler_version_patch=$(cut -d. -f3 <<< "${bundler_version}")
# Version 2.1 of bundler seems to have some issues with nokogiri:
#
# https://app.asana.com/0/1107901397356088/1199504270687298
# Version 2.2.22 of bundler comes with a fix to ensure the 'bundle
# update --conservative' flag works as expected - important when
# doing a 'bundle update' on a about-to-be-published gem after
# bumping a gem version.
need_better_bundler=false
if [ "${bundler_version_major}" -lt 2 ]
then
need_better_bundler=true
elif [ "${bundler_version_major}" -eq 2 ]
then
if [ "${bundler_version_minor}" -lt 2 ]
then
need_better_bundler=true
elif [ "${bundler_version_minor}" -eq 2 ]
then
if [ "${bundler_version_patch}" -lt 22 ]
then
need_better_bundler=true
fi
fi
fi
if [ "${need_better_bundler}" = true ]
then
gem install --no-document bundler
fi
make bundle_install
# https://bundler.io/v2.0/bundle_lock.html#SUPPORTING-OTHER-PLATFORMS
#
# "If you want your bundle to support platforms other than the one
# you're running locally, you can run bundle lock --add-platform
# PLATFORM to add PLATFORM to the lockfile, force bundler to
# re-resolve and consider the new platform when picking gems, all
# without needing to have a machine that matches PLATFORM handy to
# install those platform-specific gems on.'
#
# This affects nokogiri, which will try to reinstall itself in
# Docker builds where it's already installed if this is not run.
for platform in x86_64-darwin-21 x86_64-linux x86_64-linux-musl
do
grep "${platform:?}" Gemfile.lock >/dev/null 2>&1 || bundle lock --add-platform "${platform:?}"
done
}
set_ruby_local_version() {
latest_ruby_version="$(cut -d' ' -f1 <<< "${ruby_versions}")"
echo "${latest_ruby_version}" > .ruby-version
}
latest_python_version() {
major_minor=${1}
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
pyenv install --list | grep "^ ${major_minor}." | grep -v -- -dev | tail -1 | xargs
}
install_pyenv() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install pyenv || true
if ! type pyenv 2>/dev/null
then
# https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer
>&2 cat <<EOF
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
EOF
fi
else
curl https://pyenv.run | bash
fi
}
set_pyenv_env_variables() {
# looks like pyenv scripts aren't -u clean:
#
# https://app.circleci.com/pipelines/github/apiology/cookiecutter-pypackage/15/workflows/10506069-7662-46bd-b915-2992db3f795b/jobs/15
set +u
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
set -u
}
ensure_pyenv() {
if ! type pyenv >/dev/null 2>&1 && ! [ -f "${HOME}/.pyenv/bin/pyenv" ]
then
install_pyenv
fi
if ! type pyenv >/dev/null 2>&1
then
set_pyenv_env_variables
fi
}
install_package() {
homebrew_package=${1:?homebrew package}
apt_package=${2:-${homebrew_package}}
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install "${homebrew_package}"
elif type apt-get >/dev/null 2>&1
then
update_apt
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "${apt_package}"
else
>&2 echo "Teach me how to install packages on this plaform"
exit 1
fi
}
ensure_python_build_requirements() {
ensure_dev_library zlib.h zlib zlib1g-dev
ensure_dev_library bzlib.h bzip2 libbz2-dev
ensure_dev_library openssl/ssl.h openssl libssl-dev
ensure_dev_library ffi.h libffi libffi-dev
ensure_dev_library sqlite3.h sqlite3 libsqlite3-dev
ensure_dev_library lzma.h xz liblzma-dev
ensure_dev_library readline/readline.h readline libreadline-dev
}
# You can find out which feature versions are still supported / have
# been release here: https://www.python.org/downloads/
ensure_python_versions() {
# You can find out which feature versions are still supported / have
# been release here: https://www.python.org/downloads/
python_versions="$(latest_python_version 3.10)"
echo "Latest Python versions: ${python_versions}"
ensure_python_build_requirements
for ver in $python_versions
do
if [ "$(uname)" == Darwin ]
then
if [ -z "${HOMEBREW_OPENSSL_PREFIX:-}" ]
then
HOMEBREW_OPENSSL_PREFIX="$(brew --prefix openssl)"
fi
pyenv_install() {
CFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip2/include -I${HOMEBREW_OPENSSL_PREFIX}/include" LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib -L${HOMEBREW_OPENSSL_PREFIX}/lib" pyenv install --skip-existing "$@"
}
major_minor="$(cut -d. -f1-2 <<<"${ver}")"
pyenv_install "${ver}"
else
pyenv install -s "${ver}"
fi
done
}
ensure_pyenv_virtualenvs() {
latest_python_version="$(cut -d' ' -f1 <<< "${python_versions}")"
virtualenv_name="quality-${latest_python_version}"
pyenv virtualenv "${latest_python_version}" "${virtualenv_name}" || true
# You can use this for your global stuff!
pyenv virtualenv "${latest_python_version}" mylibs || true
# shellcheck disable=SC2086
pyenv local "${virtualenv_name}" ${python_versions} mylibs
}
ensure_pip_and_wheel() {
# pip 22 seems to be better at finding pandas pre-compiled wheels
# for macOS, so let's make sure we're using at least that version
major_pip_version=$(pip --version | cut -d' ' -f2 | cut -d '.' -f 1)
if [[ major_pip_version -lt 21 ]]
then
pip install 'pip>=22'
fi
# wheel is helpful for being able to cache long package builds
pip show wheel >/dev/null 2>&1 || pip install wheel
}
ensure_python_requirements() {
make pip_install
}
ensure_shellcheck() {
if ! type shellcheck >/dev/null 2>&1
then
install_package shellcheck
fi
}
ensure_overcommit() {
# don't run if we're in the middle of a cookiecutter child project
# test, or otherwise don't have a Git repo to install hooks into...
if [ -d .git ]
then
bundle exec overcommit --install
else
>&2 echo 'Not in a git repo; not installing git hooks'
fi
}
ensure_rugged_packages_installed() {
install_package icu4c libicu-dev # needed by rugged, needed by undercover
install_package pkg-config # needed by rugged, needed by undercover
install_package cmake # needed by rugged, needed by undercover
}
ensure_rbenv
ensure_ruby_versions
set_ruby_local_version
ensure_rugged_packages_installed
ensure_bundle
ensure_pyenv
ensure_python_versions
ensure_pyenv_virtualenvs
ensure_pip_and_wheel
ensure_python_requirements
ensure_shellcheck
ensure_overcommit