| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "regexp" |
| 7 | "sort" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Exec runs a tool and returns its standard output. |
| 15 | type Exec func(ctx context.Context, name string, args ...string) (string, error) |
| 16 | |
| 17 | // Tools are the executables the capability report asks; "" is absent. |
| 18 | // Claude is the pinned CLI, which nothing looks up (ClaudeCLI). |
| 19 | type Tools struct { |
| 20 | Xcodebuild, Xcrun, Swift, Node, SRT, RG, Codex, Git string |
| 21 | Claude ClaudeCLI |
| 22 | } |
| 23 | |
| 24 | // Capabilities is a Mac's capability report (spec §1.3): Xcode, the |
| 25 | // available simulators, toolchain and harness versions and the tags. |
| 26 | // Only versions and names leave the machine; ValidCapabilities checks. |
| 27 | func Capabilities(ctx context.Context, x Exec, t Tools) wire.Capabilities { |
| 28 | ask := func(name string, args ...string) string { |
| 29 | if name == "" { |
| 30 | return "" |
| 31 | } |
| 32 | c, cancel := context.WithTimeout(ctx, QueryLimit) |
| 33 | defer cancel() |
| 34 | out, err := x(c, name, args...) |
| 35 | if err != nil { |
| 36 | return "" |
| 37 | } |
| 38 | return out |
| 39 | } |
| 40 | c := wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagMacOS}} |
| 41 | c.Xcode = ParseXcode(ask(t.Xcodebuild, "-version")) |
| 42 | if c.Xcode.Version != "" { |
| 43 | c.Tags = append(c.Tags, wire.TagXcode) |
| 44 | c.Simulators = ParseSimulators([]byte(ask(t.Xcrun, "simctl", "list", "-j", "devices", "available"))) |
| 45 | } |
| 46 | for _, s := range c.Simulators { |
| 47 | if strings.HasPrefix(s.Runtime, "iOS ") { |
| 48 | c.Tags = append(c.Tags, wire.TagIOSSimulator) |
| 49 | break |
| 50 | } |
| 51 | } |
| 52 | if m := swiftVersion.FindStringSubmatch(ask(t.Swift, "--version") + " "); m != nil { |
| 53 | c.Toolchains.Swift = m[1] |
| 54 | } |
| 55 | c.Toolchains.Node = word(nodeVersion, ask(t.Node, "--version")) |
| 56 | c.Harness.SRT = word(plainVersion, ask(t.SRT, "--version")) |
| 57 | // Claude Code is never run to ask: the pinned version, only while the |
| 58 | // pinned file verifies (spec §8.2). |
| 59 | c.Harness.Claude = t.Claude.Version() |
| 60 | c.Harness.Codex = word(plainVersion, ask(t.Codex, "--version")) |
| 61 | // No claude or codex tag: a Mac worker runs checks only until slice |
| 62 | // F, so nothing that needs a harness is placed on it. |
| 63 | return c |
| 64 | } |
| 65 | |
| 66 | var ( |
| 67 | xcodeVersion = regexp.MustCompile(`(?m)^Xcode ([0-9][0-9A-Za-z.]*)\s*$`) |
| 68 | xcodeBuild = regexp.MustCompile(`(?m)^Build version ([0-9A-Za-z]+)\s*$`) |
| 69 | swiftVersion = regexp.MustCompile(`(?:Swift version ([0-9]+\.[0-9]+(?:\.[0-9]+)?)[ \n(]|$)`) |
| 70 | nodeVersion = regexp.MustCompile(`\bv[0-9]+\.[0-9]+\.[0-9]+\b`) |
| 71 | plainVersion = regexp.MustCompile(`\b[0-9]+\.[0-9]+\.[0-9]+\b`) |
| 72 | runtimeKey = regexp.MustCompile(`^com\.apple\.CoreSimulator\.SimRuntime\.(iOS|watchOS|tvOS|xrOS|visionOS)-([0-9]+(?:-[0-9]+)*)$`) |
| 73 | udidPattern = regexp.MustCompile(`^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$`) |
| 74 | ) |
| 75 | |
| 76 | func word(re *regexp.Regexp, s string) string { return re.FindString(s) } |
| 77 | |
| 78 | // ParseXcode reads `xcodebuild -version`: "Xcode 16.2" and "Build version |
| 79 | // 16C5032a". |
| 80 | func ParseXcode(out string) wire.Xcode { |
| 81 | v, b := xcodeVersion.FindStringSubmatch(out), xcodeBuild.FindStringSubmatch(out) |
| 82 | if v == nil || b == nil { |
| 83 | return wire.Xcode{} |
| 84 | } |
| 85 | return wire.Xcode{Version: v[1], Build: b[1]} |
| 86 | } |
| 87 | |
| 88 | // ParseSimulators reads `xcrun simctl list -j devices available`: the |
| 89 | // available devices under each runtime, newest runtime first, at most |
| 90 | // wire.MaxSimulators, each field in the capability string rules. |
| 91 | func ParseSimulators(raw []byte) []wire.Simulator { |
| 92 | var list struct { |
| 93 | Devices map[string][]struct { |
| 94 | Name string `json:"name"` |
| 95 | UDID string `json:"udid"` |
| 96 | State string `json:"state"` |
| 97 | IsAvailable *bool `json:"isAvailable"` |
| 98 | } `json:"devices"` |
| 99 | } |
| 100 | out := []wire.Simulator{} |
| 101 | if json.Unmarshal(raw, &list) != nil { |
| 102 | return out |
| 103 | } |
| 104 | type sim struct { |
| 105 | wire.Simulator |
| 106 | os string |
| 107 | ver []int |
| 108 | } |
| 109 | var all []sim |
| 110 | for key, devices := range list.Devices { |
| 111 | m := runtimeKey.FindStringSubmatch(key) |
| 112 | if m == nil { |
| 113 | continue |
| 114 | } |
| 115 | var ver []int |
| 116 | for _, p := range strings.Split(m[2], "-") { |
| 117 | n, _ := strconv.Atoi(p) |
| 118 | ver = append(ver, n) |
| 119 | } |
| 120 | runtime := m[1] + " " + strings.ReplaceAll(m[2], "-", ".") |
| 121 | for _, d := range devices { |
| 122 | if d.IsAvailable != nil && !*d.IsAvailable { |
| 123 | continue |
| 124 | } |
| 125 | s := wire.Simulator{Name: d.Name, Runtime: runtime, UDID: d.UDID, State: d.State} |
| 126 | if !udidPattern.MatchString(s.UDID) || wire.ValidCapabilities(wire.Capabilities{Simulators: []wire.Simulator{s}}) != nil { |
| 127 | continue |
| 128 | } |
| 129 | all = append(all, sim{s, m[1], ver}) |
| 130 | } |
| 131 | } |
| 132 | sort.Slice(all, func(i, j int) bool { |
| 133 | a, b := all[i], all[j] |
| 134 | if (a.os == "iOS") != (b.os == "iOS") { |
| 135 | return a.os == "iOS" |
| 136 | } |
| 137 | if c := compareVersions(a.ver, b.ver); c != 0 { |
| 138 | return c > 0 |
| 139 | } |
| 140 | if a.Name != b.Name { |
| 141 | return a.Name < b.Name |
| 142 | } |
| 143 | return a.UDID < b.UDID |
| 144 | }) |
| 145 | for _, s := range all { |
| 146 | if len(out) == wire.MaxSimulators { |
| 147 | break |
| 148 | } |
| 149 | out = append(out, s.Simulator) |
| 150 | } |
| 151 | return out |
| 152 | } |
| 153 | |
| 154 | func compareVersions(a, b []int) int { |
| 155 | for i := range max(len(a), len(b)) { |
| 156 | x, y := 0, 0 |
| 157 | if i < len(a) { |
| 158 | x = a[i] |
| 159 | } |
| 160 | if i < len(b) { |
| 161 | y = b[i] |
| 162 | } |
| 163 | if x != y { |
| 164 | return x - y |
| 165 | } |
| 166 | } |
| 167 | return 0 |
| 168 | } |
| 169 | |
| 170 | // runtimeVersion is "iOS 18.2"'s numbers. |
| 171 | func runtimeVersion(runtime string) []int { |
| 172 | _, v, _ := strings.Cut(runtime, " ") |
| 173 | var out []int |
| 174 | for _, p := range strings.Split(v, ".") { |
| 175 | n, _ := strconv.Atoi(p) |
| 176 | out = append(out, n) |
| 177 | } |
| 178 | return out |
| 179 | } |
| 180 | |