-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·286 lines (252 loc) · 6.78 KB
/
init.sh
File metadata and controls
executable file
·286 lines (252 loc) · 6.78 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
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
#!/usr/bin/env bash
ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)
INIT_PYTHON_VERSION=3.12.8
SED_INPLACE_SUFFIX=.tmp
VERBOSE=0
AUTO_YES=0
FORCE=0
DRY_RUN=0
USAGE="
Usage: ${BASH_SOURCE[0]} [options]
Available options are:
-y, --yes Automatic yes to prompts
-f, --force Skip validation
-n, --dry-run Don't actually do anything, just show what would be done
-v, --verbose Be more verbose/talkative during the operation
-h, --help Print this message.
-- Stop handling options.
"
function print_error
{
# shellcheck disable=SC2145
echo -e "\033[31m$@\033[0m" 1>&2
}
function print_message
{
# shellcheck disable=SC2145
echo -e "\033[32m$@\033[0m"
}
function print_verbose
{
if [[ $VERBOSE -ne 0 ]]; then
echo -e "$@"
fi
}
function print_usage
{
echo "$USAGE"
}
function is_number
{
[[ $1 =~ ^[0-9]+$ ]]
}
function on_interrupt_trap
{
print_error "An interrupt signal was detected."
exit 1
}
trap on_interrupt_trap INT
while [[ -n $1 ]]; do
case $1 in
-h|--help)
print_usage
exit 0
;;
-v|--verbose)
VERBOSE=1
shift
;;
-y|--yes)
AUTO_YES=1
shift
;;
-f|--force)
FORCE=1
shift
;;
-n|--dry-run)
DRY_RUN=1
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
read -r -p "Project name: " PROJECT_NAME
read -r -p "Project description (Do not use quotation marks): " PROJECT_DESC
read -r -e -i "$INIT_PYTHON_VERSION" -p "Python version: " PYTHON_VERSION
read -r -p "Github ID: " GITHUB_ID
read -r -p "User name: " USER_NAME
read -r -p "User e-mail: " USER_EMAIL
IFS="." read -r PYTHON_MAJOR PYTHON_MINOR PYTHON_PATCH <<< "$PYTHON_VERSION"
PYTHON_MAJOR=${PYTHON_MAJOR:-0}
PYTHON_MINOR=${PYTHON_MINOR:-0}
PYTHON_PATCH=${PYTHON_PATCH:-0}
if ! is_number "$PYTHON_MAJOR"; then
print_error "The Python major version number is incorrect: '$PYTHON_MAJOR'"
exit 1
fi
if ! is_number "$PYTHON_MINOR"; then
print_error "The Python minor version number is incorrect: '$PYTHON_MINOR'"
exit 1
fi
if ! is_number "$PYTHON_PATCH"; then
print_error "The Python patch version number is incorrect: '$PYTHON_PATCH'"
exit 1
fi
# shellcheck disable=SC2001
PACKAGE_LOWER=$(echo "${PROJECT_NAME,,}" | sed -e 's/[^a-zA-Z0-9]/_/g')
YEAR=$(date +%Y)
COMMON_FLAGS=()
if [[ $FORCE -ne 0 ]]; then
COMMON_FLAGS+=(-f)
fi
if [[ $VERBOSE -ne 0 ]]; then
COMMON_FLAGS+=(-v)
fi
INIT_FILES=(
".github/workflows/docker-deploy.yml"
".github/workflows/python-deploy.yml"
".github/workflows/python-test.yml"
".run/main.run.xml"
".vim/coc-settings.json"
"__PACKAGE_LOWER__/logging/formatters/colored.py"
"__PACKAGE_LOWER__/logging/logging.py"
"__PACKAGE_LOWER__/logging/__init__.py"
"__PACKAGE_LOWER__/aio/__init__.py"
"__PACKAGE_LOWER__/aio/policy.py"
"__PACKAGE_LOWER__/aio/run.py"
"__PACKAGE_LOWER__/apps/master/__init__.py"
"__PACKAGE_LOWER__/apps/__init__.py"
"__PACKAGE_LOWER__/__init__.py"
"__PACKAGE_LOWER__/entrypoint.py"
"__PACKAGE_LOWER__/__main__.py"
"__PACKAGE_LOWER__/types/string/to_boolean.py"
"__PACKAGE_LOWER__/types/string/__init__.py"
"__PACKAGE_LOWER__/types/override.py"
"__PACKAGE_LOWER__/types/__init__.py"
"__PACKAGE_LOWER__/arguments.py"
"__PACKAGE_LOWER__/assets/.gitignore"
"__PACKAGE_LOWER__/assets/__init__.py"
"__PACKAGE_LOWER__/system/environ.py"
"__PACKAGE_LOWER__/system/__init__.py"
"tester/__init__.py"
"tester/test_entrypoint.py"
".dockerignore"
".gitignore"
".gitlab-ci.yml"
".vimspector.json"
"Dockerfile"
"LICENSE"
"MANIFEST.in"
"README.md"
"api.rest"
"black.sh"
"build-docker.sh"
"build-pyinstaller.sh"
"build.sh"
"ci.sh"
"clean.sh"
"env-template"
"flake8.ini"
"flake8.sh"
"install.dev.sh"
"isort.cfg"
"isort.sh"
"main.py"
"mypy.ini"
"mypy.sh"
"project.dic"
"pytest.ini"
"pytest.sh"
"python"
"requirements.deploy.txt"
"requirements.develop.txt"
"requirements.main.txt"
"requirements.test.txt"
"requirements.txt"
"run"
"setup.cfg"
"setup.py"
"twine.sh"
"uninstall.sh"
"version"
)
if [[ $FORCE -eq 0 ]]; then
for file in "${INIT_FILES[@]}"; do
file_path="$ROOT_DIR/$file"
if [[ ! -f "$file_path" ]]; then
print_error "Not found initialize file: '$file_path'"
exit 1
fi
if [[ ! -w "$file_path" ]]; then
print_error "Not writable initialize file: '$file_path'"
exit 1
fi
done
fi
SED_ARGS=(
-e "s/__PROJECT_NAME__/$PROJECT_NAME/g"
-e "s/__PROJECT_DESC__/$PROJECT_DESC/g"
-e "s/__PACKAGE_LOWER__/$PACKAGE_LOWER/g"
-e "s/__PYTHON_VERSION__/$PYTHON_VERSION/g"
-e "s/__PYTHON_MAJOR__/$PYTHON_MAJOR/g"
-e "s/__PYTHON_MINOR__/$PYTHON_MINOR/g"
-e "s/__PYTHON_PATCH__/$PYTHON_PATCH/g"
-e "s/__USER_NAME__/$USER_NAME/g"
-e "s/__USER_EMAIL__/$USER_EMAIL/g"
-e "s/__GITHUB_ID__/$GITHUB_ID/g"
-e "s/__YEAR__/$YEAR/g"
)
print_verbose "[[VARIABLES REPORT]]"
print_verbose "Project name: '${PROJECT_NAME}'"
print_verbose "Project description: '${PROJECT_DESC}'"
print_verbose "Project lower: ${PACKAGE_LOWER}"
print_verbose "Python version: ${PYTHON_VERSION}"
print_verbose "Python major: ${PYTHON_MAJOR}"
print_verbose "Python minor: ${PYTHON_MINOR}"
print_verbose "Python patch: ${PYTHON_PATCH}"
print_verbose "User name: '${USER_NAME}'"
print_verbose "User e-mail: '${USER_EMAIL}'"
print_verbose "Github ID: '${GITHUB_ID}'"
print_verbose "Year: ${YEAR}"
if [[ $AUTO_YES -eq 0 ]]; then
read -r -p "Are you sure you want to continue with the installation? (y/n) " YN
if [[ "${YN,,}" != 'y' ]]; then
print_error "The job has been canceled"
exit 1
fi
fi
INIT_FILES_LENGTH="${#INIT_FILES[@]}";
for (( i = 0; i < INIT_FILES_LENGTH; i++ )); do
file_path="$ROOT_DIR/${INIT_FILES[$i]}"
temp_path="$ROOT_DIR/${INIT_FILES[$i]}$SED_INPLACE_SUFFIX"
if [[ $DRY_RUN -eq 0 ]]; then
sed "-i$SED_INPLACE_SUFFIX" "${SED_ARGS[@]}" "$file_path"
fi
print_verbose "[$((i + 1))/$INIT_FILES_LENGTH] Update file: $file_path"
if [[ $DRY_RUN -eq 0 ]]; then
rm "${COMMON_FLAGS[@]}" "$temp_path"
fi
done
if [[ $DRY_RUN -eq 0 ]]; then
mv "${COMMON_FLAGS[@]}" "$ROOT_DIR/__PACKAGE_LOWER__" "$ROOT_DIR/$PACKAGE_LOWER"
fi
print_message "Initialization was successful!"
if [[ $AUTO_YES -eq 0 ]]; then
read -r -p "Do you want to remove init scripts that are no longer used? (y/n) " YN
else
YN=y
fi
if [[ "${YN,,}" != 'y' ]]; then
exit 0
fi
if [[ $DRY_RUN -ne 0 ]]; then
exit 0
fi
rm "${COMMON_FLAGS[@]}" "$(realpath "${BASH_SOURCE[0]}")"