-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctc.sh
executable file
·173 lines (150 loc) · 5.12 KB
/
ctc.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
# CasusToets Test environment
#!/bin/sh
NC='\033[0m'
CYAN='\033[0;36m'
RED='\033[0;31m'
# Usage info
function show_help {
cat << EOF
Usage: ${0##*/} [-fhlv] [FOLDER]
Set the case exam template app state using git and cp and optional Laravel commands.
It can also run PHP_CodeSniffer to preset a feedback md file in the students project
root directory
-f also creates a fresh feedback file with phpcs output in the
student folder and opens it with VSCode
-h display this help and exit
-l include Laravel commands like migrations and cache clearing
-v verbose mode. Can be used multiple times for increased
verbosity.
[FOLDER] specifies the source folder of the students project to copy from. If
omitted, ctc will only reset the case exam to the latest commit.
EOF
}
function exec_command {
((verbose >= 3)) && set -x
${1}
exit_status=$?
{ set +x; } 2>/dev/null
return $exit_status
}
function log {
((verbose >= $1)) && echo -e $2
}
# Initialize our own variables:
feedback=0
include_laravel=0
verbose=1
quiet=0
folder=""
# Resetting OPTIND is necessary if getopts was used previously in the script.
# It is a good idea to make OPTIND local if you process options in a function.
OPTIND=1
while getopts :fhlv opt; do
case $opt in
f ) feedback=1
;;
h ) show_help
exit 0
;;
l ) include_laravel=1
;;
v ) verbose=$((verbose+1))
;;
* ) echo -e "${RED}Invalid option: $OPTARG${NC}" 1>&2
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and sentinel --
# Remember the start folder on startup
StartFolder=$(pwd)
log 3 "${CYAN}Will return to folder $StartFolder${NC}"
# Read the environment variables file
log 3 "Reading .env file"
set -o allexport
source .env
set +o allexport
# Grab and validate the student project source folder
StudentAccountName=$1
if [ ! -z "$StudentAccountName" ]
then
# Assign and validate student folder
StudentFolder="$StudentsBaseFolder/$StudentsBasePrefix$StudentAccountName"
if [ ! -d $StudentFolder ]
then
echo -e "${RED}$StudentFolder does not exist${NC}" 1>&2
exit 1
fi
fi
if [ ! -z "$StudentAccountName" ]
then
log 2 "${CYAN}Student account name set to:${NC} $StudentAccountName"
else
log 2 "No student account name specified. Will only reset the template folder"
fi
# =================== Use GIT to reset the project ==========================
log 1 "${CYAN}Switch to Case Exam Template folder${NC}"
exec_command "cd $ProjectFolder"
log 1 "${CYAN}Reset the folder to the HEAD version in git${NC}"
OPTIONS="--hard"
(( verbose < 1 )) && OPTIONS="${OPTIONS} --quiet"
exec_command "git reset ${OPTIONS}"
log 1 "${CYAN}Git-clean the folder${NC}"
OPTIONS="-d --force "
(( verbose < 1 )) && OPTIONS="${OPTIONS} --quiet"
exec_command "git clean ${OPTIONS}"
# ======================== Copy files and folders ===========================
if [ ! -z "$StudentAccountName" ]
then
log 1 "${CYAN}Copy files and folders from $StudentFolder${NC}"
# Parse $FileArray into an array
IFS=' ' read -r -a FileArray <<< "$Files"
# Loop through each element in the array
for Filename in "${FileArray[@]}"
do
# log 2 "${CYAN}Remove all files/folders from: ${NC}$Filename"
# exec_command "rm -rf $Filename"
log 2 "${CYAN}Copy file/folder: ${NC}$Filename"
OPTIONS="--recursive"
((verbose >=3)) && OPTIONS="${OPTIONS} -v"
exec_command "cp ${OPTIONS} $StudentFolder/$Filename ."
done
fi
# ======= Run some composer and artisan commands to reset the app ===========
if ((include_laravel > 0))
then
OPTIONS=""
((verbose >=3)) && OPTIONS="-vvv"
log 1 "${CYAN}Update autoload file${NC}"
exec_command "./vendor/bin/sail composer ${OPTIONS} dump-autoload"
log 1 "${CYAN}Clear all the compiled views${NC}"
exec_command "./vendor/bin/sail artisan view:clear ${OPTIONS}"
log 1 "${CYAN}Make fresh database${NC}"
exec_command "./vendor/bin/sail artisan migrate:fresh ${OPTIONS}"
if (($?<=0))
then
log 1 "${CYAN}Seed the database${NC}"
exec_command "./vendor/bin/sail artisan db:seed ${OPTIONS}"
else
log 1 "${RED}Error during migration. Skipping database seeding${NC}"
fi
fi
if ((feedback>0))
then
filename="$StudentFolder/feedback.md"
log 1 "${CYAN}Create feedback file from template${NC}"
cp $StartFolder/FEEDBACK_TEMPLATE.md $filename
sed -i "s/{{STUDENT_ACCOUNT_NAME}}/$StudentAccountName/" $filename
log 1 "${CYAN}Run PHP_CodeSniffer fixer for line endings${NC}"
docker-compose exec -T laravel.test ./vendor/bin/phpcbf --sniffs=Generic.Files.LineEndings
log 1 "${CYAN}Run PHP_CodeSniffer${NC}"
docker-compose exec -T laravel.test ./vendor/bin/phpcs | tee -a $filename
log 1 "${CYAN}This output is also written to feedback.md${NC}"
echo '```' >> $filename
code $filename
fi
# Finally, return to the start folder
log 1 "${CYAN}Return to the start folder${NC}"
cd $StartFolder
# End of file