-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-init.sh
executable file
·85 lines (72 loc) · 2.28 KB
/
project-init.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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Constants
readonly SCRIPT_NAME="${0##*/}"
readonly BUILD_DIR="build"
# Color codes for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly NC='\033[0m' # No Color
# Function to print error messages
print_error() {
printf "${RED}Error: %s${NC}\n" "$1" >&2
}
# Function to print success messages
print_success() {
printf "${GREEN}Success: %s${NC}\n" "$1"
}
# Function to check if a command exists
command_exists() {
command -v "$1" &>/dev/null
}
# Main function
main() {
# Check if CMake is installed
if ! command_exists cmake; then
print_error "CMake is not installed. Please install CMake and try again."
exit 1
fi
# Create build directory if it doesn't exist
mkdir -p "${BUILD_DIR}"
# Generate build files and compile_commands.json
echo "Generating build files and compile_commands.json..."
if ! cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_STANDARD=17 -B "${BUILD_DIR}" .; then
print_error "Failed to generate build files."
exit 1
fi
print_success "Build files and compile_commands.json generated."
# Build the project
echo "Building the project..."
if ! cmake --build "${BUILD_DIR}"; then
print_error "Failed to build the project."
exit 1
fi
print_success "Project built successfully."
# Run the tests
echo "Running tests..."
if ! (cd "${BUILD_DIR}" && ./test_solution); then
print_error "Some tests failed."
else
print_success "All tests passed."
fi
# Symlink compile_commands.json to project root
echo "Creating symlink for compile_commands.json..."
if [[ -L compile_commands.json ]]; then
rm compile_commands.json
elif [[ -e compile_commands.json ]]; then
print_error "A file named compile_commands.json already exists and is not a symlink."
exit 1
fi
if ! ln -s "${BUILD_DIR}/compile_commands.json" .; then
print_error "Failed to create symlink for compile_commands.json."
exit 1
fi
print_success "Symlink for compile_commands.json created in project root."
echo "All tasks completed successfully!"
echo "You can now use compile_commands.json for your LSP."
echo "The project is set up to use C17 standard."
echo "To run tests again, use 'ctest' in the ${BUILD_DIR} directory."
}
# Execute main function
main "$@"