-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·210 lines (175 loc) · 4.53 KB
/
build.sh
File metadata and controls
executable file
·210 lines (175 loc) · 4.53 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
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
BUILD_MODE="optimized"
COMMAND="build"
OUTPUT_DIR="./bin"
BINARY_NAME="byebye"
# Parse arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
debug)
BUILD_MODE="debug"
shift
;;
optimized)
BUILD_MODE="optimized"
shift
;;
install)
COMMAND="install"
shift
;;
test)
COMMAND="test"
shift
;;
clean)
COMMAND="clean"
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
show_help
exit 1
;;
esac
done
}
show_help() {
cat << EOF
${BLUE}byebye Build Script${NC}
Usage: ./build.sh [mode] [command]
${YELLOW}Modes:${NC}
debug - Build with debug symbols and no optimizations (default: optimized)
optimized - Build with optimizations and strip symbols (default)
${YELLOW}Commands:${NC}
build - Build binary (default)
install - Install binary to system
test - Run tests
clean - Clean build artifacts
${YELLOW}Examples:${NC}
./build.sh # Build optimized binary
./build.sh debug # Build debug binary
./build.sh debug test # Run tests in debug mode
./build.sh optimized install # Install optimized binary
EOF
}
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
# Clean build artifacts
clean() {
print_info "Cleaning build artifacts..."
rm -rf "${OUTPUT_DIR}"
go clean
print_success "Clean complete"
}
# Build the binary
build() {
print_info "Building ${BUILD_MODE} binary..."
mkdir -p "${OUTPUT_DIR}"
if [ "$BUILD_MODE" = "debug" ]; then
print_info "Debug build: symbols included, no optimizations"
go build \
-v \
-gcflags="all=-N -l" \
-o "${OUTPUT_DIR}/${BINARY_NAME}" \
.
else
print_info "Optimized build: stripped, maximum optimizations"
go build \
-v \
-ldflags="-s -w" \
-tags=release \
-trimpath \
-o "${OUTPUT_DIR}/${BINARY_NAME}" \
.
fi
BINARY_PATH="${OUTPUT_DIR}/${BINARY_NAME}"
SIZE=$(du -h "${BINARY_PATH}" | cut -f1)
print_success "Binary built: ${BINARY_PATH} (${SIZE})"
}
# Run tests
run_tests() {
print_info "Running tests in ${BUILD_MODE} mode..."
if [ "$BUILD_MODE" = "debug" ]; then
go test -v -race -cover ./...
else
go test -v -race -cover -run=. ./... || true
go test -bench=. -benchmem ./... || true
fi
print_success "Tests complete"
}
# Install binary
install_binary() {
# First build it
build
print_info "Installing ${BINARY_NAME}..."
INSTALL_PATH="${HOME}/.local/bin/${BINARY_NAME}"
mkdir -p "${HOME}/.local/bin"
cp "${OUTPUT_DIR}/${BINARY_NAME}" "${INSTALL_PATH}"
chmod +x "${INSTALL_PATH}"
if command -v sudo &> /dev/null && [ "$EUID" -ne 0 ]; then
print_warn "Running install as non-root. Use 'sudo ./build.sh optimized install' for system-wide install"
fi
print_success "Installed to: ${INSTALL_PATH}"
print_info "Make sure ${HOME}/.local/bin is in your PATH"
}
# Download/verify dependencies
verify_deps() {
print_info "Verifying dependencies..."
go mod download
go mod tidy
print_success "Dependencies verified"
}
# Main execution
main() {
parse_args "$@"
print_info "Build Mode: ${BUILD_MODE}"
print_info "Command: ${COMMAND}"
# Verify deps before any command
verify_deps
case $COMMAND in
build)
build
;;
test)
run_tests
;;
install)
install_binary
;;
clean)
clean
;;
*)
print_error "Unknown command: $COMMAND"
show_help
exit 1
;;
esac
print_success "Done!"
}
# Run main
main "$@"