forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrayban_dat.sh
More file actions
executable file
·399 lines (347 loc) · 10.7 KB
/
Copy pathrayban_dat.sh
File metadata and controls
executable file
·399 lines (347 loc) · 10.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
IOS_DIR="$APP_DIR/ios"
BOUNDARY_HELPER="$IOS_DIR/rayban_dat_plugin_boundary.rb"
STATE_DIR="$APP_DIR/.dart_tool/rayban_dat_build"
DEFAULT_LOCK_BACKUP="$STATE_DIR/default_Podfile.lock"
DEFAULT_GENERATED_XCCONFIG_BACKUP="$STATE_DIR/default_Generated.xcconfig"
DEFAULT_GENERATED_XCCONFIG_ABSENT="$STATE_DIR/default_Generated.xcconfig.absent"
DEFAULT_EXPORT_ENVIRONMENT_BACKUP="$STATE_DIR/default_flutter_export_environment.sh"
DEFAULT_EXPORT_ENVIRONMENT_ABSENT="$STATE_DIR/default_flutter_export_environment.sh.absent"
PODFILE_LOCK="$IOS_DIR/Podfile.lock"
PODS_MANIFEST="$IOS_DIR/Pods/Manifest.lock"
PLUGIN_METADATA="$APP_DIR/.flutter-plugins-dependencies"
PLUGIN_REGISTRANT="$IOS_DIR/Runner/GeneratedPluginRegistrant.m"
GENERATED_XCCONFIG="$IOS_DIR/Flutter/Generated.xcconfig"
FLUTTER_EXPORT_ENVIRONMENT="$IOS_DIR/Flutter/flutter_export_environment.sh"
FLUTTER_BIN="${FLUTTER_BIN:-flutter}"
POD_BIN="${POD_BIN:-pod}"
RUBY_BIN="${RUBY_BIN:-ruby}"
usage() {
cat <<'USAGE'
Usage:
scripts/rayban_dat.sh run [flutter run options]
scripts/rayban_dat.sh build [ios|ipa] [flutter build options]
scripts/rayban_dat.sh restore
The run/build commands create the dedicated Ray-Ban DAT dependency graph, use
the raybanDat Flutter flavor with OMI_RAYBAN_DAT=true, and restore the standard
mcumgr_flutter CocoaPods graph when Flutter exits. Use restore after an
interrupted transaction that could not run its EXIT cleanup.
USAGE
}
fail() {
echo "rayban-dat: $*" >&2
exit 1
}
without_dat_flag() {
(
unset OMI_RAYBAN_DAT
"$@"
)
}
run_flutter_without_dat() {
(
unset OMI_RAYBAN_DAT
cd "$APP_DIR"
"$FLUTTER_BIN" "$@"
)
}
metadata_contains_mcumgr() {
"$RUBY_BIN" -rjson -e '
document = JSON.parse(File.binread(ARGV.fetch(0)))
platform_plugins = document.fetch("plugins").fetch(ARGV.fetch(1))
exit(platform_plugins.any? { |plugin| plugin["name"] == "mcumgr_flutter" } ? 0 : 1)
' "$PLUGIN_METADATA" "$1"
}
assert_non_dat_flutter_environment() {
local generated_file
for generated_file in "$GENERATED_XCCONFIG" "$FLUTTER_EXPORT_ENVIRONMENT"; do
[[ -f "$generated_file" ]] || continue
if grep -Fq 'FLAVOR=raybanDat' "$generated_file" ||
grep -Fq 'OMI_RAYBAN_DAT=true' "$generated_file" ||
grep -Fq 'T01JX1JBWUJBTl9EQVQ9dHJ1ZQ==' "$generated_file"; then
echo "rayban-dat: DAT flavor leaked into $generated_file" >&2
return 1
fi
done
}
assert_default_generated_state() {
[[ -f "$PODFILE_LOCK" ]] || {
echo "rayban-dat: default Podfile.lock is missing" >&2
return 1
}
if [[ "${1:-}" == 'with-pods' ]]; then
[[ -f "$PODS_MANIFEST" ]] || {
echo "rayban-dat: default Pods/Manifest.lock is missing" >&2
return 1
}
cmp -s "$PODFILE_LOCK" "$PODS_MANIFEST" || {
echo "rayban-dat: default Podfile.lock and Pods/Manifest.lock do not match" >&2
return 1
}
fi
grep -q 'mcumgr_flutter' "$PODFILE_LOCK" || {
echo "rayban-dat: default Podfile.lock does not contain mcumgr_flutter" >&2
return 1
}
grep -q 'SwiftProtobuf' "$PODFILE_LOCK" || {
echo "rayban-dat: default Podfile.lock does not contain SwiftProtobuf" >&2
return 1
}
metadata_contains_mcumgr ios || {
echo "rayban-dat: default Flutter plugin metadata does not contain mcumgr_flutter" >&2
return 1
}
grep -q 'McumgrFlutterPlugin' "$PLUGIN_REGISTRANT" || {
echo "rayban-dat: default iOS plugin registrant does not contain McumgrFlutterPlugin" >&2
return 1
}
assert_non_dat_flutter_environment
}
assert_dat_generated_state() {
[[ -f "$PODFILE_LOCK" ]] || {
echo "rayban-dat: DAT Podfile.lock is missing" >&2
return 1
}
[[ -f "$PODS_MANIFEST" ]] || {
echo "rayban-dat: DAT Pods/Manifest.lock is missing" >&2
return 1
}
cmp -s "$PODFILE_LOCK" "$PODS_MANIFEST" || {
echo "rayban-dat: DAT Podfile.lock and Pods/Manifest.lock do not match" >&2
return 1
}
if grep -q 'mcumgr_flutter' "$PODFILE_LOCK"; then
echo "rayban-dat: DAT Podfile.lock still contains mcumgr_flutter" >&2
return 1
fi
if grep -q 'SwiftProtobuf' "$PODFILE_LOCK"; then
echo "rayban-dat: DAT Podfile.lock still contains the CocoaPods SwiftProtobuf copy" >&2
return 1
fi
if grep -q 'iOSMcuManagerLibrary' "$PODFILE_LOCK"; then
echo "rayban-dat: DAT Podfile.lock still contains iOSMcuManagerLibrary" >&2
return 1
fi
if metadata_contains_mcumgr ios; then
echo "rayban-dat: DAT Flutter plugin metadata still contains the iOS mcumgr_flutter entry" >&2
return 1
fi
if ! metadata_contains_mcumgr android; then
echo "rayban-dat: DAT transform unexpectedly removed the Android mcumgr_flutter entry" >&2
return 1
fi
if grep -q 'McumgrFlutterPlugin' "$PLUGIN_REGISTRANT"; then
echo "rayban-dat: DAT iOS plugin registrant still registers McumgrFlutterPlugin" >&2
return 1
fi
}
stage_original_lock() {
if [[ ! -f "$DEFAULT_LOCK_BACKUP" ]]; then
return 0
fi
cp -p "$DEFAULT_LOCK_BACKUP" "$PODFILE_LOCK"
}
verify_original_lock_unchanged() {
if [[ ! -f "$DEFAULT_LOCK_BACKUP" ]]; then
return 0
fi
if ! cmp -s "$PODFILE_LOCK" "$DEFAULT_LOCK_BACKUP"; then
echo "rayban-dat: default Podfile.lock changed during pod install" >&2
return 1
fi
if ! cmp -s "$PODS_MANIFEST" "$DEFAULT_LOCK_BACKUP"; then
echo "rayban-dat: default Pods/Manifest.lock changed during pod install" >&2
return 1
fi
}
snapshot_generated_file() {
local source_file="$1"
local backup_file="$2"
local absent_marker="$3"
rm -f "$backup_file" "$absent_marker"
if [[ -f "$source_file" ]]; then
cp -p "$source_file" "$backup_file"
else
: >"$absent_marker"
fi
}
snapshot_flutter_environment() {
snapshot_generated_file \
"$GENERATED_XCCONFIG" \
"$DEFAULT_GENERATED_XCCONFIG_BACKUP" \
"$DEFAULT_GENERATED_XCCONFIG_ABSENT"
snapshot_generated_file \
"$FLUTTER_EXPORT_ENVIRONMENT" \
"$DEFAULT_EXPORT_ENVIRONMENT_BACKUP" \
"$DEFAULT_EXPORT_ENVIRONMENT_ABSENT"
}
restore_generated_file() {
local destination_file="$1"
local backup_file="$2"
local absent_marker="$3"
if [[ -f "$backup_file" && ! -e "$absent_marker" ]]; then
cp -p "$backup_file" "$destination_file"
cmp -s "$destination_file" "$backup_file"
return 0
fi
if [[ -f "$absent_marker" && ! -e "$backup_file" ]]; then
rm -f "$destination_file"
return 0
fi
if [[ ! -e "$backup_file" && ! -e "$absent_marker" ]]; then
return 0
fi
echo "rayban-dat: invalid generated-file recovery state for $destination_file" >&2
return 1
}
restore_flutter_environment() {
local restore_failed=0
restore_generated_file \
"$GENERATED_XCCONFIG" \
"$DEFAULT_GENERATED_XCCONFIG_BACKUP" \
"$DEFAULT_GENERATED_XCCONFIG_ABSENT" || restore_failed=1
restore_generated_file \
"$FLUTTER_EXPORT_ENVIRONMENT" \
"$DEFAULT_EXPORT_ENVIRONMENT_BACKUP" \
"$DEFAULT_EXPORT_ENVIRONMENT_ABSENT" || restore_failed=1
return "$restore_failed"
}
clear_wrapper_state() {
rm -f \
"$DEFAULT_LOCK_BACKUP" \
"$DEFAULT_GENERATED_XCCONFIG_BACKUP" \
"$DEFAULT_GENERATED_XCCONFIG_ABSENT" \
"$DEFAULT_EXPORT_ENVIRONMENT_BACKUP" \
"$DEFAULT_EXPORT_ENVIRONMENT_ABSENT"
if [[ -d "$STATE_DIR" ]] && ! rmdir "$STATE_DIR"; then
echo "rayban-dat: recovery state contains unexpected files: $STATE_DIR" >&2
return 1
fi
}
restore_default_state() {
local restore_failed=0
echo "rayban-dat: restoring the default mcumgr_flutter build graph"
if ! without_dat_flag "$RUBY_BIN" "$BOUNDARY_HELPER" restore; then
echo "rayban-dat: failed to restore generated Flutter plugin files" >&2
restore_failed=1
fi
if ! run_flutter_without_dat pub get --enforce-lockfile; then
echo "rayban-dat: failed to regenerate the default Flutter plugin graph" >&2
restore_failed=1
fi
# CocoaPods must resolve the default graph from the exact pre-DAT lock. If
# the DAT lock were left in place here, it could select newer mcumgr
# transitive versions and then appear clean after the lockfile was copied.
if ! stage_original_lock; then
echo "rayban-dat: failed to stage the original Podfile.lock for CocoaPods" >&2
restore_failed=1
fi
if ! (
unset OMI_RAYBAN_DAT
cd "$IOS_DIR"
"$POD_BIN" install
); then
echo "rayban-dat: failed to reinstall the default CocoaPods graph" >&2
restore_failed=1
fi
if ! verify_original_lock_unchanged; then
restore_failed=1
fi
if ! restore_flutter_environment; then
echo "rayban-dat: failed to restore Flutter's default flavor environment" >&2
restore_failed=1
fi
if ((restore_failed == 0)) && ! assert_default_generated_state with-pods; then
restore_failed=1
fi
if ((restore_failed == 0)) && ! clear_wrapper_state; then
restore_failed=1
fi
if ((restore_failed == 0)); then
echo "rayban-dat: default build graph restored"
else
echo "rayban-dat: restore incomplete; run scripts/rayban_dat.sh restore" >&2
fi
return "$restore_failed"
}
finish_transaction() {
local action_status="$1"
local restore_status=0
trap - EXIT
restore_default_state || restore_status=$?
if ((action_status != 0)); then
exit "$action_status"
fi
exit "$restore_status"
}
[[ -f "$BOUNDARY_HELPER" ]] || fail "missing plugin boundary helper: $BOUNDARY_HELPER"
command="${1:-}"
case "$command" in
run | build | restore)
shift
;;
-h | --help)
usage
exit 0
;;
*)
usage >&2
exit 2
;;
esac
build_artifact=''
if [[ "$command" == 'build' ]]; then
if (($# == 0)) || [[ "${1:-}" == -* ]]; then
build_artifact='ios'
else
build_artifact="$1"
shift
fi
case "$build_artifact" in
ios | ipa) ;;
*) fail "unsupported build artifact '$build_artifact' (expected ios or ipa)" ;;
esac
fi
if [[ "$command" == 'restore' ]]; then
(($# == 0)) || fail 'restore does not accept additional arguments'
restore_default_state
exit $?
fi
[[ ! -e "$STATE_DIR" ]] || fail "stale DAT transaction found; run scripts/rayban_dat.sh restore first"
run_flutter_without_dat pub get --enforce-lockfile
assert_default_generated_state
mkdir -p "$STATE_DIR"
cp -p "$PODFILE_LOCK" "$DEFAULT_LOCK_BACKUP"
snapshot_flutter_environment
trap 'finish_transaction "$?"' EXIT
OMI_RAYBAN_DAT=1 "$RUBY_BIN" "$BOUNDARY_HELPER" prepare
(
export OMI_RAYBAN_DAT=1
cd "$IOS_DIR"
"$POD_BIN" install
)
assert_dat_generated_state
if [[ "$command" == 'run' ]]; then
(
export OMI_RAYBAN_DAT=1
cd "$APP_DIR"
"$FLUTTER_BIN" run \
--flavor raybanDat \
--dart-define=OMI_RAYBAN_DAT=true \
--no-pub \
"$@"
)
else
(
export OMI_RAYBAN_DAT=1
cd "$APP_DIR"
"$FLUTTER_BIN" build "$build_artifact" \
--flavor raybanDat \
--dart-define=OMI_RAYBAN_DAT=true \
--no-pub \
"$@"
)
fi