forked from duongductrong/Snapzy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_and_run.sh
More file actions
executable file
·308 lines (265 loc) · 6.5 KB
/
Copy pathbuild_and_run.sh
File metadata and controls
executable file
·308 lines (265 loc) · 6.5 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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="Snapzy"
DEBUG_BUNDLE_NAME="Snapzy Debug"
SCHEME="Snapzy"
PROJECT="Snapzy.xcodeproj"
LOG_SUBSYSTEM="${LOG_SUBSYSTEM:-Snapzy}"
MODE="run"
CONFIGURATION="${CONFIGURATION:-Debug}"
LOG_LEVEL="${LOG_LEVEL:-default,error,fault}"
CLEAN=0
QUIET=1
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DERIVED_DATA_PATH="${DERIVED_DATA_PATH:-$ROOT_DIR/.build/xcode-derived-data}"
if [[ -t 1 ]]; then
BLUE=$'\033[0;34m'
GREEN=$'\033[0;32m'
RED=$'\033[0;31m'
BOLD=$'\033[1m'
NC=$'\033[0m'
else
BLUE=""
GREEN=""
RED=""
BOLD=""
NC=""
fi
info() { printf "%sinfo:%s %s\n" "$BLUE$BOLD" "$NC" "$1"; }
success() { printf "%ssuccess:%s %s\n" "$GREEN$BOLD" "$NC" "$1"; }
fail() {
printf "%serror:%s %s\n" "$RED$BOLD" "$NC" "$1" >&2
exit 1
}
usage() {
cat <<USAGE
${BOLD}Usage:${NC} $0 [run|--logs|--telemetry|--debug|--verify] [options]
${BOLD}Modes:${NC}
run Kill, build, and launch Snapzy.app (default)
--logs, logs Launch then stream unified logs for process == "Snapzy"
--telemetry Launch then stream unified logs for subsystem == "$LOG_SUBSYSTEM"
--debug, debug Build then launch the app binary under lldb
--verify, verify Launch and confirm the Snapzy process is running
${BOLD}Options:${NC}
--configuration C Build configuration. Default: Debug
--derived-data PATH Build DerivedData path. Default: .build/xcode-derived-data
--log-level LEVELS default,info,debug,error,fault,all. Default: default,error,fault
--clean Clean before building
--verbose Show full xcodebuild output
--help, -h Show this help
${BOLD}Examples:${NC}
$0
$0 --verify
$0 --logs --log-level all
$0 --configuration Release
USAGE
}
require_macos() {
[[ "$(uname -s)" == "Darwin" ]] || fail "This script only supports macOS."
}
require_command() {
command -v "$1" >/dev/null 2>&1 || fail "Missing required command: $1"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
run)
MODE="run"
shift
;;
--logs|logs)
MODE="logs"
shift
;;
--telemetry|telemetry)
MODE="telemetry"
shift
;;
--debug|debug)
MODE="debug"
shift
;;
--verify|verify)
MODE="verify"
shift
;;
--configuration)
[[ $# -ge 2 ]] || fail "--configuration requires a value."
CONFIGURATION="$2"
shift 2
;;
--derived-data|--derived-data-path)
[[ $# -ge 2 ]] || fail "--derived-data requires a path."
DERIVED_DATA_PATH="$2"
shift 2
;;
--log-level)
[[ $# -ge 2 ]] || fail "--log-level requires a value."
LOG_LEVEL="$2"
shift 2
;;
--clean)
CLEAN=1
shift
;;
--verbose)
QUIET=0
shift
;;
--help|-h)
usage
exit 0
;;
*)
fail "Unknown option: $1"
;;
esac
done
}
message_type_predicate() {
local levels="$1"
local type_clauses=""
if [[ "$levels" == "all" ]]; then
printf ""
return
fi
IFS=',' read -r -a level_array <<<"$levels"
for level in "${level_array[@]}"; do
level="${level//[[:space:]]/}"
case "$level" in
default|info|debug|error|fault)
if [[ -n "$type_clauses" ]]; then
type_clauses="$type_clauses OR messageType == $level"
else
type_clauses="messageType == $level"
fi
;;
*)
fail "Invalid log level: '$level'. Use default, info, debug, error, fault, or all."
;;
esac
done
printf " AND (%s)" "$type_clauses"
}
process_log_predicate() {
printf "process == \"%s\"" "$APP_NAME"
message_type_predicate "$LOG_LEVEL"
}
telemetry_log_predicate() {
printf "subsystem == \"%s\"" "$LOG_SUBSYSTEM"
message_type_predicate "$LOG_LEVEL"
}
build_products_dir() {
printf "%s/Build/Products/%s" "$DERIVED_DATA_PATH" "$CONFIGURATION"
}
app_bundle_path() {
local bundle_name="$APP_NAME"
if [[ "$CONFIGURATION" == "Debug" ]]; then
bundle_name="$DEBUG_BUNDLE_NAME"
fi
printf "%s/%s.app" "$(build_products_dir)" "$bundle_name"
}
app_binary_path() {
printf "%s/Contents/MacOS/%s" "$(app_bundle_path)" "$APP_NAME"
}
stop_app() {
if pgrep -x "$APP_NAME" >/dev/null 2>&1; then
info "Stopping existing $APP_NAME process..."
pkill -x "$APP_NAME" >/dev/null 2>&1 || true
sleep 0.5
fi
}
run_xcodebuild() {
local action="$1"
local args=(
xcodebuild
-project "$PROJECT"
-scheme "$SCHEME"
-configuration "$CONFIGURATION"
-derivedDataPath "$DERIVED_DATA_PATH"
)
if [[ "$QUIET" -eq 1 ]]; then
args+=(-quiet)
fi
args+=("$action")
"${args[@]}"
}
build_app() {
cd "$ROOT_DIR"
if [[ "$CLEAN" -eq 1 ]]; then
info "Cleaning $SCHEME ($CONFIGURATION)..."
run_xcodebuild clean
fi
info "Building $SCHEME ($CONFIGURATION)..."
run_xcodebuild build
local app_bundle
app_bundle="$(app_bundle_path)"
[[ -d "$app_bundle" ]] || fail "Build finished but app bundle was not found: $app_bundle"
[[ -x "$(app_binary_path)" ]] || fail "Built app binary is not executable: $(app_binary_path)"
success "Build ready: $app_bundle"
}
open_app() {
local app_bundle
app_bundle="$(app_bundle_path)"
info "Launching $APP_NAME..."
/usr/bin/open -n "$app_bundle"
success "Launched $APP_NAME"
}
verify_app() {
open_app
sleep 2
if pgrep -x "$APP_NAME" >/dev/null 2>&1; then
success "$APP_NAME is running."
else
fail "$APP_NAME did not stay running after launch."
fi
}
stream_logs() {
local predicate="$1"
open_app
cleanup_stream() {
printf "\n"
info "Stopping $APP_NAME..."
pkill -x "$APP_NAME" >/dev/null 2>&1 || true
success "App stopped."
}
trap cleanup_stream INT TERM
info "Streaming logs for predicate: $predicate"
/usr/bin/log stream --info --debug --style compact --predicate "$predicate"
}
launch_debugger() {
require_command lldb
info "Launching under lldb..."
exec lldb -o run -- "$(app_binary_path)"
}
main() {
parse_args "$@"
require_macos
require_command xcodebuild
require_command pgrep
require_command pkill
cd "$ROOT_DIR"
stop_app
build_app
case "$MODE" in
run)
open_app
;;
logs)
stream_logs "$(process_log_predicate)"
;;
telemetry)
stream_logs "$(telemetry_log_predicate)"
;;
verify)
verify_app
;;
debug)
launch_debugger
;;
*)
fail "Unsupported mode: $MODE"
;;
esac
}
main "$@"