-
Notifications
You must be signed in to change notification settings - Fork 0
/
verde.sh
executable file
·231 lines (200 loc) · 4.8 KB
/
verde.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
#!/bin/bash -eu
DIR=$(readlink $(which verde) | xargs dirname)
TP_BUILDER_DIR="$DIR/scripts/tp-builder.sh"
SUPPORTED_LANGUAGES=("c" "cpp" "java")
CODE_FILE=""
CODE_LANGUAGE=""
TEST_CASES=()
# get source code file
get_source_code() {
local code_files=()
for language in "${SUPPORTED_LANGUAGES[@]}"; do
for file in *."$language"; do
if [ -f "$file" ]; then
code_files+=("$file")
fi
done
done
if [ ${#code_files[@]} -eq 0 ]; then
echo "Nenhum arquivo de código-fonte encontrado" >&2
exit 1
elif [ ${#code_files[@]} -eq 1 ]; then
CODE_FILE="${code_files[0]}"
else
PS3="Selecione um arquivo de código-fonte (1-${#code_files[@]}): "
select file in "${code_files[@]}"; do
if [ -n "$file" ]; then
CODE_FILE="$file"
break
else
echo "Opção inválida. Por favor, tente novamente." >&2
fi
done
fi
CODE_LANGUAGE="${CODE_FILE##*.}"
}
# get test cases files (.in and .out)
get_test_cases() {
for file in *.in; do
if [ -f "$file" -a -f "${file%.*}.out" ]; then
TEST_CASES+=("${file%.*}")
fi
done
if [ ${#TEST_CASES[@]} -eq 0 ]; then
echo "Nenhum caso de teste encontrado" >&2
exit 1
fi
}
# compare two files and show differences
get_files_diff() {
local file1=$1
local file2=$2
# get number of lines of each file
local lines_arq1=$(wc -l < "$file1")
local lines_arq2=$(wc -l < "$file2")
# get number of different lines
local diff_lines=$(git diff --ignore-cr-at-eol --no-index --stat "$file1" "$file2" | tail -1 | awk '{print $4}')
# check if files are equal or different
if [ -z "$diff_lines" ]; then
echo -e "$(tput setaf 3)$(tput bold)\n Nota: 100% \U0001F389 \n$(tput sgr0)"
else
# calculate difference percentage
local diff_perc=$(awk "BEGIN { printf \"%.2f\", ($diff_lines / ($lines_arq1 + $lines_arq2)) * 100 }")
# calculate equality percentage
local equal_perc=$(awk "BEGIN { printf \"%.2f\", 100 - $diff_perc }")
# show equality percentage
echo -e "$(tput setaf 3)$(tput bold)\n Nota: $equal_perc%$(tput sgr0)\n"
# show different lines
echo -e "$(tput setaf 1)$(tput bold) \U000026A0 Diferenças:$(tput sgr0)"
git diff --ignore-cr-at-eol --no-index --line-prefix=" " --word-diff=color -U0 --color=always "$file1" "$file2" | tail -n +5
echo
fi
}
# compile code
compile() {
case $CODE_LANGUAGE in
"java")
javac "$CODE_FILE"
;;
"c")
gcc -o "${CODE_FILE%.*}" "$CODE_FILE"
;;
"cpp")
g++ -o "${CODE_FILE%.*}" "$CODE_FILE"
;;
*)
echo "Linguagem não suportada" >&2
exit 1
;;
esac
}
# execute code
execute() {
case $CODE_LANGUAGE in
"java")
java "${CODE_FILE%.*}"
;;
"c")
./"${CODE_FILE%.*}"
;;
"cpp")
./"${CODE_FILE%.*}"
;;
*)
echo "Linguagem não suportada" >&2
exit 1
;;
esac
}
# test code with all test cases
test_code() {
get_test_cases
for test_case in "${TEST_CASES[@]}"; do
case $CODE_LANGUAGE in
"java")
java "${CODE_FILE%.*}" < "$test_case.in" > "$test_case.res.out"
;;
"c")
./"${CODE_FILE%.*}" < "$test_case.in" > "$test_case.res.out"
;;
"cpp")
./"${CODE_FILE%.*}" < "$test_case.in" > "$test_case.res.out"
;;
*)
echo "Linguagem não suportada" >&2
exit 1
;;
esac
echo -e "$(tput setaf 6)$(tput bold)> Caso de teste: $(tput smul)$test_case.in$(tput sgr0)"
get_files_diff "$test_case.res.out" "$test_case.out"
done
}
# compile and execute code
compile_and_execute() {
get_source_code
compile
execute
}
# compile, execute and test code
execute_tests() {
get_source_code
compile
test_code
}
# menu options
menu() {
echo "Bem-vindo ao Verde CLI!"
PS3="Selecione a opção desejada: "
select opt in "Compilar e executar código" "Executar casos de teste" "TP Builder"; do
case $opt in
"Executar código")
compile_and_execute
break
;;
"Corrigir exercício")
execute_tests
break
;;
"TP Builder")
bash "$TP_BUILDER_DIR"
exit 0
;;
*)
echo "Opção inválida!"
exit 1
;;
esac
done
}
# clear screen
clear -x
# process flags
while getopts 'hctb' opt; do
case $opt in
c)
compile_and_execute
exit 0
;;
t)
execute_tests
exit 0
;;
b)
bash "$TP_BUILDER_DIR"
exit 0
;;
*)
echo "Uso: verde [OPÇÃO]"
echo "Opções:"
echo " -h Mostra esta mensagem de ajuda"
echo " -c Compila e executa seu código"
echo " -t Executa os casos de teste"
echo " -b Executa o TP Builder"
exit 0
;;
esac
done
# show menu if no flag is passed
if [ $OPTIND -eq 1 ]; then
menu
fi