| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "os" |
| 6 | "path" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // XcodeGate is the gate a repository with an Xcode project and no gate |
| 15 | // of its own gets on a Mac (spec §4.4): `xcodebuild test` with its one |
| 16 | // shared scheme (or the one named like the project) on the newest |
| 17 | // available iPhone simulator, Xcode's writes in the attempt's scratch |
| 18 | // (XcodeWrites), without signing. ok is false when there is no project; reason is |
| 19 | // checks_unavailable when there are several candidates or none fits. |
| 20 | func XcodeGate(tree string, sims []wire.Simulator) (argv []string, ok bool, reason string) { |
| 21 | root, err := os.OpenRoot(tree) |
| 22 | if err != nil { |
| 23 | return nil, false, "" |
| 24 | } |
| 25 | defer root.Close() |
| 26 | fsys := root.FS() |
| 27 | var workspaces, projects []string |
| 28 | for _, dir := range []string{".", "*"} { |
| 29 | for _, kind := range []string{"xcworkspace", "xcodeproj"} { |
| 30 | found, _ := fs.Glob(fsys, path.Join(dir, "*."+kind)) |
| 31 | for _, f := range found { |
| 32 | if info, err := root.Lstat(f); err != nil || !info.IsDir() { |
| 33 | continue |
| 34 | } |
| 35 | if kind == "xcworkspace" { |
| 36 | workspaces = append(workspaces, f) |
| 37 | } else { |
| 38 | projects = append(projects, f) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | if len(workspaces) == 0 && len(projects) == 0 { |
| 44 | return nil, false, "" |
| 45 | } |
| 46 | flag, container := "", "" |
| 47 | switch { |
| 48 | case len(workspaces) == 1: |
| 49 | flag, container = "-workspace", workspaces[0] |
| 50 | case len(workspaces) == 0 && len(projects) == 1: |
| 51 | flag, container = "-project", projects[0] |
| 52 | default: |
| 53 | return nil, true, "checks_unavailable" |
| 54 | } |
| 55 | var schemes []string |
| 56 | for _, c := range append(append([]string{}, workspaces...), projects...) { |
| 57 | found, _ := fs.Glob(fsys, path.Join(c, "xcshareddata/xcschemes/*.xcscheme")) |
| 58 | for _, f := range found { |
| 59 | if info, err := root.Lstat(f); err == nil && info.Mode().IsRegular() { |
| 60 | schemes = appendNew(schemes, strings.TrimSuffix(path.Base(f), ".xcscheme")) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | scheme := "" |
| 65 | if len(schemes) == 1 { |
| 66 | scheme = schemes[0] |
| 67 | } else { |
| 68 | name := strings.TrimSuffix(path.Base(container), path.Ext(container)) |
| 69 | for _, s := range schemes { |
| 70 | if s == name { |
| 71 | scheme = s |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | sim, found := NewestIPhone(sims) |
| 76 | if scheme == "" || !found { |
| 77 | return nil, true, "checks_unavailable" |
| 78 | } |
| 79 | cmd := "exec /usr/bin/xcodebuild test " + flag + " " + quote(container) + " -scheme " + quote(scheme) + |
| 80 | " -destination " + quote("platform=iOS Simulator,id="+sim.UDID) + " " + XcodeWrites + " " + XcodeResult("checks") + " CODE_SIGNING_ALLOWED=NO" |
| 81 | return []string{"/bin/bash", "-c", cmd}, true, "" |
| 82 | } |
| 83 | |
| 84 | // XcodeResult names an xcodebuild action's result bundle in the |
| 85 | // attempt's scratch. Without it xcodebuild stages one as |
| 86 | // ResultBundle_<date>.xcresult in the user's DARWIN_USER_TEMP_DIR |
| 87 | // (/private/var/folders/<..>/T, from confstr, not $TMPDIR), which the |
| 88 | // checks profile does not let it write: the Studio's sandbox log |
| 89 | // showed those denials. The path must not exist yet, so each action in |
| 90 | // one attempt has its own name; xcodebuild makes the folders. |
| 91 | func XcodeResult(name string) string { |
| 92 | return `-resultBundlePath "$TMPDIR/Results/` + name + `.xcresult"` |
| 93 | } |
| 94 | |
| 95 | // XcodeWrites sends what xcodebuild writes for itself into the |
| 96 | // attempt's scratch ($TMPDIR: the gate's, which the checks profile lets |
| 97 | // it write and the run removes), not the runner user's ~/Library. Every |
| 98 | // xcodebuild the synthesised gate, P2 and P6 run carries it, so P2 |
| 99 | // proves the way checks build. |
| 100 | // - -derivedDataPath: build products and intermediates, XCBuildData |
| 101 | // with its PIF cache (the "workspace" folder P2's script build could |
| 102 | // not write under srt), the module cache and the activity logs. |
| 103 | // The result bundle is XcodeResult's. |
| 104 | // - -clonedSourcePackagesDirPath: the packages' checkouts and |
| 105 | // workspace state (inside DerivedData by default; named so no |
| 106 | // default can move them). |
| 107 | // - -packageCachePath: SwiftPM's caches (~/Library/Caches/ |
| 108 | // org.swift.swiftpm by default), in xcodebuild since Xcode 13. |
| 109 | // |
| 110 | // SwiftPM's ~/.swiftpm (its security, configuration and cache links, |
| 111 | // and the lock it takes there) moves with XDG_CONFIG_HOME, which a |
| 112 | // sandboxed gate sets to the attempt's scratch (Gate.Run). |
| 113 | // |
| 114 | // xcodebuild applies -derivedDataPath to an action on a scheme, so a |
| 115 | // target is built through its scheme. |
| 116 | const XcodeWrites = `-derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache"` |
| 117 | |
| 118 | // XcodeGateName names the synthesised gate on the task page. |
| 119 | const XcodeGateName = "xcodebuild test" |
| 120 | |
| 121 | // NewestIPhone is the available iPhone simulator on the newest iOS |
| 122 | // runtime, the highest model number there (an SE counts as 0), then the |
| 123 | // first by name, so the choice is the same every time. |
| 124 | func NewestIPhone(sims []wire.Simulator) (wire.Simulator, bool) { |
| 125 | var best wire.Simulator |
| 126 | found := false |
| 127 | for _, s := range sims { |
| 128 | if !strings.HasPrefix(s.Runtime, "iOS ") || !strings.HasPrefix(s.Name, "iPhone") || !udidPattern.MatchString(s.UDID) { |
| 129 | continue |
| 130 | } |
| 131 | if !found { |
| 132 | best, found = s, true |
| 133 | continue |
| 134 | } |
| 135 | c := compareVersions(runtimeVersion(s.Runtime), runtimeVersion(best.Runtime)) |
| 136 | if c == 0 { |
| 137 | c = model(s.Name) - model(best.Name) |
| 138 | } |
| 139 | if c > 0 || c == 0 && s.Name < best.Name { |
| 140 | best = s |
| 141 | } |
| 142 | } |
| 143 | return best, found |
| 144 | } |
| 145 | |
| 146 | // quote is a POSIX shell single-quoted word. |
| 147 | func quote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" } |
| 148 | |
| 149 | func appendNew(list []string, s string) []string { |
| 150 | for _, x := range list { |
| 151 | if x == s { |
| 152 | return list |
| 153 | } |
| 154 | } |
| 155 | return append(list, s) |
| 156 | } |
| 157 | |
| 158 | var modelNumber = regexp.MustCompile(`iPhone ([0-9]+)`) |
| 159 | |
| 160 | func model(name string) int { |
| 161 | m := modelNumber.FindStringSubmatch(name) |
| 162 | if m == nil { |
| 163 | return 0 |
| 164 | } |
| 165 | n, _ := strconv.Atoi(m[1]) |
| 166 | return n |
| 167 | } |
| 168 | |