agentic-mobile-kit 0.7.9 → 0.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentic-mobile-kit",
3
- "version": "0.7.9",
3
+ "version": "0.7.11",
4
4
  "description": "One setup for AI-assisted development of React Native apps, bare or Expo: shared rules and workflows for Claude Code, Codex and Antigravity, git hooks, device checks with screenshots, and release tooling for the App Store and Google Play.",
5
5
  "license": "MIT",
6
6
  "author": "prakashpro1",
@@ -20,7 +20,7 @@ This project may also use other AI workflow frameworks (for example Superpowers,
20
20
  | Add a library (ask first) | `npx expo install <package>`: the version that matches the SDK |
21
21
  | Lint, typecheck, test (each skips what the app doesn't have yet) | `sh scripts/amk/pm-run.sh lint`, `sh scripts/amk/pm-run.sh typecheck`, `sh scripts/amk/pm-run.sh test` |
22
22
  | Check dependency versions and the app config | `npx expo install --check`, `npx expo-doctor` |
23
- | Check on devices (release builds, Maestro flows, screenshots) | `sh scripts/amk/verify.sh all --spec <id>`; another iOS scheme: `--scheme <name>`; only some screens: `--screens phone` (or `tablet`, `landscape`, …) |
23
+ | Check on devices (release builds, Maestro flows, screenshots) | `sh scripts/amk/verify.sh all --spec <id>`; another iOS scheme: `--scheme <name>`; only some screens: `--screens phone` (or `tablet`, `landscape`, …); on a real iPhone or iPad the user names: `AMK_IOS_DEVICE=<its UDID>` (the flows clear the app's data on it) |
24
24
  | Check every device at the same time, on a machine with room for it | `sh scripts/amk/verify.sh plan --spec <id>` says whether, and prints the commands: one per device, each for an `m-device-checker` subagent |
25
25
  | Specs: progress, format check, fold a finished feature into the living spec | `node scripts/amk/specs.js status`, `node scripts/amk/specs.js check`, `node scripts/amk/specs.js merge <id>` |
26
26
  | Check a release before tagging it (versions, permission texts, debug leftovers) | `node scripts/amk/release-check.js` |
@@ -20,7 +20,7 @@ This project may also use other AI workflow frameworks (for example Superpowers,
20
20
  | iOS pods (after native dependency changes) | `sh scripts/amk/pod-install.sh` |
21
21
  | iOS build (simulator, no signing) | `sh scripts/amk/ios-build.sh` |
22
22
  | Android build | `cd android && ./gradlew assembleDebug` |
23
- | Check on devices (release builds, Maestro flows, screenshots) | `sh scripts/amk/verify.sh all --spec <id>`; with product flavors it checks the first unless `--flavor <name>`; another iOS scheme: `--scheme <name>`; only some screens: `--screens phone` (or `tablet`, `landscape`, …) |
23
+ | Check on devices (release builds, Maestro flows, screenshots) | `sh scripts/amk/verify.sh all --spec <id>`; with product flavors it checks the first unless `--flavor <name>`; another iOS scheme: `--scheme <name>`; only some screens: `--screens phone` (or `tablet`, `landscape`, …); on a real iPhone or iPad the user names: `AMK_IOS_DEVICE=<its UDID>` (the flows clear the app's data on it) |
24
24
  | Check every device at the same time, on a machine with room for it | `sh scripts/amk/verify.sh plan --spec <id>` says whether, and prints the commands: one per device, each for an `m-device-checker` subagent |
25
25
  | Specs: progress, format check, fold a finished feature into the living spec | `node scripts/amk/specs.js status`, `node scripts/amk/specs.js check`, `node scripts/amk/specs.js merge <id>` |
26
26
  | Check a release before tagging it (versions, permission texts, debug leftovers) | `node scripts/amk/release-check.js` |
@@ -15,7 +15,7 @@ const inProgress = d => {
15
15
  const specs = fs.existsSync('specs') ? fs.readdirSync('specs').filter(inProgress) : [];
16
16
  if (specs.length === 0) process.exit(0); // no spec in progress (quick change, bug fix, or finished work)
17
17
 
18
- const changed = execSync('git status --porcelain', { encoding: 'utf8' })
18
+ const changed = execSync('git status --porcelain --untracked-files=all', { encoding: 'utf8' }) // every new file, not just its new folder
19
19
  .split('\n').filter(Boolean).map(l => l.slice(3));
20
20
  const codeChanged = changed.some(f => !f.startsWith('specs/') && !f.startsWith('.amk/'));
21
21
  const handoffUpdated = changed.some(f => /^specs\/[^/]+\/progress\.md$/.test(f));
@@ -4,6 +4,9 @@
4
4
  # archives with: apps with one per environment (for example "Release Staging") get theirs.
5
5
  # AMK_IOS_SCHEME picks the scheme (default: the one named like the workspace).
6
6
  # Output goes to ios/DerivedData: ios/build holds React Native codegen files from pod install.
7
+ # `ios-build.sh device <UDID>` builds for that iPhone or iPad instead, signed by Xcode's automatic signing for the team
8
+ # in AMK_IOS_TEAM, with the Apple account in Xcode: the first time, Xcode registers the device with the team and makes a
9
+ # development profile for it. The project's own signing settings stay as they are.
7
10
  set -eu
8
11
  config=${CONFIGURATION:-Debug}
9
12
  workspace=$(ls -d ios/*.xcworkspace 2>/dev/null | head -n 1)
@@ -15,6 +18,17 @@ if [ "$config" = Release ]; then
15
18
  [ -n "$config" ] || config=Release
16
19
  fi
17
20
 
21
+ if [ "${1:-}" = device ]; then
22
+ [ -n "${AMK_IOS_TEAM:-}" ] || { echo "Set AMK_IOS_TEAM to the Apple team to sign with (Xcode > Settings > Accounts lists them)."; exit 1; }
23
+ # signed for development whatever the project signs its releases with (a distribution profile doesn't install)
24
+ xcodebuild -workspace "$workspace" -scheme "$scheme" -configuration "$config" -destination "id=$2" \
25
+ -derivedDataPath ios/DerivedData -allowProvisioningUpdates -allowProvisioningDeviceRegistration \
26
+ DEVELOPMENT_TEAM="$AMK_IOS_TEAM" CODE_SIGN_STYLE=Automatic CODE_SIGN_IDENTITY="Apple Development" \
27
+ PROVISIONING_PROFILE_SPECIFIER= -quiet build
28
+ echo "iOS device build OK ($scheme, $config, team $AMK_IOS_TEAM)"
29
+ exit 0
30
+ fi
31
+
18
32
  # only this Mac's simulator architecture: a Release build would otherwise compile Intel and Apple silicon both,
19
33
  # and fail to link libraries that ship without an Intel simulator slice. Not when the project or a pod excludes it
20
34
  # for the simulator (Google's ML Kit pods exclude arm64): then Xcode builds what the project allows.
@@ -20,10 +20,17 @@
20
20
  # (no flows found: a smoke check that launches the app and takes a screenshot)
21
21
  # Evidence per screen: ios/ and android/ (phone, portrait), then -landscape/, -tablet/ and -tablet-landscape/
22
22
  # Env: AMK_ANDROID_FLAVOR, AMK_IOS_SCHEME, AMK_SCREENS (defaults for the options), AMK_IOS_DEVICE (simulator name
23
- # or UDID), AMK_ANDROID_DEVICE (adb serial: the only way to check on a phone, whose app data the flows clear),
23
+ # or UDID, or a connected iPhone's or iPad's: the only way to check on one, whose app data the flows clear),
24
+ # AMK_ANDROID_DEVICE (adb serial: the only way to check on a phone, whose app data the flows clear),
24
25
  # AMK_ANDROID_AVD (the emulator to use, started if it isn't running; other devices are left alone),
25
- # AMK_IOS_TABLET (iPad simulator name or UDID), AMK_ANDROID_TABLET_AVD (tablet emulator), AMK_ANDROID_TABLET (serial),
26
- # AMK_PARALLEL (1 or 0: overrides plan's choice)
26
+ # AMK_IOS_TABLET (iPad simulator, or a connected iPad: name or UDID), AMK_ANDROID_TABLET_AVD (tablet emulator),
27
+ # AMK_ANDROID_TABLET (serial), AMK_IOS_TEAM (the Apple team ID that signs builds for real iPhones and iPads;
28
+ # default: the project's), AMK_PARALLEL (1 or 0: overrides plan's choice), AMK_DEVICE_WAIT (minutes to wait for a
29
+ # device in use, 30), AMK_LOCK_DIR (where runs on this machine claim devices; /tmp/agentic-mobile-kit-<user id>)
30
+ # A device in use by another run (any project on this machine) is skipped for a free one of the same kind, or waited
31
+ # for when it was named or none is free.
32
+ # Real iPhones and iPads (iOS 17 or later): Maestro doesn't run on them, so their flows run through maestro-runner, with
33
+ # WebDriverAgent signed by the same team. The app is opened once first, and crash reports it leaves join the evidence.
27
34
  # Flows that log in read ${MAESTRO_EMAIL} and the like from a local, gitignored .maestro/.env.local (never committed).
28
35
  set -eu
29
36
 
@@ -77,32 +84,113 @@ screens_for() {
77
84
  maestro_version() { maestro --version 2> /dev/null | tail -n 1 | tr -d '\r '; }
78
85
  maestro_at_least() { [ "$(printf '%s\n%s\n' "$1" "$(maestro_version)" | sort -V | head -n 1)" = "$1" ]; }
79
86
 
87
+ # Locks shared by every project and agent on this machine, so two runs never use one device (or build one project)
88
+ # at once: a run claims a device by creating a folder named after it, and gives it back when it's done with it. A
89
+ # claim whose run ended without giving it back is taken over.
90
+ locks=${AMK_LOCK_DIR:-/tmp/agentic-mobile-kit-$(id -u 2> /dev/null || echo 0)}
91
+ mkdir -p "$locks" 2> /dev/null || true
92
+ project=$(printf %s "$root" | cksum | cut -d ' ' -f 1)
93
+ held=""
94
+ lock_path() { echo "$locks/$(printf %s "$1" | tr -c 'A-Za-z0-9._-' _)"; }
95
+ # whether a Maestro run started outside the kit is using device $1 (its command line names the device)
96
+ maestro_on() { ps -ax -o command 2> /dev/null | grep -E -- "--(device|udid)[ =]$1( |$)" | grep -q maestro; }
97
+ # claims $1 for this run; with a device ID in $2, also when no other Maestro run is using that device
98
+ claim() {
99
+ l=$(lock_path "$1")
100
+ if mkdir "$l" 2> /dev/null; then
101
+ printf '%s\n%s\n%s\n' "$$" "$(basename "$root") (${label:-${spec:-$mode}})" "$(date +%H:%M)" > "$l/owner"
102
+ held="$held $l"
103
+ if [ -n "${2:-}" ] && maestro_on "$2"; then unclaim "$1"; return 1; fi
104
+ return 0
105
+ fi
106
+ owner=$(sed -n 1p "$l/owner" 2> /dev/null || true)
107
+ [ "$owner" != "$$" ] || return 0 # this run holds it already
108
+ # its run ended, or was stopped before it could say who it was
109
+ if { [ -n "$owner" ] && ! kill -0 "$owner" 2> /dev/null; } || { [ -z "$owner" ] && [ -n "$(find "$l" -maxdepth 0 -mmin +1 2> /dev/null)" ]; }; then
110
+ rm -f "$l/owner"
111
+ rmdir "$l" 2> /dev/null && { claim "$@"; return; }
112
+ fi
113
+ return 1
114
+ }
115
+ unclaim() { l=$(lock_path "$1"); rm -f "$l/owner"; rmdir "$l" 2> /dev/null || true; held=$(echo "$held" | sed "s| $l||"); }
116
+ release() { for l in $held; do rm -f "$l/owner"; rmdir "$l" 2> /dev/null || true; done; held=""; }
117
+ trap release EXIT
118
+ trap 'exit 130' INT TERM
119
+ holder() {
120
+ l=$(lock_path "$1")
121
+ if [ -f "$l/owner" ]; then echo "$(sed -n 2p "$l/owner") since $(sed -n 3p "$l/owner")"; else echo "another Maestro run"; fi
122
+ }
123
+ # claims $1 as claim does, waiting while it's in use (AMK_DEVICE_WAIT minutes, 30 by default); $3 names it in messages
124
+ claim_wait() {
125
+ claim "$1" "${2:-}" && return 0
126
+ echo " waiting for $3: in use by $(holder "$1")"
127
+ waited=0
128
+ until claim "$1" "${2:-}"; do
129
+ [ "$waited" -lt $((${AMK_DEVICE_WAIT:-30} * 60)) ] || { echo " gave up waiting for $3 after ${AMK_DEVICE_WAIT:-30} minutes"; return 1; }
130
+ sleep 5
131
+ waited=$((waited + 5))
132
+ done
133
+ }
134
+
80
135
  run_flow() { # screen device app_id file name orientation
81
136
  # Maestro's own log, which has errors its output leaves out; it adds to the file, so one run's log at a time
82
137
  debug="$evidence/maestro-logs/$1-$5"
83
138
  mkdir -p "$debug" "$out/screenshots"
84
139
  rm -f "$debug/maestro.log"
85
- find "$debug" -path '*/takeScreenshot/*.png' -exec rm -f {} + # an earlier run's, which aren't this run's evidence
140
+ # an earlier run's screenshots, which aren't this run's evidence
141
+ find "$debug" \( -path '*/takeScreenshot/*.png' -o -path '*/assets/*.png' \) -exec rm -f {} +
86
142
  target=$4
87
- if [ "$6" != PORTRAIT ]; then
88
- # a turned screen: the flow runs inside one that turns the device first and back to portrait after. Maestro
89
- # reads the path in it itself, so it's a native path (a Windows path in Git Bash)
90
- native=$(cd "$(dirname "$4")" && { pwd -W 2> /dev/null || pwd; })/$(basename "$4")
143
+ # Maestro reads the path in the flows below itself, so it's a native path (a Windows path in Git Bash)
144
+ native=$(cd "$(dirname "$4")" && { pwd -W 2> /dev/null || pwd; })/$(basename "$4")
145
+ if is_real "$2"; then
146
+ # on a real device every flow runs inside one, first: the first launchApp maestro-runner finds in a flow decides
147
+ # whether it answers alerts itself, and for most it does (for permission prompts, which it can't allow ahead),
148
+ # pressing the last button of the app's own alerts too, such as a Delete. Maestro never answers an app's alerts.
149
+ # So one that says to leave permissions alone comes first, where it never runs (on Android only)
150
+ on_complete="" turn=""
151
+ if [ "$6" != PORTRAIT ]; then
152
+ # WebDriverAgent turns the device, then fails the step unless the app in front turned too: here the home
153
+ # screen, which most iPhones never turn. Whether the app turned shows in its screenshots. It knows
154
+ # LANDSCAPE_LEFT only as "landscape"
155
+ on_complete='onFlowComplete:\n - setOrientation:\n orientation: PORTRAIT\n optional: true\n'
156
+ turn='- setOrientation:\n orientation: landscape\n optional: true\n'
157
+ fi
158
+ printf "appId: %s\n${on_complete}---\n- runFlow:\n when:\n platform: Android\n commands:\n - launchApp:\n permissions:\n all: unset\n${turn}- runFlow: '%s'\n" "$3" "$native" > "$debug/screen.yaml"
159
+ target=$debug/screen.yaml
160
+ elif [ "$6" != PORTRAIT ]; then
161
+ # a turned screen: the flow runs inside one that turns the device first and back to portrait after
91
162
  printf "appId: %s\nonFlowComplete:\n - setOrientation: PORTRAIT\n---\n- setOrientation: %s\n- runFlow: '%s'\n" "$3" "$6" "$native" > "$debug/screen.yaml"
92
163
  target=$debug/screen.yaml
93
164
  fi
165
+ rc=0
166
+ if is_real "$2"; then
167
+ # it also saves each named screenshot next to its flow file: the ones it makes there move to its log folder, so
168
+ # the project stays as it was (they're in its report too)
169
+ pngs() { find "$(dirname "$4")" \( -path "$root/.amk" -o -name node_modules \) -prune -o -name '*.png' -print | sort; }
170
+ pngs "$@" > "$debug/.pngs"
171
+ # with the app's build: on a real device, clearState installs the app again
172
+ MAESTRO_RUNNER_NO_TELEMETRY=1 DO_NOT_TRACK=1 npx -y "$runner" --platform ios --driver wda --device "$2" \
173
+ --team-id "$team" --wda-bundle-id "$3.wda" --app-file "$device_app" test -e APP_ID="$3" --output "$debug" \
174
+ --flatten "$target" > "$out/$5.log" 2>&1 || rc=$?
175
+ pngs "$@" | comm -13 "$debug/.pngs" - | while IFS= read -r f; do mv "$f" "$debug/"; done
176
+ # its takeScreenshot files, not the ones of a step that failed (<step>-after.png)
177
+ find "$debug" -path '*/assets/*.png' ! -name '*-[0-9][0-9][0-9]-after.png' -exec cp {} "$out/screenshots/" \;
178
+ return $rc
179
+ fi
94
180
  # screenshots use relative names: older Maestro saves them in the current folder, then in --test-output-dir, and
95
181
  # from 2.7 in the debug output under <flow>/takeScreenshot/, from where they join the screen's evidence
96
- rc=0
97
182
  (cd "$out" && maestro --device "$2" test --test-output-dir "$out" --debug-output "$debug" --flatten-debug-output \
98
183
  -e APP_ID="$3" "$target" > "$out/$5.log" 2>&1) || rc=$?
99
184
  find "$debug" -path '*/takeScreenshot/*.png' -exec cp {} "$out/screenshots/" \;
100
185
  return $rc
101
186
  }
102
187
 
103
- run_flows() { # screen device app_id orientation (PORTRAIT, or LANDSCAPE_LEFT to turn it)
188
+ run_flows() { # screen device app_id orientation (PORTRAIT, or LANDSCAPE_LEFT to turn it) [again]
104
189
  out="$evidence/$1"
105
190
  mkdir -p "$out"
191
+ # this run's screenshots are the ones newer than this; the status before it, for running the screen again
192
+ : > "$out/.started"
193
+ [ -n "${5:-}" ] || before=$status
106
194
  files=$(flow_files)
107
195
  if [ -z "$files" ]; then
108
196
  # launchApp returns before the app has drawn, and a splash screen stays up until the JavaScript has loaded:
@@ -117,16 +205,30 @@ run_flows() { # screen device app_id orientation (PORTRAIT, or LANDSCAPE_LEFT to
117
205
  name=$(basename "$f" .yaml)
118
206
  ok=0
119
207
  run_flow "$1" "$2" "$3" "$f" "$name" "$4" || ok=$?
120
- # an emulator sometimes drops its adb connection for a moment: reconnect, then run the flow once more
121
- if [ "$ok" != 0 ] && [ "${1%%-*}" = android ] && grep -qs 'device offline' "$debug/maestro.log"; then
208
+ # Maestro now and then stops before a flow starts (it printed nothing), and an emulator sometimes drops its adb
209
+ # connection for a moment: run the flow once more (after reconnecting)
210
+ if [ "$ok" != 0 ] && [ ! -s "$out/$name.log" ]; then
211
+ echo " $1: Maestro stopped before flow $name started; running it once more"
212
+ ok=0
213
+ run_flow "$1" "$2" "$3" "$f" "$name" "$4" || ok=$?
214
+ elif [ "$ok" != 0 ] && [ "${1%%-*}" = android ] && grep -qs 'device offline' "$debug/maestro.log"; then
122
215
  echo " $1: the device went offline during flow $name; running it once more"
123
216
  "$adb" reconnect offline > /dev/null 2>&1 || true
124
217
  android_ready "$2" || true
125
218
  ok=0
126
219
  run_flow "$1" "$2" "$3" "$f" "$name" "$4" || ok=$?
220
+ elif [ "$ok" != 0 ] && is_real "$2" && grep -qs 'explicitly trusted' "$out/$name.log"; then
221
+ # as launch_check found it does: here after clearState installed the app again, which it opened a moment later
222
+ echo " $1: the device didn't open the app it had just installed again; running flow $name once more"
223
+ sleep 15
224
+ ok=0
225
+ run_flow "$1" "$2" "$3" "$f" "$name" "$4" || ok=$?
127
226
  fi
128
227
  if [ "$ok" = 0 ]; then
129
228
  passed=$((passed + 1))
229
+ elif is_real "$2"; then
230
+ echo " $1: flow $name failed, see ${out#"$root"/}/$name.log and maestro-runner's own log ${debug#"$root"/}/maestro-runner.log"
231
+ status=1
130
232
  else
131
233
  echo " $1: flow $name failed, see ${out#"$root"/}/$name.log and Maestro's own log ${debug#"$root"/}/maestro.log"
132
234
  status=1
@@ -140,11 +242,40 @@ run_flows() { # screen device app_id orientation (PORTRAIT, or LANDSCAPE_LEFT to
140
242
  rm -f "$png.bmp"
141
243
  done ;;
142
244
  esac
245
+ # a screen checked in the wrong orientation would pass for another screen: a turned one's screenshots are wider than
246
+ # tall, an upright one's taller than wide. A device that didn't turn usually does the next time
247
+ case $1 in *-landscape) want=wide was=upright ;; *) want=tall was=turned ;; esac
248
+ wrong=$(find "$out" -name '*.png' -newer "$out/.started" | WANT=$want node -e '
249
+ const fs = require("fs");
250
+ for (const f of fs.readFileSync(0, "utf8").split("\n").filter(Boolean)) {
251
+ const b = Buffer.alloc(24), fd = fs.openSync(f, "r");
252
+ fs.readSync(fd, b, 0, 24, 0);
253
+ fs.closeSync(fd);
254
+ if ((b.readUInt32BE(16) > b.readUInt32BE(20)) !== (process.env.WANT === "wide")) console.log(f);
255
+ }' | wc -l | tr -d ' ')
256
+ if [ "$wrong" != 0 ] && [ -z "${5:-}" ]; then
257
+ echo " $1: $wrong screenshots show the screen $was, not as this screen needs; running its flows once more"
258
+ find "$out" -name '*.png' -newer "$out/.started" -exec rm -f {} +
259
+ status=$before
260
+ run_flows "$1" "$2" "$3" "$4" again
261
+ return
262
+ fi
143
263
  shots=$(find "$out" -name '*.png' | wc -l | tr -d ' ')
144
264
  echo "$1: flows $passed/$total passed, $shots screenshots in ${out#"$root"/}"
145
265
  if [ "$shots" = 0 ]; then
146
266
  echo " $1: no screenshots were saved, so there's no evidence; treating this as a failure"
147
267
  status=1
268
+ elif [ "$wrong" != 0 ]; then
269
+ echo " $1: $wrong screenshots show the screen $was: the device or the app didn't turn, so this screen isn't checked; treating this as a failure"
270
+ status=1
271
+ # an app that sets its orientation in code (an orientation library, or native code) can keep it although its native
272
+ # settings allow turning: where it does, so the team can say which is right
273
+ set_in=$(git grep -l -E 'lockTo(Portrait|Landscape)|OrientationLock\.|lockPlatformAsync|setRequestedOrientation|supportedInterfaceOrientationsFor' \
274
+ -- ':!scripts/amk' ':!*.md' 2> /dev/null | head -n 3 | tr '\n' ' ')
275
+ if [ -n "$set_in" ]; then
276
+ case $was in upright) other=landscape ;; *) other=portrait ;; esac
277
+ echo " $1: the app sets its orientation in its code (${set_in% }), which can keep it $was. If it's meant to stay $was, take $other out of its native settings (UISupportedInterfaceOrientations in Info.plist, screenOrientation in AndroidManifest.xml) and this screen isn't checked any more; otherwise change that code"
278
+ fi
148
279
  fi
149
280
  }
150
281
 
@@ -153,6 +284,10 @@ run_flows() { # screen device app_id orientation (PORTRAIT, or LANDSCAPE_LEFT to
153
284
  check_screens() { # platform phone tablet app_id screens…
154
285
  p=$1 phone=$2 tab=$3 id=$4
155
286
  shift 4
287
+ # before 2.6, two Maestro runs clash even on different devices (one port): checks on this machine take turns
288
+ if ! maestro_at_least 2.6.0; then
289
+ claim_wait maestro "" "Maestro (before 2.6 it runs one check at a time on this machine)" || { status=1; return; }
290
+ fi
156
291
  for screen in "$@"; do
157
292
  case $screen in
158
293
  tablet-*) [ -n "$tab" ] || continue; on=$tab where=$p-tablet ;;
@@ -165,15 +300,16 @@ check_screens() { # platform phone tablet app_id screens…
165
300
  upright=LANDSCAPE_LEFT turned=PORTRAIT
166
301
  fi
167
302
  case $screen in *-landscape) turn=$turned where=$where-landscape ;; *) turn=$upright ;; esac
168
- # Maestro turns devices from 2.0 on, and taps the right place on a turned iPad from 2.3
303
+ # Maestro turns devices from 2.0 on, and taps the right place on a turned iPad from 2.3 (real ones: maestro-runner)
169
304
  need=2.0.0
170
305
  case $where in ios-tablet*) need=2.3.0 ;; esac
171
- if [ "$turn" != PORTRAIT ] && ! maestro_at_least $need; then
306
+ if [ "$turn" != PORTRAIT ] && ! is_real "$on" && ! maestro_at_least $need; then
172
307
  echo "$where: not checked: turning the device needs Maestro ${need%.0} or later (this is $(maestro_version))"
173
308
  continue
174
309
  fi
175
310
  run_flows "$where" "$on" "$id" "$turn"
176
311
  done
312
+ release # this platform's devices, and Maestro's turn, for other runs
177
313
  }
178
314
 
179
315
  quick() {
@@ -210,54 +346,175 @@ ios() {
210
346
  sh scripts/amk/pod-install.sh > "$evidence/pod-install.log" 2>&1
211
347
  fi
212
348
  fi
213
- # the simulators first, so they boot while Xcode builds
349
+ # the simulators first, so they boot while Xcode builds. A real iPhone or iPad only when named, as the flows clear
350
+ # the app's data on it
214
351
  device="" tablet=""
215
352
  if [ "$build_only" = 0 ]; then
216
353
  screens=$(screens_for ios)
217
354
  [ -n "$screens" ] || { echo "ios: none of the app's screens is $only"; return; }
218
355
  if echo "$screens" | grep -q '^phone-'; then
219
- device=$(simulator iPhone "${AMK_IOS_DEVICE:-}")
356
+ take_simulator iPhone "${AMK_IOS_DEVICE:-}" || { status=1; return; }
357
+ device=$taken
220
358
  if [ -z "$device" ]; then
221
- if [ -n "${AMK_IOS_DEVICE:-}" ]; then echo "ios: no simulator has the name or UDID \"$AMK_IOS_DEVICE\""; else echo "ios: no iPhone simulator found (set AMK_IOS_DEVICE)"; fi
359
+ if [ -n "${AMK_IOS_DEVICE:-}" ]; then echo "ios: no simulator or connected iPhone or iPad has the name or UDID \"$AMK_IOS_DEVICE\""; else echo "ios: no iPhone simulator found (set AMK_IOS_DEVICE)"; fi
222
360
  status=1; return
223
361
  fi
224
362
  fi
225
363
  if echo "$screens" | grep -q '^tablet-'; then
226
- tablet=$(simulator iPad "${AMK_IOS_TABLET:-}")
227
- if [ -z "$tablet" ] && [ -n "${AMK_IOS_TABLET:-}" ]; then echo "ios: no simulator has the name or UDID \"$AMK_IOS_TABLET\""; status=1; return; fi
364
+ take_simulator iPad "${AMK_IOS_TABLET:-}" || { status=1; return; }
365
+ tablet=$taken
366
+ if [ -z "$tablet" ] && [ -n "${AMK_IOS_TABLET:-}" ]; then echo "ios: no simulator or connected iPad has the name or UDID \"$AMK_IOS_TABLET\""; status=1; return; fi
228
367
  [ -n "$tablet" ] || echo "ios: the app runs on iPad, but there's no iPad simulator, so iPads aren't checked (add one in Xcode, or set AMK_IOS_TABLET)"
229
368
  fi
230
- for d in $device $tablet; do xcrun simctl boot "$d" 2> /dev/null || true; done
369
+ [ -n "$device$tablet" ] || return
370
+ for d in $device $tablet; do
371
+ if is_real "$d"; then echo "ios: checking on $d, a real device; the flows clear the app's data on it"
372
+ else xcrun simctl boot "$d" 2> /dev/null || true; fi
373
+ done
374
+ else
375
+ # the plan's build step: a build for each real device named too
376
+ for n in "${AMK_IOS_DEVICE:-}" "${AMK_IOS_TABLET:-}"; do
377
+ [ -z "$n" ] || [ -n "$(simulators iPhone "$n")" ] || { r=$(real_ios "$n"); [ -z "$r" ] || reals="$reals $r"; }
378
+ done
379
+ fi
380
+ team=$(ios_team)
381
+ if [ -n "$reals" ] && [ -z "$team" ]; then
382
+ echo "ios: a real device runs only apps signed by an Apple team: set AMK_IOS_TEAM to your team ID (Xcode > Settings > Accounts lists them)"
383
+ status=1; return
231
384
  fi
385
+ sims=$build_only # whether to build for simulators: the plan's build step always does
386
+ for d in $device $tablet; do is_real "$d" || sims=1; done
387
+ # one build of this project at a time; a check without its own build waits for one that's under way
388
+ claim_wait "build-ios-$project" "" "the iOS build of this project" || { status=1; return; }
232
389
  if [ "$no_build" = 0 ]; then
233
- if ! CONFIGURATION=Release sh scripts/amk/ios-build.sh > "$evidence/ios-build.log" 2>&1; then
390
+ if [ "$sims" = 1 ] && ! CONFIGURATION=Release sh scripts/amk/ios-build.sh > "$evidence/ios-build.log" 2>&1; then
234
391
  echo "ios: release build FAILED, see ${evidence#"$root"/}/ios-build.log"; status=1; return
235
392
  fi
236
- [ "$build_only" = 0 ] || { echo "ios: release build done"; return; }
393
+ for d in $reals; do
394
+ if ! CONFIGURATION=Release AMK_IOS_TEAM=$team sh scripts/amk/ios-build.sh device "$d" > "$evidence/ios-device-build.log" 2>&1; then
395
+ echo "ios: release build for $d FAILED, see ${evidence#"$root"/}/ios-device-build.log"; status=1; return
396
+ fi
397
+ done
237
398
  fi
399
+ unclaim "build-ios-$project"
400
+ [ "$build_only" = 0 ] || { echo "ios: release build done"; return; }
238
401
  # the newest app: the scheme just built (another scheme's app may still be there from an earlier build)
239
402
  app=$(ls -dt ios/DerivedData/Build/Products/*-iphonesimulator/*.app 2> /dev/null | head -n 1)
240
- [ -n "$app" ] || { echo "ios: there's no build to check yet: run sh scripts/amk/verify.sh build first"; status=1; return; }
241
- app_id=$(/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' "$app/Info.plist")
403
+ device_app=$(ls -dt "$root"/ios/DerivedData/Build/Products/*-iphoneos/*.app 2> /dev/null | head -n 1)
404
+ a=$app
405
+ [ "$sims" = 1 ] || a=$device_app
406
+ if [ -z "$a" ] || { [ -n "$reals" ] && [ -z "$device_app" ]; }; then
407
+ echo "ios: there's no build to check yet: run sh scripts/amk/verify.sh build first"; status=1; return
408
+ fi
409
+ app_id=$(/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' "$a/Info.plist")
242
410
  for d in $device $tablet; do
243
- xcrun simctl bootstatus "$d" -b > /dev/null
244
- xcrun simctl install "$d" "$app"
411
+ if is_real "$d"; then
412
+ launch_check "$d" || { status=1; return; }
413
+ else
414
+ xcrun simctl bootstatus "$d" -b > /dev/null
415
+ xcrun simctl install "$d" "$app"
416
+ fi
245
417
  done
246
418
  check_screens ios "$device" "$tablet" "$app_id" $screens
419
+ for d in $reals; do crash_reports "$d"; done
247
420
  }
248
421
 
249
- # a simulator's UDID: the one named in $2 (name or UDID), else a booted $1 (iPhone or iPad), else one with the newest iOS
250
- simulator() {
422
+ # the simulators for $1 (iPhone or iPad), best first: the one named in $2 (name or UDID); else the booted ones,
423
+ # then the others, newest iOS first
424
+ simulators() {
251
425
  xcrun simctl list devices available -j | KIND=$1 WANT=$2 node -e '
252
426
  const all = Object.entries(JSON.parse(require("fs").readFileSync(0, "utf8")).devices)
253
427
  .flatMap(([rt, ds]) => ds.map(d => ({ ...d, v: (rt.match(/iOS-(\d+)-(\d+)/) || []).slice(1).map(Number) })))
254
428
  .filter(d => d.v.length);
255
429
  const want = process.env.WANT;
256
430
  // by device type: a simulator can have any name, such as a separate one kept for tests
257
- const kind = all.filter(d => (d.deviceTypeIdentifier || d.name).includes(process.env.KIND));
258
- const pick = want ? all.find(d => d.udid === want || d.name === want)
259
- : kind.find(d => d.state === "Booted") || kind.sort((a, b) => b.v[0] - a.v[0] || b.v[1] - a.v[1])[0];
260
- console.log(pick ? pick.udid : "");'
431
+ const kind = all.filter(d => (d.deviceTypeIdentifier || d.name).includes(process.env.KIND))
432
+ .sort((a, b) => (b.state === "Booted") - (a.state === "Booted") || b.v[0] - a.v[0] || b.v[1] - a.v[1]);
433
+ const list = want ? all.filter(d => d.udid === want || d.name === want).slice(0, 1) : kind;
434
+ console.log(list.map(d => d.udid).join("\n"));'
435
+ }
436
+ # a simulator for $1 (iPhone or iPad) that this run holds, in $taken: the one named in $2, once it's free; else the
437
+ # first free one; else the best one, once it's free. Empty when there's none. A name no simulator has can be a real
438
+ # device's
439
+ take_simulator() {
440
+ taken=""
441
+ candidates=$(simulators "$1" "$2")
442
+ if [ -z "$candidates" ] && [ -n "$2" ]; then
443
+ candidates=$(real_ios "$2")
444
+ [ -z "$candidates" ] || reals="$reals $candidates"
445
+ fi
446
+ [ -n "$candidates" ] || return 0
447
+ first=$(echo "$candidates" | head -n 1)
448
+ if [ -z "$2" ]; then
449
+ for u in $candidates; do claim "ios-$u" "$u" && { taken=$u; break; }; done
450
+ [ -z "$taken" ] || [ "$taken" = "$first" ] || echo "ios: $first is in use by $(holder "ios-$first"), so checking on $taken"
451
+ fi
452
+ [ -n "$taken" ] || { claim_wait "ios-$first" "$first" "the $1 $first" || return 1; taken=$first; }
453
+ }
454
+
455
+ # Real iPhones and iPads, through Xcode's devicectl (iOS 17 or later). Maestro doesn't run on them, so their flows run
456
+ # through maestro-runner: pinned, as a newer one may behave differently, and without its usage data
457
+ runner=maestro-runner@1.1.29
458
+ reals="" # the real devices this run checks on
459
+ is_real() { case " $reals " in *" $1 "*) ;; *) return 1 ;; esac; }
460
+ # the UDID of the connected iPhone or iPad with the name or UDID $1
461
+ real_ios() {
462
+ xcrun devicectl list devices --json-output "$evidence/.devices.json" > /dev/null 2>&1 || return 0
463
+ WANT=$1 node -e 'const want = process.env.WANT;
464
+ const d = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")).result.devices.find(d => d.hardwareProperties?.reality === "physical" &&
465
+ [d.identifier, d.deviceProperties?.name, d.hardwareProperties.udid].includes(want));
466
+ if (d) console.log(d.hardwareProperties.udid);' "$evidence/.devices.json"
467
+ }
468
+ # the Apple team that signs the app and WebDriverAgent for real devices: AMK_IOS_TEAM, else the project's
469
+ ios_team() { echo "${AMK_IOS_TEAM:-$(sed -n 's/.*DEVELOPMENT_TEAM = "\{0,1\}\([A-Z0-9]\{10\}\)"\{0,1\};.*/\1/p' ios/*.xcodeproj/project.pbxproj 2> /dev/null | head -n 1)}"; }
470
+ # installs the app on the real device $1 and opens it, so what the device needs (trusting the team, unlocking) shows
471
+ # before the flows, and an app that closes at once is caught with its crash report. Notes the crash reports already there
472
+ launch_check() {
473
+ exe=$(/usr/libexec/PlistBuddy -c 'Print CFBundleExecutable' "$device_app/Info.plist")
474
+ crashes "$1" > "$evidence/.crashes-$1"
475
+ log=$evidence/launch-$1.log
476
+ if ! xcrun devicectl device install app --device "$1" "$device_app" > "$log" 2>&1; then
477
+ echo "ios: the app didn't install on $1 (is it unlocked, with Developer Mode on?), see ${log#"$root"/}"
478
+ return 1
479
+ fi
480
+ # a device now and then refuses an app it has just installed for a moment, as if it didn't trust the team
481
+ tries=0
482
+ until said=$(xcrun devicectl device process launch --device "$1" --terminate-existing "$app_id" 2>&1); do
483
+ echo "$said" >> "$log"
484
+ tries=$((tries + 1))
485
+ case $said in
486
+ *'explicitly trusted'*) [ "$tries" -lt 3 ] || {
487
+ echo "ios: $1 doesn't open apps from this team yet: on it, Settings > General > VPN & Device Management > Developer App > Trust, then run this again"
488
+ return 1; } ;;
489
+ *) echo "ios: the app didn't open on $1 (is it unlocked?), see ${log#"$root"/}"; return 1 ;;
490
+ esac
491
+ sleep 10
492
+ done
493
+ echo "$said" >> "$log"
494
+ sleep 10
495
+ xcrun devicectl device info processes --device "$1" --json-output "$evidence/.processes.json" > /dev/null 2>&1 || true
496
+ if ! grep -qs "/$(basename "$device_app")/" "$evidence/.processes.json"; then
497
+ echo "ios: the app closed within 10 seconds of opening on $1"
498
+ crash_reports "$1"
499
+ return 1
500
+ fi
501
+ }
502
+ # the app's crash reports on the real device $1
503
+ crashes() {
504
+ xcrun devicectl device info files --device "$1" --domain-type systemCrashLogs --json-output "$evidence/.files.json" > /dev/null 2>&1 || return 0
505
+ EXE=$exe node -e 'for (const f of JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")).result.files || [])
506
+ if (f.relativePath.startsWith(process.env.EXE + "-") && f.relativePath.endsWith(".ips")) console.log(f.relativePath)' "$evidence/.files.json"
507
+ }
508
+ # the ones that weren't there before launch_check join the evidence: the app crashed during the checks
509
+ crash_reports() {
510
+ new=$(crashes "$1" | grep -v -x -F -f "$evidence/.crashes-$1" || true)
511
+ [ -n "$new" ] || return 0
512
+ mkdir -p "$evidence/crashes"
513
+ for c in $new; do
514
+ xcrun devicectl device copy from --device "$1" --domain-type systemCrashLogs --source "$c" --destination "$evidence/crashes/$c" > /dev/null 2>&1 || true
515
+ done
516
+ echo "ios: the app crashed on $1 during the checks: $(echo "$new" | wc -l | tr -d ' ') crash reports in ${evidence#"$root"/}/crashes"
517
+ status=1
261
518
  }
262
519
 
263
520
  # a running emulator's adb serial: of the AVD named in $1 if one is, else of the kind in $2 (phone or tablet) if given
@@ -285,11 +542,42 @@ avd_width() {
285
542
  END { if (d) print int((w + 0 < h + 0 ? w : h) * 160 / d) }' \
286
543
  "${ANDROID_AVD_HOME:-${ANDROID_USER_HOME:-$HOME/.android}/avd}/$1.avd/config.ini" 2> /dev/null || true
287
544
  }
288
- # the first AVD of the kind $1 (phone or tablet)
289
- first_avd() {
290
- for a in $avds; do
291
- if [ "$(kind_of "$(avd_width "$a")")" = "$1" ]; then echo "$a"; return; fi
292
- done
545
+ # the emulators of the kind $1 (phone or tablet), best first: the running ones, then the others
546
+ emulators() {
547
+ {
548
+ for d in $("$adb" devices | awk 'NR > 1 && $2 == "device" && /^emulator-/ { print $1 }'); do
549
+ [ "$(kind_of "$(device_width "$d")")" != "$1" ] || "$adb" -s "$d" emu avd name 2> /dev/null | head -n 1 | tr -d '\r'
550
+ done
551
+ for a in $avds; do [ "$(kind_of "$(avd_width "$a")")" != "$1" ] || echo "$a"; done
552
+ } | awk 'NF && !seen[$0]++'
553
+ }
554
+ # an Android device of the kind $1 that this run holds, in $taken (its serial): the device ($2, an adb serial) or
555
+ # emulator ($3) named for it, once it's free; else the first free emulator of that kind, running or not; else the
556
+ # best one, once it's free. Empty when there's none of that kind. An emulator is claimed by its name, however named
557
+ take_android() {
558
+ taken="" pick=""
559
+ if [ -n "$2" ]; then
560
+ key=android-$2
561
+ case $2 in emulator-*) key=android-avd-$("$adb" -s "$2" emu avd name 2> /dev/null | head -n 1 | tr -d '\r') ;; esac
562
+ claim_wait "$key" "$2" "$2" || return 1
563
+ taken=$2
564
+ return 0
565
+ fi
566
+ if [ -n "$3" ]; then
567
+ echo "$avds" | grep -qxF "$3" || { echo "android: there's no emulator named $3 (see: emulator -list-avds)"; return 1; }
568
+ names=$3
569
+ else
570
+ names=$(emulators "$1")
571
+ fi
572
+ [ -n "$names" ] || return 0
573
+ first=$(echo "$names" | head -n 1)
574
+ if [ -z "$3" ]; then
575
+ for n in $names; do claim "android-avd-$n" "$(running_emulator "$n")" && { pick=$n; break; }; done
576
+ [ -z "$pick" ] || [ "$pick" = "$first" ] || echo "android: emulator $first is in use by $(holder "android-avd-$first"), so checking on $pick"
577
+ fi
578
+ [ -n "$pick" ] || { claim_wait "android-avd-$first" "$(running_emulator "$first")" "emulator $first" || return 1; pick=$first; }
579
+ taken=$(running_emulator "$pick")
580
+ [ -n "$taken" ] || taken=$(start_emulator "$pick") || { echo "android: emulator $pick didn't start"; return 1; }
293
581
  }
294
582
  # starts the AVD $1 and prints its serial once adb sees it (up to 5 minutes)
295
583
  start_emulator() {
@@ -350,19 +638,15 @@ android() {
350
638
  [ -n "$screens" ] || { echo "android: none of the app's screens is $only"; return; }
351
639
  avds=$("$sdk/emulator/emulator" -list-avds 2> /dev/null || true)
352
640
  if echo "$screens" | grep -q '^phone-'; then
353
- device=${AMK_ANDROID_DEVICE:-$(running_emulator "${AMK_ANDROID_AVD:-}" phone)}
641
+ take_android phone "${AMK_ANDROID_DEVICE:-}" "${AMK_ANDROID_AVD:-}" || { status=1; return; }
642
+ # only tablet emulators: one of them
643
+ [ -n "$taken" ] || take_android tablet "" "" || { status=1; return; }
644
+ device=$taken
354
645
  if [ -z "$device" ]; then
355
- avd=${AMK_ANDROID_AVD:-$(first_avd phone)}
356
- [ -n "$avd" ] || avd=$(echo "$avds" | head -n 1) # only tablet emulators: one of them
357
- if [ -n "$avd" ]; then
358
- echo "$avds" | grep -qxF "$avd" || { echo "android: there's no emulator named $avd (see: emulator -list-avds)"; status=1; return; }
359
- device=$(start_emulator "$avd") || { echo "android: emulator $avd didn't start"; status=1; return; }
360
- else
361
646
  phone=$("$adb" devices | awk 'NR > 1 && $2 == "device" { print $1; exit }')
362
647
  echo "android: no emulator found (create one in Android Studio)"
363
648
  [ -z "$phone" ] || echo " or check on the phone $phone, which clears the app's data on it: AMK_ANDROID_DEVICE=$phone sh scripts/amk/verify.sh android"
364
649
  status=1; return
365
- fi
366
650
  fi
367
651
  case $device in
368
652
  emulator-*) echo "android: checking on $device" ;;
@@ -370,17 +654,9 @@ android() {
370
654
  esac
371
655
  fi
372
656
  if echo "$screens" | grep -q '^tablet-'; then
373
- tablet=${AMK_ANDROID_TABLET:-$(running_emulator "${AMK_ANDROID_TABLET_AVD:-}" tablet)}
374
- if [ -z "$tablet" ]; then
375
- tablet_avd=${AMK_ANDROID_TABLET_AVD:-$(first_avd tablet)}
376
- if [ -z "$tablet_avd" ]; then
377
- echo "android: the app runs on tablets, but there's no tablet emulator, so tablets aren't checked (create one in Android Studio: Device Manager > Tablet, or set AMK_ANDROID_TABLET_AVD)"
378
- elif ! echo "$avds" | grep -qxF "$tablet_avd"; then
379
- echo "android: there's no emulator named $tablet_avd (see: emulator -list-avds)"; status=1; return
380
- else
381
- tablet=$(start_emulator "$tablet_avd") || { echo "android: emulator $tablet_avd didn't start, so tablets aren't checked"; tablet=""; }
382
- fi
383
- fi
657
+ take_android tablet "${AMK_ANDROID_TABLET:-}" "${AMK_ANDROID_TABLET_AVD:-}" || { status=1; return; }
658
+ tablet=$taken
659
+ [ -n "$tablet" ] || echo "android: the app runs on tablets, but there's no tablet emulator, so tablets aren't checked (create one in Android Studio: Device Manager > Tablet, or set AMK_ANDROID_TABLET_AVD)"
384
660
  [ "$tablet" != "$device" ] || tablet="" # only tablet emulators: the one in use is checked as the phone
385
661
  case $tablet in
386
662
  "") ;;
@@ -398,6 +674,8 @@ android() {
398
674
  sleep 2
399
675
  done
400
676
  fi
677
+ # one build of this project at a time; a check without its own build waits for one that's under way
678
+ claim_wait "build-android-$project" "" "the Android build of this project" || { status=1; return; }
401
679
  if [ "$no_build" = 0 ]; then
402
680
  # native code for that CPU type only, as run-android --active-arch-only does: much faster than all four
403
681
  set -- -PreactNativeArchitectures="$abi"
@@ -410,8 +688,9 @@ android() {
410
688
  if ! (cd android && ./gradlew "assemble${variant}Release" --no-daemon -q "$@") > "$evidence/android-build.log" 2>&1; then
411
689
  echo "android: release build FAILED, see ${evidence#"$root"/}/android-build.log"; status=1; return
412
690
  fi
413
- [ "$build_only" = 0 ] || { echo "android: release build done"; return; }
414
691
  fi
692
+ unclaim "build-android-$project"
693
+ [ "$build_only" = 0 ] || { echo "android: release build done"; return; }
415
694
  # apps that split APKs by CPU type get one per type: take this device's, else the universal one
416
695
  apks=$(ls android/app/build/outputs/apk/${flavor:+$flavor/}release/*.apk 2> /dev/null || true)
417
696
  [ -n "$apks" ] || { echo "android: there's no build to check yet: run sh scripts/amk/verify.sh build first"; status=1; return; }
@@ -446,10 +725,10 @@ build() {
446
725
  build_only=1
447
726
  ios_pid=""
448
727
  if [ "$(uname -s)" = Darwin ]; then
449
- (ios; exit $status) > "$evidence/build-ios.out" 2>&1 &
728
+ (trap release EXIT; ios; exit $status) > "$evidence/build-ios.out" 2>&1 &
450
729
  ios_pid=$!
451
730
  fi
452
- (android; exit $status) > "$evidence/build-android.out" 2>&1 &
731
+ (trap release EXIT; android; exit $status) > "$evidence/build-android.out" 2>&1 &
453
732
  android_pid=$!
454
733
  [ -z "$ios_pid" ] || wait "$ios_pid" || status=1
455
734
  wait "$android_pid" || status=1