Skip to content

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

App: download the repository and pull its updates in one click

LNPANDYa5cceef4f855
diff --git a/frontend/NS-Port-Builder-macOS-arm64.zip b/frontend/NS-Port-Builder-macOS-arm64.zip
new file mode 100644
index 00000000..c8b5767a
Binary files /dev/null and b/frontend/NS-Port-Builder-macOS-arm64.zip differ
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index f712880f..c0d1a568 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -144,6 +144,119 @@ final class BuildModel: ObservableObject {
return ""
}
+ // ---- Repository download and updates (a clone is the scripts' home; not everyone has git habits) ----
+
+ static let repoURL = "https://github.com/LNPANDY/NX2Apple.git"
+ /// Clone/pull/check in flight; the buttons disable and the status line says what is happening.
+ @Published var repoBusy = false
+ @Published var repoStatus = ""
+ /// nil = not checked yet, 0 = up to date, n = n commits behind origin/main.
+ @Published var repoBehind: Int?
+
+ 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()]
+ }
+
+ /// 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 ~/NX2Apple (the discovery seeds cover it) and select it.
+ func cloneRepo() {
+ guard !running, !repoBusy else { return }
+ guard let git = Self.gitBinary() else { repoStatus = t("repo.gitMissing"); return }
+ let dest = NSHomeDirectory() + "/NX2Apple"
+ if FileManager.default.fileExists(atPath: dest) {
+ if Self.isRepo(dest) { repoPath = dest; repoStatus = ""; checkRepoUpdates() }
+ else { repoStatus = String(format: t("repo.destBusy"), shortPath(dest)) }
+ return
+ }
+ repoBusy = true; repoStatus = t("repo.downloading")
+ append("git clone --depth 1 \(Self.repoURL) \(dest)\n")
+ 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()) {
+ line in DispatchQueue.main.async { self.append(line + "\n") }
+ }
+ DispatchQueue.main.async {
+ self.repoBusy = false
+ if ok && Self.isRepo(dest) {
+ self.repoPath = dest
+ self.repoStatus = String(format: self.t("repo.downloadDone"), self.shortPath(dest))
+ } else {
+ self.repoStatus = self.t("repo.downloadFail")
+ try? FileManager.default.removeItem(atPath: dest) // a failed clone leaves an empty folder
+ }
+ }
+ }
+ }
+
+ /// Compare the checkout with origin/main. `silent` is the launch-time check: no status text, no
+ /// error noise when offline — it only fills repoBehind when it can.
+ func checkRepoUpdates(silent: Bool = false) {
+ guard !running, !repoBusy, repoOK, let git = Self.gitBinary() else {
+ if !silent, repoOK, Self.gitBinary() == nil { repoStatus = t("repo.gitMissing") }
+ return
+ }
+ repoBusy = true
+ if !silent { repoStatus = t("repo.checking") }
+ let dir = repoPath
+ 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 }
+ }
+ DispatchQueue.main.async {
+ self.repoBusy = false
+ if behind >= 0 {
+ self.repoBehind = behind
+ if !silent { self.repoStatus = behind == 0 ? self.t("repo.upToDate") : String(format: self.t("repo.behind"), behind) }
+ } else if !silent {
+ self.repoStatus = self.t("repo.netFail")
+ }
+ }
+ }
+ }
+
+ /// Fast-forward the checkout to origin/main. The build keeps everything it needs under
+ /// <id>_build/ outside the repository, so a pull never disturbs a previous build's files.
+ func pullRepo() {
+ guard !running, !repoBusy, repoOK, let git = Self.gitBinary() else { return }
+ repoBusy = true; repoStatus = t("repo.pulling")
+ let dir = repoPath
+ DispatchQueue.global().async {
+ let r = Self.runQuiet([git, "-C", dir, "pull", "--ff-only", "origin", "main"], env: Self.gitEnv())
+ DispatchQueue.main.async {
+ self.repoBusy = false
+ if r.ok {
+ self.repoBehind = 0; self.repoStatus = self.t("repo.pulled")
+ if !r.out.isEmpty { self.append(r.out + "\n") }
+ } else {
+ self.repoStatus = self.t("repo.pullFail")
+ self.append(r.out + "\n")
+ }
+ }
+ }
+ }
+
var basePath: String { slot("base").paths.first ?? "" }
var keysPath: String { slot("keys").paths.first ?? "" }
var updatePath: String { slot("update").paths.first ?? "" }
@@ -873,7 +986,10 @@ struct ContentView: View {
footer
}
.frame(minWidth: 720, idealWidth: 880, minHeight: 560, idealHeight: 820)
- .onAppear { m.checkDeps() }
+ .onAppear {
+ m.checkDeps()
+ if m.repoOK { m.checkRepoUpdates(silent: true) } // quiet fetch: fills repoBehind, offline is not an error
+ }
}
// The scrollable part, split out so it can be rendered on its own for layout checks.
@@ -930,7 +1046,19 @@ struct ContentView: View {
VStack(alignment: .leading, spacing: 10) {
HStack {
sectionHeader("shippingbox", m.t("repo.title"), note: m.t("repo.note"))
- Button(m.t("repo.choose")) { pickRepo() }
+ Spacer(minLength: 8)
+ if m.repoOK {
+ if let n = m.repoBehind, n > 0 {
+ Button(m.t("repo.pull")) { m.pullRepo() }
+ .disabled(m.repoBusy || m.running)
+ }
+ Button(m.t("repo.check")) { m.checkRepoUpdates() }
+ .disabled(m.repoBusy || m.running)
+ } else {
+ Button(m.t("repo.download")) { m.cloneRepo() }
+ .disabled(m.repoBusy || m.running)
+ Button(m.t("repo.choose")) { pickRepo() }
+ }
}
HStack(spacing: 10) {
Image(systemName: m.repoOK ? "checkmark.circle.fill" : "xmark.circle.fill")
@@ -945,6 +1073,14 @@ struct ContentView: View {
.lineLimit(1).truncationMode(.head)
.help(m.repoPath)
Spacer(minLength: 8)
+ if m.repoBusy { ProgressView().controlSize(.small) }
+ }
+ if !m.repoStatus.isEmpty {
+ Text(m.repoStatus)
+ .font(.system(size: 11.5))
+ .foregroundStyle((m.repoBehind ?? 0) > 0 ? Color.orange : Color.secondary)
+ } else if !m.repoOK {
+ Text(m.t("repo.firstHint")).font(.system(size: 11.5)).foregroundStyle(.secondary)
}
}.padding(8)
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 8f7096db..9265ceac 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -40,11 +40,34 @@ enum L10n {
"repo.title": ("项目仓库", "Project repository"),
"repo.note": ("App 调用这里的构建脚本", "the app runs the build scripts from here"),
"repo.choose": ("选择仓库…", "Choose repository…"),
- "repo.notFound": ("未找到本项目——点右侧选择 clone 的位置",
- "Project not found — pick your clone with the button"),
+ "repo.notFound": ("未找到本项目——下载,或点右侧选择 clone 的位置",
+ "Project not found — download it, or pick your clone with the button"),
"repo.notRepo": ("不是本项目的仓库:%@", "Not this project's repository: %@"),
"repo.pickMessage": ("选择本项目(含 dm/tools/nx_extract/Program.cs)的文件夹",
"Pick this project's folder (the one holding dm/tools/nx_extract/Program.cs)"),
+ "repo.download": ("下载仓库", "Download repository"),
+ "repo.downloading": ("正在下载到 ~/NX2Apple…(进度见下方日志,约一分钟)",
+ "Downloading to ~/NX2Apple… (progress in the log below, about a minute)"),
+ "repo.downloadDone": ("仓库已下载到 %@", "Repository downloaded to %@"),
+ "repo.downloadFail": ("下载失败——详情见日志(网络或 git 的问题)",
+ "Download failed — see the log (network or git)"),
+ "repo.destBusy": ("%@ 已存在且不是本项目——换个位置,或点「选择仓库…」",
+ "%@ already exists and is not this project — use another place, or Choose repository…"),
+ "repo.firstHint": ("第一次用?点「下载仓库」自动获取项目(约一分钟,需要网络)",
+ "First time here? Download repository fetches the project for you (about a minute, needs network)"),
+ "repo.check": ("检查更新", "Check for updates"),
+ "repo.checking": ("正在检查更新…", "Checking for updates…"),
+ "repo.upToDate": ("已是最新版本", "Up to date"),
+ "repo.behind": ("有更新:%d 个新提交,点「拉取更新」", "Updates available: %d new commits — pull them"),
+ "repo.pull": ("拉取更新", "Pull updates"),
+ "repo.pulling": ("正在更新…(构建不受影响,之前的构建文件不在仓库里)",
+ "Updating… (your builds are safe — their files live outside the repository)"),
+ "repo.pulled": ("已更新到最新", "Updated to the latest"),
+ "repo.pullFail": ("更新失败——详情见日志(本地文件被改过?)",
+ "Update failed — see the log (local changes?)"),
+ "repo.gitMissing": ("没找到 git:在终端运行 xcode-select --install 装一下命令行工具",
+ "git not found: run xcode-select --install in Terminal to get the Command Line Tools"),
+ "repo.netFail": ("检查更新失败(网络不通?)", "Could not check for updates (offline?)"),
// ---- game data ----
"data.title": ("游戏数据", "Game data"),