-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons
executable file
·215 lines (213 loc) · 7.32 KB
/
commons
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
#!/usr/bin/env bash
declare MSG_OPENED='false'
clear_stdin() {
while read -r -t 0; do read -r; done
}
msg_start() {
[[ "${MSG_OPENED}" == 'true' ]] \
&& exception 'Message already started'
MSG_OPENED='true'
printf -- '%s ... ' "$@" >&2
}
msg_end() {
declare -r close_message=${1:-DONE}
[[ "${MSG_OPENED}" = 'false' ]] \
&& exception 'Message not started'
MSG_OPENED='false'
printf -- '[ %s ]\n' "${close_message}" >&2
}
confirm() {
printf -- '%s [YES/No] ' "${1:-'Are you sure?'}" >&2
clear_stdin
read -r
[[ "${REPLY}" =~ ^[Yy]([Ee][Ss])?$ || -z "${REPLY}" ]] \
&& return 0
[[ "${REPLY}" =~ ^[Nn][Oo]?$ ]] \
&& return 1
confirm 'Type'
}
prompt() {
declare -r message="${1:-Enter value}"
printf -- '%s: ' "${message}" >&2
clear_stdin
read -r
[[ -n "${REPLY}" ]] \
&& return 0
prompt "${message}"
}
exception() {
declare -r message="${1:-Unknown exception}"
declare -ri code=${2:-1}
(( code == 1 )) \
&& printf -- '%s in %s() [ #%d ]\n' "${message}" "${FUNCNAME[1]}" "${code}" >&2 \
|| printf -- '%s [ #%d ]\n' "${message}" "${code}" >&2
exit "${code}"
}
warning() {
declare -r message="${1:-Unknown warning}"
printf -- '%s\n' "${message}" >&2
}
check_x() {
[[ -x "${1}" ]] \
|| chmod +x "${1}"
}
print_usage() {
declare out
declare cmd="${CMD}"
[[ -n "${CMD}" ]] \
&& cmd="-${cmd}"
# shellcheck disable=SC2155
declare usage="$(get_md_section "## SYNOPSIS" < "${DIR}/documentation/${SCRIPT}${cmd}.md")"
[[ -n "${usage}" ]] \
&& out="usage: ${usage}"
# shellcheck disable=SC2155
declare commands="$(get_md_section "## COMMANDS" < "${DIR}/documentation/${SCRIPT}${cmd}.md")"
[[ -n "${commands}" ]] \
&& out="${out}"$'\n'"${commands}"
# shellcheck disable=SC2155
declare options="$(get_md_section "## OPTIONS" < "${DIR}/documentation/${SCRIPT}${cmd}.md")"
# shellcheck disable=SC2001
[[ -n "${options}" ]] \
&& out="${out}"$'\n'"$(sed 's/^/ /g' <<< "${options}")"
fmt -w "$(tput cols)" <<< "${out}"
}
check_command() {
for cmd in "${@}"; do
command -v "${cmd}" >/dev/null 2>&1 \
|| exception "Command ${cmd} not found"
done
}
get_md_section() {
declare -r section="${1}"
awk -v section="${section}" '
BEGIN {
out = 0
}
/^##/ {
if ($0 == section) {
out = 1; next
}
out = 0
}
{
if (out == 1) {
print
}
}' \
| sed 's/`//g'
}
process_user_scripts() {
declare -r type="${1:-}"
[[ -z "${type}" ]] \
&& exception "Missing type argument in process_user_scripts"
[[ -z "${ACADEMY_LANG}" ]] \
&& warning "Undefined ACADEMY_LANG variable. Skipping.." \
&& return 0
[[ ! -f "${DIR}/${type}_${ACADEMY_LANG}" ]] \
&& exception "Missing ${type} script '${type}_${ACADEMY_LANG}' for selected language."
declare -r RESULTS="${WORKING_DIR}/.results"
declare -r ACADEMY_CACHE="${HOME}/.academy"
mkdir -p "${RESULTS}"
mkdir -p "${ACADEMY_CACHE}"
declare editable_files
declare -a CHANGED_FILES
if [[ -n "${ACADEMY_CHANGED_FILES}" ]]; then
mapfile -t CHANGED_FILES <<< "${ACADEMY_CHANGED_FILES// /$'\n'}"
else
# shellcheck disable=SC2086
# shellcheck disable=SC2164
editable_files="$(cd "${WORKING_DIR}"; ls -d -- ${ACADEMY_EDITABLE})"
# shellcheck disable=SC2034
[[ -n "$editable_files" ]] \
&& mapfile -t CHANGED_FILES < <(
git -C "${WORKING_DIR}" diff HEAD..HEAD~1 --name-only 2>/dev/null \
| grep -Fx "${editable_files}"
)
fi
# shellcheck disable=SC1090
source "${DIR}/${type}_${ACADEMY_LANG}"
}
get_artifact_url() {
declare project_id="${1:-}"
declare branch="${2:-}"
declare artifact_path="${3:-}"
declare job="${4:-}"
[[ -z "${project_id}" || -z "${branch}" || -z "${artifact_path}" || -z "${job}" ]] \
&& exception "Missing or empty get_artifact_url arguments"
echo "${CI_API_V4_URL}/projects/${project_id}/jobs/artifacts/${branch}/raw/${artifact_path}?job=${job}"
}
io_tests() {
# defaults
declare -r badge_name='100-badge-iotest'
declare -r IOTEST_FOLDER="${WORKING_DIR}/iotest"
declare -r IOTEST_LOG="${RESULTS}/iotest.log"
generate_badge 'IO Tests' '0/0' '' "${badge_name}"
printf -- '' > "${IOTEST_LOG}"
[[ ! -d "${IOTEST_FOLDER}" ]] \
&& return
"${DIR}/iotest" "${IOTEST_FOLDER}" "${1}" "${2:-}" | tee "${IOTEST_LOG}"
declare summary passed count
summary="$(grep 'Summary:' < "${IOTEST_LOG}" | cut -d ' ' -f2-)"
count="$(cut -d',' -f1 <<< "${summary}" | cut -d' ' -f2)"
passed="$(cut -d',' -f3 <<< "${summary}" | cut -d' ' -f3)"
declare test_color='success'
[[ ${passed} -lt ${count} ]] \
&& test_color='critical'
declare link=''
declare title=''
[[ -n "${CI}" ]] \
&& title='View IO tests log' \
&& link="$(get_artifact_url "${CI_PROJECT_ID}" "${CI_COMMIT_BRANCH}" "${IOTEST_LOG#"${WORKING_DIR}/"}" "${CI_JOB_NAME}")"
generate_badge 'IO Tests' "${passed}/${count}" "${test_color}" "${badge_name}" "${link}" "${title}"
}
java_junit_tests() {
# defaults
declare -r badge_name='200-badge-java-junit'
declare -r JUNIT_LOG="${RESULTS}/junit.log"
generate_badge 'JUnit Tests' '0/0' '' "${badge_name}"
printf -- '' > "${JUNIT_LOG}"
# process
declare -r JUNIT_FILE="${ACADEMY_CACHE}/junit.jar"
[[ -f "${JUNIT_FILE}" ]] \
|| curl -sL "https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.9.2/junit-platform-console-standalone-1.9.2.jar" > "${JUNIT_FILE}"
declare -i found
declare -i successful
javac -cp "${JUNIT_FILE}" "${WORKING_DIR}"/*.java > "${JUNIT_LOG}"
java -jar "${JUNIT_FILE}" -cp "${WORKING_DIR}" --scan-classpath --disable-banner --details=summary >> "${JUNIT_LOG}"
cat "${JUNIT_LOG}"
found=$(grep -oP '\d(?= tests found)' "${JUNIT_LOG}")
successful=$(grep -oP '\d(?= tests successful)' "${JUNIT_LOG}")
declare link=''
declare title=''
[[ -n "${CI}" ]] \
&& title='View JUnit tests log' \
&& link="$(get_artifact_url "${CI_PROJECT_ID}" "${CI_COMMIT_BRANCH}" "${JUNIT_LOG#"${WORKING_DIR}/"}" "${CI_JOB_NAME}")"
generate_badge 'JUnit Tests' "${successful}/${found}" '' "${badge_name}" "${link}" "${title}"
}
java_code_style() {
# defaults
declare -r badge_name='300-badge-java-code-style'
generate_badge 'Code Style' 'n/a' '' "${badge_name}"
# shellcheck disable=SC2034
declare -r CHECKSTYLE_LOG="${RESULTS}/checkstyle.log"
declare -r CHECKSTYLE_VERION="10.7.0"
declare -r CHECKSTYLE_FILE="${ACADEMY_CACHE}/checkstyle.jar"
[[ -f "${CHECKSTYLE_FILE}" ]] \
|| curl -sL "https://github.com/checkstyle/checkstyle/releases/download/checkstyle-${CHECKSTYLE_VERION}/checkstyle-10.7.0-all.jar" > "${CHECKSTYLE_FILE}"
# process
# shellcheck disable=SC2086
java -jar "${CHECKSTYLE_FILE}" -c 'google_checks.xml' ${ACADEMY_EDITABLE} | grep '^\[\(WARN\|ERROR\)' > "${CHECKSTYLE_LOG}"
# shellcheck disable=SC2155
declare -ri errs=$(cut -d" " -f2 < "${CHECKSTYLE_LOG}" | sort -ut: -k1,2 | wc -l)
# shellcheck disable=SC2155
declare -ri lines=$(compgen -G "${WORKING_DIR}/${ACADEMY_EDITABLE}" | xargs cat | wc -l)
declare -i perc=100
(( errs > 0 )) \
&& (( perc = 99 - errs * 100 / lines ))
declare link=''
declare title=''
[[ -n "${CI}" ]] \
&& title='View Code Style log' \
&& link="$(get_artifact_url "${CI_PROJECT_ID}" "${CI_COMMIT_BRANCH}" "${CHECKSTYLE_LOG#"${WORKING_DIR}/"}" "${CI_JOB_NAME}")"
generate_badge 'Code Style' "${perc}%" '' "${badge_name}" "${link}" "${title}"
}