-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.sh
executable file
·159 lines (127 loc) · 3.7 KB
/
get.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
#!/usr/bin/env bash
set -e +o pipefail
# Log levels
i="\033[0;36m" # info
g="\033[0;32m" # green
e="\033[0;31m" # error
l="\033[0;90m" # log test
r="\033[0m" # reset
# Logger
# This function log messages
# Usage: log <LEVEL> <MESSAGE>
log() {
echo -e " $1--> $l$2$r"
}
# Fatal logger
# This function log fatal messages
# Usage: fatal <MESSAGE> <EXIT_CODE>
fatal() {
log "$e" "$1"
exit "$([ $# -eq 2 ] && echo "$2" || echo 1)"
}
# Greetings
# This function print the greetings message for this coverage reporter
# Usage: greetings
greetings() {
cat << EOF
______ __
/ ____/___ ____/ /___ ________ __
/ / / __ \/ __ / __ \`/ ___/ / / /
/ /___/ /_/ / /_/ / /_/ / /__/ /_/ /
\____/\____/\__,_/\__,_/\___/\__, /
/____/
Codacy Coverage Reporter
EOF
}
greetings
# Error trap
# This function prints succeeded or failed message depending on last exit status
# Usage: exit_trap
exit_trap() {
EXIT_NUM=$?
echo
if [ $EXIT_NUM -eq 0 ];
then
log "$g" "Succeeded!"
else
fatal "Failed!"
fi
}
trap exit_trap EXIT
trap 'fatal Interrupted' INT
download() {
local url="$1"
local output="${2:--}"
if command -v curl > /dev/null 2>&1; then
curl -# -LS "$url" -o "$output"
elif command -v wget > /dev/null 2>&1; then
wget "$url" -O "$output"
else
fatal "Could not find curl or wget, please install one."
fi
}
download_reporter() {
if [ "$os_name" = "Linux" ] || [ "$os_name" = "Darwin" ]; then
# OS name lower case
suffix=$(echo "$os_name" | tr '[:upper:]' '[:lower:]')
else
suffix="assembly.jar"
fi
local binary_name="codacy-coverage-reporter-$suffix"
local reporter_path=$1
if [ ! -f "$reporter_path" ]
then
log "$i" "Downloading the codacy reporter $binary_name... ($CODACY_REPORTER_VERSION)"
binary_url="https://artifacts.codacy.com/bin/codacy-coverage-reporter/$CODACY_REPORTER_VERSION/$binary_name"
download "$binary_url" "$reporter_path"
else
log "$i" "Codacy reporter $binary_name already in cache"
fi
}
os_name=$(uname)
# Find the latest version in case is not specified
if [ -z "$CODACY_REPORTER_VERSION" ] || [ "$CODACY_REPORTER_VERSION" = "latest" ]; then
CODACY_REPORTER_VERSION=$(download "https://artifacts.codacy.com/bin/codacy-coverage-reporter/latest")
fi
# Temporary folder for downloaded files
if [ -z "$CODACY_REPORTER_TMP_FOLDER" ]; then
if [ "$os_name" = "Linux" ]; then
CODACY_REPORTER_TMP_FOLDER="$HOME/.cache/codacy/coverage-reporter"
elif [ "$os_name" = "Darwin" ]; then
CODACY_REPORTER_TMP_FOLDER="$HOME/Library/Caches/Codacy/coverage-reporter"
else
CODACY_REPORTER_TMP_FOLDER=".codacy-coverage"
fi
fi
# Set binary name
if [ "$os_name" = "Linux" ] || [ "$os_name" = "Darwin" ]; then
reporter_filename="codacy-coverage-reporter"
else
reporter_filename="codacy-coverage-reporter-assembly.jar"
fi
# Folder containing the binary
reporter_folder="$CODACY_REPORTER_TMP_FOLDER"/"$CODACY_REPORTER_VERSION"
# Create the reporter folder if not exists
mkdir -p "$reporter_folder"
# Set binary path
reporter_path="$reporter_folder"/"$reporter_filename"
download_reporter "$reporter_path"
if [ "$os_name" = "Linux" ] || [ "$os_name" = "Darwin" ]; then
chmod +x "$reporter_path"
run_command="$reporter_path"
else
run_command="java -jar \"$reporter_path\""
fi
if [ -z "$run_command" ]
then
fatal "Codacy coverage reporter binary could not be found."
fi
if [ "$#" -eq 1 ] && [ "$1" = "download" ];
then
log "$g" "Codacy reporter download succeded";
elif [ "$#" -gt 0 ];
then
eval "$run_command $*"
else
eval "$run_command \"report\""
fi