-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.sh
executable file
·336 lines (274 loc) · 10.2 KB
/
framework.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
# load environment variables
source conf.env
# Show java framework ascii art
if [[ "$2" != "--re-run" ]]; then
echo -e "${COLOR_BLUE}"
cat .framework/ascii_art.txt
echo -e "${COLOR_RESET}\n"
fi
# Show help
if [[ "$1" == "-h" || "$1" == "--help" || "$#" -eq 0 ]]; then
echo "Usage: $0 [OPTION]"
echo " -i, --init Initialize project repository"
echo " -b, --build Build project and output a .war in $PROJECT_OUTPUT"
echo " -r, --run Build project and run it on tomcat server"
echo " --init-dev Initialize project repository with framework development files"
echo " --build-dev Build framework and output a .jar in $DEV_JAVA_OUTPUT"
echo " --run-dev Build framework, project and run it on tomcat server"
exit 0
fi
# Initialize project repository
if [[ "$1" == "-i" || "$1" == "--init" ]]; then
# Check configuration
source .framework/check_config.sh
# Create project directories
mkdir -p "$PROJECT_JAVA_SRC"
mkdir -p "$PROJECT_JAVA_LIB"
mkdir -p "$PROJECT_WEB_SRC"
mkdir -p "$PROJECT_OUTPUT"
# Create web.xml jakarta from ./.framework/web.xml and evaluate variables in the file
export PROJECT_JAVA_SRC
export PROJECT_CONFIG_FILE
envsubst < .framework/web.xml > "$PROJECT_WEB_XML"
envsubst < .framework/web.xml > "$PROJECT_CONFIG_FILE"
# copy app.xml
cp .framework/app.xml "$PROJECT_CONFIG_FILE"
# Move framework.jar to $PROJECT_JAVA_LIB
cp .framework/framework.jar "$PROJECT_JAVA_LIB"
# Copy lib files from .framework
cp -r .framework/lib/* "$PROJECT_JAVA_LIB"
# Project creation test
error=0
if [[ -z "$PROJECT_NAME" ]]; then
error+=1
echo "\$PROJECT_NAME is not set"
fi
if [[ ! -d "$PROJECT_JAVA_SRC" ]]; then
error+=1
echo "\$PROJECT_JAVA_SRC cannot be created"
fi
if [[ ! -d "$PROJECT_JAVA_LIB" ]]; then
error+=1
echo "\$PROJECT_JAVA_LIB cannot be created"
fi
if [[ ! -f "$PROJECT_WEB_XML" ]]; then
error+=1
echo "\$PROJECT_WEB_XML cannot be created"
fi
if [[ ! -d "$PROJECT_WEB_SRC" ]]; then
error+=1
echo "\$PROJECT_WEB_SRC cannot be created"
fi
if [[ ! -d "$PROJECT_OUTPUT" ]]; then
error+=1
echo "\$PROJECT_OUTPUT cannot be created"
fi
if [[ ! -f "$PROJECT_JAVA_LIB/framework.jar" ]]; then
error+=1
echo "\$PROJECT_JAVA_LIB/framework.jar cannot be created"
fi
# Report errors
if [[ $error -ne 0 ]]; then
echo -e "${COLOR_RED}ERROR: Please check your repository permission${COLOR_RESET}\n"
exit 1
else
echo -e "${COLOR_GREEN}SUCCESS: Project repository initialized successfully${COLOR_RESET}\n"
fi
exit 0
fi
# Initialize project repository with framework development files
if [[ "$1" == "--init-dev" ]]; then
# Init project repository
"$0" -i --re-run
if [ $? -ne 0 ]; then
exit 1
fi
# Create framework dir
mkdir -p "$DEV_JAVA_SRC"
mkdir -p "$DEV_JAVA_LIB"
mkdir -p "$DEV_JAVA_OUTPUT"
# Copy framework development files from .framework
cp -r .framework/dev_files/* "$DEV_JAVA_SRC"
# Copy lib files from .framework
cp -r .framework/lib/* "$DEV_JAVA_LIB"
# Report success
echo -e "${COLOR_GREEN}SUCCESS: Project development files initialized successfully ${COLOR_RESET}\n"
exit 0
fi
# Function to compile java
function compile {
source_directory=$1
out_directory=$2
classpath=$3
source_files=($(find "$source_directory" -type f -name "*.java")) # get all java files
reminding_files=${#source_files[@]}
while [ ${#source_files[@]} -ne 0 ]; do # while there are files to compile (boucle because of other class dependencies)
for i in "${!source_files[@]}"; do
# echo -cp "$classpath":"$out_directory" -d "$out_directory" "${source_files[i]}"
javac -parameters -cp "$classpath:$out_directory" -d "$out_directory" "${source_files[i]}" 2>compilation.log
compilation=$(<compilation.log) # ignore error dependency
if [ ${#compilation[0]} -eq 0 ]; then # remove compiled files
unset "source_files[$i]"
fi
done
if [ ${#source_files[@]} -eq "$reminding_files" ]; then # if no file has been compiled
echo -e "${COLOR_RED}ERROR: Compilation failed due to unresolved dependencies or mistakes in the code ${COLOR_RESET}"
cat compilation.log
rm -rf compilation.log
exit 1
fi
reminding_files=${#source_files[@]}
done
rm -rf compilation.log
}
# Build project
if [[ "$1" == "-b" || "$1" == "--build" ]]; then
# Check if project is initialized
if [[ ! -d "$PROJECT_JAVA_SRC" ]]; then
echo -e "${COLOR_RED}ERROR: Project is not initialized${COLOR_RESET}\n"
exit 1
fi
# Make a temp dir in $PROJECT_OUTPUT
rm -rf "$PROJECT_OUTPUT/project"
temp_dir="$PROJECT_OUTPUT/project"
echo -e "${COLOR_BLUE}Building project in $temp_dir...${COLOR_RESET}"
# Create .war structure
mkdir -p "$PROJECT_OUTPUT/project"
mkdir -p "$temp_dir/WEB-INF/classes"
mkdir -p "$temp_dir/WEB-INF/lib"
mkdir -p "$temp_dir/META-INF"
# Copy web.xml to $temp_dir/WEB-INF
cp "$PROJECT_WEB_XML" "$temp_dir/WEB-INF"
# Copy all lib .jar to $temp_dir/WEB-INF/lib
cp -R "$PROJECT_JAVA_LIB"/* "$temp_dir/WEB-INF/lib"
# Compile java files
echo -e "${COLOR_BLUE}Compiling java files...${COLOR_RESET}"
compilation=$(compile "$PROJECT_JAVA_SRC" "$temp_dir/WEB-INF/classes" "$PROJECT_JAVA_LIB/*:$TOMCAT_LIB/*")
if [ ${#compilation[0]} -ne 0 ]; then
echo "$compilation"
exit 1
fi
# Copy web files
echo -e "${COLOR_BLUE}Copying web files...${COLOR_RESET}"
cp -r "$PROJECT_WEB_SRC"/* "$temp_dir"
# Create .war file
echo -e "${COLOR_BLUE}Creating .war file...${COLOR_RESET}"
jar -cvf "$PROJECT_OUTPUT/$PROJECT_NAME.war" -C "$temp_dir" .
# Remove temp dir
rm -rf "$temp_dir"
# Report success
echo -e "${COLOR_GREEN}SUCCESS: Project built successfully in ${PROJECT_OUTPUT} ${COLOR_RESET}\n"
exit 0
fi
# Build framework
if [[ "$1" == "--build-dev" ]]; then
# Check if framework dir is initialized
if [[ ! -d "$DEV_JAVA_SRC" ]]; then
echo -e "${COLOR_RED}ERROR: Framework dev repository is not initialized${COLOR_RESET}\n"
exit 1
fi
# Make a temp dir in $PROJECT_OUTPUT
rm -rf "$DEV_JAVA_OUTPUT/framework"
temp_dir=$(mktemp -d "$DEV_JAVA_OUTPUT/framework")
echo -e "${COLOR_BLUE}Building framework in $temp_dir...${COLOR_RESET}"
# Compile java files
echo -e "${COLOR_BLUE}Compiling java files...${COLOR_RESET}"
compilation=$(compile "$DEV_JAVA_SRC" "$temp_dir" "$DEV_JAVA_LIB/*:$TOMCAT_LIB/*")
if [ ${#compilation[0]} -ne 0 ]; then
echo "$compilation"
exit 1
fi
# Create .jar file
echo -e "${COLOR_BLUE}Creating .jar file...${COLOR_RESET}"
jar -cvf "$DEV_JAVA_OUTPUT/framework.jar" -C "$temp_dir" .
# Remove temp dir
rm -rf "$temp_dir"
# Copy framework files to .framework
cp -r "$DEV_JAVA_LIB"/* .framework/lib
cp -r "$DEV_JAVA_SRC"/* .framework/dev_files
# Copy framework.jar to project lib
cp "$DEV_JAVA_OUTPUT/framework.jar" "$PROJECT_JAVA_LIB"
# Report success
echo -e "${COLOR_GREEN}SUCCESS: Framework built successfully in ${DEV_JAVA_OUTPUT} ${COLOR_RESET}\n"
exit 0
fi
# Run project
if [[ "$1" == "-r" || "$1" == "--run" ]]; then
# Build project
"$0" -b --re-run
if [ $? -ne 0 ]; then
exit 1
fi
# Copy war in webapps
echo -e "${COLOR_BLUE}Copying .war file in webapps...${COLOR_RESET}"
cp "$PROJECT_OUTPUT/$PROJECT_NAME.war" "$TOMCAT_WEBAPPS"
# Run tomcat with hot reload feature
echo -e "${COLOR_BLUE}Running tomcat server... please wait!${COLOR_RESET}"
export CATALINA_OPTS="-Dcatalina.http.port=${TOMCAT_PORT}"
"$TOMCAT_BIN"/catalina.sh start >> 'tomcat.log' 2>&1
# "$TOMCAT_BIN"/catalina.sh run
# Check if tomcat is running on port $TOMCAT_PORT
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:"${TOMCAT_PORT}")
# Max delay 10 seconds
delay=0
while [ "$response" != "200" ]; do
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:"${TOMCAT_PORT}")
sleep 1
delay=$((delay+1))
if [ "$delay" -gt 15 ]; then
echo -e "${COLOR_RED}ERROR: (timeout 15s) Tomcat server failed to start${COLOR_RESET}\n"
echo "Verify your tomcat configuration and if the port $TOMCAT_PORT is not already in use"
echo "If problem persists, try to run your tomcat server manually"
echo "You can also check the logs in tomcat.log"
exit 1
fi
done
# Show tomcat logs
# tail -f "$TOMCAT_HOME"/logs/catalina.out &
# Report success
echo -e "${COLOR_GREEN}Tomcat server is running on port ${TOMCAT_PORT} ${COLOR_RESET}\n"
echo -e "Open ${COLOR_BLUE}http://localhost:${TOMCAT_PORT}/${PROJECT_NAME}${COLOR_RESET} in your browser"
echo -e "Changed files will be automatically reloaded (May take a while to see change on browser)\n"
echo -e "${COLOR_RED}Press Ctrl+C to stop server...${COLOR_RESET}\n"
touch .framework/timestamp
while true; do
changes=$(find "$PWD" -type f -newer .framework/timestamp)
# If any files have been modified
# if [ ${#changes} -ne 0 ]; then
# "$TOMCAT_BIN"/catalina.sh stop >> 'tomcat.log' 2>&1
# Clear the output
# clear
# echo -ne "\033c"
# echo -e "\n****************************\n${COLOR_BLUE}Reloading web application...${COLOR_RESET}\n\n"
# touch .framework/timestamp
# "$0" -r --re-run
# if [ $? -ne 0 ]; then
# exit 1
# fi
# fi
# Sleep for 1 second before checking for changes again
# sleep 1
done
exit 0
fi
# Run dev
if [[ "$1" == "--run-dev" ]]; then
# Build framework
"$0" --build-dev --re-run
if [ $? -ne 0 ]; then
exit 1
fi
# Run project
"$0" -r --re-run
if [ $? -ne 0 ]; then
exit 1
fi
exit 0
fi
# Show error
echo -e "${COLOR_RED}"
echo "Invalid option: $1"
echo "Try '$0 --help' for more information"
echo -e "${COLOR_RESET}"
exit 1