-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.sh
More file actions
executable file
·175 lines (155 loc) · 4.73 KB
/
compile.sh
File metadata and controls
executable file
·175 lines (155 loc) · 4.73 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
#!/usr/bin/env bash
###############################################################################
# NAME : compile.sh
# DESCRIPTION : Safe wrapper for CI/test/commit workflow.
# Subcommands:
# - test : update submodules, run CI, run tests
# - commit : update submodules, run CI, run tests,
# auto-increment VERSION, git commit & push
# AUTHOR : Your Name
# DATE CREATED : 2025-09-11
###############################################################################
# USAGE:
# ./compile.sh test
# ./compile.sh commit
###############################################################################
set -Eeuo pipefail
IFS=$'\n\t'
#------------------------------#
# Logging helpers
#------------------------------#
blue='\033[34m'; green='\033[92m'; yellow='\033[33m'; red='\033[91m'; reset='\033[0m'
log_info() { printf "${blue}[* INFO ]${reset} %s\n" "$*"; }
log_pass() { printf "${green}[+ PASS ]${reset} %s\n" "$*"; }
log_warn() { printf "${yellow}[! WARN ]${reset} %s\n" "$*"; }
log_fail() { printf "${red}[- FAIL ]${reset} %s\n" "$*"; }
trap 'log_fail "Unexpected error at ${BASH_SOURCE[0]##*/}:${LINENO}"; exit 1' ERR
#------------------------------#
# Utility checks
#------------------------------#
require_bin() {
command -v "$1" >/dev/null 2>&1 || { log_fail "Missing required command: $1"; exit 1; }
}
ensure_git_repo() {
git rev-parse --git-dir >/dev/null 2>&1 || { log_fail "Not inside a Git repository."; exit 1; }
}
#------------------------------#
# Core steps
#------------------------------#
update_submodules() {
log_info "Updating submodules (init + sync + recursive update)…"
make submodules-init
make submodules-update
log_pass "Submodules updated."
}
run_ci() {
log_info "Running CI (format + lint + tests)…"
make ci
log_pass "CI passed."
}
run_tests() {
log_info "Running test suite…"
make test
log_pass "All tests passed."
}
prompt_commit_msg() {
local msg
echo
read -rp "Enter git commit message: " msg
if [[ -z "${msg}" ]]; then
log_fail "Commit message cannot be empty."
exit 1
fi
printf "%s" "$msg"
}
bump_version() {
local version_file="VERSION"
local current next major minor patch
if [[ ! -f $version_file ]]; then
next="0.1.0"
echo "$next" > "$version_file"
log_info "VERSION file created with initial version $next"
else
current="$(<"$version_file")"
if [[ $current =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
patch=$((patch + 1))
next="${major}.${minor}.${patch}"
echo "$next" > "$version_file"
log_info "VERSION bumped: $current → $next"
else
log_warn "VERSION file malformed ($current), resetting to 0.1.0"
echo "0.1.0" > "$version_file"
next="0.1.0"
fi
fi
git add "$version_file"
echo "$next"
}
do_commit_and_push() {
ensure_git_repo
# Stage everything (including VERSION bump later)
git add -A
if git diff --cached --quiet; then
log_warn "No staged changes to commit (working tree clean)."
return 0
fi
# Commit message prompt
local msg
msg="$(prompt_commit_msg)"
# Auto bump VERSION before commit
local new_ver
new_ver="$(bump_version)"
log_info "Committing changes…"
git commit -m "$msg" -m "Version: $new_ver"
log_info "Pushing to current upstream…"
git push
log_pass "Push complete (version $new_ver)."
}
#------------------------------#
# Main
#------------------------------#
main() {
require_bin git
require_bin make
local cmd="${1:-}"
case "$cmd" in
test)
update_submodules
run_ci
run_tests
log_pass "Workflow 'test' completed successfully."
;;
commit)
update_submodules
run_ci
run_tests
do_commit_and_push
log_pass "Workflow 'commit' completed successfully."
;;
""|help|-h|--help)
cat <<'USAGE'
Usage:
./compile.sh test
- Update submodules
- Run CI (format + lint + tests)
- Run tests (explicit)
./compile.sh commit
- Update submodules
- Run CI (format + lint + tests)
- Run tests (explicit)
- Prompt for commit message
- Auto-increment VERSION (patch)
- Git commit & push
USAGE
;;
*)
log_fail "Unknown subcommand: $cmd"
echo "Try: ./compile.sh test | commit"
exit 2
;;
esac
}
main "$@"