Skip to content

Commit fa6604a

Browse files
refactor(make.sh): refactor file make.sh
a large refactor
1 parent 9457830 commit fa6604a

File tree

1 file changed

+78
-43
lines changed

1 file changed

+78
-43
lines changed

make.sh

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,113 @@
11
#!/bin/bash
22

3-
# `${1%.cpp}` catches the whole file name(include path), delete the suffix `.cpp`
4-
filename=${1%.cpp}
3+
# `${1%.cpp}` extracts the file name (including path) by removing the `.cpp` suffix.
4+
filename="${1%.cpp}"
55

6-
# @brief: Receive a string as parameter, output it in blue.
7-
# `\033[34m` sets the color to blue, \
8-
# `\033[0m` clear all the colors
6+
# Definitions for colors
7+
BLUE="\033[34m"
8+
RED="\033[31m"
9+
RESET="\033[0m"
10+
11+
# @brief: Outputs the provided string in blue.
12+
# `\033[34m` sets the color to blue.
13+
# `\033[0m` resets the text color to default.
914
blueOutput () {
10-
echo -e "\033[34m${1}\033[0m"
15+
echo -e "${BLUE}${1}${RESET}"
1116
}
1217

13-
# @brief: Receive a string as parameter, output it in red.
14-
# `\033[31m` sets the color to red, \
15-
# `\033[0m` clear all the colors
18+
# @brief: Outputs the provided string in red.
19+
# `\033[31m` sets the color to red.
20+
# `\033[0m` resets the text color to default.
1621
redOutput () {
17-
echo -e "\033[31m${1}\033[0m"
22+
echo -e "${RED}${1}${RESET}"
1823
}
1924

20-
# @brief: read default test as `stdin`, redirect `stdout` to `${filename}.ans` .
21-
# If found `${filename}.in`, \
22-
# ask user if they want to use `${filename}.in` as testcase. \
23-
# Output was been redirected to `${filename}.ans`
25+
# @brief: Reads the default test case as `stdin` and redirects `stdout` to `${filename}.ans`.
26+
# If `${filename}.in` exists, prompts the user to use it as the test case.
27+
# The program output is saved to `${filename}.ans`.
2428
tryUsingDefaultTestcase() {
25-
if test -r ${filename}.in; then # Test if `${filename}.in` exists and readable.
29+
if [[ -r ${filename}.in ]]; then # Checks if `${filename}.in` exists and is readable.
2630
echo ""
27-
blueOutput "[Info]:\033[0m Testcase ${filename}.in was detected."
28-
echo "------> Using this testcase? [Y/n]"
31+
blueOutput "[Info]:${RESET} Test case ${filename}.in detected."
32+
echo "------> Use this test case as stdin? [Y/n]"
2933
read -r operation
3034

31-
if [[ "$operation" != [Nn]* ]]; then # If `$operation` is not "N" or "n", include empty input.
32-
./${filename}.out <${filename}.in >${filename}.ans # Run program.
35+
if [[ "$operation" != [Nn]* ]]; then # Proceed if the user input is not "N" or "n".
36+
blueOutput "[Info]:${RESET} Using ${filename}.in as the test case."
37+
38+
./${filename}.out < "${filename}.in" > "${filename}.ans" # Executes the program with input redirection.
3339

34-
blueOutput "[Info]:\033[0m Reading ${filename}.in as testcase."
35-
blueOutput "[Info]:\033[0m Your answer is below, which will be saved as ${filename}.ans.\n"
40+
blueOutput "[Info]:${RESET} Output is shown below and saved as ${filename}.ans.\n"
3641

37-
cat ${filename}.ans
42+
cat "${filename}.ans"
3843

39-
blueOutput "\n[Hint]:\033[0m You can try \"diff ${filename}.ans <Standard Answer>\" to debug."
44+
blueOutput "\n[Hint]:${RESET} You can use \"diff ${filename}.ans <Standard Answer>\" to compare with the expected output."
4045
fi
4146
fi
4247
}
4348

