|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/anthropics/maestro-ios-device/internal/device" |
| 12 | + "github.com/anthropics/maestro-ios-device/internal/maestro" |
| 13 | + "github.com/anthropics/maestro-ios-device/internal/portforward" |
| 14 | + "github.com/anthropics/maestro-ios-device/internal/runner" |
| 15 | + "github.com/anthropics/maestro-ios-device/internal/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var version = "dev" // set via -ldflags |
| 19 | + |
| 20 | +func fatal(format string, args ...any) { |
| 21 | + fmt.Printf("❌ "+format+"\n", args...) |
| 22 | + os.Exit(1) |
| 23 | +} |
| 24 | + |
| 25 | +func printBanner() { |
| 26 | + fmt.Printf("maestro-ios-device %s (Unofficial — devicelab.dev)\n\n", version) |
| 27 | +} |
| 28 | + |
| 29 | +func main() { |
| 30 | + if len(os.Args) > 1 && os.Args[1] == "setup" { |
| 31 | + printBanner() |
| 32 | + if err := maestro.RunSetup(); err != nil { |
| 33 | + fatal("Setup failed: %s", err) |
| 34 | + } |
| 35 | + return |
| 36 | + } |
| 37 | + run() |
| 38 | +} |
| 39 | + |
| 40 | +func run() { |
| 41 | + teamID := flag.String("team-id", "", "Apple Developer Team ID (required)") |
| 42 | + deviceUDID := flag.String("device", "", "Target device UDID (required)") |
| 43 | + port := flag.Int("driver-host-port", 0, "Local port (default: auto-assign from 6001)") |
| 44 | + showVersion := flag.Bool("version", false, "Show version") |
| 45 | + help := flag.Bool("help", false, "Show help") |
| 46 | + |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if *showVersion { |
| 50 | + fmt.Printf("maestro-ios-device %s (Unofficial — devicelab.dev)\n", version) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + printBanner() |
| 55 | + |
| 56 | + if *help { |
| 57 | + printUsage() |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if *teamID == "" || *deviceUDID == "" { |
| 62 | + printUsage() |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + if ok, _ := maestro.IsPatched(); !ok { |
| 67 | + fatal("Maestro not patched. Run: maestro-ios-device setup") |
| 68 | + } |
| 69 | + |
| 70 | + localPort, err := utils.ResolvePort(*port) |
| 71 | + if err != nil { |
| 72 | + fatal("%s", err) |
| 73 | + } |
| 74 | + |
| 75 | + dev, err := device.Get(*deviceUDID) |
| 76 | + if err != nil { |
| 77 | + fatal("%s", err) |
| 78 | + } |
| 79 | + fmt.Printf("📱 %s (%s) - iOS %s\n\n", dev.Name, dev.Serial, dev.OSVersion) |
| 80 | + |
| 81 | + ctx, cancel := context.WithCancel(context.Background()) |
| 82 | + defer cancel() |
| 83 | + |
| 84 | + sigChan := make(chan os.Signal, 1) |
| 85 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 86 | + |
| 87 | + // Build & start runner |
| 88 | + r := runner.New(*deviceUDID, *teamID) |
| 89 | + defer r.Cleanup() |
| 90 | + |
| 91 | + if err := r.Build(ctx); err != nil { |
| 92 | + fatal("Build failed: %s", err) |
| 93 | + } |
| 94 | + |
| 95 | + if err := r.Start(ctx); err != nil { |
| 96 | + fatal("Start failed: %s", err) |
| 97 | + } |
| 98 | + |
| 99 | + pf := portforward.New(dev.Entry, uint16(localPort), runner.DevicePort) |
| 100 | + defer pf.Stop() |
| 101 | + |
| 102 | + if err := pf.Start(); err != nil { |
| 103 | + fatal("Port forward failed: %s", err) |
| 104 | + } |
| 105 | + |
| 106 | + if err := pf.Verify(); err != nil { |
| 107 | + fatal("%s", err) |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Println() |
| 111 | + fmt.Println("✅ Ready! Run:") |
| 112 | + fmt.Printf(" maestro --driver-host-port %d --device %s test <flow.yaml>\n\n", localPort, *deviceUDID) |
| 113 | + fmt.Println("Press Ctrl+C to stop.") |
| 114 | + |
| 115 | + <-sigChan |
| 116 | + fmt.Println("\n🛑 Stopping...") |
| 117 | +} |
| 118 | + |
| 119 | +func printUsage() { |
| 120 | + fmt.Println(`maestro-ios-device - Run Maestro tests on real iOS devices |
| 121 | +
|
| 122 | +⚠️ Unofficial community tool. Not affiliated with mobile.dev or Maestro. |
| 123 | + Stop-gap until PR #2856 is merged. |
| 124 | +
|
| 125 | +Usage: |
| 126 | + maestro-ios-device --team-id TEAM_ID --device UDID [options] |
| 127 | +
|
| 128 | +Required: |
| 129 | + --team-id Apple Developer Team ID |
| 130 | + --device iOS device UDID |
| 131 | +
|
| 132 | +Options: |
| 133 | + --driver-host-port Local port for Maestro connection (default: 6001) |
| 134 | + --version Show version |
| 135 | + --help Show this help |
| 136 | +
|
| 137 | +Examples: |
| 138 | + maestro-ios-device --team-id ABC123XYZ --device 00008030-001234567890 |
| 139 | +
|
| 140 | + Then run tests: |
| 141 | + maestro --driver-host-port 6001 --device 00008030-001234567890 test flow.yaml |
| 142 | +
|
| 143 | +Finding your Team ID: |
| 144 | + security find-identity -v -p codesigning | grep "Developer" |
| 145 | +
|
| 146 | +Finding your Device UDID: |
| 147 | + xcrun xctrace list devices |
| 148 | +
|
| 149 | +Docs: https://github.com/devicelab-dev/maestro-ios-device |
| 150 | +Built by DeviceLab — https://devicelab.dev`) |
| 151 | +} |
0 commit comments