-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios-hot-reload
309 lines (254 loc) Β· 8.27 KB
/
ios-hot-reload
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
309
#!/bin/bash
VERSION="1.0.2"
INSTALL_DIR="$HOME/.ios-hot-reload"
BIN_DIR="$INSTALL_DIR/bin"
# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
RESET='\033[0m'
# Function to print colored text
print_color() {
printf "${!1}%s${RESET}\n" "$2"
}
# Function to print a box message
print_box_message() {
local message="$1"
local width=$((${#message} + 4))
printf "\nβ%sβ\n" "$(printf 'β%.0s' $(seq 1 $width))"
printf "β ${CYAN}%s${RESET} β\n" "$message"
printf "β%sβ\n\n" "$(printf 'β%.0s' $(seq 1 $width))"
}
# Function to show a progress bar
show_progress_bar() {
local duration=$1
local steps=20
local sleep_time=$(bc <<<"scale=3; $duration / $steps")
for ((i = 0; i <= steps; i++)); do
local percentage=$((i * 100 / steps))
local completed=$((i * steps / steps))
printf "\r[%-${steps}s] %d%%" $(printf '='%.0s $(seq 1 $completed)) $percentage
sleep $sleep_time
done
printf "\n"
}
# Timestamp function
get_timestamp() {
date "+%Y-%m-%d %H:%M:%S"
}
# Function to run the hot reload
run_hot_reload() {
# Check if current directory is an Xcode project
if [ ! -d "$(pwd)/"*.xcodeproj ] && [ ! -d "$(pwd)/"*.xcworkspace ]; then
print_color "RED" "Error: Current directory is not an Xcode project."
echo "Please run this command from the root of your Xcode project."
exit 1
fi
# Find project file
PROJECT_FILE=$(find "$(pwd)" -name "*.xcodeproj")
if [ -z "$PROJECT_FILE" ]; then
print_color "RED" "Error: No Xcode project or workspace found in the current directory."
exit 1
fi
if [ -n "$PROJECT_FILE" ]; then
BUILD_CMD="xcodebuild -project ${PROJECT_FILE##*/}"
else
print_color "RED" "Error: No Xcode project or workspace found in the current directory."
exit 1
fi
# Get scheme name
SCHEME_NAME=$(xcodebuild -list -json | grep -m 1 '"name" :' | sed 's/.*: "\(.*\)".*/\1/')
if [ -z "$SCHEME_NAME" ]; then
print_color "RED" "Error: No scheme found in the project."
exit 1
fi
# Directory to watch (usually the source code directory)
WATCH_DIR="$(pwd)"
# Paths to ignore
IGNORE_PATHS=(
"--exclude" '.*build.*'
"--exclude" '.*\.xcodeproj'
"--exclude" '.*\.xcworkspace'
)
# Lock file
LOCK_FILE="/tmp/auto_build_run.lock"
# File to record the last build time
LAST_BUILD_FILE="/tmp/last_build_time"
# File to record the last message output time
LAST_MESSAGE_FILE="/tmp/last_message_time"
# Debounce time in seconds
DEBOUNCE_SECONDS=5
# Flag to indicate if a build is in progress
BUILD_IN_PROGRESS=0
# Record the active application at the start of the script
ORIGINAL_APP=$(osascript -e 'tell application "System Events" to get name of first application process whose frontmost is true')
# Function to display messages
show_message() {
current_time=$(date +%s)
if [ ! -f "$LAST_MESSAGE_FILE" ] || [ $((current_time - $(cat "$LAST_MESSAGE_FILE"))) -ge 3 ]; then
print_color "BLUE" "$1"
echo "$current_time" >"$LAST_MESSAGE_FILE"
fi
}
# Function to return focus to the specified application
return_focus() {
local app_name="$1"
osascript <<EOF
tell application "$app_name"
activate
end tell
EOF
}
# Function to ensure lock file is removed
cleanup() {
rm -f "$LOCK_FILE"
}
# Call cleanup function when script exits
trap cleanup EXIT
# Function to create a summary of the build log
summarize_build_log() {
print_color "YELLOW" "Build Summary:"
print_color "YELLOW" "Errors: $(grep -c "error:" "$BUILD_LOG")"
print_color "YELLOW" "Warnings: $(grep -c "warning:" "$BUILD_LOG")"
echo "------------------------"
}
# Function to display colored error messages
display_colored_errors() {
grep -E "error:|warning:" "$BUILD_LOG" | sort | uniq | head -n 10 |
sed 's/.*error:/\x1b[31mError:\x1b[0m/' |
sed 's/.*warning:/\x1b[33mWarning:\x1b[0m/'
}
# Build and run function
build_and_run() {
# Skip if build is in progress
if [ $BUILD_IN_PROGRESS -eq 1 ]; then
show_message "Build in progress. Skipping."
return
fi
# Implement debounce
current_time=$(date +%s)
if [ -f "$LAST_BUILD_FILE" ]; then
last_build_time=$(cat "$LAST_BUILD_FILE")
time_diff=$((current_time - last_build_time))
if [ $time_diff -lt $DEBOUNCE_SECONDS ]; then
show_message "Less than $DEBOUNCE_SECONDS seconds since last build. Skipping."
return
fi
fi
# Set build in progress flag
BUILD_IN_PROGRESS=1
print_color "MAGENTA" "π¨ $(get_timestamp) Change detected. Starting build..."
BUILD_LOG=$(mktemp)
# Generate project files
print_color "YELLOW" "π Generating project files..."
tuist generate --no-open
# Run build and redirect all output to file
print_color "YELLOW" "ποΈ Building project..."
$BUILD_CMD build -scheme "$SCHEME_NAME" -sdk iphonesimulator -configuration Debug -derivedDataPath build >"$BUILD_LOG" 2>&1
build_result=$?
# Record current time as last build time
echo "$current_time" >"$LAST_BUILD_FILE"
if [ $build_result -ne 0 ]; then
print_color "RED" "β Build failed. Error details:"
echo "------------------------"
summarize_build_log
echo "------------------------"
print_color "RED" "Unique errors and warnings:"
display_colored_errors
echo "------------------------"
print_color "YELLOW" "For full details, check $BUILD_LOG"
BUILD_IN_PROGRESS=0
return
fi
print_color "GREEN" "βββββββββββββββββββββββββββ"
print_color "GREEN" "β π BUILD SUCCEEDED π β"
print_color "GREEN" "βββββββββββββββββββββββββββ"
# Get app name
APP_PATH=$(find build -name "*.app" -print -quit)
if [ -z "$APP_PATH" ]; then
print_color "RED" "Error: Built app not found."
BUILD_IN_PROGRESS=0
return
fi
APP_NAME=$(basename "$APP_PATH" .app)
# Get Bundle ID
PLIST_BUDDY="/usr/libexec/PlistBuddy"
if [ ! -f "$PLIST_BUDDY" ]; then
print_color "RED" "Error: PlistBuddy not found. Cannot retrieve Bundle ID."
BUILD_IN_PROGRESS=0
return
fi
BUNDLE_ID=$("$PLIST_BUDDY" -c "Print :CFBundleIdentifier" "$APP_PATH/Info.plist")
if [ -z "$BUNDLE_ID" ]; then
print_color "RED" "Error: Bundle ID not found."
BUILD_IN_PROGRESS=0
return
fi
print_color "CYAN" "π± App Path: $APP_PATH"
print_color "CYAN" "π Bundle ID: $BUNDLE_ID"
# Launch simulator (does nothing if already running)
open -a Simulator
# Install and launch app
xcrun simctl install booted "$APP_PATH"
xcrun simctl launch booted "$BUNDLE_ID"
print_color "GREEN" "β
$(get_timestamp) Build and launch completed. Continuing to monitor for changes..."
# Return focus to original application
return_focus "$ORIGINAL_APP"
# Reset build in progress flag
BUILD_IN_PROGRESS=0
}
print_color "BLUE" "π Monitoring for file changes..."
print_color "BLUE" "π₯οΈ Original application: $ORIGINAL_APP"
# Initial build and run
build_and_run
# Monitor for file changes and build/run when changes occur
fswatch --event-flags -r "${IGNORE_PATHS[@]}" "$WATCH_DIR" 2>/dev/null | while read line; do
build_and_run
done
}
# Function to uninstall the script
uninstall_script() {
print_color "YELLOW" "Uninstalling ios-hot-reload..."
# Remove the installation directory
rm -rf "$INSTALL_DIR"
# Remove the completion script
rm -rf "/usr/local/etc/bash_completion.d/ios-hot-reload"
# Determine which shell configuration file to modify
SHELL_CONFIG="$HOME/.zshrc"
if [ -f "$HOME/.bashrc" ]; then
SHELL_CONFIG="$HOME/.bashrc"
fi
# Remove the PATH addition from the shell configuration file
if [ -f "$SHELL_CONFIG" ]; then
sed -i.bak '/# ios-hot-reload PATH addition/d' "$SHELL_CONFIG"
sed -i.bak '/export PATH=.*ios-hot-reload/d' "$SHELL_CONFIG"
rm -f "${SHELL_CONFIG}.bak"
fi
print_color "GREEN" "Uninstallation completed. Please restart your terminal or run 'source $SHELL_CONFIG' to apply changes."
}
# Function to display version
show_version() {
print_color "BLUE" "ios-hot-reload version $VERSION"
}
# Main command logic
case "$1" in
run)
run_hot_reload
;;
uninstall)
uninstall_script
;;
-v | --version)
show_version
;;
*)
echo "Usage: ios-hot-reload [run|uninstall|-v|--version]"
echo " run : Run the hot reload script"
echo " uninstall : Remove the script"
echo " -v|--version : Display the current version"
exit 1
;;
esac