package mac import ( "context" "encoding/json" "regexp" "sort" "strconv" "strings" "amadan.net/oficina/equipo/internal/wire" ) // Exec runs a tool and returns its standard output. type Exec func(ctx context.Context, name string, args ...string) (string, error) // Tools are the executables the capability report asks; "" is absent. // Claude is the pinned CLI, which nothing looks up (ClaudeCLI). type Tools struct { Xcodebuild, Xcrun, Swift, Node, SRT, RG, Codex, Git string Claude ClaudeCLI } // Capabilities is a Mac's capability report (spec §1.3): Xcode, the // available simulators, toolchain and harness versions and the tags. // Only versions and names leave the machine; ValidCapabilities checks. func Capabilities(ctx context.Context, x Exec, t Tools) wire.Capabilities { ask := func(name string, args ...string) string { if name == "" { return "" } c, cancel := context.WithTimeout(ctx, QueryLimit) defer cancel() out, err := x(c, name, args...) if err != nil { return "" } return out } c := wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagMacOS}} c.Xcode = ParseXcode(ask(t.Xcodebuild, "-version")) if c.Xcode.Version != "" { c.Tags = append(c.Tags, wire.TagXcode) c.Simulators = ParseSimulators([]byte(ask(t.Xcrun, "simctl", "list", "-j", "devices", "available"))) } for _, s := range c.Simulators { if strings.HasPrefix(s.Runtime, "iOS ") { c.Tags = append(c.Tags, wire.TagIOSSimulator) break } } if m := swiftVersion.FindStringSubmatch(ask(t.Swift, "--version") + " "); m != nil { c.Toolchains.Swift = m[1] } c.Toolchains.Node = word(nodeVersion, ask(t.Node, "--version")) c.Harness.SRT = word(plainVersion, ask(t.SRT, "--version")) // Claude Code is never run to ask: the pinned version, only while the // pinned file verifies (spec §8.2). c.Harness.Claude = t.Claude.Version() c.Harness.Codex = word(plainVersion, ask(t.Codex, "--version")) // No claude or codex tag: a Mac worker runs checks only until slice // F, so nothing that needs a harness is placed on it. return c } var ( xcodeVersion = regexp.MustCompile(`(?m)^Xcode ([0-9][0-9A-Za-z.]*)\s*$`) xcodeBuild = regexp.MustCompile(`(?m)^Build version ([0-9A-Za-z]+)\s*$`) swiftVersion = regexp.MustCompile(`(?:Swift version ([0-9]+\.[0-9]+(?:\.[0-9]+)?)[ \n(]|$)`) nodeVersion = regexp.MustCompile(`\bv[0-9]+\.[0-9]+\.[0-9]+\b`) plainVersion = regexp.MustCompile(`\b[0-9]+\.[0-9]+\.[0-9]+\b`) runtimeKey = regexp.MustCompile(`^com\.apple\.CoreSimulator\.SimRuntime\.(iOS|watchOS|tvOS|xrOS|visionOS)-([0-9]+(?:-[0-9]+)*)$`) udidPattern = regexp.MustCompile(`^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$`) ) func word(re *regexp.Regexp, s string) string { return re.FindString(s) } // ParseXcode reads `xcodebuild -version`: "Xcode 16.2" and "Build version // 16C5032a". func ParseXcode(out string) wire.Xcode { v, b := xcodeVersion.FindStringSubmatch(out), xcodeBuild.FindStringSubmatch(out) if v == nil || b == nil { return wire.Xcode{} } return wire.Xcode{Version: v[1], Build: b[1]} } // ParseSimulators reads `xcrun simctl list -j devices available`: the // available devices under each runtime, newest runtime first, at most // wire.MaxSimulators, each field in the capability string rules. func ParseSimulators(raw []byte) []wire.Simulator { var list struct { Devices map[string][]struct { Name string `json:"name"` UDID string `json:"udid"` State string `json:"state"` IsAvailable *bool `json:"isAvailable"` } `json:"devices"` } out := []wire.Simulator{} if json.Unmarshal(raw, &list) != nil { return out } type sim struct { wire.Simulator os string ver []int } var all []sim for key, devices := range list.Devices { m := runtimeKey.FindStringSubmatch(key) if m == nil { continue } var ver []int for _, p := range strings.Split(m[2], "-") { n, _ := strconv.Atoi(p) ver = append(ver, n) } runtime := m[1] + " " + strings.ReplaceAll(m[2], "-", ".") for _, d := range devices { if d.IsAvailable != nil && !*d.IsAvailable { continue } s := wire.Simulator{Name: d.Name, Runtime: runtime, UDID: d.UDID, State: d.State} if !udidPattern.MatchString(s.UDID) || wire.ValidCapabilities(wire.Capabilities{Simulators: []wire.Simulator{s}}) != nil { continue } all = append(all, sim{s, m[1], ver}) } } sort.Slice(all, func(i, j int) bool { a, b := all[i], all[j] if (a.os == "iOS") != (b.os == "iOS") { return a.os == "iOS" } if c := compareVersions(a.ver, b.ver); c != 0 { return c > 0 } if a.Name != b.Name { return a.Name < b.Name } return a.UDID < b.UDID }) for _, s := range all { if len(out) == wire.MaxSimulators { break } out = append(out, s.Simulator) } return out } func compareVersions(a, b []int) int { for i := range max(len(a), len(b)) { x, y := 0, 0 if i < len(a) { x = a[i] } if i < len(b) { y = b[i] } if x != y { return x - y } } return 0 } // runtimeVersion is "iOS 18.2"'s numbers. func runtimeVersion(runtime string) []int { _, v, _ := strings.Cut(runtime, " ") var out []int for _, p := range strings.Split(v, ".") { n, _ := strconv.Atoi(p) out = append(out, n) } return out }