-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.sh
executable file
·267 lines (216 loc) · 7.83 KB
/
bootstrap.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
#!/usr/bin/env bash
# bootstrap.sh: customize python-chrisapp-template with project details
#
# WARNING: This script is for advanced users only! Do not proceed
# unless you understand what this does. New developers would find
# it easier to use python-chrisapp-template as is. Simply ignore
# and optionally delete this file.
# ========================================
# CONFIGURATION
# ========================================
# ----------------------------------------
# STEP 1. Change these values to your liking.
# ----------------------------------------
PLUGIN_NAME="$(basename $(dirname $(realpath $0)))" # name of current directory
PLUGIN_TITLE='My ChRIS Plugin'
SCRIPT_NAME='commandname'
DESCRIPTION='A ChRIS plugin to do something awesome'
ORGANIZATION='FNNDSC'
EMAIL='[email protected]'
# Github Actions: automatically test and build your code.
# https://github.com/FNNDSC/python-chrisapp-template/wiki/Continuous-Integration
#
# These options will fail unless your Github settings are preconfigured.
# Repositories under github.com/FNNDSC are preconfigured, so these defaults might work.
# Please review the file .github/workflows/ci.yml before you push it.
# Automatically test on Github Actions each time you run `git push`
# If the value is "no" then tests are not performed. There are no side effects.
ENABLE_ACTIONS_TEST=yes
# Automatically build images on Github Actions each time you run `git push`,
# and also publish to https://chrisstore.co each time you run `git push --tags`
# If the value is "no" then builds will not be automated.
ENABLE_ACTIONS_BUILD=yes
# WARNING: the default configuration in .github/workflows/ci.yml is to allow for
# the build to proceed regardless of whether tests pass. To modify this behavior
# and other advanced features (such as multi-architecture builds such as arm64, ppc64le)
# you must edit .github/workflows/ci.yml by hand.
# ----------------------------------------
# STEP 2. Uncomment the line where it says READY=yes
# ----------------------------------------
#READY=yes
# ----------------------------------------
# STEP 3. Run: ./bootstrap.sh
# ----------------------------------------
if [ "$(uname -o 2> /dev/null)" != 'GNU/Linux' ]; then
>&2 echo "error: this script only works on GNU/Linux."
fi
if ! [ "$READY" = 'yes' ]; then
>&2 echo "error: you are not READY."
exit 1
fi
cd $(dirname "$0")
# ========================================
# VALIDATE INPUT
# ========================================
function contains_invalid_characters () {
[[ "$1" = *"/"* ]]
}
# given a variable name, exit if the variable's value contains invalid characters.
function check_variable_value_valid () {
local varname="$1"
local varvalue="${!varname}"
if contains_invalid_characters "$varvalue"; then
>&2 echo "error: invalid characters in $varname=$varvalue"
exit 1
fi
}
# may not contain '/'
check_variable_value_valid PLUGIN_NAME
check_variable_value_valid SCRIPT_NAME
check_variable_value_valid ORGANIZATION
check_variable_value_valid EMAIL
# ========================================
# COMMIT THE USER-SET CONFIG
# ========================================
# print command to run before running it
function verb () {
set -x
"$@"
{ set +x; } 2> /dev/null
}
# fail on error
set -e
set -o pipefail
verb git commit -m 'Configure python-chrisapp-template/bootstrap.sh' -- "$0"
# ========================================
# REPLACE VALUES
# ========================================
# execute sed on all files in project, excluding hidden paths and venv/
function replace_in_all () {
if [ -z "$2" ]; then
return
fi
find . -type f \
-not -path '*/\.*/*' -not -path '*/\venv/*' -not -name 'bootstrap.sh' \
-exec sed -i -e "s/$1/$2/g" '{}' \;
}
replace_in_all commandname "$SCRIPT_NAME"
replace_in_all pl-appname "$PLUGIN_NAME"
replace_in_all '[email protected]' "$EMAIL"
replace_in_all FNNDSC "$ORGANIZATION"
# .github/
if [ "${ENABLE_ACTIONS_TEST,,}" = 'yes' ]; then
sed -i -e '/delete this line to enable automatic testing/d' .github/workflows/ci.yml
fi
if [ "${ENABLE_ACTIONS_BUILD,,}" = 'yes' ]; then
sed -i -e '/delete this line and uncomment the line below to enable automatic builds/d' .github/workflows/ci.yml
sed -i -e 's/# *if: github\.event_name/if: github\.event_name/' .github/workflows/ci.yml
fi
# replace "/" with "\/" in string
function escape_slashes () {
sed 's/\//\\&/g' <<< "$@"
}
escaped_description="$(escape_slashes "$DESCRIPTION")"
escaped_title="$(escape_slashes "$PLUGIN_TITLE")"
# README.md
temp_file=$(mktemp)
sed -e'/^# ChRIS Plugin Title$/'\{ -e:1 -en\;b1 -e\} -ed README.md \
| sed "s/^# ChRIS Plugin Title\$/# $escaped_title/" \
| sed '/^END README TEMPLATE -->$/d' \
| sed "s/fnndsc/${ORGANIZATION,,}/g" \
| sed "s/app\\.py/$SCRIPT_NAME.py/g" \
> $temp_file
mv $temp_file README.md
# Dockerfile
sed "s#ARG SRCDIR=/usr/local/src/app#ARG SRCDIR=/usr/local/src/$PLUGIN_NAME#" Dockerfile \
| sed "s/org\.opencontainers\.image\.title=\"ChRIS Plugin Title\"/org.opencontainers.image.title=\"$escaped_title\"/" \
| sed "s/org\.opencontainers\.image\.description=\"A ChRIS plugin that\.\.\.\"/org.opencontainers.image.description=\"$escaped_description\"/" \
> $temp_file
mv $temp_file Dockerfile
# setup.py
function guess_https_url () {
local origin="$(git remote get-url origin)"
local https_url="$origin"
if [[ "$https_url" = "git@"* ]]; then
# convert SSH url to HTTPS url by
# 1. change last ':' to '/'
# 2. replace leading 'git@' with 'https://'
https_url="$(
echo "$https_url" \
| sed 's#\(.*\):#\1/#' \
| sed 's#^git@#https://#'
)"
fi
echo "${https_url:0:-4}" # remove trailing ".git"
}
appname_without_prefix="$(sed -E 's/(pl|dbg|ep)-//' <<< "$PLUGIN_NAME")"
sed "s/name='.*'/name='$appname_without_prefix'/" setup.py \
| sed "s/description='.*'/description='$escaped_description'/" \
| sed "s/py_modules=\['app'\]/py_modules=['$SCRIPT_NAME']/" \
| sed "s/app:main/$SCRIPT_NAME:main/" \
| sed "s#url='.*'#url='$(guess_https_url)'#" \
| sed "s/app\.py/$SCRIPT_NAME.py/" \
> $temp_file
mv $temp_file setup.py
# app.py
# FIGlet over HTTPS, since it's probably not installed locally
function figlet_wrapper () {
curl -fsSG 'https://figlet.chrisproject.org/' --data-urlencode "message=$*" \
| grep -v '^[[:space:]]*$'
}
function inject_figleted_title () {
python << EOF
for line in open('app.py'):
if line == 'ChRIS Plugin Template Title\n':
print(r"""$1""")
else:
print(line, end='')
EOF
}
figleted_title="$(figlet_wrapper "$PLUGIN_NAME")"
echo "$figleted_title"
inject_figleted_title "$figleted_title" \
| sed "s/title='My ChRIS plugin'/title='$escaped_title'/" \
| sed "s/description='cli description'/description='$escaped_description'/" \
> "$SCRIPT_NAME.py"
rm app.py
# tests/
for test_file in tests/*.py; do
sed "s/from app import/from $SCRIPT_NAME import/" $test_file > $temp_file
mv $temp_file $test_file
done
# ========================================
# SETUP
# ========================================
if ! [ -e venv ]; then
verb python -m venv venv
fi
>&2 echo + source venv/bin/activate
source venv/bin/activate
verb pip install -r requirements.txt
verb pip install -e '.[dev]'
if [ -z "$TERM" ]; then
tput=tput
else
tput=true
fi
$tput bold
>&2 printf '\n%s\n\n' '✨Done!✨'
$tput sgr0
$tput setaf 3
>&2 echo 'To undo these actions and start over, run:'
>&2 printf '\n\t%s\n\t%s\n\t%s\n\t%s\n\n' \
'git reset --hard' \
'git clean -df' \
'rm -rf venv *.egg-info' \
"git reset 'HEAD^'"
$tput setaf 6
>&2 echo 'Activate the Python virtual environment by running:'
>&2 printf '\n\t%s\n\n' 'source venv/bin/activate'
>&2 echo 'Save these changes by running:'
>&2 printf '\n\t%s\n\n' 'git add -A && git commit -m "Run bootstrap.sh"'
$tput setaf 2
echo 'For more information on how to get started, see README.md'
$tput sgr0
verb rm -v "$0"
# Note to self: consider rewriting this in Python?