Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Add ProcessRunner and GitCheckout services to NXCore (#387)

v0lumehi5cacb070138b
diff --git a/frontend/ARCHITECTURE.md b/frontend/ARCHITECTURE.md
index fa67c331..244995ab 100644
--- a/frontend/ARCHITECTURE.md
+++ b/frontend/ARCHITECTURE.md
@@ -16,7 +16,7 @@ The plan and the steps are in [LNPANDY/NX2Apple#387](https://github.com/LNPANDY/
Step 1 of #387 made the targets and moved the files that already fit. Step 2 moved the language out of `BuildModel` into the `Localizer` store. Step 3 moved the build log into the `BuildLog` store. Step 4 moved the pure logic into `NXCore`. The rest still lives in the app target and moves with the later steps.
- `BuildModel` is the model of the whole launcher and reaches into every layer, so the files that use it cannot leave the app target yet. It no longer owns the language: it holds the `Localizer` it was given and forwards `lang` and `t()` to it, for the text it writes itself (alerts, status, failures).
-- `NXCore` holds the value types and static functions that need only Foundation: app and engine update parsing, `Localization.swift`, the release notes, the device and signing tools, the cloud folder and saves migration, the bundled tools, input staging, the build target and the pipe reader. Step 4 added four types: `TitleID` (parse a title ID from a file name, its owner, the update version, the build key), `GameInputs` (classify files into the four rows, row mismatch, allowed file types), `BuildProgress` (stage lines, progress details, durations, the steps of Xcode and the install, failure info) and `Toolchain` (probing Python, the venv, dotnet and Xcode, and the dependency rows `Dep`). `Stage` and `StepState` moved with `BuildProgress`. What reads `MetalPin`, `EngineLayout` or `OnboardingDefaults` (`detectDepsSync`, `metalDep`, `needsIOSPlatform`) and the handling of the build's JSON events stay in `BuildModel` until the later store steps.
+- `NXCore` holds the value types and static functions that need only Foundation: app and engine update parsing, `Localization.swift`, the release notes, the device and signing tools, the cloud folder and saves migration, the bundled tools, input staging, the build target and the pipe reader. Step 4 added four types: `TitleID` (parse a title ID from a file name, its owner, the update version, the build key), `GameInputs` (classify files into the four rows, row mismatch, allowed file types), `BuildProgress` (stage lines, progress details, durations, the steps of Xcode and the install, failure info) and `Toolchain` (probing Python, the venv, dotnet and Xcode, and the dependency rows `Dep`). `Stage` and `StepState` moved with `BuildProgress`. Step 5 added the first services: `ProcessRunner` (a protocol, with `SystemProcessRunner` as the real one), `ProcessGroups` (the process groups that Stop and quitting end) and `GitCheckout` (a protocol, with `SystemGitCheckout`, plus `GitRules` for the decisions that need no git). `ProcessRunner` is where a build process starts: `capture` runs to the end and keeps the output, `stream` hands out lines, `start` returns at once and can lead a process group. `BuildModel` holds a runner and a checkout, takes them in `init`, and uses them for the clone, fetch, pull, adoption of a ZIP copy, the build run, the tools of a stage, the archive step and `openOnDevice`. `Toolchain`, `Diagnostics`, `OlderTools`, `Uninstall`, `EngineLayout.runSync` and the update staging take a runner or a checkout as a parameter with the real one as default. Tests pass in a fake. These still start a `Process` themselves and move with the step that owns them: `AppSignature` (codesign) and `AppUpdate` (ditto, xattr) with the `AppUpdateStore`, `DeviceTools.run` (devicectl with a timeout and input) and `SaveTransfer` with the device store, `ShellInstaller.run` (a blocking read with start and end callbacks) with the build store, `EngineUpdater` (ditto) with the engine store, the administrator prompt in `LiveOnboardingDriver` and the game start in `LiveConsoleDriver`. The tests that stand in for `xcodebuild` and `devicectl` through `BuildModel.xcrunPath` stay too: `ShellInstaller` and `DeviceTools` run those tools themselves, so they move with the device store. What reads `MetalPin`, `EngineLayout` or `OnboardingDefaults` (`detectDepsSync`, `metalDep`, `needsIOSPlatform`) and the handling of the build's JSON events stay in `BuildModel` until the later store steps.
- `NXCoreTests` tests `NXCore` alone. It links no other target, so these tests run without SwiftUI. New tests of pure logic go there.
- `NXModel` holds `Localizer`: `lang` and `t()`. The app creates one with each `BuildModel`, and `ContentView` hands it to every view as an environment object. A view that shows text declares `@EnvironmentObject var loc: Localizer` and calls `loc.t(...)`. A language change redraws these views and nothing else.
- `NXModel` also holds `BuildLog`: the log text, its tail trimming and the line buffer. `BuildModel` holds it in a `let` and does not publish it, and only `BuildLogView` (in `NXLauncher`) observes it. A log line redraws the log and no game card. `LauncherPerformanceTests` writes 1000 lines during a build and allows 0 cards. `BuildModel` is not `@MainActor` yet, so it reaches the log through `BuildLog.onMain`.
diff --git a/frontend/Sources/NX2Apple/ContentView.swift b/frontend/Sources/NX2Apple/ContentView.swift
index 4f73d481..ea6a398e 100644
--- a/frontend/Sources/NX2Apple/ContentView.swift
+++ b/frontend/Sources/NX2Apple/ContentView.swift
@@ -177,74 +177,23 @@ final class BuildModel: ObservableObject {
/// The language and the strings: views take them from the environment, this model only for the text it writes itself.
let localizer: Localizer
- init(localizer: Localizer = Localizer()) { self.localizer = localizer }
+ /// The services: the only places that start a process (`ProcessRunner`) and that run git (`GitCheckout`). Tests pass in fakes.
+ let runner: ProcessRunner
+ let gitCheckout: GitCheckout
+ init(localizer: Localizer = Localizer(), runner: ProcessRunner = SystemProcessRunner(), gitCheckout: GitCheckout? = nil) {
+ self.localizer = localizer
+ self.runner = runner
+ self.gitCheckout = gitCheckout ?? SystemGitCheckout(runner: runner)
+ _ = Self.endGroupsWhenQuitting
+ }
- private var proc: Process?
+ private var proc: RunningProcess?
private var staging: String?
- // ---- Process groups: Stop and quitting end the whole build ----
- // The script runs its tools as children (the shader split with its workers, dotnet, xcodebuild) and cleans
- // them up only on Ctrl-C: a SIGTERM to the script alone leaves them running, orphaned. So each build process
- // starts in a process group of its own, and Stop, quitting the app or a SIGTERM to it end the whole group.
-
- /// The groups of the builds running now (their leaders' PIDs).
- private static var groups = Set<pid_t>()
- private static let groupsLock = NSLock()
- private static let exitHandlers: Void = {
- NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: nil) { _ in endAllGroups() }
- signal(SIGTERM, SIG_IGN) // handled below: end the builds, then exit
- let source = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .global())
- source.setEventHandler { endAllGroups(); exit(0) }
- source.resume()
- sigtermSource = source
+ /// The process groups of the builds are `ProcessGroups` (NXCore). Quitting the app ends them: the notification is AppKit's.
+ private static let endGroupsWhenQuitting: Void = {
+ NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: nil) { _ in ProcessGroups.endAll() }
}()
- private static var sigtermSource: DispatchSourceSignal?
-
- /// Run `p` as the leader of a new process group: Python sets the group, then execs into the real command.
- /// It also restores SIGTERM and SIGINT to their defaults first: the app ignores SIGTERM (to end its builds before
- /// it exits), and an ignored signal is inherited across exec, which would make Stop's SIGTERM do nothing.
- nonisolated static func inOwnGroup(_ p: Process, python: String) {
- let command = p.executableURL?.path ?? python
- let start = "import os, signal, sys; signal.signal(signal.SIGTERM, signal.SIG_DFL); signal.signal(signal.SIGINT, signal.SIG_DFL); "
- + "os.setpgid(0, 0); os.execv(sys.argv[1], sys.argv[1:])"
- p.arguments = ["-c", start, command] + (p.arguments ?? [])
- p.executableURL = URL(fileURLWithPath: python)
- }
-
- static func track(_ p: Process) {
- _ = exitHandlers
- groupsLock.lock(); groups.insert(p.processIdentifier); groupsLock.unlock()
- }
-
- static func untrack(_ p: Process) {
- groupsLock.lock(); groups.remove(p.processIdentifier); groupsLock.unlock()
- }
-
- /// A build's process group is still running (the app update waits for it: its relaunch would end the build).
- static var buildProcessesAlive: Bool {
- groupsLock.lock(); defer { groupsLock.unlock() }
- return groups.contains { killpg($0, 0) == 0 }
- }
-
- /// SIGTERM to every process of the group, SIGKILL 5 s later to whatever is left of it.
- nonisolated static func endGroup(_ leader: pid_t) {
- guard leader > 1 else { return }
- killpg(leader, SIGTERM)
- DispatchQueue.global().asyncAfter(deadline: .now() + 5) {
- if killpg(leader, 0) == 0 { killpg(leader, SIGKILL) }
- }
- }
-
- private static func endAllGroups() {
- groupsLock.lock(); let all = groups; groupsLock.unlock()
- for leader in all {
- killpg(leader, SIGTERM)
- }
- if !all.isEmpty {
- usleep(1_500_000) // the app is about to exit: one grace period for the whole set, then kill what remains
- for leader in all where killpg(leader, 0) == 0 { killpg(leader, SIGKILL) }
- }
- }
/// The checkout of this repository the app drives. A released .app ships on its own,
/// so the path is not known at build time: look next to the app bundle, walk up from
@@ -359,39 +308,16 @@ final class BuildModel: ObservableObject {
var pendingEngine: (engine: EngineUpdate.Engine, channel: EngineChannel)?
var usesEngineStore: Bool { engineBundle != nil }
- nonisolated static func gitBinary() -> String? {
- for c in ["/usr/bin/git", "/opt/homebrew/bin/git", "/usr/local/bin/git"]
- where FileManager.default.isExecutableFile(atPath: c) { return c }
- return nil
- }
- nonisolated static func gitEnv() -> [String: String] {
- ["PATH": "/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin", "HOME": NSHomeDirectory()]
- }
-
/// The PATH of the processes that run the build, installing packages and writing the RomFS image: the bundled tools in front of
/// `base` (BundledTools.swift), which is Homebrew's folders and the system's. One place, so that no environment forgets the bundle.
nonisolated static func toolPath(_ base: String = "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin", python: String? = nil) -> String {
BundledTools.path(base, python: python)
}
- /// Run a command with all output captured (no streaming; fetch / rev-list / pull print little).
- nonisolated static func runQuiet(_ args: [String], env: [String: String]) -> (ok: Bool, out: String) {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: args[0])
- p.arguments = Array(args.dropFirst())
- p.environment = env
- p.standardInput = FileHandle.nullDevice
- let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe
- do { try p.run() } catch { return (false, error.localizedDescription) }
- let data = pipe.fileHandleForReading.readDataToEndOfFile()
- p.waitUntilExit()
- return (p.terminationStatus == 0, String(data: data, encoding: .utf8) ?? "")
- }
-
/// First-time flow: clone the project to ~/nxnative (the discovery seeds cover it) and select it.
func cloneRepo() {
guard !running, !repoBusy, !usesEngineStore else { return }
- guard let git = Self.gitBinary() else { repoStatus = t("repo.gitMissing"); return }
+ guard gitCheckout.binary != nil else { repoStatus = t("repo.gitMissing"); return }
let dest = NSHomeDirectory() + "/NX2Apple"
if FileManager.default.fileExists(atPath: dest) {
if Self.isRepo(dest) { repoPath = dest; repoStatus = ""; checkRepoUpdates() }
@@ -400,10 +326,9 @@ final class BuildModel: ObservableObject {
}
repoBusy = true; repoStatus = t("repo.downloading")
append("git clone --depth 1 \(Self.repoURL) \(dest)\n")
+ let checkout = gitCheckout
DispatchQueue.global().async {
- // Shallow: a quarter of the bytes and a third of the time of a full clone, and fetch /
- // pull keep working on it (verified); the history is of no use to someone who wants to build.
- let ok = Self.runCaptured([git, "clone", "--depth", "1", "--single-branch", "--progress", Self.repoURL, dest], env: Self.gitEnv()) {
+ let ok = checkout.clone(url: Self.repoURL, into: dest) {
line in DispatchQueue.main.async { self.append(line + "\n") }
}
DispatchQueue.main.async {
@@ -424,20 +349,15 @@ final class BuildModel: ObservableObject {
/// error noise when offline — it only fills repoBehind when it can.
func checkRepoUpdates(silent: Bool = false) {
guard repoOK, repoIsGit, !usesEngineStore else { return } // a release app updates its engine by version instead
- guard !running, !repoBusy, let git = Self.gitBinary() else {
- if !silent, Self.gitBinary() == nil { repoStatus = t("repo.gitMissing") }
+ guard !running, !repoBusy, gitCheckout.binary != nil else {
+ if !silent, gitCheckout.binary == nil { repoStatus = t("repo.gitMissing") }
return
}
repoBusy = true
if !silent { repoStatus = t("repo.checking") }
- let dir = repoPath
+ let dir = repoPath, checkout = gitCheckout
DispatchQueue.global().async {
- let fetched = Self.runQuiet([git, "-C", dir, "fetch", "origin", "main"], env: Self.gitEnv())
- var behind = -1
- if fetched.ok {
- let count = Self.runQuiet([git, "-C", dir, "rev-list", "--count", "HEAD..origin/main"], env: Self.gitEnv())
- if count.ok { behind = Int(count.out.trimmingCharacters(in: .whitespacesAndNewlines)) ?? -1 }
- }
+ let behind = checkout.behind(dir) ?? -1
DispatchQueue.main.async {
self.repoBusy = false
if behind >= 0 {
@@ -462,11 +382,11 @@ final class BuildModel: ObservableObject {
}
func pullRepo() {
- guard !running, !repoBusy, !usesEngineStore, repoOK, repoIsGit, let git = Self.gitBinary() else { return }
+ guard !running, !repoBusy, !usesEngineStore, repoOK, repoIsGit, gitCheckout.binary != nil else { return }
repoBusy = true; repoStatus = t("repo.pulling")
- let dir = repoPath
+ let dir = repoPath, checkout = gitCheckout
DispatchQueue.global().async {
- let r = Self.runQuiet([git, "-C", dir, "pull", "--ff-only", "origin", "main"], env: Self.gitEnv())
+ let r = checkout.pull(dir)
DispatchQueue.main.async {
self.repoBusy = false
if r.ok {
@@ -485,14 +405,11 @@ final class BuildModel: ObservableObject {
/// to its tracked files and no build running, so a checkout someone works in is never touched. Settings says what
/// happened (or why not), and the library's activity line names the new commits.
func autoPull(_ behind: Int) {
- guard !running, !repoBusy, !usesEngineStore, repoOK, repoIsGit, let git = Self.gitBinary() else { return }
+ guard !running, !repoBusy, !usesEngineStore, repoOK, repoIsGit, gitCheckout.binary != nil else { return }
repoBusy = true
- let dir = repoPath
+ let dir = repoPath, checkout = gitCheckout
DispatchQueue.global().async {
- let branch = Self.runQuiet([git, "-C", dir, "rev-parse", "--abbrev-ref", "HEAD"], env: Self.gitEnv())
- let status = Self.runQuiet([git, "-C", dir, "status", "--porcelain", "--untracked-files=no"], env: Self.gitEnv())
- let allowed = branch.ok && status.ok && Self.autoPullAllowed(branch: branch.out, status: status.out)
- let pulled = allowed ? Self.runQuiet([git, "-C", dir, "pull", "--ff-only", "origin", "main"], env: Self.gitEnv()) : nil
+ let pulled = checkout.autoPull(dir)
DispatchQueue.main.async {
self.repoBusy = false
guard let pulled else { self.repoStatus = self.t("repo.autoSkipped"); return }
@@ -508,41 +425,16 @@ final class BuildModel: ObservableObject {
}
}
- /// A checkout the automatic update may move: on main, with no changes to tracked files (untracked files stay; a pull
- /// that would overwrite one fails as a fast-forward and says so).
- nonisolated static func autoPullAllowed(branch: String, status: String) -> Bool {
- branch.trimmingCharacters(in: .whitespacesAndNewlines) == "main" && status.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
- }
-
- /// The local branch that keeps a ZIP copy as it was when it is connected to git.
- nonisolated static func zipCopyBranch(date: Date = Date()) -> String {
- let f = DateFormatter()
- f.locale = Locale(identifier: "en_US_POSIX"); f.dateFormat = "yyyy-MM-dd"
- return "zip-copy-" + f.string(from: date)
- }
-
- /// What the launch does with the configured repository when it is a copy without .git (#87: a ZIP copy never updated
- /// itself, so its build tools were older than the app).
- enum ZipAdoption: Equatable { case notNeeded, adopt, waitsForBuild, autoUpdateOff }
-
- /// Pure: adopt a valid checkout without .git into git, but never while a build runs (or another repository task),
- /// and not when the player turned the automatic update off.
- nonisolated static func zipAdoption(isRepo: Bool, isGit: Bool, buildRunning: Bool, repoBusy: Bool, autoUpdate: Bool) -> ZipAdoption {
- guard isRepo, !isGit else { return .notNeeded }
- if buildRunning || repoBusy { return .waitsForBuild }
- return autoUpdate ? .adopt : .autoUpdateOff
- }
-
/// The launch-time step for a ZIP copy; a git checkout is left to checkRepoUpdates. Settings and the library's
/// activity line say what happened.
func adoptAtLaunch(remote: String = BuildModel.repoURL) {
guard !usesEngineStore else { return }
- switch Self.zipAdoption(isRepo: repoOK, isGit: repoIsGit, buildRunning: running, repoBusy: repoBusy, autoUpdate: autoUpdateRepo) {
+ switch GitRules.zipAdoption(isRepo: repoOK, isGit: repoIsGit, buildRunning: running, repoBusy: repoBusy, autoUpdate: autoUpdateRepo) {
case .notNeeded: break
case .waitsForBuild: repoStatus = t("repo.zipWaits")
case .autoUpdateOff: break // Settings says so next to the repository (repo.zipNoAuto): the player chose this
case .adopt:
- if Self.gitBinary() == nil { repoStatus = t("repo.gitMissing") }
+ if gitCheckout.binary == nil { repoStatus = t("repo.gitMissing") }
else { adoptRepoAsGit(automatic: true, remote: remote) }
}
}
@@ -556,37 +448,12 @@ final class BuildModel: ObservableObject {
/// outside the repository.
/// `automatic`: the launch-time adoption (adoptAtLaunch); `remote` is GitHub unless a test points it at a local repository.
func adoptRepoAsGit(automatic: Bool = false, remote: String = BuildModel.repoURL) {
- guard !running, !repoBusy, !usesEngineStore, repoOK, !repoIsGit, let git = Self.gitBinary() else { return }
+ guard !running, !repoBusy, !usesEngineStore, repoOK, !repoIsGit, gitCheckout.binary != nil else { return }
repoBusy = true; repoStatus = t("repo.adopting")
- let dir = repoPath
+ let dir = repoPath, checkout = gitCheckout
DispatchQueue.global().async {
- var out = "", ok = true
- let branch = Self.zipCopyBranch()
- func run(_ args: [String]) -> (ok: Bool, out: String) {
- let r = Self.runQuiet([git, "-C", dir] + args, env: Self.gitEnv())
- out += "$ git \(args.joined(separator: " "))\n\(r.out)\n"
- return r
- }
- // Nothing in the folder changes before the fetch has worked, so an offline failure leaves the copy as it was.
- ok = run(["init"]).ok && run(["remote", "add", "origin", remote]).ok && run(["fetch", "--depth", "1", "origin", "main"]).ok
- if ok {
- let tree = run(["ls-tree", "-r", "-z", "--name-only", "origin/main"])
- let present = tree.out.split(separator: "\0").map(String.init).filter { FileManager.default.fileExists(atPath: dir + "/" + $0) }
- let list = NSTemporaryDirectory() + "nspb-zip-paths-\(UUID().uuidString.prefix(8)).txt"
- defer { try? FileManager.default.removeItem(atPath: list) }
- let identity = ["-c", "user.name=NX2Apple", "-c", "user.email=nx2apple@localhost", "-c", "commit.gpgsign=false"]
- ok = tree.ok
- && run(["symbolic-ref", "HEAD", "refs/heads/" + branch]).ok
- && ((try? Data(present.joined(separator: "\0").utf8).write(to: URL(fileURLWithPath: list))) != nil)
- && run(["add", "--pathspec-from-file=" + list, "--pathspec-file-nul"]).ok
- && run(identity + ["commit", "--allow-empty", "-m", "The ZIP copy as it was before it was connected to git"]).ok
- && run(["checkout", "-f", "-B", "main", "origin/main"]).ok
- }
- if ok { _ = Self.runQuiet([git, "-C", dir, "branch", "--set-upstream-to=origin/main", "main"], env: Self.gitEnv()) } // checkout -B usually set it
- // A failed adoption (offline) must not leave the .git that `git init` made: the copy would count as a
- // checkout from then on, and neither the launch nor the button would try again. The guard above
- // means there was no .git before, so this removes only what this call created.
- if !ok { try? FileManager.default.removeItem(atPath: dir + "/.git") }
+ let branch = GitRules.zipCopyBranch()
+ let (ok, out) = checkout.adopt(dir, remote: remote, branch: branch)
DispatchQueue.main.async {
self.repoBusy = false
self.append(out + "\n")
@@ -624,7 +491,7 @@ final class BuildModel: ObservableObject {
var tail = "" // what failureKind reads when the sync stops (only touched on the main queue)
DispatchQueue.global().async {
let py = chosen.isEmpty ? Toolchain.findPython().path : chosen
- let ok = EngineLayout.runSync(repo: repo, python: py) { line in
+ let ok = EngineLayout.runSync(repo: repo, python: py, runner: self.runner) { line in
DispatchQueue.main.async { self.append(line + "\n"); tail = String((tail + line + "\n").suffix(4000)) }
}
DispatchQueue.main.async {
@@ -837,7 +704,8 @@ final class BuildModel: ObservableObject {
/// A downloaded zip to a staged app: its digest first, then unzip (ditto, as the release was packed), then the app inside.
nonisolated static func stage(zip: URL, release: AppUpdate.Release, in root: URL, expectedID: String,
requireDigest: Bool,
- runningOS: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion) -> Result<AppUpdate.Staged, StageFailure> {
+ runningOS: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion,
+ runner: ProcessRunner = SystemProcessRunner()) -> Result<AppUpdate.Staged, StageFailure> {
let fm = FileManager.default
switch AppUpdate.checkDigest(zip, expected: release.digest) {
case .match: break
@@ -847,14 +715,11 @@ final class BuildModel: ObservableObject {
let dir = root.appendingPathComponent("app", isDirectory: true)
try? fm.removeItem(at: dir)
try? fm.createDirectory(at: dir, withIntermediateDirectories: true)
- let unzip = Process()
- unzip.executableURL = URL(fileURLWithPath: "/usr/bin/ditto")
- unzip.arguments = ["-x", "-k", zip.path, dir.path]
- do { try unzip.run() } catch { return .failure(.unpack) }
- unzip.waitUntilExit()
+ let unzip = runner.capture(ProcessLaunch(executable: "/usr/bin/ditto", arguments: ["-x", "-k", zip.path, dir.path]))
+ guard unzip.started else { return .failure(.unpack) }
try? fm.removeItem(at: zip)
// The archive holds one app: NX2Apple.app, or the bundle under an older name in a release from before a rename
- guard unzip.terminationStatus == 0,
+ guard unzip.status == 0,
let app = ((try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? []).first(where: { $0.pathExtension == "app" })
else { return .failure(.unpack) }
if AppUpdate.bundleProblem(app, expectedID: expectedID, tag: release.tag) != nil {
@@ -871,7 +736,7 @@ final class BuildModel: ObservableObject {
/// No build of this app is running: none in the model, and no process group of one still alive (a stopped
/// build's tools can take a moment to go).
- var mayInstallAppUpdate: Bool { !running && !Self.buildProcessesAlive }
+ var mayInstallAppUpdate: Bool { !running && !ProcessGroups.buildProcessesAlive }
/// Swap the staged app in. `relaunch` (Restart now, the Update button) opens the new copy and exits; at quit
/// the next launch is the new version. Refused while a build runs.
@@ -1142,7 +1007,7 @@ final class BuildModel: ObservableObject {
}
if compressed, !Toolchain.runOK(py, ["-c", "import nsz"]) {
say(t("msg.installingNsz"))
- let r = Self.runCaptured([py, "-m", "pip", "install", "nsz"],
+ let r = runner.runCaptured([py, "-m", "pip", "install", "nsz"],
env: ["PATH": Self.toolPath(python: py),
"HOME": NSHomeDirectory()].merging(BundledTools.environment(for: py)) { $1 }) { line in
DispatchQueue.main.async { self.append(line + "\n") }
@@ -1187,10 +1052,6 @@ final class BuildModel: ObservableObject {
say(String(format: t("msg.running"), py) + " " + args.joined(separator: " "))
lastBuildCommand = [py] + args
- let p = Process()
- p.executableURL = URL(fileURLWithPath: py)
- p.arguments = args
- p.currentDirectoryURL = URL(fileURLWithPath: engine)
// The root of the runtime the apphost looks for: derived from the detected installation,
// not assumed to be ~/.dotnet (Homebrew keeps it in …/opt/dotnet/libexec). Only set when the
// derivation actually answered — a wrong root is worse than none, and the pipeline derives
@@ -1209,12 +1070,8 @@ final class BuildModel: ObservableObject {
// The bundled Python lives inside the signed app: a .pyc written there would change what the signature seals
// (the packages are compiled at bundle time), and a user site must not shadow its pinned packages.
denv.merge(BundledTools.environment(for: py)) { $1 }
- p.environment = denv
- Self.inOwnGroup(p, python: py)
- let pipe = Pipe()
- p.standardOutput = pipe; p.standardError = pipe
- p.terminationHandler = { [weak self] status in
- Self.untrack(status)
+ let launch = ProcessLaunch(executable: py, arguments: args, environment: denv, directory: engine, groupPython: py)
+ let onExit: (Int32) -> Void = { [weak self] status in
DispatchQueue.main.async {
guard let self else { return }
self.running = false
@@ -1226,7 +1083,7 @@ final class BuildModel: ObservableObject {
self.cleanupStaging()
return
}
- if status.terminationStatus == 0 {
+ if status == 0 {
self.markAllDone()
if self.target == BuildTarget.mac {
// The .app is self-contained (its RomFS is inside), and the marker file
@@ -1257,13 +1114,13 @@ final class BuildModel: ObservableObject {
} else {
if let i = self.stages.lastIndex(where: { $0.state == .running }) {
self.stages[i].state = .failed
- self.stages[i].detail = String(format: self.t("msg.exitCode"), status.terminationStatus)
+ self.stages[i].detail = String(format: self.t("msg.exitCode"), status)
self.noteFailure()
} else {
// The last real line says more than the exit code alone, and the message
// points at the log: "Build failed (exit code 1)" with nothing else sent a
// reporter looking for a log that a short window had below the fold (#52)
- self.doneNote = String(format: self.t("msg.buildFailed"), status.terminationStatus)
+ self.doneNote = String(format: self.t("msg.buildFailed"), status)
if let last = BuildProgress.lastMeaningfulLine(self.logTail) {
self.doneNote = String(format: self.t("msg.buildFailedReason"), last)
}
@@ -1273,12 +1130,12 @@ final class BuildModel: ObservableObject {
}
}
}
- PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in
- guard let s = String(data: d, encoding: .utf8) else { return }
- DispatchQueue.main.async { self?.ingest(s) }
- }
- proc = p
- do { try p.run(); Self.track(p) } catch { fail(String(format: t("msg.launchFailed"), error.localizedDescription)) }
+ do {
+ proc = try runner.start(launch, onOutput: { [weak self] d in
+ guard let s = String(data: d, encoding: .utf8) else { return }
+ DispatchQueue.main.async { self?.ingest(s) }
+ }, onExit: onExit)
+ } catch { fail(String(format: t("msg.launchFailed"), error.localizedDescription)) }
}
private var shellDeviceChecked = false
@@ -1469,7 +1326,7 @@ final class BuildModel: ObservableObject {
func stop() {
guard running, !stopping, let proc, proc.isRunning else { return }
stopping = true
- Self.endGroup(proc.processIdentifier) // the script and every tool it started
+ ProcessGroups.endGroup(proc.processIdentifier) // the script and every tool it started
}
private func cleanupStaging() {
@@ -1692,24 +1549,17 @@ final class BuildModel: ObservableObject {
}
return
}
- let p = Process()
- p.executableURL = URL(fileURLWithPath: pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath)
// dest/romfs.img: one file, which main.m's per-game lookup takes before any folder.
let engine = EngineLayout.root(repoPath)
- p.arguments = [engine + "/" + Self.romfsImageTool, "--reuse", romfs, dest + "/romfs.img"]
- let romfsPy = p.executableURL!.path
- p.environment = ["PATH": Self.toolPath("/usr/bin:/bin:/usr/local/bin:/opt/homebrew/bin", python: romfsPy), "PYTHONPATH": engine + "/dm/tools"]
- .merging(BundledTools.environment(for: romfsPy)) { $1 }
- Self.inOwnGroup(p, python: p.executableURL!.path)
- let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe
- PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in
- guard let s = String(data: d, encoding: .utf8) else { return }
- DispatchQueue.main.async { self?.append(s) }
- }
- p.terminationHandler = { [weak self] st in Self.untrack(st); DispatchQueue.main.async {
+ let romfsPy = pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath
+ let launch = ProcessLaunch(executable: romfsPy, arguments: [engine + "/" + Self.romfsImageTool, "--reuse", romfs, dest + "/romfs.img"],
+ environment: ["PATH": Self.toolPath("/usr/bin:/bin:/usr/local/bin:/opt/homebrew/bin", python: romfsPy), "PYTHONPATH": engine + "/dm/tools"]
+ .merging(BundledTools.environment(for: romfsPy)) { $1 },
+ groupPython: romfsPy)
+ let onExit: (Int32) -> Void = { [weak self] st in DispatchQueue.main.async {
guard let self else { return }
let cancelled = self.stopping
- let succeeded = st.terminationStatus == 0 && !cancelled
+ let succeeded = st == 0 && !cancelled
self.stopping = false
if succeeded {
self.archivePath = dest
@@ -1720,17 +1570,21 @@ final class BuildModel: ObservableObject {
self.stages[i].state = succeeded ? .done : .failed
self.stages[i].detail = succeeded
? String(format: self.t("prog.archiveDone"), dest)
- : String(format: self.t("msg.archiveFailed"), st.terminationStatus)
+ : String(format: self.t("msg.archiveFailed"), st)
}
if succeeded { return then(dest) }
self.cleanupStaging()
self.running = false
self.done = false
if cancelled { self.resetProgress() }
- self.doneNote = cancelled ? self.t("msg.stopped") : String(format: self.t("msg.archiveFailed"), st.terminationStatus)
+ self.doneNote = cancelled ? self.t("msg.stopped") : String(format: self.t("msg.archiveFailed"), st)
}}
- proc = p
- do { try p.run(); Self.track(p) } catch {
+ do {
+ proc = try runner.start(launch, onOutput: { [weak self] d in
+ guard let s = String(data: d, encoding: .utf8) else { return }
+ DispatchQueue.main.async { self?.append(s) }
+ }, onExit: onExit)
+ } catch {
if let i = stages.firstIndex(where: { $0.id == idx }) {
stages[i].state = .failed; stages[i].detail = t("msg.archiveNoStart")
}
@@ -1764,8 +1618,8 @@ final class BuildModel: ObservableObject {
started: s.state == .running ? Date() : nil))
}
} }
- installer.processStarted = { p in DispatchQueue.main.async { self.proc = p; Self.track(p) } }
- installer.processEnded = { p in Self.untrack(p) }
+ installer.processStarted = { p in DispatchQueue.main.async { self.proc = p; ProcessGroups.track(p) } }
+ installer.processEnded = { p in ProcessGroups.untrack(p) }
// The size and the connection are shown with Continue, "Wait for a cable" and "Not now" (ShellCopyCard)
installer.decideCopy = { plan in
let answered = DispatchSemaphore(value: 0)
@@ -1969,14 +1823,10 @@ final class BuildModel: ObservableObject {
stages.append(Stage(id: id, name: name, state: .running, started: Date()))
}
append("[\(name)] \(command.joined(separator: " "))\n")
- let p = Process()
- p.executableURL = URL(fileURLWithPath: command[0])
- p.arguments = Array(command.dropFirst())
- p.environment = ["PATH": "/usr/bin:/bin:/usr/sbin:/sbin", "HOME": NSHomeDirectory(), "USER": NSUserName(), "LANG": "en_US.UTF-8"]
- Self.inOwnGroup(p, python: pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath)
- let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe
+ let launch = ProcessLaunch(command, environment: ["PATH": "/usr/bin:/bin:/usr/sbin:/sbin", "HOME": NSHomeDirectory(), "USER": NSUserName(), "LANG": "en_US.UTF-8"],
+ groupPython: pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath)
var buffer = "", lastError = ""
- PipeReader.attach(to: pipe.fileHandleForReading) { [weak self] d in
+ let onOutput: (Data) -> Void = { [weak self] d in
guard let chunk = String(data: d, encoding: .utf8) else { return }
DispatchQueue.main.async {
guard let self else { return }
@@ -1992,45 +1842,36 @@ final class BuildModel: ObservableObject {
}
}
}
- p.terminationHandler = { [weak self] status in
- Self.untrack(status)
+ let onExit: (Int32) -> Void = { [weak self] status in
DispatchQueue.main.async {
guard let self else { return }
- pipe.fileHandleForReading.readabilityHandler = nil
if self.stopping {
self.stopping = false; self.running = false
self.resetProgress(); self.doneNote = self.t("msg.stopped"); self.cleanupStaging()
return
}
- let ok = status.terminationStatus == 0
+ let ok = status == 0
if let i = self.stages.firstIndex(where: { $0.id == id }) {
self.stages[i].state = ok ? .done : .failed
if ok { self.stages[i].percent = 100; self.stages[i].detail = "" }
}
- then(ok ? nil : (lastError.isEmpty ? String(format: self.t("msg.exitCode"), status.terminationStatus) : lastError))
+ then(ok ? nil : (lastError.isEmpty ? String(format: self.t("msg.exitCode"), status) : lastError))
}
}
- proc = p
- do { try p.run(); Self.track(p) } catch { then(error.localizedDescription) }
+ do { proc = try runner.start(launch, onOutput: onOutput, onExit: onExit) } catch { then(error.localizedDescription) }
}
/// Start the game on the device it was installed on.
func openOnDevice(udid: String, app: String, completion: @escaping (String?) -> Void) {
let info = NSDictionary(contentsOfFile: app + "/Info.plist")
guard let bundle = info?["CFBundleIdentifier"] as? String else { return completion(String(format: t("dev.noProject"), shortPath(app))) }
- let xcrun = xcrunPath
+ let launch = ProcessLaunch(executable: xcrunPath, arguments: ["devicectl", "device", "process", "launch", "--device", udid, bundle], mergesErrors: true)
+ let runner = runner
DispatchQueue.global(qos: .userInitiated).async {
- let p = Process(), out = Pipe()
- p.executableURL = URL(fileURLWithPath: xcrun)
- p.arguments = ["devicectl", "device", "process", "launch", "--device", udid, bundle]
- p.standardOutput = out; p.standardError = out
+ let r = runner.capture(launch)
var message: String?
- do {
- try p.run()
- let text = String(decoding: out.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
- p.waitUntilExit()
- if p.terminationStatus != 0 { message = text.split(separator: "\n").last(where: { $0.contains("rror") }).map(String.init) ?? text }
- } catch { message = error.localizedDescription }
+ if !r.started { message = r.out }
+ else if r.status != 0 { message = r.out.split(separator: "\n").last(where: { $0.contains("rror") }).map(String.init) ?? r.out }
DispatchQueue.main.async { completion(message) }
}
}
@@ -2170,7 +2011,7 @@ final class BuildModel: ObservableObject {
ok = r.ok
} else {
let collected = LineBox()
- ok = Self.runCaptured(["/bin/bash", "-c",
+ ok = self.runner.runCaptured(["/bin/bash", "-c",
cmd.map { $0.contains(" ") ? "'" + $0 + "'" : $0 }.joined(separator: " ")],
env: ["PATH": "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"HOME": NSHomeDirectory(), "NONINTERACTIVE": "1", "USER": NSUserName()])
@@ -2203,27 +2044,6 @@ final class BuildModel: ObservableObject {
var lines: [String] { lock.lock(); defer { lock.unlock() }; return box }
}
- /// Run a command, streaming its output line by line to `line`. Returns success.
- nonisolated static func runCaptured(_ args: [String], env: [String: String], line: @escaping (String) -> Void) -> Bool {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: args[0])
- p.arguments = Array(args.dropFirst())
- p.environment = env
- let out = Pipe(), err = Pipe()
- p.standardOutput = out; p.standardError = err; p.standardInput = FileHandle.nullDevice
- do { try p.run() } catch { line("could not start \(args[0]): \(error.localizedDescription)"); return false }
- for h in [out.fileHandleForReading, err.fileHandleForReading] {
- PipeReader.attach(to: h) { d in
- if let s = String(data: d, encoding: .utf8) {
- for l in s.split(separator: "\n") { line(String(l)) }
- }
- }
- }
- p.waitUntilExit()
- out.fileHandleForReading.readabilityHandler = nil; err.fileHandleForReading.readabilityHandler = nil
- return p.terminationStatus == 0
- }
-
var allDepsPresent: Bool { depsChecked && deps.allSatisfy { $0.present } }
/// Home shown as ~ (shorter in a one-line row, and it keeps the account name out of
diff --git a/frontend/Sources/NX2Apple/Diagnostics.swift b/frontend/Sources/NX2Apple/Diagnostics.swift
index 9e2eff1a..de9ec1ff 100644
--- a/frontend/Sources/NX2Apple/Diagnostics.swift
+++ b/frontend/Sources/NX2Apple/Diagnostics.swift
@@ -217,10 +217,10 @@ struct Diagnostics {
}
/// The commit, branch and distance from main of a checkout, from its own refs.
- static func git(_ repo: String) -> (commit: String?, branch: String?, behind: [(ref: String, count: Int)], lastFetch: Date?) {
- guard !repo.isEmpty, FileManager.default.fileExists(atPath: repo + "/.git"), let git = BuildModel.gitBinary() else { return (nil, nil, [], nil) }
+ static func git(_ repo: String, checkout: GitCheckout = SystemGitCheckout()) -> (commit: String?, branch: String?, behind: [(ref: String, count: Int)], lastFetch: Date?) {
+ guard !repo.isEmpty, FileManager.default.fileExists(atPath: repo + "/.git"), checkout.binary != nil else { return (nil, nil, [], nil) }
func run(_ args: [String]) -> String? {
- let r = BuildModel.runQuiet([git, "-C", repo] + args, env: BuildModel.gitEnv())
+ let r = checkout.run(repo, args)
let s = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
return r.ok && !s.isEmpty ? s : nil
}
@@ -250,13 +250,14 @@ struct Diagnostics {
return (pins["metal_macosx"], pins["metal_iphoneos"])
}
- /// A command's output when it exits 0, trimmed; nil when it fails or prints nothing. The tests stand in for BuildModel.runQuiet.
+ /// A command's output when it exits 0, trimmed; nil when it fails or prints nothing. The tests stand in for the ProcessRunner.
typealias Probe = (_ args: [String]) -> String?
- static func probe(env: [String: String] = ["HOME": NSHomeDirectory(), "PATH": "/usr/bin:/bin", "TMPDIR": NSTemporaryDirectory()]) -> Probe {
+ static func probe(env: [String: String] = ["HOME": NSHomeDirectory(), "PATH": "/usr/bin:/bin", "TMPDIR": NSTemporaryDirectory()],
+ runner: ProcessRunner = SystemProcessRunner()) -> Probe {
{ args in
guard FileManager.default.isExecutableFile(atPath: args[0]) else { return nil }
- let r = BuildModel.runQuiet(args, env: env)
+ let r = runner.runQuiet(args, env: env)
let s = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
return r.ok && !s.isEmpty ? s : nil
}
@@ -285,9 +286,9 @@ struct Diagnostics {
}
/// What a tool prints for its version, nil when it does not run.
- static func version(_ tool: String, _ args: [String], env: [String: String]) -> String? {
+ static func version(_ tool: String, _ args: [String], env: [String: String], runner: ProcessRunner = SystemProcessRunner()) -> String? {
guard FileManager.default.isExecutableFile(atPath: tool) else { return nil }
- let r = BuildModel.runQuiet([tool] + args, env: env)
+ let r = runner.runQuiet([tool] + args, env: env)
let s = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
return r.ok && !s.isEmpty ? String(s.split(separator: "\n").last ?? "") : nil
}
diff --git a/frontend/Sources/NX2Apple/Engine.swift b/frontend/Sources/NX2Apple/Engine.swift
index 8c7bdfd5..35b06844 100644
--- a/frontend/Sources/NX2Apple/Engine.swift
+++ b/frontend/Sources/NX2Apple/Engine.swift
@@ -108,9 +108,9 @@ enum EngineLayout {
}
/// Run the sync and stream its lines (blocking: call it off the main thread). git comes from the system's or Homebrew's PATH.
- static func runSync(repo: String, python: String, line: @escaping (String) -> Void) -> Bool {
+ static func runSync(repo: String, python: String, runner: ProcessRunner = SystemProcessRunner(), line: @escaping (String) -> Void) -> Bool {
let env = ["PATH": BuildModel.toolPath("/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin", python: python), "HOME": NSHomeDirectory(),
"LANG": "en_US.UTF-8", "TMPDIR": NSTemporaryDirectory()].merging(BundledTools.environment(for: python)) { $1 }
- return BuildModel.runCaptured(syncArguments(repo: repo, python: python), env: env, line: line)
+ return runner.runCaptured(syncArguments(repo: repo, python: python), env: env, line: line)
}
}
diff --git a/frontend/Sources/NX2Apple/LiveOnboardingDriver.swift b/frontend/Sources/NX2Apple/LiveOnboardingDriver.swift
index 77bd2c02..bcc2ab6c 100644
--- a/frontend/Sources/NX2Apple/LiveOnboardingDriver.swift
+++ b/frontend/Sources/NX2Apple/LiveOnboardingDriver.swift
@@ -85,26 +85,15 @@ struct OnboardingEnvironment {
static func liveProbe() async -> XcodeProbe {
await Task.detached {
var probe = XcodeProbe()
- let p = Process()
- p.executableURL = URL(fileURLWithPath: "/usr/bin/xcode-select"); p.arguments = ["-p"]
- let pipe = Pipe(); p.standardOutput = pipe; p.standardError = FileHandle.nullDevice
- var selected = ""
- if (try? p.run()) != nil {
- let data = pipe.fileHandleForReading.readDataToEndOfFile(); p.waitUntilExit()
- if p.terminationStatus == 0 { selected = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" }
- }
+ let runner = SystemProcessRunner()
+ let selected = runner.output("/usr/bin/xcode-select", ["-p"])?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if let r = selected.range(of: ".app/Contents/Developer") {
probe.selectedIsXcode = true
probe.appPath = String(selected[..<r.lowerBound]) + ".app"
} else { probe.appPath = xcodeApps().first }
if let dir = probe.developerDir {
- let v = Process()
- v.executableURL = URL(fileURLWithPath: "/usr/bin/xcodebuild"); v.arguments = ["-version"]
- v.environment = ["DEVELOPER_DIR": dir, "PATH": "/usr/bin:/bin"]
- let out = Pipe(); v.standardOutput = out; v.standardError = FileHandle.nullDevice
- if (try? v.run()) != nil {
- let data = out.fileHandleForReading.readDataToEndOfFile(); v.waitUntilExit()
- if v.terminationStatus == 0 { probe.version = OnboardingNVN.xcodeVersion(from: String(data: data, encoding: .utf8) ?? "") }
+ if let text = runner.output("/usr/bin/xcodebuild", ["-version"], env: ["DEVELOPER_DIR": dir, "PATH": "/usr/bin:/bin"]) {
+ probe.version = OnboardingNVN.xcodeVersion(from: text)
}
}
if probe.selectedIsXcode {
@@ -123,7 +112,7 @@ struct OnboardingEnvironment {
var text: String { lock.lock(); defer { lock.unlock() }; return buffer }
}
let box = Box()
- let ok = BuildModel.runCaptured(command, env: ["PATH": "/usr/bin:/bin:/usr/sbin:/sbin", "HOME": NSHomeDirectory(), "USER": NSUserName()]) { l in
+ let ok = SystemProcessRunner().runCaptured(command, env: ["PATH": "/usr/bin:/bin:/usr/sbin:/sbin", "HOME": NSHomeDirectory(), "USER": NSUserName()]) { l in
box.add(l)
line(l)
}
diff --git a/frontend/Sources/NX2Apple/OlderTools.swift b/frontend/Sources/NX2Apple/OlderTools.swift
index 69720221..443fd289 100644
--- a/frontend/Sources/NX2Apple/OlderTools.swift
+++ b/frontend/Sources/NX2Apple/OlderTools.swift
@@ -1,4 +1,5 @@
import Foundation
+import NXCore
/// Which library games were built with older build tools (LNPANDY/NX2Apple#65, after seedds/nxnative#209): a finished build
/// records the engine's commit (LibraryGame.toolsCommit); once the engine has moved on, a game is marked when files a build uses
@@ -45,19 +46,19 @@ final class ToolsAge: ObservableObject {
}
/// The engine's commit (the pin plus the series), for a git engine only (an engine tree has no history to compare against).
- nonisolated static func head(repo: String) -> String? {
+ nonisolated static func head(repo: String, git: GitCheckout = SystemGitCheckout()) -> String? {
let engine = EngineLayout.root(repo)
- guard !engine.isEmpty, FileManager.default.fileExists(atPath: engine + "/.git"), let git = BuildModel.gitBinary() else { return nil }
- let r = BuildModel.runQuiet([git, "-C", engine, "rev-parse", "HEAD"], env: BuildModel.gitEnv())
+ guard !engine.isEmpty, FileManager.default.fileExists(atPath: engine + "/.git"), git.binary != nil else { return nil }
+ let r = git.run(engine, ["rev-parse", "HEAD"])
let commit = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
return r.ok && !commit.isEmpty ? commit : nil
}
/// The files changed between a recorded commit and the engine's HEAD; nil when git cannot tell (a commit the engine does not
/// have, as for a game built before step 2 or after engine/ was made anew), which marks nothing rather than everything.
- nonisolated static func changedFiles(since commit: String, repo: String) -> [String]? {
- guard let git = BuildModel.gitBinary() else { return nil }
- let r = BuildModel.runQuiet([git, "-C", EngineLayout.root(repo), "diff", "--name-only", "--no-renames", commit, "HEAD", "--"], env: BuildModel.gitEnv())
+ nonisolated static func changedFiles(since commit: String, repo: String, git: GitCheckout = SystemGitCheckout()) -> [String]? {
+ guard git.binary != nil else { return nil }
+ let r = git.run(EngineLayout.root(repo), ["diff", "--name-only", "--no-renames", commit, "HEAD", "--"])
guard r.ok else { return nil }
return r.out.split(separator: "\n").map(String.init).filter { !$0.isEmpty }
}
diff --git a/frontend/Sources/NX2Apple/ShellInstall.swift b/frontend/Sources/NX2Apple/ShellInstall.swift
index 2182b1d7..8fb73b57 100644
--- a/frontend/Sources/NX2Apple/ShellInstall.swift
+++ b/frontend/Sources/NX2Apple/ShellInstall.swift
@@ -623,7 +623,7 @@ struct ShellInstaller {
p.arguments = Array(command.dropFirst())
p.environment = env ?? ["PATH": "/usr/bin:/bin:/usr/sbin:/sbin", "HOME": NSHomeDirectory(), "USER": NSUserName(), "LANG": "en_US.UTF-8"]
p.standardInput = FileHandle.nullDevice
- if ownGroup { BuildModel.inOwnGroup(p, python: python) }
+ if ownGroup { ProcessGroups.inOwnGroup(p, python: python) }
let pipe = Pipe(); p.standardOutput = pipe; p.standardError = pipe
do { try p.run() } catch { return ToolResult(status: 127, output: error.localizedDescription, lastError: error.localizedDescription) }
processStarted(p)
diff --git a/frontend/Sources/NX2Apple/Uninstall.swift b/frontend/Sources/NX2Apple/Uninstall.swift
index bc9158e0..de556386 100644
--- a/frontend/Sources/NX2Apple/Uninstall.swift
+++ b/frontend/Sources/NX2Apple/Uninstall.swift
@@ -1,4 +1,5 @@
import AppKit
+import NXCore
/// Uninstalling the launcher (NX Play's Uninstall): everything the app put outside its bundle, with the
/// player's games and keys left alone. What goes:
@@ -53,9 +54,9 @@ enum Uninstall {
return p.count >= f.count && Array(p.prefix(f.count)) == f
}
- nonisolated static func gitOrigin(_ checkout: String) -> String? {
- guard let git = BuildModel.gitBinary() else { return nil }
- let r = BuildModel.runQuiet([git, "-C", checkout, "remote", "get-url", "origin"], env: BuildModel.gitEnv())
+ nonisolated static func gitOrigin(_ checkout: String, git: GitCheckout = SystemGitCheckout()) -> String? {
+ guard git.binary != nil else { return nil }
+ let r = git.run(checkout, ["remote", "get-url", "origin"])
let url = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
return r.ok && !url.isEmpty ? url : nil
}
diff --git a/frontend/Sources/NXCore/GitCheckout.swift b/frontend/Sources/NXCore/GitCheckout.swift
new file mode 100644
index 00000000..2d952d54
--- /dev/null
+++ b/frontend/Sources/NXCore/GitCheckout.swift
@@ -0,0 +1,136 @@
+import Foundation
+
+/// The repository checkout the launcher drives: clone, fetch, pull, how far it is behind origin/main, and the adoption of a ZIP copy
+/// into git. It starts git through a `ProcessRunner` and touches the disk, and keeps no state, so the launcher's flows can be tested
+/// with a fake.
+package protocol GitCheckout {
+ /// The git binary, nil when this Mac has none.
+ var binary: String? { get }
+ /// `git -C dir args`, output captured. A Mac without git answers with a failure.
+ func run(_ dir: String, _ args: [String]) -> ProcessResult
+ /// A shallow clone of `url` into `dest`, its progress lines to `line` (blocking). true when git exited 0.
+ func clone(url: String, into dest: String, line: @escaping (String) -> Void) -> Bool
+ /// How many commits origin/main has that the checkout does not, after a fetch. nil when git cannot tell (offline).
+ func behind(_ dir: String) -> Int?
+ /// Fast-forward to origin/main.
+ func pull(_ dir: String) -> ProcessResult
+ /// The pull of the automatic update: only for a checkout on main with no changes to its tracked files. nil: not allowed.
+ func autoPull(_ dir: String) -> ProcessResult?
+ /// Connect a copy without .git to `remote` in place and bring it to origin/main, the copy kept on the local branch `branch`.
+ /// `log` is the commands and what they printed. A failed adoption removes the .git it made.
+ func adopt(_ dir: String, remote: String, branch: String) -> (ok: Bool, log: String)
+}
+
+/// The decisions about a checkout that need no git: pure over their inputs.
+package enum GitRules {
+ /// A checkout the automatic update may move: on main, with no changes to tracked files (untracked files stay; a pull
+ /// that would overwrite one fails as a fast-forward and says so).
+ package static func autoPullAllowed(branch: String, status: String) -> Bool {
+ branch.trimmingCharacters(in: .whitespacesAndNewlines) == "main" && status.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
+ }
+
+ /// The local branch that keeps a ZIP copy as it was when it is connected to git.
+ package static func zipCopyBranch(date: Date = Date()) -> String {
+ let f = DateFormatter()
+ f.locale = Locale(identifier: "en_US_POSIX"); f.dateFormat = "yyyy-MM-dd"
+ return "zip-copy-" + f.string(from: date)
+ }
+
+ /// What the launch does with the configured repository when it is a copy without .git (#87: a ZIP copy never updated
+ /// itself, so its build tools were older than the app).
+ package enum ZipAdoption: Equatable { case notNeeded, adopt, waitsForBuild, autoUpdateOff }
+
+ /// Adopt a valid checkout without .git into git, but never while a build runs (or another repository task),
+ /// and not when the player turned the automatic update off.
+ package static func zipAdoption(isRepo: Bool, isGit: Bool, buildRunning: Bool, repoBusy: Bool, autoUpdate: Bool) -> ZipAdoption {
+ guard isRepo, !isGit else { return .notNeeded }
+ if buildRunning || repoBusy { return .waitsForBuild }
+ return autoUpdate ? .adopt : .autoUpdateOff
+ }
+}
+
+/// Runs the system's git.
+package struct SystemGitCheckout: GitCheckout {
+ private let runner: ProcessRunner
+ private let candidates: [String]
+
+ package init(runner: ProcessRunner = SystemProcessRunner(),
+ candidates: [String] = ["/usr/bin/git", "/opt/homebrew/bin/git", "/usr/local/bin/git"]) {
+ self.runner = runner; self.candidates = candidates
+ }
+
+ package var binary: String? {
+ candidates.first { FileManager.default.isExecutableFile(atPath: $0) }
+ }
+
+ /// A GUI app has no shell PATH: git gets the folders Homebrew and the system put it in.
+ package static func environment() -> [String: String] {
+ ["PATH": "/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin", "HOME": NSHomeDirectory()]
+ }
+
+ package func run(_ dir: String, _ args: [String]) -> ProcessResult {
+ guard let git = binary else { return .failedToStart("git is not installed") }
+ return runner.runQuiet([git, "-C", dir] + args, env: Self.environment())
+ }
+
+ package func clone(url: String, into dest: String, line: @escaping (String) -> Void) -> Bool {
+ guard let git = binary else { return false }
+ // Shallow: a quarter of the bytes and a third of the time of a full clone, and fetch /
+ // pull keep working on it (verified); the history is of no use to someone who wants to build.
+ return runner.runCaptured([git, "clone", "--depth", "1", "--single-branch", "--progress", url, dest], env: Self.environment(), line: line)
+ }
+
+ package func behind(_ dir: String) -> Int? {
+ guard run(dir, ["fetch", "origin", "main"]).ok else { return nil }
+ let count = run(dir, ["rev-list", "--count", "HEAD..origin/main"])
+ guard count.ok else { return nil }
+ return Int(count.out.trimmingCharacters(in: .whitespacesAndNewlines))
+ }
+
+ package func pull(_ dir: String) -> ProcessResult {
+ run(dir, ["pull", "--ff-only", "origin", "main"])
+ }
+
+ package func autoPull(_ dir: String) -> ProcessResult? {
+ let branch = run(dir, ["rev-parse", "--abbrev-ref", "HEAD"])
+ let status = run(dir, ["status", "--porcelain", "--untracked-files=no"])
+ guard branch.ok, status.ok, GitRules.autoPullAllowed(branch: branch.out, status: status.out) else { return nil }
+ return pull(dir)
+ }
+
+ /// A ZIP copy has no .git: adopt it into git in place (init, remote, fetch, checkout) so the update buttons work and the folder keeps
+ /// its path. The copy may hold the player's own edits (a tweaked game profile), and the checkout resets every tracked file to
+ /// origin/main, so the copy is recorded first: after the fetch, the files of the copy that origin/main also has are committed on the
+ /// local branch `branch` (fixed local author, no git identity needed). Only those paths: a build folder or any other file that
+ /// origin/main does not track is never committed (and the checkout leaves such files where they are). The build's own files live
+ /// outside the repository.
+ package func adopt(_ dir: String, remote: String, branch: String) -> (ok: Bool, log: String) {
+ var out = "", ok = true
+ func step(_ args: [String]) -> ProcessResult {
+ let r = run(dir, args)
+ out += "$ git \(args.joined(separator: " "))\n\(r.out)\n"
+ return r
+ }
+ // Nothing in the folder changes before the fetch has worked, so an offline failure leaves the copy as it was.
+ ok = step(["init"]).ok && step(["remote", "add", "origin", remote]).ok && step(["fetch", "--depth", "1", "origin", "main"]).ok
+ if ok {
+ let tree = step(["ls-tree", "-r", "-z", "--name-only", "origin/main"])
+ let present = tree.out.split(separator: "\0").map(String.init).filter { FileManager.default.fileExists(atPath: dir + "/" + $0) }
+ let list = NSTemporaryDirectory() + "nspb-zip-paths-\(UUID().uuidString.prefix(8)).txt"
+ defer { try? FileManager.default.removeItem(atPath: list) }
+ let identity = ["-c", "user.name=NX2Apple", "-c", "user.email=nx2apple@localhost", "-c", "commit.gpgsign=false"]
+ ok = tree.ok
+ && step(["symbolic-ref", "HEAD", "refs/heads/" + branch]).ok
+ && ((try? Data(present.joined(separator: "\0").utf8).write(to: URL(fileURLWithPath: list))) != nil)
+ && step(["add", "--pathspec-from-file=" + list, "--pathspec-file-nul"]).ok
+ && step(identity + ["commit", "--allow-empty", "-m", "The ZIP copy as it was before it was connected to git"]).ok
+ && step(["checkout", "-f", "-B", "main", "origin/main"]).ok
+ }
+ if ok { _ = run(dir, ["branch", "--set-upstream-to=origin/main", "main"]) } // checkout -B usually set it
+ // A failed adoption (offline) must not leave the .git that `git init` made: the copy would count as a
+ // checkout from then on, and neither the launch nor the button would try again. The caller checked that there was no .git
+ // before, so this removes only what this call created.
+ if !ok { try? FileManager.default.removeItem(atPath: dir + "/.git") }
+ return (ok, out)
+ }
+}
diff --git a/frontend/Sources/NXCore/ProcessGroups.swift b/frontend/Sources/NXCore/ProcessGroups.swift
new file mode 100644
index 00000000..6f44aac8
--- /dev/null
+++ b/frontend/Sources/NXCore/ProcessGroups.swift
@@ -0,0 +1,68 @@
+import Foundation
+
+/// Process groups: Stop and quitting end the whole build.
+///
+/// The script runs its tools as children (the shader split with its workers, dotnet, xcodebuild) and cleans them up only on Ctrl-C:
+/// a SIGTERM to the script alone leaves them running, orphaned. So each build process starts in a process group of its own, and Stop,
+/// quitting the app or a SIGTERM to it end the whole group. `ProcessRunner.start` does the grouping. The app also calls `endAll()`
+/// when it quits (the notification is AppKit's, so it is hooked up in the app target).
+package enum ProcessGroups {
+ /// The groups of the builds running now (their leaders' PIDs).
+ private static var groups = Set<pid_t>()
+ private static let lock = NSLock()
+ private static let sigtermHandler: Void = {
+ signal(SIGTERM, SIG_IGN) // handled below: end the builds, then exit
+ let source = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .global())
+ source.setEventHandler { endAll(); exit(0) }
+ source.resume()
+ sigtermSource = source
+ }()
+ private static var sigtermSource: DispatchSourceSignal?
+
+ /// Run `p` as the leader of a new process group: Python sets the group, then execs into the real command.
+ /// It also restores SIGTERM and SIGINT to their defaults first: the app ignores SIGTERM (to end its builds before
+ /// it exits), and an ignored signal is inherited across exec, which would make Stop's SIGTERM do nothing.
+ package static func inOwnGroup(_ p: Process, python: String) {
+ let command = p.executableURL?.path ?? python
+ let start = "import os, signal, sys; signal.signal(signal.SIGTERM, signal.SIG_DFL); signal.signal(signal.SIGINT, signal.SIG_DFL); "
+ + "os.setpgid(0, 0); os.execv(sys.argv[1], sys.argv[1:])"
+ p.arguments = ["-c", start, command] + (p.arguments ?? [])
+ p.executableURL = URL(fileURLWithPath: python)
+ }
+
+ package static func track(_ p: Process) {
+ _ = sigtermHandler
+ lock.lock(); groups.insert(p.processIdentifier); lock.unlock()
+ }
+
+ package static func untrack(_ p: Process) {
+ lock.lock(); groups.remove(p.processIdentifier); lock.unlock()
+ }
+
+ /// A build's process group is still running (the app update waits for it: its relaunch would end the build).
+ package static var buildProcessesAlive: Bool {
+ lock.lock(); defer { lock.unlock() }
+ return groups.contains { killpg($0, 0) == 0 }
+ }
+
+ /// SIGTERM to every process of the group, SIGKILL 5 s later to whatever is left of it.
+ package static func endGroup(_ leader: pid_t) {
+ guard leader > 1 else { return }
+ killpg(leader, SIGTERM)
+ DispatchQueue.global().asyncAfter(deadline: .now() + 5) {
+ if killpg(leader, 0) == 0 { killpg(leader, SIGKILL) }
+ }
+ }
+
+ /// The app is about to exit: SIGTERM to every group, one grace period for the whole set, then SIGKILL for what remains.
+ package static func endAll() {
+ lock.lock(); let all = groups; lock.unlock()
+ for leader in all {
+ killpg(leader, SIGTERM)
+ }
+ if !all.isEmpty {
+ usleep(1_500_000)
+ for leader in all where killpg(leader, 0) == 0 { killpg(leader, SIGKILL) }
+ }
+ }
+}
diff --git a/frontend/Sources/NXCore/ProcessRunner.swift b/frontend/Sources/NXCore/ProcessRunner.swift
new file mode 100644
index 00000000..d02a93ee
--- /dev/null
+++ b/frontend/Sources/NXCore/ProcessRunner.swift
@@ -0,0 +1,160 @@
+import Foundation
+
+/// What to start: the tool, its arguments and where it runs. Standard input is always empty for a captured or streamed run.
+package struct ProcessLaunch {
+ package var executable: String
+ package var arguments: [String]
+ /// nil: the app's own environment.
+ package var environment: [String: String]?
+ package var directory: String?
+ /// stderr goes into the same stream as stdout.
+ package var mergesErrors = false
+ /// Start in a process group of its own, through this Python (`ProcessGroups`). nil: no group.
+ package var groupPython: String?
+
+ package init(executable: String, arguments: [String] = [], environment: [String: String]? = nil, directory: String? = nil,
+ mergesErrors: Bool = false, groupPython: String? = nil) {
+ self.executable = executable; self.arguments = arguments; self.environment = environment; self.directory = directory
+ self.mergesErrors = mergesErrors; self.groupPython = groupPython
+ }
+
+ /// `command[0]` is the tool, the rest its arguments.
+ package init(_ command: [String], environment: [String: String]? = nil, directory: String? = nil,
+ mergesErrors: Bool = false, groupPython: String? = nil) {
+ self.init(executable: command[0], arguments: Array(command.dropFirst()), environment: environment, directory: directory,
+ mergesErrors: mergesErrors, groupPython: groupPython)
+ }
+}
+
+/// How a captured run ended. A process that could not start is `started == false` with the reason in `out`.
+package struct ProcessResult: Equatable {
+ package var started: Bool
+ package var status: Int32
+ package var out: String
+ package var err: String
+ package var ok: Bool { started && status == 0 }
+
+ package init(started: Bool = true, status: Int32 = 0, out: String = "", err: String = "") {
+ self.started = started; self.status = status; self.out = out; self.err = err
+ }
+
+ package static func failedToStart(_ reason: String) -> ProcessResult { ProcessResult(started: false, status: -1, out: reason) }
+}
+
+/// A process the runner started and that has not ended yet (Stop reads its identifier).
+package protocol RunningProcess: AnyObject {
+ var processIdentifier: Int32 { get }
+ var isRunning: Bool { get }
+}
+
+extension Process: RunningProcess {}
+
+/// The only place that starts a process: process groups, environment and captured output. A service takes one through its
+/// initializer, so a test passes in a fake that answers without starting anything.
+package protocol ProcessRunner {
+ /// Run to the end and capture the output (blocking).
+ func capture(_ launch: ProcessLaunch) -> ProcessResult
+ /// Run to the end and hand each output line to `line` as it arrives (blocking). true when it exited 0.
+ func stream(_ launch: ProcessLaunch, line: @escaping (String) -> Void) -> Bool
+ /// Start and return at once. `onOutput` gets the combined output in chunks on a background queue, `onExit` the exit status.
+ /// With `groupPython` the process is the leader of a group of its own that `ProcessGroups` tracks until it exits.
+ func start(_ launch: ProcessLaunch, onOutput: @escaping (Data) -> Void, onExit: @escaping (Int32) -> Void) throws -> RunningProcess
+}
+
+extension ProcessRunner {
+ /// All output captured, none streamed (fetch, rev-list and pull print little). A failure to start is `ok == false`, its reason in `out`.
+ package func runQuiet(_ args: [String], env: [String: String]) -> ProcessResult {
+ capture(ProcessLaunch(args, environment: env, mergesErrors: true))
+ }
+
+ /// Whether the tool exits 0, its output thrown away.
+ package func runOK(_ tool: String, _ args: [String], env: [String: String]? = nil) -> Bool {
+ capture(ProcessLaunch(executable: tool, arguments: args, environment: env)).ok
+ }
+
+ /// The tool's stdout when it exits 0, nil when it does not start or fails.
+ package func output(_ tool: String, _ args: [String], env: [String: String]? = nil) -> String? {
+ let r = capture(ProcessLaunch(executable: tool, arguments: args, environment: env))
+ return r.ok ? r.out : nil
+ }
+
+ /// Stream the output line by line. A failure to start is reported as a line and returns false.
+ package func runCaptured(_ args: [String], env: [String: String], line: @escaping (String) -> Void) -> Bool {
+ stream(ProcessLaunch(args, environment: env), line: line)
+ }
+}
+
+/// Starts real processes.
+package struct SystemProcessRunner: ProcessRunner {
+ package init() {}
+
+ private func process(_ launch: ProcessLaunch) -> Process {
+ let p = Process()
+ p.executableURL = URL(fileURLWithPath: launch.executable)
+ p.arguments = launch.arguments
+ if let env = launch.environment { p.environment = env }
+ if let dir = launch.directory { p.currentDirectoryURL = URL(fileURLWithPath: dir) }
+ return p
+ }
+
+ package func capture(_ launch: ProcessLaunch) -> ProcessResult {
+ let p = process(launch)
+ p.standardInput = FileHandle.nullDevice
+ let out = Pipe(), err = Pipe()
+ p.standardOutput = out
+ p.standardError = launch.mergesErrors ? out : err
+ do { try p.run() } catch { return .failedToStart(error.localizedDescription) }
+ // Both pipes are drained before the exit is awaited, so the child cannot block on a full pipe.
+ let errData = LockedData()
+ let drained = DispatchGroup()
+ if !launch.mergesErrors {
+ drained.enter()
+ DispatchQueue.global().async { errData.set(err.fileHandleForReading.readDataToEndOfFile()); drained.leave() }
+ }
+ let outData = out.fileHandleForReading.readDataToEndOfFile()
+ drained.wait()
+ p.waitUntilExit()
+ return ProcessResult(status: p.terminationStatus, out: String(data: outData, encoding: .utf8) ?? "",
+ err: String(data: errData.value, encoding: .utf8) ?? "")
+ }
+
+ package func stream(_ launch: ProcessLaunch, line: @escaping (String) -> Void) -> Bool {
+ let p = process(launch)
+ let out = Pipe(), err = Pipe()
+ p.standardOutput = out; p.standardError = err; p.standardInput = FileHandle.nullDevice
+ do { try p.run() } catch { line("could not start \(launch.executable): \(error.localizedDescription)"); return false }
+ for h in [out.fileHandleForReading, err.fileHandleForReading] {
+ PipeReader.attach(to: h) { d in
+ if let s = String(data: d, encoding: .utf8) {
+ for piece in s.split(separator: "\n") { line(String(piece)) }
+ }
+ }
+ }
+ p.waitUntilExit()
+ out.fileHandleForReading.readabilityHandler = nil; err.fileHandleForReading.readabilityHandler = nil
+ return p.terminationStatus == 0
+ }
+
+ package func start(_ launch: ProcessLaunch, onOutput: @escaping (Data) -> Void, onExit: @escaping (Int32) -> Void) throws -> RunningProcess {
+ let p = process(launch)
+ if let python = launch.groupPython { ProcessGroups.inOwnGroup(p, python: python) }
+ let pipe = Pipe()
+ p.standardOutput = pipe; p.standardError = pipe
+ PipeReader.attach(to: pipe.fileHandleForReading, onData: onOutput)
+ p.terminationHandler = { done in
+ ProcessGroups.untrack(done)
+ onExit(done.terminationStatus)
+ }
+ try p.run()
+ if launch.groupPython != nil { ProcessGroups.track(p) }
+ return p
+ }
+}
+
+/// The data a background read leaves for the thread that waits for it.
+private final class LockedData: @unchecked Sendable {
+ private let lock = NSLock()
+ private var data = Data()
+ func set(_ new: Data) { lock.lock(); data = new; lock.unlock() }
+ var value: Data { lock.lock(); defer { lock.unlock() }; return data }
+}
diff --git a/frontend/Sources/NXCore/Toolchain.swift b/frontend/Sources/NXCore/Toolchain.swift
index 57eb6fd6..6cd7d8dd 100644
--- a/frontend/Sources/NXCore/Toolchain.swift
+++ b/frontend/Sources/NXCore/Toolchain.swift
@@ -31,16 +31,8 @@ package enum Toolchain {
/// live instead of trusting `which` — that is why .NET (in ~/.dotnet) and the
/// Python packages (in /usr/local/bin/python3, not /usr/bin/python3) used to show
/// as missing while a terminal could see them.
- package static func runOK(_ tool: String, _ args: [String], env: [String: String]? = nil) -> Bool {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: tool)
- p.arguments = args
- if let env { p.environment = env }
- p.standardOutput = Pipe(); p.standardError = Pipe()
- p.standardInput = FileHandle.nullDevice
- do { try p.run() } catch { return false }
- p.waitUntilExit()
- return p.terminationStatus == 0
+ package static func runOK(_ tool: String, _ args: [String], env: [String: String]? = nil, runner: ProcessRunner = SystemProcessRunner()) -> Bool {
+ runner.runOK(tool, args, env: env)
}
package static func firstExisting(_ paths: [String]) -> String? {
@@ -134,22 +126,15 @@ package enum Toolchain {
/// Create the venv (reusing an existing one) and install the packages into it. Returns the
/// combined output so a failure can be shown instead of swallowed — the pip output used to go
/// to a pipe nobody read, and `externally-managed-environment` never reached the user.
- package static func makeVenv(base: String) -> (ok: Bool, log: String) {
+ package static func makeVenv(base: String, runner: ProcessRunner = SystemProcessRunner()) -> (ok: Bool, log: String) {
var log = ""
func run(_ exe: String, _ args: [String]) -> Bool {
- let p = Process(); p.executableURL = URL(fileURLWithPath: exe); p.arguments = args
- let out = Pipe(), err = Pipe()
- p.standardOutput = out; p.standardError = err; p.standardInput = FileHandle.nullDevice
- p.environment = ["PATH": BundledTools.path("/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin", python: base), "HOME": NSHomeDirectory()].merging(BundledTools.environment(for: base)) { $1 }
- do { try p.run() } catch { log += "could not start \(exe): \(error.localizedDescription)\n"; return false }
- // both pipes are drained on this thread (before waitUntilExit, so the child cannot
- // block on a full pipe), and the log string keeps a single owner
- let outData = out.fileHandleForReading.readDataToEndOfFile()
- let errData = err.fileHandleForReading.readDataToEndOfFile()
- p.waitUntilExit()
- log += String(data: outData, encoding: .utf8) ?? ""
- log += String(data: errData, encoding: .utf8) ?? ""
- return p.terminationStatus == 0
+ let env = ["PATH": BundledTools.path("/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin", python: base), "HOME": NSHomeDirectory()].merging(BundledTools.environment(for: base)) { $1 }
+ let r = runner.capture(ProcessLaunch(executable: exe, arguments: args, environment: env))
+ if !r.started { log += "could not start \(exe): \(r.out)\n"; return false }
+ log += r.out
+ log += r.err
+ return r.ok
}
let vpy = venvPython()
// An existing venv below minPython (built from Apple's 3.9 by an older app, issue #75) is
@@ -186,18 +171,9 @@ package enum Toolchain {
}
/// "3.12.4" → (3, 12). nil when the interpreter cannot be run.
- package static func pythonVersion(_ path: String) -> (Int, Int)? {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: path)
- p.arguments = ["-c", "import sys; print('%d.%d' % sys.version_info[:2])"]
- p.environment = ProcessInfo.processInfo.environment.merging(BundledTools.environment(for: path)) { $1 }
- let pipe = Pipe()
- p.standardOutput = pipe; p.standardError = Pipe()
- p.standardInput = FileHandle.nullDevice
- guard (try? p.run()) != nil else { return nil }
- p.waitUntilExit()
- guard p.terminationStatus == 0 else { return nil }
- let out = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
+ package static func pythonVersion(_ path: String, runner: ProcessRunner = SystemProcessRunner()) -> (Int, Int)? {
+ let env = ProcessInfo.processInfo.environment.merging(BundledTools.environment(for: path)) { $1 }
+ guard let out = runner.output(path, ["-c", "import sys; print('%d.%d' % sys.version_info[:2])"], env: env) else { return nil }
let parts = out.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: ".")
guard parts.count == 2, let a = Int(parts[0]), let b = Int(parts[1]) else { return nil }
return (a, b)
@@ -277,17 +253,8 @@ package enum Toolchain {
}
cands += uvPythonCandidates()
cands += versionManagerPythonCandidates()
- let p = Process()
- p.executableURL = URL(fileURLWithPath: "/usr/bin/which")
- p.arguments = ["-a", "python3"]
- let pipe = Pipe()
- p.standardOutput = pipe; p.standardError = Pipe()
- p.standardInput = FileHandle.nullDevice
- if (try? p.run()) != nil {
- p.waitUntilExit()
- let out = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
- cands += out.split(separator: "\n").map(String.init)
- }
+ let which = SystemProcessRunner().capture(ProcessLaunch(executable: "/usr/bin/which", arguments: ["-a", "python3"]))
+ if which.started { cands += which.out.split(separator: "\n").map(String.init) }
return cands
}
@@ -336,17 +303,8 @@ package enum Toolchain {
return nil
}
- package static func dotnetRoot(for dotnet: String) -> String? {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: dotnet)
- p.arguments = ["--list-runtimes"]
- let pipe = Pipe()
- p.standardOutput = pipe; p.standardError = Pipe(); p.standardInput = FileHandle.nullDevice
- p.environment = ["HOME": NSHomeDirectory(), "PATH": "/usr/bin:/bin", "TMPDIR": NSTemporaryDirectory()]
- guard (try? p.run()) != nil else { return nil }
- p.waitUntilExit()
- guard p.terminationStatus == 0 else { return nil }
- let out = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
+ package static func dotnetRoot(for dotnet: String, runner: ProcessRunner = SystemProcessRunner()) -> String? {
+ guard let out = runner.output(dotnet, ["--list-runtimes"], env: ["HOME": NSHomeDirectory(), "PATH": "/usr/bin:/bin", "TMPDIR": NSTemporaryDirectory()]) else { return nil }
// No directory fallback: for Homebrew's symlink layout the binary's directory is not the root,
// and a wrong DOTNET_ROOT is worse than none (the apphost stops searching once it is set).
// When this returns nil the pipeline derives and verifies a root itself.
@@ -371,18 +329,8 @@ package enum Toolchain {
/// Whether the selected developer directory exists (`xcode-select -p`), which is the same
/// source of truth xcodebuild and xcrun use — so an Xcode beta, a custom location, or a
/// Command Line Tools-only setup is reported for what it is instead of "no Xcode".
- package static func developerDir() -> String? {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: "/usr/bin/xcode-select")
- p.arguments = ["-p"]
- let pipe = Pipe()
- p.standardOutput = pipe
- p.standardError = FileHandle.nullDevice
- do { try p.run() } catch { return nil }
- let data = pipe.fileHandleForReading.readDataToEndOfFile()
- p.waitUntilExit()
- guard p.terminationStatus == 0,
- let dir = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines),
+ package static func developerDir(runner: ProcessRunner = SystemProcessRunner()) -> String? {
+ guard let dir = runner.output("/usr/bin/xcode-select", ["-p"])?.trimmingCharacters(in: .whitespacesAndNewlines),
!dir.isEmpty else { return nil }
var isDir: ObjCBool = false
return FileManager.default.fileExists(atPath: dir, isDirectory: &isDir) && isDir.boolValue ? dir : nil
@@ -460,17 +408,9 @@ package enum Toolchain {
}
/// First line of a tool's output (the Metal compiler's version line), nil when it does not run or says nothing.
- package static func firstOutputLine(_ tool: String, _ args: [String]) -> String? {
- let p = Process()
- p.executableURL = URL(fileURLWithPath: tool)
- p.arguments = args
- let out = Pipe()
- p.standardOutput = out; p.standardError = Pipe(); p.standardInput = FileHandle.nullDevice
- do { try p.run() } catch { return nil }
- let data = out.fileHandleForReading.readDataToEndOfFile()
- p.waitUntilExit()
- guard p.terminationStatus == 0 else { return nil }
- return String(decoding: data, as: UTF8.self).split(separator: "\n", omittingEmptySubsequences: true).first.map(String.init)
+ package static func firstOutputLine(_ tool: String, _ args: [String], runner: ProcessRunner = SystemProcessRunner()) -> String? {
+ guard let out = runner.output(tool, args) else { return nil }
+ return out.split(separator: "\n", omittingEmptySubsequences: true).first.map(String.init)
}
package static func pythonDetail(py: String, ok: Bool) -> String {
diff --git a/frontend/Tests/NX2AppleTests/EngineLayoutTests.swift b/frontend/Tests/NX2AppleTests/EngineLayoutTests.swift
index 29f228fc..64f1e9bf 100644
--- a/frontend/Tests/NX2AppleTests/EngineLayoutTests.swift
+++ b/frontend/Tests/NX2AppleTests/EngineLayoutTests.swift
@@ -167,7 +167,7 @@ final class EngineLayoutTests: XCTestCase {
let tools = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
.deletingLastPathComponent().appendingPathComponent("tools").path
let code = "import sys; sys.path.insert(0, sys.argv[1]); import engine, pathlib; print(engine.stamp_key(pathlib.Path(sys.argv[2])))"
- let r = BuildModel.runQuiet(["/usr/bin/env", "python3", "-c", code, tools, repo], env: ["PATH": "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"])
+ let r = SystemProcessRunner().runQuiet(["/usr/bin/env", "python3", "-c", code, tools, repo], env: ["PATH": "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"])
try XCTSkipUnless(r.ok, "python3 did not run: \(r.out)")
XCTAssertEqual(r.out.trimmingCharacters(in: .whitespacesAndNewlines), EngineLayout.stampKey(repo))
}
diff --git a/frontend/Tests/NX2AppleTests/LibraryTests.swift b/frontend/Tests/NX2AppleTests/LibraryTests.swift
index c2c954b3..d189fdcd 100644
--- a/frontend/Tests/NX2AppleTests/LibraryTests.swift
+++ b/frontend/Tests/NX2AppleTests/LibraryTests.swift
@@ -259,7 +259,7 @@ final class LibraryTests: XCTestCase {
let p = Process()
p.executableURL = URL(fileURLWithPath: "/bin/sh")
p.arguments = ["-c", "sleep 60 & sleep 60 & wait"]
- BuildModel.inOwnGroup(p, python: "/usr/bin/python3")
+ ProcessGroups.inOwnGroup(p, python: "/usr/bin/python3")
try p.run()
func members() -> Int {
let ps = Process(), out = Pipe()
@@ -273,7 +273,7 @@ final class LibraryTests: XCTestCase {
while members() < 3 && tries < 50 { usleep(100_000); tries += 1 }
XCTAssertEqual(members(), 3) // the shell and its two children, in the leader's group
let stopped = Date()
- BuildModel.endGroup(p.processIdentifier)
+ ProcessGroups.endGroup(p.processIdentifier)
p.waitUntilExit()
tries = 0
while members() > 0 && tries < 30 { usleep(100_000); tries += 1 }
diff --git a/frontend/Tests/NX2AppleTests/OlderToolsTests.swift b/frontend/Tests/NX2AppleTests/OlderToolsTests.swift
index 71b739a3..d2e7950b 100644
--- a/frontend/Tests/NX2AppleTests/OlderToolsTests.swift
+++ b/frontend/Tests/NX2AppleTests/OlderToolsTests.swift
@@ -20,7 +20,7 @@ final class OlderToolsTests: XCTestCase {
}
@discardableResult private func git(_ args: String..., in dir: URL) throws -> String {
- let r = BuildModel.runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: BuildModel.gitEnv())
+ let r = SystemProcessRunner().runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: SystemGitCheckout.environment())
guard r.ok else { throw NSError(domain: "git", code: 1, userInfo: [NSLocalizedDescriptionKey: "git \(args.joined(separator: " ")): \(r.out)"]) }
return r.out.trimmingCharacters(in: .whitespacesAndNewlines)
}
diff --git a/frontend/Tests/NX2AppleTests/RepoAutoUpdateTests.swift b/frontend/Tests/NX2AppleTests/RepoAutoUpdateTests.swift
index 8a1338a0..f9adb6d3 100644
--- a/frontend/Tests/NX2AppleTests/RepoAutoUpdateTests.swift
+++ b/frontend/Tests/NX2AppleTests/RepoAutoUpdateTests.swift
@@ -22,7 +22,7 @@ final class RepoAutoUpdateTests: XCTestCase {
}
@discardableResult private func git(_ args: String..., in dir: URL) throws -> String {
- let r = BuildModel.runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: BuildModel.gitEnv())
+ let r = SystemProcessRunner().runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: SystemGitCheckout.environment())
guard r.ok else { throw NSError(domain: "git", code: 1, userInfo: [NSLocalizedDescriptionKey: "git \(args.joined(separator: " ")): \(r.out)"]) }
return r.out.trimmingCharacters(in: .whitespacesAndNewlines)
}
@@ -72,11 +72,4 @@ final class RepoAutoUpdateTests: XCTestCase {
XCTAssertEqual(m.repoStatus, m.t("repo.autoSkipped"))
XCTAssertEqual(try git("log", "-1", "--format=%s", in: checkout), "one")
}
-
- func testOnlyMainWithoutTrackedChangesMayBeMoved() {
- XCTAssertTrue(BuildModel.autoPullAllowed(branch: "main\n", status: ""))
- XCTAssertFalse(BuildModel.autoPullAllowed(branch: "device-install", status: ""))
- XCTAssertFalse(BuildModel.autoPullAllowed(branch: "main", status: " M scripts/build_ipa_from_nsp.py\n"))
- XCTAssertFalse(BuildModel.autoPullAllowed(branch: "HEAD", status: ""), "a detached HEAD is not main")
- }
}
diff --git a/frontend/Tests/NX2AppleTests/RepoFlowFakeTests.swift b/frontend/Tests/NX2AppleTests/RepoFlowFakeTests.swift
new file mode 100644
index 00000000..eade666a
--- /dev/null
+++ b/frontend/Tests/NX2AppleTests/RepoFlowFakeTests.swift
@@ -0,0 +1,110 @@
+import XCTest
+@testable import NX2Apple
+@testable import NXCore
+
+/// The repository flows of BuildModel with a fake `GitCheckout`: the model's states and texts for each answer git can give,
+/// with no git and no network. (RepoAutoUpdateTests and RepoToolsTests run the real git against local repositories.)
+final class RepoFlowFakeTests: XCTestCase {
+ private var temporary: URL!
+ private var saved: [(String, Any?)] = []
+
+ override func setUpWithError() throws {
+ temporary = FileManager.default.temporaryDirectory.appendingPathComponent("nxnative-fake-" + UUID().uuidString)
+ // what makes it a checkout to the launcher: the extractor's project and a .git
+ let marker = temporary.appendingPathComponent("dm/tools/nx_extract/Program.cs")
+ try FileManager.default.createDirectory(at: marker.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try "x".write(to: marker, atomically: true, encoding: .utf8)
+ try FileManager.default.createDirectory(at: temporary.appendingPathComponent(".git"), withIntermediateDirectories: true)
+ saved = ["repoPath", "autoUpdateRepo"].map { ($0, UserDefaults.standard.object(forKey: $0)) }
+ }
+
+ override func tearDownWithError() throws {
+ for (key, value) in saved { UserDefaults.standard.set(value, forKey: key) }
+ try? FileManager.default.removeItem(at: temporary)
+ }
+
+ @MainActor private func model(_ git: GitCheckout) -> BuildModel {
+ let m = BuildModel(gitCheckout: git)
+ m.repoPath = temporary.path
+ m.autoUpdateRepo = false
+ return m
+ }
+
+ @MainActor private func settle(_ m: BuildModel) {
+ let done = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in !m.repoBusy }, object: nil)
+ wait(for: [done], timeout: 10)
+ }
+
+ @MainActor func testTheCheckShowsHowFarBehindTheCheckoutIs() {
+ let git = FakeGitCheckout(); git.behindCount = 3
+ let m = model(git)
+ m.checkRepoUpdates()
+ settle(m)
+ XCTAssertEqual(m.repoBehind, 3)
+ XCTAssertEqual(m.repoStatus, String(format: m.t("repo.behind"), 3))
+ XCTAssertEqual(git.calls, ["behind " + temporary.path])
+ }
+
+ @MainActor func testAnOfflineCheckSaysSoAndLeavesTheCountUnknown() {
+ let git = FakeGitCheckout(); git.behindCount = nil
+ let m = model(git)
+ m.checkRepoUpdates()
+ settle(m)
+ XCTAssertNil(m.repoBehind)
+ XCTAssertEqual(m.repoStatus, m.t("repo.netFail"))
+ }
+
+ @MainActor func testASilentOfflineCheckSaysNothing() {
+ let git = FakeGitCheckout(); git.behindCount = nil
+ let m = model(git)
+ m.checkRepoUpdates(silent: true)
+ settle(m)
+ XCTAssertEqual(m.repoStatus, "")
+ }
+
+ @MainActor func testADivergedCheckoutGetsTheRepositoryPickerAdvice() {
+ let git = FakeGitCheckout()
+ git.pullResult = ProcessResult(status: 128, out: "fatal: Not possible to fast-forward, aborting.")
+ let m = model(git)
+ m.pullRepo()
+ settle(m)
+ XCTAssertEqual(m.repoStatus, m.t("repo.pullDiverged"))
+ XCTAssertEqual(git.calls, ["pull " + temporary.path])
+ }
+
+ @MainActor func testAMacWithoutGitIsToldSoAndNothingRuns() {
+ let git = FakeGitCheckout(); git.binary = nil
+ let m = model(git)
+ m.checkRepoUpdates()
+ XCTAssertEqual(m.repoStatus, m.t("repo.gitMissing"))
+ XCTAssertFalse(m.repoBusy)
+ XCTAssertTrue(git.calls.isEmpty)
+ }
+
+ @MainActor func testTheAutomaticUpdateOfACheckoutOnABranchIsSkipped() {
+ let git = FakeGitCheckout(); git.autoPullResult = nil
+ let m = model(git)
+ m.autoPull(2)
+ settle(m)
+ XCTAssertEqual(m.repoStatus, m.t("repo.autoSkipped"))
+ XCTAssertNil(m.autoPulled)
+ }
+}
+
+/// A `GitCheckout` that runs nothing: each call is recorded and answered from the fields.
+final class FakeGitCheckout: GitCheckout, @unchecked Sendable {
+ var binary: String? = "/fake/git"
+ var behindCount: Int? = 0
+ var pullResult = ProcessResult()
+ var autoPullResult: ProcessResult?
+ var adoptResult: (ok: Bool, log: String) = (true, "")
+ var cloneOK = true
+ var calls: [String] = []
+
+ func run(_ dir: String, _ args: [String]) -> ProcessResult { calls.append("run \(dir) \(args.joined(separator: " "))"); return ProcessResult() }
+ func clone(url: String, into dest: String, line: @escaping (String) -> Void) -> Bool { calls.append("clone \(url) \(dest)"); line("Cloning"); return cloneOK }
+ func behind(_ dir: String) -> Int? { calls.append("behind \(dir)"); return behindCount }
+ func pull(_ dir: String) -> ProcessResult { calls.append("pull \(dir)"); return pullResult }
+ func autoPull(_ dir: String) -> ProcessResult? { calls.append("autoPull \(dir)"); return autoPullResult }
+ func adopt(_ dir: String, remote: String, branch: String) -> (ok: Bool, log: String) { calls.append("adopt \(dir) \(remote) \(branch)"); return adoptResult }
+}
diff --git a/frontend/Tests/NX2AppleTests/RepoToolsTests.swift b/frontend/Tests/NX2AppleTests/RepoToolsTests.swift
index 1eb6ba84..0f4065c1 100644
--- a/frontend/Tests/NX2AppleTests/RepoToolsTests.swift
+++ b/frontend/Tests/NX2AppleTests/RepoToolsTests.swift
@@ -29,25 +29,12 @@ final class RepoToolsTests: XCTestCase {
}
@discardableResult private func git(_ args: String..., in dir: URL) throws -> String {
- let r = BuildModel.runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: BuildModel.gitEnv())
+ let r = SystemProcessRunner().runQuiet(["/usr/bin/git", "-C", dir.path] + args, env: SystemGitCheckout.environment())
guard r.ok else { throw NSError(domain: "git", code: 1, userInfo: [NSLocalizedDescriptionKey: "git \(args.joined(separator: " ")): \(r.out)"]) }
return r.out.trimmingCharacters(in: .whitespacesAndNewlines)
}
- // MARK: the decision
-
- func testAdoptionDecision() {
- func decide(isRepo: Bool = true, isGit: Bool = false, running: Bool = false, busy: Bool = false, auto: Bool = true) -> BuildModel.ZipAdoption {
- BuildModel.zipAdoption(isRepo: isRepo, isGit: isGit, buildRunning: running, repoBusy: busy, autoUpdate: auto)
- }
- XCTAssertEqual(decide(), .adopt, "a ZIP copy is connected")
- XCTAssertEqual(decide(isGit: true), .notNeeded, "a git checkout is the normal update's business")
- XCTAssertEqual(decide(isRepo: false), .notNeeded, "no valid repository, nothing to adopt")
- XCTAssertEqual(decide(running: true), .waitsForBuild, "never while a build runs")
- XCTAssertEqual(decide(busy: true), .waitsForBuild)
- XCTAssertEqual(decide(auto: false), .autoUpdateOff, "the player turned the automatic update off")
- XCTAssertEqual(decide(running: true, auto: false), .waitsForBuild)
- }
+ // The decision (GitRules.zipAdoption) is tested in NXCoreTests/GitCheckoutTests.
// MARK: the missing-tool check
@@ -113,7 +100,7 @@ final class RepoToolsTests: XCTestCase {
let m = openZip(f, auto: true)
XCTAssertTrue(m.repoIsGit)
XCTAssertTrue(m.autoAdopted)
- XCTAssertEqual(m.repoStatus, String(format: m.t("repo.adoptedAuto"), BuildModel.zipCopyBranch()))
+ XCTAssertEqual(m.repoStatus, String(format: m.t("repo.adoptedAuto"), GitRules.zipCopyBranch()))
XCTAssertEqual(BuildModel.missingTools(in: f.zip.path), [], "the tools of origin/main are there now")
XCTAssertEqual(try git("rev-parse", "--abbrev-ref", "HEAD", in: f.zip), "main")
XCTAssertEqual(try String(contentsOf: f.zip.appendingPathComponent(BuildModel.buildScript), encoding: .utf8), "new")
@@ -126,7 +113,7 @@ final class RepoToolsTests: XCTestCase {
try write(["dm/mine/notes.txt"], in: f.zip, text: "mine") // a file origin does not have
try write(["dm/tools/build/huge.bin"], in: f.zip, text: "build output")
let m = openZip(f, auto: true)
- let branch = BuildModel.zipCopyBranch()
+ let branch = GitRules.zipCopyBranch()
XCTAssertEqual(try git("show", "main:" + BuildModel.buildScript, in: f.zip), "new", "main has origin's version")
XCTAssertEqual(try String(contentsOf: f.zip.appendingPathComponent(BuildModel.buildScript), encoding: .utf8), "new")
XCTAssertEqual(try git("show", branch + ":" + BuildModel.buildScript, in: f.zip), "my tweak", "the branch has the edit")
diff --git a/frontend/Tests/NX2AppleTests/SourceSizeTests.swift b/frontend/Tests/NX2AppleTests/SourceSizeTests.swift
index e9f35674..8d79a3f6 100644
--- a/frontend/Tests/NX2AppleTests/SourceSizeTests.swift
+++ b/frontend/Tests/NX2AppleTests/SourceSizeTests.swift
@@ -7,7 +7,7 @@ final class SourceSizeTests: XCTestCase {
/// Path under frontend/Sources → its line count when the limit came in. Lower the number when the file shrinks.
private static let aboveTheLimit: [String: Int] = [
- "NX2Apple/ContentView.swift": 2943,
+ "NX2Apple/ContentView.swift": 2763,
"NX2Apple/LauncherView.swift": 973,
"NXCore/Localization.swift": 4237,
]
diff --git a/frontend/Tests/NX2AppleTests/UninstallTests.swift b/frontend/Tests/NX2AppleTests/UninstallTests.swift
index 36b4d5f8..d130d216 100644
--- a/frontend/Tests/NX2AppleTests/UninstallTests.swift
+++ b/frontend/Tests/NX2AppleTests/UninstallTests.swift
@@ -36,8 +36,8 @@ final class UninstallTests: XCTestCase {
private func originFixture(checkout: URL) throws {
let git = "/usr/bin/git"
- _ = BuildModel.runQuiet([git, "-C", checkout.path, "init", "-b", "main"], env: BuildModel.gitEnv())
- _ = BuildModel.runQuiet([git, "-C", checkout.path, "remote", "add", "origin", "https://github.com/LNPANDY/NX2Apple.git"], env: BuildModel.gitEnv())
+ _ = SystemProcessRunner().runQuiet([git, "-C", checkout.path, "init", "-b", "main"], env: SystemGitCheckout.environment())
+ _ = SystemProcessRunner().runQuiet([git, "-C", checkout.path, "remote", "add", "origin", "https://github.com/LNPANDY/NX2Apple.git"], env: SystemGitCheckout.environment())
}
func testThePlanTakesStateAndItsOwnCheckoutAndLeavesGamesAndKeys() throws {
@@ -68,7 +68,7 @@ final class UninstallTests: XCTestCase {
func testARepoWithoutOurOriginIsNotTouched() throws {
let checkout = home.appendingPathComponent("NX2Apple").path
let git = "/usr/bin/git"
- _ = BuildModel.runQuiet([git, "-C", checkout, "remote", "add", "origin", "https://github.com/someone/else.git"], env: BuildModel.gitEnv())
+ _ = SystemProcessRunner().runQuiet([git, "-C", checkout, "remote", "add", "origin", "https://github.com/someone/else.git"], env: SystemGitCheckout.environment())
let plan = Uninstall.plan(home: home.path, gamesFolder: games.path, checkouts: [checkout])
XCTAssertTrue(plan.checkouts.isEmpty, "a fork with another origin is the player's own work")
}
diff --git a/frontend/Tests/NXCoreTests/FakeProcessRunner.swift b/frontend/Tests/NXCoreTests/FakeProcessRunner.swift
new file mode 100644
index 00000000..19977ac8
--- /dev/null
+++ b/frontend/Tests/NXCoreTests/FakeProcessRunner.swift
@@ -0,0 +1,43 @@
+import Foundation
+@testable import NXCore
+
+/// A `ProcessRunner` that starts nothing: it records what it was asked to run and answers from a script.
+final class FakeProcessRunner: ProcessRunner, @unchecked Sendable {
+ private let lock = NSLock()
+ private var recorded: [ProcessLaunch] = []
+ /// The answer for a launch. The default is a run that exits 0 and prints nothing.
+ var answer: (ProcessLaunch) -> ProcessResult = { _ in ProcessResult() }
+ /// What `stream` prints, and whether it exits 0.
+ var lines: [String] = []
+ var streamOK = true
+ /// What `start` reports: the output chunks, then the exit status. nil: it fails to start.
+ var startOutput: [String]? = []
+ var startStatus: Int32 = 0
+
+ var launches: [ProcessLaunch] { lock.lock(); defer { lock.unlock() }; return recorded }
+ /// Each launch as one line: the tool's name and its arguments.
+ var commands: [String] { launches.map { ([($0.executable as NSString).lastPathComponent] + $0.arguments).joined(separator: " ") } }
+
+ private func record(_ l: ProcessLaunch) { lock.lock(); recorded.append(l); lock.unlock() }
+
+ func capture(_ launch: ProcessLaunch) -> ProcessResult { record(launch); return answer(launch) }
+
+ func stream(_ launch: ProcessLaunch, line: @escaping (String) -> Void) -> Bool {
+ record(launch)
+ lines.forEach(line)
+ return streamOK
+ }
+
+ func start(_ launch: ProcessLaunch, onOutput: @escaping (Data) -> Void, onExit: @escaping (Int32) -> Void) throws -> RunningProcess {
+ record(launch)
+ guard let output = startOutput else { throw CocoaError(.fileNoSuchFile) }
+ output.forEach { onOutput(Data($0.utf8)) }
+ onExit(startStatus)
+ return FakeRunningProcess()
+ }
+}
+
+final class FakeRunningProcess: RunningProcess {
+ var processIdentifier: Int32 { 0 }
+ var isRunning: Bool { false }
+}
diff --git a/frontend/Tests/NXCoreTests/GitCheckoutTests.swift b/frontend/Tests/NXCoreTests/GitCheckoutTests.swift
new file mode 100644
index 00000000..84628e31
--- /dev/null
+++ b/frontend/Tests/NXCoreTests/GitCheckoutTests.swift
@@ -0,0 +1,161 @@
+import XCTest
+@testable import NXCore
+
+/// `SystemGitCheckout` over a fake runner: which git commands each flow runs, and what it makes of the answers.
+final class GitCheckoutTests: XCTestCase {
+ private func checkout(_ fake: FakeProcessRunner, binary: String = "/usr/bin/git") -> SystemGitCheckout {
+ SystemGitCheckout(runner: fake, candidates: [binary])
+ }
+
+ private func git(_ fake: FakeProcessRunner) -> SystemGitCheckout { checkout(fake, binary: "/usr/bin/true") } // a file that exists
+
+ func testNoGitMeansNoBinaryAndEveryRunFails() {
+ let fake = FakeProcessRunner()
+ let none = checkout(fake, binary: "/nonexistent/git")
+ XCTAssertNil(none.binary)
+ XCTAssertFalse(none.run("/repo", ["status"]).ok)
+ XCTAssertNil(none.behind("/repo"))
+ XCTAssertFalse(none.clone(url: "u", into: "/d") { _ in })
+ XCTAssertTrue(fake.launches.isEmpty, "nothing is started without git")
+ }
+
+ func testRunPutsTheDirectoryFirstAndGivesGitTheGuiEnvironment() {
+ let fake = FakeProcessRunner()
+ _ = git(fake).run("/repo", ["log", "-1"])
+ XCTAssertEqual(fake.commands, ["true -C /repo log -1"])
+ XCTAssertEqual(fake.launches[0].environment, SystemGitCheckout.environment())
+ XCTAssertEqual(SystemGitCheckout.environment()["PATH"], "/usr/bin:/bin:/opt/homebrew/bin:/usr/local/bin")
+ }
+
+ func testBehindFetchesThenCountsTheCommitsOriginMainHas() {
+ let fake = FakeProcessRunner()
+ fake.answer = { $0.arguments.contains("rev-list") ? ProcessResult(out: "7\n") : ProcessResult() }
+ XCTAssertEqual(git(fake).behind("/repo"), 7)
+ XCTAssertEqual(fake.commands, ["true -C /repo fetch origin main", "true -C /repo rev-list --count HEAD..origin/main"])
+ }
+
+ func testBehindIsNilWhenTheFetchFailsAndItCountsNothingThen() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(status: 128, out: "could not resolve host") }
+ XCTAssertNil(git(fake).behind("/repo"))
+ XCTAssertEqual(fake.commands.count, 1)
+ }
+
+ func testBehindIsNilWhenTheCountIsNotANumber() {
+ let fake = FakeProcessRunner()
+ fake.answer = { $0.arguments.contains("rev-list") ? ProcessResult(out: "fatal") : ProcessResult() }
+ XCTAssertNil(git(fake).behind("/repo"))
+ }
+
+ func testPullIsAFastForwardOnlyOfOriginMain() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(status: 1, out: "Not possible to fast-forward") }
+ let r = git(fake).pull("/repo")
+ XCTAssertFalse(r.ok)
+ XCTAssertEqual(r.out, "Not possible to fast-forward")
+ XCTAssertEqual(fake.commands, ["true -C /repo pull --ff-only origin main"])
+ }
+
+ func testAutoPullPullsOnlyAMainWithNoChangesToTrackedFiles() {
+ func run(branch: String, status: String) -> (pulled: ProcessResult?, commands: [String]) {
+ let fake = FakeProcessRunner()
+ fake.answer = { l in
+ if l.arguments.contains("rev-parse") { return ProcessResult(out: branch) }
+ if l.arguments.contains("status") { return ProcessResult(out: status) }
+ return ProcessResult(out: "Fast-forward")
+ }
+ return (git(fake).autoPull("/repo"), fake.commands)
+ }
+ let clean = run(branch: "main\n", status: "")
+ XCTAssertEqual(clean.pulled?.out, "Fast-forward")
+ XCTAssertEqual(clean.commands.last, "true -C /repo pull --ff-only origin main")
+ XCTAssertNil(run(branch: "device-install\n", status: "").pulled)
+ XCTAssertNil(run(branch: "main\n", status: " M scripts/build.py\n").pulled)
+ XCTAssertEqual(run(branch: "device-install\n", status: "").commands.count, 2, "a refused checkout is not pulled")
+ }
+
+ func testAutoPullDoesNotPullWhenGitCannotTellTheBranch() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(status: 128) }
+ XCTAssertNil(git(fake).autoPull("/repo"))
+ XCTAssertFalse(fake.commands.contains { $0.contains("pull") })
+ }
+
+ func testCloneIsShallowSingleBranchAndStreamsItsProgress() {
+ let fake = FakeProcessRunner()
+ fake.lines = ["Receiving objects: 50%"]
+ let lines = NSMutableArray()
+ let ok = git(fake).clone(url: "https://example.test/repo.git", into: "/dest") { lines.add($0) }
+ XCTAssertTrue(ok)
+ XCTAssertEqual(lines as? [String], ["Receiving objects: 50%"])
+ XCTAssertEqual(fake.commands, ["true clone --depth 1 --single-branch --progress https://example.test/repo.git /dest"])
+ fake.streamOK = false
+ XCTAssertFalse(git(fake).clone(url: "u", into: "/d") { _ in })
+ }
+
+ func testAdoptionRunsInitRemoteFetchThenRecordsTheCopyAndChecksOutMain() throws {
+ let dir = try temporaryFolder()
+ try "kept".write(toFile: dir + "/a.txt", atomically: true, encoding: .utf8)
+ let fake = FakeProcessRunner()
+ fake.answer = { $0.arguments.contains("ls-tree") ? ProcessResult(out: "a.txt\0b.txt\0") : ProcessResult() }
+ let r = git(fake).adopt(dir, remote: "https://example.test/repo.git", branch: "zip-copy-2026-11-01")
+ XCTAssertTrue(r.ok)
+ XCTAssertTrue(r.log.contains("$ git init"))
+ let subcommands = fake.launches.map { l -> String in
+ // after `-C dir`, skipping the identity options of the commit
+ let rest = Array(l.arguments.dropFirst(2))
+ return rest.first == "-c" ? "commit" : rest[0]
+ }
+ XCTAssertEqual(subcommands, ["init", "remote", "fetch", "ls-tree", "symbolic-ref", "add", "commit", "checkout", "branch"])
+ XCTAssertTrue(fake.commands.contains("true -C \(dir) symbolic-ref HEAD refs/heads/zip-copy-2026-11-01"))
+ XCTAssertTrue(fake.commands.contains("true -C \(dir) checkout -f -B main origin/main"))
+ }
+
+ func testAFailedFetchStopsTheAdoptionAndRemovesTheGitFolderItMade() throws {
+ let dir = try temporaryFolder()
+ let fake = FakeProcessRunner()
+ fake.answer = { l in
+ if l.arguments.contains("init") { try? FileManager.default.createDirectory(atPath: dir + "/.git", withIntermediateDirectories: true) }
+ return l.arguments.contains("fetch") ? ProcessResult(status: 128, out: "could not resolve host") : ProcessResult()
+ }
+ let r = git(fake).adopt(dir, remote: "https://example.test/repo.git", branch: "zip-copy-2026-11-01")
+ XCTAssertFalse(r.ok)
+ XCTAssertTrue(r.log.contains("could not resolve host"))
+ XCTAssertFalse(FileManager.default.fileExists(atPath: dir + "/.git"), "the copy must not count as a checkout after a failed adoption")
+ XCTAssertFalse(fake.commands.contains { $0.contains(" checkout ") }, "nothing in the folder changes before the fetch has worked")
+ }
+
+ // MARK: Pure decisions
+
+ func testAutoPullIsAllowedOnlyOnMainWithoutChanges() {
+ XCTAssertTrue(GitRules.autoPullAllowed(branch: "main\n", status: ""))
+ XCTAssertFalse(GitRules.autoPullAllowed(branch: "device-install", status: ""))
+ XCTAssertFalse(GitRules.autoPullAllowed(branch: "main", status: " M scripts/build_ipa_from_nsp.py\n"))
+ XCTAssertFalse(GitRules.autoPullAllowed(branch: "HEAD", status: ""), "a detached HEAD is not main")
+ }
+
+ func testTheZipCopyBranchCarriesTheDate() {
+ let date = Calendar(identifier: .gregorian).date(from: DateComponents(year: 2026, month: 11, day: 1, hour: 12))!
+ XCTAssertEqual(GitRules.zipCopyBranch(date: date), "zip-copy-2026-11-01")
+ }
+
+ func testAZipCopyIsAdoptedOnlyWhenNothingElseRunsAndTheAutomaticUpdateIsOn() {
+ func decide(isRepo: Bool = true, isGit: Bool = false, running: Bool = false, busy: Bool = false, auto: Bool = true) -> GitRules.ZipAdoption {
+ GitRules.zipAdoption(isRepo: isRepo, isGit: isGit, buildRunning: running, repoBusy: busy, autoUpdate: auto)
+ }
+ XCTAssertEqual(decide(), .adopt)
+ XCTAssertEqual(decide(isGit: true), .notNeeded)
+ XCTAssertEqual(decide(isRepo: false), .notNeeded)
+ XCTAssertEqual(decide(running: true), .waitsForBuild)
+ XCTAssertEqual(decide(busy: true), .waitsForBuild)
+ XCTAssertEqual(decide(auto: false), .autoUpdateOff)
+ XCTAssertEqual(decide(running: true, auto: false), .waitsForBuild)
+ }
+
+ private func temporaryFolder() throws -> String {
+ let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("gitcheckout-\(UUID().uuidString)")
+ try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
+ addTeardownBlock { try? FileManager.default.removeItem(at: url) }
+ return url.path
+ }
+}
diff --git a/frontend/Tests/NXCoreTests/ProcessRunnerTests.swift b/frontend/Tests/NXCoreTests/ProcessRunnerTests.swift
new file mode 100644
index 00000000..cbcef38e
--- /dev/null
+++ b/frontend/Tests/NXCoreTests/ProcessRunnerTests.swift
@@ -0,0 +1,178 @@
+import XCTest
+@testable import NXCore
+
+/// `ProcessRunner`: the real one against small system tools, and the helpers over a fake.
+final class ProcessRunnerTests: XCTestCase {
+ private let real = SystemProcessRunner()
+
+ // MARK: The real runner
+
+ func testCaptureKeepsStdoutAndStderrApart() {
+ let r = real.capture(ProcessLaunch(["/bin/sh", "-c", "echo out; echo err >&2; exit 3"]))
+ XCTAssertTrue(r.started)
+ XCTAssertEqual(r.status, 3)
+ XCTAssertFalse(r.ok)
+ XCTAssertEqual(r.out, "out\n")
+ XCTAssertEqual(r.err, "err\n")
+ }
+
+ func testRunQuietMergesBothStreams() {
+ let r = real.runQuiet(["/bin/sh", "-c", "echo out; echo err >&2"], env: ["PATH": "/usr/bin:/bin"])
+ XCTAssertTrue(r.ok)
+ XCTAssertEqual(Set(r.out.split(separator: "\n")), ["out", "err"])
+ }
+
+ func testTheEnvironmentIsExactlyTheOneGiven() {
+ let r = real.runQuiet(["/usr/bin/env"], env: ["ONLY": "this"])
+ XCTAssertEqual(r.out.trimmingCharacters(in: .whitespacesAndNewlines), "ONLY=this")
+ }
+
+ func testTheDirectoryIsTheOneGiven() {
+ let dir = NSTemporaryDirectory()
+ let r = real.capture(ProcessLaunch(executable: "/bin/pwd", directory: dir))
+ let printed = r.out.trimmingCharacters(in: .whitespacesAndNewlines)
+ XCTAssertEqual(URL(fileURLWithPath: printed).resolvingSymlinksInPath().path, URL(fileURLWithPath: dir).resolvingSymlinksInPath().path)
+ }
+
+ func testAToolThatDoesNotExistFailsToStartWithAReason() {
+ let r = real.runQuiet(["/nonexistent/tool"], env: [:])
+ XCTAssertFalse(r.started)
+ XCTAssertFalse(r.ok)
+ XCTAssertFalse(r.out.isEmpty)
+ XCTAssertFalse(real.runOK("/nonexistent/tool", []))
+ XCTAssertNil(real.output("/nonexistent/tool", []))
+ }
+
+ func testRunOKAndOutputFollowTheExitStatus() {
+ XCTAssertTrue(real.runOK("/usr/bin/true", []))
+ XCTAssertFalse(real.runOK("/usr/bin/false", []))
+ XCTAssertEqual(real.output("/bin/echo", ["hello"]), "hello\n")
+ XCTAssertNil(real.output("/usr/bin/false", []))
+ }
+
+ func testCaptureDoesNotBlockOnAFullStderrPipe() {
+ // Far more than a pipe holds, on the stream that is read second.
+ let r = real.capture(ProcessLaunch(["/bin/sh", "-c", "head -c 300000 /dev/zero | tr '\\0' x >&2; echo done"]))
+ XCTAssertTrue(r.ok)
+ XCTAssertEqual(r.out, "done\n")
+ XCTAssertEqual(r.err.count, 300000)
+ }
+
+ func testStreamGivesEachLineAndTheResult() {
+ let lines = Collected()
+ let ok = real.runCaptured(["/bin/sh", "-c", "echo one; echo two >&2; exit 0"], env: ["PATH": "/usr/bin:/bin"]) { lines.add($0) }
+ XCTAssertTrue(ok)
+ XCTAssertEqual(Set(lines.values), ["one", "two"])
+ XCTAssertFalse(real.runCaptured(["/usr/bin/false"], env: [:]) { _ in })
+ }
+
+ func testStreamReportsAToolThatDoesNotStartAsALine() {
+ let lines = Collected()
+ XCTAssertFalse(real.runCaptured(["/nonexistent/tool"], env: [:]) { lines.add($0) })
+ XCTAssertEqual(lines.values.count, 1)
+ XCTAssertTrue(lines.values[0].hasPrefix("could not start /nonexistent/tool"))
+ }
+
+ func testStartReportsOutputAndTheExitStatus() throws {
+ let output = Collected(), exited = expectation(description: "exit")
+ var status: Int32 = -1
+ _ = try real.start(ProcessLaunch(["/bin/sh", "-c", "echo started; exit 5"]), onOutput: { output.add(String(decoding: $0, as: UTF8.self)) },
+ onExit: { status = $0; exited.fulfill() })
+ wait(for: [exited], timeout: 10)
+ XCTAssertEqual(status, 5)
+ let deadline = Date().addingTimeInterval(5)
+ while !output.values.joined().contains("started") && Date() < deadline { usleep(20_000) }
+ XCTAssertTrue(output.values.joined().contains("started"))
+ }
+
+ func testStartThrowsForAToolThatDoesNotExist() {
+ XCTAssertThrowsError(try real.start(ProcessLaunch(["/nonexistent/tool"]), onOutput: { _ in }, onExit: { _ in }))
+ }
+
+ /// With a group the process leads a group of its own, and the group is tracked until it exits.
+ func testStartInAGroupLeadsItsOwnGroupAndEndsWithIt() throws {
+ guard FileManager.default.isExecutableFile(atPath: "/usr/bin/python3") else { throw XCTSkip("System Python is required") }
+ let exited = expectation(description: "exit")
+ let process = try real.start(ProcessLaunch(["/bin/sh", "-c", "sleep 30"], groupPython: "/usr/bin/python3"), onOutput: { _ in },
+ onExit: { _ in exited.fulfill() })
+ let pid = process.processIdentifier
+ var tries = 0
+ while getpgid(pid) != pid && tries < 100 { usleep(50_000); tries += 1 }
+ XCTAssertEqual(getpgid(pid), pid, "the process is the leader of its own group")
+ XCTAssertTrue(ProcessGroups.buildProcessesAlive)
+ ProcessGroups.endGroup(pid)
+ wait(for: [exited], timeout: 10)
+ var alive = true
+ tries = 0
+ while alive && tries < 50 { alive = ProcessGroups.buildProcessesAlive; if alive { usleep(50_000) }; tries += 1 }
+ XCTAssertFalse(alive, "an ended process is no longer tracked")
+ }
+
+ // MARK: The helpers over a fake
+
+ func testRunQuietAsksForOneMergedRunWithTheGivenEnvironment() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(out: "hi") }
+ let r = fake.runQuiet(["/usr/bin/git", "status"], env: ["A": "1"])
+ XCTAssertEqual(r.out, "hi")
+ XCTAssertEqual(fake.commands, ["git status"])
+ XCTAssertTrue(fake.launches[0].mergesErrors)
+ XCTAssertEqual(fake.launches[0].environment, ["A": "1"])
+ }
+
+ func testRunOKAndOutputReadTheFakesAnswer() {
+ let fake = FakeProcessRunner()
+ fake.answer = { $0.executable == "/good" ? ProcessResult(out: "yes\n") : ProcessResult(status: 1) }
+ XCTAssertTrue(fake.runOK("/good", []))
+ XCTAssertFalse(fake.runOK("/bad", []))
+ XCTAssertEqual(fake.output("/good", []), "yes\n")
+ XCTAssertNil(fake.output("/bad", []))
+ }
+
+ // MARK: A service that takes the runner
+
+ func testToolchainReadsTheDeveloperDirectoryThroughTheRunner() throws {
+ let fake = FakeProcessRunner()
+ let folder = NSTemporaryDirectory()
+ fake.answer = { _ in ProcessResult(out: folder + "\n") }
+ XCTAssertEqual(Toolchain.developerDir(runner: fake), folder)
+ XCTAssertEqual(fake.commands, ["xcode-select -p"])
+ fake.answer = { _ in ProcessResult(status: 1) }
+ XCTAssertNil(Toolchain.developerDir(runner: fake))
+ }
+
+ func testToolchainParsesThePythonVersionFromTheRunner() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(out: "3.12\n") }
+ let v = Toolchain.pythonVersion("/fake/python3", runner: fake)
+ XCTAssertEqual(v?.0, 3)
+ XCTAssertEqual(v?.1, 12)
+ fake.answer = { _ in ProcessResult(out: "not a version") }
+ XCTAssertNil(Toolchain.pythonVersion("/fake/python3", runner: fake))
+ fake.answer = { _ in .failedToStart("no such file") }
+ XCTAssertNil(Toolchain.pythonVersion("/fake/python3", runner: fake))
+ }
+
+ func testToolchainTakesTheFirstLineOfAToolsOutput() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(out: "Apple metal version 32023\nTarget: air64\n") }
+ XCTAssertEqual(Toolchain.firstOutputLine("/usr/bin/xcrun", ["metal", "--version"], runner: fake), "Apple metal version 32023")
+ XCTAssertEqual(fake.commands, ["xcrun metal --version"])
+ }
+
+ func testMakeVenvLogsWhatTheToolsPrintAndStopsWhenOneFails() {
+ let fake = FakeProcessRunner()
+ fake.answer = { _ in ProcessResult(status: 1, out: "so far", err: "externally-managed-environment") }
+ let r = Toolchain.makeVenv(base: "/fake/python3", runner: fake)
+ XCTAssertFalse(r.ok)
+ XCTAssertTrue(r.log.contains("externally-managed-environment"))
+ }
+}
+
+/// Lines from a pipe's threads, read afterwards.
+private final class Collected: @unchecked Sendable {
+ private let lock = NSLock()
+ private var box: [String] = []
+ func add(_ s: String) { lock.lock(); box.append(s); lock.unlock() }
+ var values: [String] { lock.lock(); defer { lock.unlock() }; return box }
+}