-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.sh
More file actions
executable file
·449 lines (381 loc) · 10.8 KB
/
Copy pathcompile.sh
File metadata and controls
executable file
·449 lines (381 loc) · 10.8 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash
# ============================================
# Contact Management System - Build Script
# CMPE343 - Fall 2025-2026 - Project 2
# Mac/Linux Version
# ============================================
# Color codes (ANSI)
RESET='\033[0m'
BOLD='\033[1m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTA='\033[35m'
CYAN='\033[36m'
WHITE='\033[37m'
# ============================================
# Configuration Variables
# ============================================
PROJECT_NAME="Contact Management System"
PROJECT_COURSE="CMPE343 - Fall 2025-2026 - Project 2"
SRC_DIR="src"
BIN_DIR="bin"
LIB_DIR="."
JAR_FILE="mysql-connector-j-8.2.0.jar"
MAIN_CLASS="Main"
MIN_JAVA_VERSION=17
LOG_FILE="compile.log"
BUILD_MODE="release"
ENABLE_WARNINGS=true
ENABLE_DEPRECATION=true
RUN_TESTS=false
CLEAN_BUILD=false
RUN_AFTER_BUILD=false
# ============================================
# Helper Functions
# ============================================
print_header() {
echo ""
echo -e "${CYAN}${BOLD}============================================${RESET}"
echo -e "${CYAN}${BOLD} ${PROJECT_NAME}${RESET}"
echo -e "${CYAN}${BOLD} ${PROJECT_COURSE}${RESET}"
echo -e "${CYAN}${BOLD}============================================${RESET}"
echo ""
}
print_success() {
echo -e "${GREEN}[OK]${RESET} $1"
}
print_error() {
echo -e "${RED}[ERROR]${RESET} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${RESET} $1"
}
print_info() {
echo -e "${BLUE}[INFO]${RESET} $1"
}
print_step() {
echo -e "${MAGENTA}[$1]${RESET} $2"
}
check_java() {
print_step "1/6" "Checking Java version..."
if ! command -v java &> /dev/null; then
print_error "Java not found! Please check your Java JDK installation."
return 1
fi
JAVA_VERSION=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | sed '/^$/d' | cut -d'.' -f1)
if [ -z "$JAVA_VERSION" ]; then
JAVA_VERSION=$(java -version 2>&1 | awk 'NR==1 {print $3}' | tr -d '"')
JAVA_MAJOR=$(echo $JAVA_VERSION | cut -d'.' -f1)
else
JAVA_MAJOR=$JAVA_VERSION
fi
if [ "$JAVA_MAJOR" -lt "$MIN_JAVA_VERSION" ]; then
print_error "Java version insufficient! Minimum Java ${MIN_JAVA_VERSION} required."
print_info "Current version: $(java -version 2>&1 | head -n 1)"
return 1
fi
print_success "Java version suitable: $(java -version 2>&1 | head -n 1 | cut -d'"' -f2)"
return 0
}
check_dependencies() {
print_step "2/6" "Checking dependencies..."
if [ ! -f "${LIB_DIR}/${JAR_FILE}" ]; then
print_error "JAR file not found: ${JAR_FILE}"
print_info "Please ensure ${JAR_FILE} is in the ${LIB_DIR} folder."
return 1
fi
print_success "JAR file present: ${JAR_FILE}"
return 0
}
clean_build() {
print_step "CLEAN" "Cleaning previous build files..."
if [ -d "$BIN_DIR" ]; then
print_info "Deleting bin folder..."
rm -rf "$BIN_DIR"
fi
if [ -f "$LOG_FILE" ]; then
rm -f "$LOG_FILE"
fi
print_success "Cleanup completed!"
return 0
}
find_java_files() {
print_step "3/6" "Scanning Java source files..."
JAVA_FILES=$(find "$SRC_DIR" -name "*.java" 2>/dev/null)
FILE_COUNT=$(echo "$JAVA_FILES" | grep -c ".java" || echo "0")
if [ "$FILE_COUNT" -eq 0 ]; then
print_error "No Java source files found!"
return 1
fi
print_success "${FILE_COUNT} Java files found"
return 0
}
create_directories() {
if [ ! -d "$BIN_DIR" ]; then
mkdir -p "$BIN_DIR"
print_success "bin folder created"
else
print_info "bin folder exists"
fi
return 0
}
compile_java() {
print_step "4/6" "Compiling Java files..."
JAVAC_OPTS="-encoding UTF-8"
JAVAC_OPTS="${JAVAC_OPTS} -cp .:${JAR_FILE}"
JAVAC_OPTS="${JAVAC_OPTS} -d ${BIN_DIR}"
if [ "$BUILD_MODE" == "debug" ]; then
JAVAC_OPTS="${JAVAC_OPTS} -g:lines,vars,source"
print_info "Debug mode active"
else
JAVAC_OPTS="${JAVAC_OPTS} -g:none"
fi
if [ "$ENABLE_WARNINGS" == "true" ]; then
JAVAC_OPTS="${JAVAC_OPTS} -Xlint:all"
fi
if [ "$ENABLE_DEPRECATION" == "true" ]; then
JAVAC_OPTS="${JAVAC_OPTS} -Xlint:deprecation"
fi
START_TIME=$(date +%H:%M:%S)
ERROR_COUNT=0
WARNING_COUNT=0
echo ""
print_info "Starting compilation..."
if [ "$BUILD_MODE" == "debug" ]; then
print_info "Debug mode: Active"
fi
echo ""
javac $JAVAC_OPTS $(find "$SRC_DIR" -name "*.java") > "$LOG_FILE" 2>&1
COMPILE_RESULT=$?
END_TIME=$(date +%H:%M:%S)
if [ $COMPILE_RESULT -ne 0 ]; then
print_error "Compilation error occurred!"
echo ""
print_info "Error details:"
cat "$LOG_FILE"
echo ""
print_warning "For full error log, check: ${LOG_FILE}"
return 1
fi
# Check warning count
if grep -qi "warning" "$LOG_FILE" 2>/dev/null; then
WARNING_COUNT=$(grep -ic "warning" "$LOG_FILE" 2>/dev/null || echo "0")
if [ "$WARNING_COUNT" -gt 0 ]; then
print_warning "${WARNING_COUNT} warnings found"
print_info "For warnings, check: ${LOG_FILE}"
fi
fi
print_success "Compilation successful!"
return 0
}
count_classes() {
print_step "5/6" "Counting compiled classes..."
CLASS_COUNT=$(find "$BIN_DIR" -name "*.class" 2>/dev/null | wc -l | tr -d ' ')
print_success "${CLASS_COUNT} class files created"
return 0
}
run_tests() {
if [ "$RUN_TESTS" == "false" ]; then
return 0
fi
print_step "6/6" "Running tests..."
if [ ! -d "test" ]; then
print_info "Test folder not found, tests skipped"
return 0
fi
print_info "Test folder found, compiling tests..."
javac -encoding UTF-8 -cp "${BIN_DIR}:${JAR_FILE}" -d "${BIN_DIR}" test/tests/*.java >/dev/null 2>&1
if [ $? -ne 0 ]; then
print_error "Failed to compile tests."
return 1
fi
print_info "Running ValidationUtil tests..."
java -Dfile.encoding=UTF-8 -cp "${BIN_DIR}:${JAR_FILE}" test.tests.ValidationUtilTestRunner
if [ $? -ne 0 ]; then
print_error "Tests failed! See output above."
return 1
fi
print_success "All tests passed."
return 0
}
show_statistics() {
echo ""
echo -e "${CYAN}${BOLD}============================================${RESET}"
echo -e "${CYAN}${BOLD} Build Statistics${RESET}"
echo -e "${CYAN}${BOLD}============================================${RESET}"
echo ""
echo " Source files: ${FILE_COUNT}"
echo " Class files: ${CLASS_COUNT}"
echo " Build mode: ${BUILD_MODE}"
echo " Java version: $(java -version 2>&1 | head -n 1 | cut -d'"' -f2)"
if [ -n "$START_TIME" ]; then
echo " Start time: ${START_TIME}"
echo " End time: ${END_TIME}"
fi
if [ "$WARNING_COUNT" -gt 0 ]; then
echo -e " ${YELLOW}Warnings: ${WARNING_COUNT}${RESET}"
fi
echo ""
echo -e "${CYAN}${BOLD}============================================${RESET}"
return 0
}
show_menu() {
echo ""
print_info "Build Options:"
echo ""
echo " 1. Normal Build (Release)"
echo " 2. Debug Build"
echo " 3. Clean and Build"
echo " 4. Build and Run Tests"
echo " 5. Build and Run Application"
echo " 6. Exit"
echo ""
read -p "Your choice (1-5): " MENU_CHOICE
case "$MENU_CHOICE" in
1)
BUILD_MODE="release"
CLEAN_BUILD=false
RUN_TESTS=false
RUN_AFTER_BUILD=false
;;
2)
BUILD_MODE="debug"
CLEAN_BUILD=false
RUN_TESTS=false
RUN_AFTER_BUILD=false
;;
3)
BUILD_MODE="release"
CLEAN_BUILD=true
RUN_TESTS=false
RUN_AFTER_BUILD=false
;;
4)
BUILD_MODE="release"
CLEAN_BUILD=false
RUN_TESTS=true
RUN_AFTER_BUILD=false
;;
5)
BUILD_MODE="release"
CLEAN_BUILD=false
RUN_TESTS=false
RUN_AFTER_BUILD=true
;;
6)
exit 0
;;
*)
print_error "Invalid selection!"
sleep 2
show_menu
;;
esac
}
show_help() {
echo ""
print_info "Usage: ./compile.sh [option]"
echo ""
echo "Options:"
echo " clean - Clean previous build files"
echo " debug - Build in debug mode"
echo " test - Build and run tests"
echo " run - Build and run application"
echo " menu - Show interactive menu"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " ./compile.sh - Interactive menu"
echo " ./compile.sh clean - Clean and build"
echo " ./compile.sh debug - Build in debug mode"
echo " ./compile.sh test - Build and run tests"
echo " ./compile.sh run - Build and run application"
echo ""
}
# ============================================
# Main Program
# ============================================
print_header
# Check command line arguments
case "$1" in
clean)
CLEAN_BUILD=true
;;
debug)
BUILD_MODE="debug"
;;
test)
RUN_TESTS=true
RUN_AFTER_BUILD=false
CLEAN_BUILD=false
BUILD_MODE="release"
;;
run)
RUN_TESTS=false
RUN_AFTER_BUILD=true
CLEAN_BUILD=false
BUILD_MODE="release"
;;
menu)
show_menu
;;
help)
show_help
exit 0
;;
"")
# Default: show menu
show_menu
;;
*)
print_error "Unknown parameter: $1"
print_info "For help: ./compile.sh help"
exit 1
;;
esac
# Checks
if ! check_java; then
exit 1
fi
if ! check_dependencies; then
exit 1
fi
# Cleanup
if [ "$CLEAN_BUILD" == "true" ]; then
clean_build
fi
# File discovery
if ! find_java_files; then
exit 1
fi
# Directory creation
create_directories
# Compilation
if ! compile_java; then
exit 1
fi
# Statistics
count_classes
show_statistics
# Test execution
if ! run_tests; then
exit 1
fi
# Success message
echo ""
echo -e "${GREEN}${BOLD}============================================${RESET}"
echo -e "${GREEN}${BOLD} Build Completed Successfully!${RESET}"
echo -e "${GREEN}${BOLD}============================================${RESET}"
echo ""
echo " To run the application manually: ${CYAN}./run.sh${RESET}"
echo ""
echo " Log file: ${CYAN}${LOG_FILE}${RESET}"
echo ""
# Optionally run application after successful build
if [ "$RUN_AFTER_BUILD" == "true" ]; then
print_info "Starting application (./run.sh)..."
./run.sh
fi
exit 0