-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
executable file
·136 lines (126 loc) · 4.66 KB
/
release.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
#!/bin/sh
POSIXLY_CORRECT=1
build_OPWD="$PWD"
BASE="$(realpath "$0")" && BASE="${BASE%/*}"
if [ "$OPWD" != "$BASE" ]; then
cd "$BASE" || log_error "Unable to change directory to ${BASE##*/}. Re-execute using a POSIX shell and check again."
fi
trap 'cd "$build_OPWD"' EXIT
# CONFIGURABLE PART
LOCAL_BUILD_DIR="$BASE/cmd"
LOCAL_BUILT_DIR="$BASE/built/bin"
EXCLUDE_BINARIES="demo getconf" # Space-separated list of binaries to avoid building
# Function to log to stdout with green color
log() {
_Xashstd_reset="\033[m"
_Xashstd_color_code="\033[32m"
printf "${_Xashstd_color_code}->${_Xashstd_reset} %s\n" "$*"
}
# Function to log_warning to stdout with yellow color
log_warning() {
_Xashstd_reset="\033[m"
_Xashstd_color_code="\033[33m"
printf "${_Xashstd_color_code}->${_Xashstd_reset} %s\n" "$*"
}
# Function to log_error to stdout with red color
log_error() {
_Xashstd_reset="\033[m"
_Xashstd_color_code="\033[31m"
printf "${_Xashstd_color_code}->${_Xashstd_reset} %s\n" "$*"
exit 1
}
# Function to build Go commands in the specified directories
build_commands() {
for main_dir in "$@"; do
if [ -d "$main_dir" ]; then
log "Processing directory: $main_dir"
# Process directories containing .go files
find "$main_dir" -type d | while IFS= read -r dir; do
if [ -d "$dir" ]; then
# Check for .go files in the current directory
if find "$dir" -maxdepth 1 -type f -name '*.go' -print -quit | grep -q .; then
binary_name=$(basename "$dir")
if ! echo "$EXCLUDE_BINARIES" | grep -qw "$binary_name"; then
log "Building Go project in \"$dir\""
# Build the binary to a temporary directory
temp_bin_dir=$(mktemp -d)
go build -o "$temp_bin_dir/$binary_name" "$dir"
# Move the binary to the target directory
mv "$temp_bin_dir/$binary_name" "$LOCAL_BUILT_DIR"
rm -rf "$temp_bin_dir"
else
log_warning "Skipping excluded binary: $binary_name"
fi
fi
elif [ "$main_dir" != "" ]; then
log_warning "Directory \"$dir\" does not exist"
fi
done
elif [ "$main_dir" != "" ]; then
log_warning "Directory \"$main_dir\" does not exist"
fi
done
}
# Function to clean up the built directory and remove binaries in specified main directories
clean_up() {
# Remove the built directory
log "Remove $(dirname "$LOCAL_BUILT_DIR")"
rm -rf "$(dirname "$LOCAL_BUILT_DIR")" >/dev/null 2>&1
# Process each main directory
for main_dir in "$@"; do
if [ -d "$main_dir" ]; then
log "Processing directory: $main_dir"
# Find all directories
find "$main_dir" -type d | while IFS= read -r dir; do
# Check for Go projects and clean them
if [ -d "$dir" ] && find "$dir" -maxdepth 1 -type f -name '*.go' -print -quit | grep -q .; then
log "Cleaning Go project in $dir"
(cd "$dir" && go clean)
fi
done
elif [ "$main_dir" != "" ]; then
log_warning "Directory \"$main_dir\" does not exist"
fi
done
}
# Function to update dependencies for Go projects
update_go_deps() {
for main_dir in "$@"; do
if [ -d "$main_dir" ]; then
log "Searching for Go projects in: $main_dir"
# Find directories containing .go files
find "$main_dir" -type d | while IFS= read -r dir; do
if [ -d "$dir" ] && find "$dir" -maxdepth 1 -type f -name '*.go' -print -quit | grep -q .; then
log "Updating Go deps for: $dir"
(cd "$dir" && go get -u && go mod tidy)
fi
done
elif [ "$main_dir" != "" ]; then
log_warning "Directory \"$main_dir\" does not exist"
fi
done
}
case "$1" in
"" | "build")
log "Starting build process"
mkdir -p "$LOCAL_BUILT_DIR"
build_commands "$LOCAL_BUILD_DIR"
log "Build process completed"
;;
"clean")
shift
log "Starting clean process"
clean_up "$LOCAL_BUILD_DIR"
log "Clean process completed"
;;
"deps")
log "Updating dependencies"
update_go_deps "$LOCAL_BUILD_DIR"
log "Dependencies updated"
;;
*)
echo "Usage: $0 {build|clean|deps}"
exit 1
;;
esac
# tar -czvf "./a-utils-$GOARCH-$GOOS.tar.gz" -C ./built/bin .