package mac import ( "io/fs" "os" "path" "regexp" "strconv" "strings" "amadan.net/oficina/equipo/internal/wire" ) // XcodeGate is the gate a repository with an Xcode project and no gate // of its own gets on a Mac (spec ยง4.4): `xcodebuild test` with its one // shared scheme (or the one named like the project) on the newest // available iPhone simulator, Xcode's writes in the attempt's scratch // (XcodeWrites), without signing. ok is false when there is no project; reason is // checks_unavailable when there are several candidates or none fits. func XcodeGate(tree string, sims []wire.Simulator) (argv []string, ok bool, reason string) { root, err := os.OpenRoot(tree) if err != nil { return nil, false, "" } defer root.Close() fsys := root.FS() var workspaces, projects []string for _, dir := range []string{".", "*"} { for _, kind := range []string{"xcworkspace", "xcodeproj"} { found, _ := fs.Glob(fsys, path.Join(dir, "*."+kind)) for _, f := range found { if info, err := root.Lstat(f); err != nil || !info.IsDir() { continue } if kind == "xcworkspace" { workspaces = append(workspaces, f) } else { projects = append(projects, f) } } } } if len(workspaces) == 0 && len(projects) == 0 { return nil, false, "" } flag, container := "", "" switch { case len(workspaces) == 1: flag, container = "-workspace", workspaces[0] case len(workspaces) == 0 && len(projects) == 1: flag, container = "-project", projects[0] default: return nil, true, "checks_unavailable" } var schemes []string for _, c := range append(append([]string{}, workspaces...), projects...) { found, _ := fs.Glob(fsys, path.Join(c, "xcshareddata/xcschemes/*.xcscheme")) for _, f := range found { if info, err := root.Lstat(f); err == nil && info.Mode().IsRegular() { schemes = appendNew(schemes, strings.TrimSuffix(path.Base(f), ".xcscheme")) } } } scheme := "" if len(schemes) == 1 { scheme = schemes[0] } else { name := strings.TrimSuffix(path.Base(container), path.Ext(container)) for _, s := range schemes { if s == name { scheme = s } } } sim, found := NewestIPhone(sims) if scheme == "" || !found { return nil, true, "checks_unavailable" } cmd := "exec /usr/bin/xcodebuild test " + flag + " " + quote(container) + " -scheme " + quote(scheme) + " -destination " + quote("platform=iOS Simulator,id="+sim.UDID) + " " + XcodeWrites + " " + XcodeResult("checks") + " CODE_SIGNING_ALLOWED=NO" return []string{"/bin/bash", "-c", cmd}, true, "" } // XcodeResult names an xcodebuild action's result bundle in the // attempt's scratch. Without it xcodebuild stages one as // ResultBundle_.xcresult in the user's DARWIN_USER_TEMP_DIR // (/private/var/folders/<..>/T, from confstr, not $TMPDIR), which the // checks profile does not let it write: the Studio's sandbox log // showed those denials. The path must not exist yet, so each action in // one attempt has its own name; xcodebuild makes the folders. func XcodeResult(name string) string { return `-resultBundlePath "$TMPDIR/Results/` + name + `.xcresult"` } // XcodeWrites sends what xcodebuild writes for itself into the // attempt's scratch ($TMPDIR: the gate's, which the checks profile lets // it write and the run removes), not the runner user's ~/Library. Every // xcodebuild the synthesised gate, P2 and P6 run carries it, so P2 // proves the way checks build. // - -derivedDataPath: build products and intermediates, XCBuildData // with its PIF cache (the "workspace" folder P2's script build could // not write under srt), the module cache and the activity logs. // The result bundle is XcodeResult's. // - -clonedSourcePackagesDirPath: the packages' checkouts and // workspace state (inside DerivedData by default; named so no // default can move them). // - -packageCachePath: SwiftPM's caches (~/Library/Caches/ // org.swift.swiftpm by default), in xcodebuild since Xcode 13. // // SwiftPM's ~/.swiftpm (its security, configuration and cache links, // and the lock it takes there) moves with XDG_CONFIG_HOME, which a // sandboxed gate sets to the attempt's scratch (Gate.Run). // // xcodebuild applies -derivedDataPath to an action on a scheme, so a // target is built through its scheme. const XcodeWrites = `-derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache"` // XcodeGateName names the synthesised gate on the task page. const XcodeGateName = "xcodebuild test" // NewestIPhone is the available iPhone simulator on the newest iOS // runtime, the highest model number there (an SE counts as 0), then the // first by name, so the choice is the same every time. func NewestIPhone(sims []wire.Simulator) (wire.Simulator, bool) { var best wire.Simulator found := false for _, s := range sims { if !strings.HasPrefix(s.Runtime, "iOS ") || !strings.HasPrefix(s.Name, "iPhone") || !udidPattern.MatchString(s.UDID) { continue } if !found { best, found = s, true continue } c := compareVersions(runtimeVersion(s.Runtime), runtimeVersion(best.Runtime)) if c == 0 { c = model(s.Name) - model(best.Name) } if c > 0 || c == 0 && s.Name < best.Name { best = s } } return best, found } // quote is a POSIX shell single-quoted word. func quote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" } func appendNew(list []string, s string) []string { for _, x := range list { if x == s { return list } } return append(list, s) } var modelNumber = regexp.MustCompile(`iPhone ([0-9]+)`) func model(name string) int { m := modelNumber.FindStringSubmatch(name) if m == nil { return 0 } n, _ := strconv.Atoi(m[1]) return n }