44-
# ---------- Functions definitions finished ----------
49+
# @brief: Removes old files if they exist.
50+
# Deletes `${filename}.out`, `${filename}.ans`, and `${filename}.log`.
51+
initCleanup() {
52+
# Remove the old `${filename}.out` if it exists.
53+
if [[ -f "${filename}.out" ]]; then
54+
rm "${filename}.out"
55+
fi
4556

46-
# Remove the old `${filename}.out` if it exists.
47-
if test -f ${filename}.out;then
48-
rm ${filename}.out
49-
fi
57+
# Remove the old `${filename}.ans` if it exists.
58+
if [[ -f "${filename}.ans" ]]; then
59+
rm "${filename}.ans"
60+
fi
5061

51-
# Remove the old `${filename}.ans` if it exists.
52-
if test -f ${filename}.ans;then
53-
rm ${filename}.ans
54-
fi
62+
# Remove the old `${filename}.log` if it exists.
63+
if [[ -f "${filename}.log" ]]; then
64+
rm "${filename}.log"
65+
fi
66+
}
5567

56-
# @brief: Compile `${filename}.cpp`
68+
# @brief: Removes empty files if they exist.
69+
# Specifically checks `${filename}.ans` and `${filename}.log`.
70+
cleanupEmptyFiles() {
71+
# Check if the `.ans` file exists and is empty.
72+
if [[ -f "${filename}.ans" && ! -s "${filename}.ans" ]]; then
73+
rm "${filename}.ans"
74+
fi
75+
76+
# Check if the `.log` file exists and is empty.
77+
if [[ -f "${filename}.log" && ! -s "${filename}.log" ]]; then
78+
rm "${filename}.log"
79+
fi
80+
}
81+
82+
# ---------- Function definitions complete ----------
83+
84+
initCleanup
85+
86+
# @brief: Compiles `${filename}.cpp` using `g++` with detailed warnings and debugging flags.
5787
# --std=c++14 is the require of CCF - China Cheating-money Foundation
5888
# c++14 for CSP-J/S, NOIp and NOI, etc.
59-
# E.g.: Your file name is `foo.cpp`, then your executable file name is `foo.out`.
89+
# Outputs compilation details to the terminal and logs plain text to `${filename}.log`.
90+
# If successful, the output executable is named `${filename}.out`.
6091
g++ -g -Wall -Wextra -pedantic --std=c++14 -Og \
6192
-Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 \
6293
-Wduplicated-cond -Wcast-qual -Wcast-align -Wnoexcept -Winline -Wdouble-promotion \
6394
-fsanitize=undefined -fsanitize=address -fanalyzer \
6495
-D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC \
6596
-fdiagnostics-color=always \
66-
$1 -o ${filename}.out
97+
"$1" -o "${filename}.out" 2>&1 \
98+
| tee /dev/tty \
99+
| sed "s/\x1B\[[0-9;]*[a-zA-Z]//g" > "${filename}.log"
67100

68-
# if [[ $? == 0 ]]; then
69-
# HINT: Sometimes `g++` will return 0 even if got trouble.
70-
if test -x ${filename}.out; then # If `${filename}.out` exists and can be executed.
71-
blueOutput "[Info]:\033[0m Successfully compiled $1"
72-
blueOutput "[Info]:\033[0m Executable file is ${filename}.out"
101+
# Check if the output file was successfully created and is executable.
102+
if [[ -x "${filename}.out" ]]; then
103+
blueOutput "[Info]:${RESET} Compilation of $1 succeeded."
104+
blueOutput "[Info]:${RESET} Executable file: ${filename}.out"
73105
tryUsingDefaultTestcase
74106
else
75-
# TODO: Output the above compile informations to `${filename}.log`
76-
redOutput "[Error]: Got trouble in compiling...\a"
77-
blueOutput "[Info]:\033[0m Exiting..."
107+
redOutput "[Error]: Compilation failed.\a"
108+
blueOutput "[Info]:${RESET} Check '${filename}.log' for details."
78109
fi
110+
111+
cleanupEmptyFiles
112+
113+
exit

0 commit comments

Comments
 (0)