-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcmd.sh
More file actions
executable file
·308 lines (266 loc) · 7.86 KB
/
cmd.sh
File metadata and controls
executable file
·308 lines (266 loc) · 7.86 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
#!/bin/bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
current_dir=$(pwd)
reset() {
cd $current_dir
}
trap reset EXIT
lint_swift_command() {
install_swiftformat
# files: if an arg is provided use it, otherwise .
# convert arg to a relative path from app/
if [ -z "${1-}" ]; then
files="."
else
# make path absolute
if [[ "$1" != /* ]]; then
files="$current_dir/$1"
else
files="$1"
fi
fi
cd "$(git rev-parse --show-toplevel)/app" &&
mkdir -p .build/caches/swiftformat &&
$SWIFTFORMAT_PATH --config rules-header.swiftformat "$files" &&
$SWIFTFORMAT_PATH --config rules.swiftformat "$files" --cache .build/caches/swiftformat
}
# Get list of changed files based on git diff
get_changed_files() {
git diff --name-only --diff-filter=ACMR HEAD
}
# Ensures node_modules are installed in local-server
setup_local_server() {
local server_dir="$(git rev-parse --show-toplevel)/local-server"
if [ ! -d "$server_dir/node_modules" ]; then
echo "Installing local-server dependencies..."
cd "$server_dir" && yarn install && yarn build && yarn copy-to-app
fi
}
lint_ts_command() {
setup_local_server
cd "$(git rev-parse --show-toplevel)/local-server" && yarn lint --fix
}
lint_shell_command() {
cd "$(git rev-parse --show-toplevel)" &&
git ls-files '*.sh' |
xargs shfmt -w
}
lint_ruby_command() {
cd "$(git rev-parse --show-toplevel)" &&
git ls-files -z -- "*.rb" "*.rake" "**/Gemfile" "**/Rakefile" "**/Fastfile" | xargs -0 bundle exec rubocop --autocorrect
}
lint_yaml_command() {
cd "$(git rev-parse --show-toplevel)" &&
git ls-files '*.yml' '*.yaml' |
grep -v '^\.yamllint\.yml$' |
xargs npx --yes prettier@latest --write
}
sync_dependencies_command() {
cd "$(git rev-parse --show-toplevel)/app" &&
./tools/dependencies/sync.sh "$@"
}
close_xcode() {
if pgrep -x "Xcode" >/dev/null; then
# Kill Xcode
pkill -x "Xcode"
# Wait for Xcode to close
while pgrep -x "Xcode" >/dev/null; do
sleep 0.01
done
fi
}
focus_dependency_command() {
cd "$(git rev-parse --show-toplevel)/app"
if [ "${SKIP_CLOSE_XCODE:-}" != "true" ]; then
close_xcode
fi
# Reset xcode state
find . -path '*.xcuserstate' 2>/dev/null | git check-ignore --stdin | xargs -I{} rm {} || true
./tools/dependencies/focus.sh "$@"
}
build_release_command() {
cd "$(git rev-parse --show-toplevel)/app" &&
./tools/release/release.sh "$@"
}
# Install swift format using a specific version (brew doesn't support this).
install_swiftformat() {
local version="0.58.0"
local force=false
if [ "${1-}" = "--force" ]; then
force=true
fi
local target_dir="$HOME/.cmd/dev/swiftformat/$version"
local target_path="$target_dir/swiftformat"
# Check if already installed
if [ -f "$target_path" ] && [ "$force" = false ]; then
# Set environment variable with binary location
export SWIFTFORMAT_PATH="$target_path"
return 0
fi
echo "Installing swiftformat $version..."
# Create tmp directory if needed
mkdir -p "$target_dir"
# Download swiftformat
local download_url="https://github.com/nicklockwood/SwiftFormat/releases/download/$version/swiftformat.zip"
local tmp_zip="$target_dir/swiftformat.zip"
curl -L -o "$tmp_zip" "$download_url"
# Unzip
unzip -o "$tmp_zip" -d "$target_dir"
# Clean up zip file
rm "$tmp_zip"
# Remove quarantine attribute to avoid Gatekeeper prompt
xattr -d com.apple.quarantine "$target_path" 2>/dev/null || true
# Set environment variable with binary location
export SWIFTFORMAT_PATH="$target_path"
echo "✅ swiftformat $($SWIFTFORMAT_PATH --version) installed at $target_path."
}
# Xcode has a weird bug where when opening the Xcode project it will not show most files.
# This seems to happen when there's lingering files from nested Swift packages.
# This function attempts to remove such files.
clean_command() {
# Signal to the file watcher that is should not regenerate files.
touch "$(git rev-parse --show-toplevel)/.build/disable-watcher"
close_xcode
cd "$(git rev-parse --show-toplevel)/app/modules"
# Remove derived files from swift packages
find . -type d -name '.build' -exec sh -c 'rm -rf "$1"' _ {} \; 2>/dev/null
find . -not -path './.git/*' 2>/dev/null |
git check-ignore --stdin |
grep 'Package.swift\|Package.resolved' |
while read file; do rm -rf "$file"; done
# Remove lock file
rm "$(git rev-parse --show-toplevel)/.build/disable-watcher"
}
test_swift_command() {
setup_local_server
# Extract --module value if provided
focussed_module=""
parsed_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--module)
focussed_module="$2"
shift 2 # Skip both --module and its value
;;
*)
parsed_args+=("$1")
shift
;;
esac
done
if [ -n "$focussed_module" ]; then
SKIP_CLOSE_XCODE=true package_swift=$(focus_dependency_command --module "$focussed_module")
if [ "${#parsed_args[@]}" -gt 0 ]; then
cd "$(dirname $package_swift)" && swift test -Xswiftc -suppress-warnings --quiet --no-parallel "${parsed_args[@]}"
else
cd "$(dirname $package_swift)" && swift test -Xswiftc -suppress-warnings --quiet --no-parallel
fi
else
# run all tests
if [ "${#parsed_args[@]}" -gt 0 ]; then
cd "$(git rev-parse --show-toplevel)/app/modules" && swift test -Xswiftc -suppress-warnings --quiet --no-parallel "${parsed_args[@]}"
else
cd "$(git rev-parse --show-toplevel)/app/modules" && swift test -Xswiftc -suppress-warnings --quiet --no-parallel
fi
fi
}
test_ts_command() {
setup_local_server
cd "$(git rev-parse --show-toplevel)/local-server" && yarn test "$@"
}
# Main command dispatcher
command=$1
shift
case "$command" in
lint:swift)
lint_swift_command "$@"
;;
lint:ts)
lint_ts_command "$@"
;;
lint:shell)
lint_shell_command "$@"
;;
lint:rb)
lint_ruby_command "$@"
;;
lint:yaml)
lint_yaml_command "$@"
;;
lint)
# Check if --diff flag is provided
if [ "${1-}" = "--diff" ]; then
echo "Running lint on changed files only..."
changed_files=$(get_changed_files)
if [ -z "$changed_files" ]; then
echo "No changed files found."
exit 0
fi
# Group files by type
swift_files=$(echo "$changed_files" | grep '\.swift$' || true)
ts_files=$(echo "$changed_files" | grep -E 'local-server/.*\.(ts|tsx)$' || true)
shell_files=$(echo "$changed_files" | grep '\.sh$' || true)
ruby_files=$(echo "$changed_files" | grep -E '\.(rb|rake)$|Gemfile$|Rakefile$|Fastfile$' || true)
yaml_files=$(echo "$changed_files" | grep -E '\.(yml|yaml)$' || true)
# Run linters for changed file types
[ -n "$swift_files" ] && echo "Linting Swift files..." && lint_swift_command
[ -n "$ts_files" ] && echo "Linting TypeScript files..." && lint_ts_command
[ -n "$shell_files" ] && echo "Linting shell files..." && lint_shell_command
[ -n "$ruby_files" ] && echo "Linting Ruby files..." && lint_ruby_command
[ -n "$yaml_files" ] && echo "Linting YAML files..." && lint_yaml_command
echo "✓ Lint complete for changed files"
else
lint_swift_command &&
lint_ts_command &&
lint_shell_command &&
lint_ruby_command &&
lint_yaml_command
fi
;;
test:swift)
test_swift_command "$@"
;;
test:ts)
test_ts_command "$@"
;;
install:swiftformat)
install_swiftformat "$@"
;;
test)
test_swift_command &&
test_ts_command
;;
sync:dependencies)
install_swiftformat &&
sync_dependencies_command "$@"
;;
focus)
focus_dependency_command "$@"
;;
open:app)
clean_command &&
cd "$(git rev-parse --show-toplevel)/app" &&
xcode_path=$(xcode-select -p) &&
xcode_path="${xcode_path%%.app*}.app" &&
open -a "$xcode_path" "./command.xcodeproj" --args -ApplePersistenceIgnoreState YES
;;
build:release)
# build the app for release.
# use --archive to also archive the app.
build_release_command "$@"
;;
clean)
# clean artifacts that might make Xcode behave weirdly and not showing files in the file hierarchy.
clean_command
;;
watch)
# Watch file changes, and update derived files when necessary.
setup_local_server
cd "$ROOT_DIR/local-server" && yarn watch
;;
*)
echo "Command not found: $command"
exit 1
;;
esac