Skip to content

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

Xcode row: an App Store Xcode that was never opened is its own state, with a one-click fix

LNPANDYf78ffb2cb1fb
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index 14953155..8c1bd0be 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -1841,6 +1841,7 @@ final class BuildModel: ObservableObject {
var detail = ""
var lastError = "" // the tool's own first error line after a failed install (brew's, pip's)
var fixHint = "" // an L10n key: what to type or click, chosen from that line
+ var actionLabel = "" // an L10n key for the button when the action is not a plain install ("Fix")
let installCmd: [String] // run via bash; empty = manual (Xcode from the App Store)
}
@@ -2067,21 +2068,56 @@ final class BuildModel: ObservableObject {
/// 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".
- nonisolated static func developerDirExists() -> Bool {
+ nonisolated 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 false }
+ 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),
- !dir.isEmpty else { return false }
+ !dir.isEmpty else { return nil }
var isDir: ObjCBool = false
- return FileManager.default.fileExists(atPath: dir, isDirectory: &isDir) && isDir.boolValue
+ return FileManager.default.fileExists(atPath: dir, isDirectory: &isDir) && isDir.boolValue ? dir : nil
+ }
+
+ nonisolated static func developerDirExists() -> Bool { developerDir() != nil }
+
+ /// An Xcode.app that is installed but not selected: an App Store download arrives as
+ /// /Applications/Xcode.app and stays unused until someone opens it or points xcode-select at
+ /// it, so `xcode-select -p` keeps saying CommandLineTools and xcodebuild fails later.
+ nonisolated static func installedXcode() -> String? {
+ for app in ["/Applications/Xcode.app", "/Applications/Xcode-beta.app"]
+ where FileManager.default.isExecutableFile(atPath: app + "/Contents/Developer/usr/bin/xcodebuild") {
+ return app
+ }
+ let apps = (try? FileManager.default.contentsOfDirectory(atPath: "/Applications")) ?? []
+ for name in apps.sorted() where name.hasPrefix("Xcode") && name.hasSuffix(".app") {
+ let app = "/Applications/" + name
+ if FileManager.default.isExecutableFile(atPath: app + "/Contents/Developer/usr/bin/xcodebuild") { return app }
+ }
+ return nil
+ }
+
+ /// What the Xcode row is looking at: the selected developer directory, the Xcode that is only
+ /// installed, and whether the iOS SDK answers. Pure, so the four cases are tested.
+ enum XcodeState: Equatable {
+ case ready
+ case noIOSSDK(selected: String) // Xcode selected, its iOS platform not downloaded
+ case commandLineToolsOnly(xcode: String) // Xcode.app installed, xcode-select still on the CLT
+ case notInstalled
+ }
+
+ nonisolated static func xcodeState(selected: String?, xcodeApp: String?, iosSDK: Bool) -> XcodeState {
+ if let selected, selected.contains(".app/Contents/Developer") {
+ return iosSDK ? .ready : .noIOSSDK(selected: selected)
+ }
+ if let xcodeApp { return .commandLineToolsOnly(xcode: xcodeApp) }
+ return iosSDK ? .ready : .notInstalled // a CLT whose xcrun does answer the SDK is usable
}
/// The commands that fetch Xcode's two downloadable parts; the setup panel and the guided setup run the same ones.
@@ -2104,13 +2140,28 @@ final class BuildModel: ObservableObject {
// xcodebuild. Ask for the SDK version itself — that is the check that matches the name.
// And ask xcode-select where Xcode is, not a fixed path: a beta lives at
// /Applications/Xcode-beta.app and the hard-coded check called that "no Xcode" (#21).
- let xcodePresent = Self.developerDirExists()
- && FileManager.default.isExecutableFile(atPath: "/usr/bin/xcodebuild")
- let iosSDK = xcodePresent && (!needsIOS || runOK("/usr/bin/xcrun", ["--sdk", "iphoneos", "--show-sdk-version"]))
- out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint",
- present: iosSDK,
- detail: iosSDK ? "" : (xcodePresent ? "dep.xcode.noSDK" : ""),
- installCmd: iosSDK ? [] : (xcodePresent ? iosPlatformCmd : [])))
+ let selected = Self.developerDir()
+ let xcodeApp = Self.installedXcode()
+ let sdkenv = runOK("/usr/bin/xcrun", ["--sdk", "iphoneos", "--show-sdk-version"])
+ // A Mac-only setup does not need the iOS platform (onboarding's platform choice, #120); a
+ // Mac build still needs a selected Xcode, so the CommandLineTools state is reported either way.
+ switch Self.xcodeState(selected: selected, xcodeApp: xcodeApp, iosSDK: !needsIOS || sdkenv) {
+ case .ready:
+ out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint", present: true, installCmd: []))
+ case .noIOSSDK:
+ out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint", present: false,
+ detail: "dep.xcode.noSDK", installCmd: iosPlatformCmd))
+ case .commandLineToolsOnly(let xcode):
+ // One admin prompt runs both steps: switch the toolchain, then let Xcode finish its
+ // first launch (license and components) - a fresh App Store download has neither.
+ let script = "xcode-select -s '" + xcode + "/Contents/Developer' && xcodebuild -runFirstLaunch"
+ let apple = "do shell script \"" + script.replacingOccurrences(of: "\"", with: "\\\"") + "\" with administrator privileges"
+ out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint", present: false,
+ detail: "dep.xcode.notSelected", actionLabel: "dep.xcode.fix",
+ installCmd: ["/usr/bin/osascript", "-e", apple]))
+ case .notInstalled:
+ out.append(Dep(id: "xcode", name: "dep.xcode", hint: "dep.xcode.hint", present: false, installCmd: []))
+ }
let mt = runOK("/usr/bin/xcrun", ["-sdk", "macosx", "metal", "--version"])
out.append(Dep(id: "metal", name: "dep.metal", hint: "dep.metal.hint",
@@ -2885,7 +2936,7 @@ struct BuilderView: View {
ProgressView().controlSize(.small)
Text(m.t("dep.installing")).font(.manrope(12)).foregroundStyle(LauncherPalette.building)
} else if !d.present {
- Button(d.installCmd.isEmpty ? m.t("dep.openAppStore") : m.t("dep.install")) { m.installDep(d) }
+ Button(d.actionLabel.isEmpty ? (d.installCmd.isEmpty ? m.t("dep.openAppStore") : m.t("dep.install")) : m.t(d.actionLabel)) { m.installDep(d) }
}
}
// Why the last install failed, and what to do about it: the tool's own
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index d2f91a70..885f0763 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -538,6 +538,9 @@ enum L10n {
"dep.openAppStore": ("打开 App Store", "Open App Store"),
"dep.xcode": ("Xcode + iOS SDK", "Xcode + iOS SDK"),
+ "dep.xcode.notSelected": ("Xcode 已安装,但 xcode-select 还指向 CommandLineTools——点「修复」会切换到 Xcode 并完成首次启动(会要管理员密码)。",
+ "Xcode is installed, but xcode-select still points at CommandLineTools — Fix switches to Xcode and finishes its first launch (asks for your admin password)."),
+ "dep.xcode.fix": ("修复", "Fix"),
"dep.xcode.hint": ("App Store 安装(Beta 版也行,用 xcode-select 指向它),约 12 GB",
"from the App Store (a beta works too — point xcode-select at it), about 12 GB"),
"dep.xcode.noSDK": ("Xcode 已装,但 iOS SDK 未下载(Xcode 15 起 iOS 平台是单独下载的)",
diff --git a/frontend/Tests/NSPortBuilderTests/XcodeStateTests.swift b/frontend/Tests/NSPortBuilderTests/XcodeStateTests.swift
new file mode 100644
index 00000000..63b4441a
--- /dev/null
+++ b/frontend/Tests/NSPortBuilderTests/XcodeStateTests.swift
@@ -0,0 +1,42 @@
+import XCTest
+@testable import NSPortBuilder
+
+/// An App Store download that was never opened leaves `xcode-select -p` on CommandLineTools.
+/// The Xcode row has to say that, and offer the one-click switch, instead of "no iOS SDK".
+final class XcodeStateTests: XCTestCase {
+ func testXcodeSelectedWithTheSDKIsReady() {
+ XCTAssertEqual(BuildModel.xcodeState(selected: "/Applications/Xcode.app/Contents/Developer",
+ xcodeApp: "/Applications/Xcode.app", iosSDK: true), .ready)
+ }
+
+ func testXcodeSelectedWithoutTheIOSPlatformAsksForTheDownload() {
+ XCTAssertEqual(BuildModel.xcodeState(selected: "/Applications/Xcode.app/Contents/Developer",
+ xcodeApp: "/Applications/Xcode.app", iosSDK: false),
+ .noIOSSDK(selected: "/Applications/Xcode.app/Contents/Developer"))
+ }
+
+ func testCommandLineToolsSelectedWhileXcodeIsInstalledIsItsOwnState() {
+ XCTAssertEqual(BuildModel.xcodeState(selected: "/Library/Developer/CommandLineTools",
+ xcodeApp: "/Applications/Xcode.app", iosSDK: false),
+ .commandLineToolsOnly(xcode: "/Applications/Xcode.app"))
+ }
+
+ func testNothingInstalled() {
+ XCTAssertEqual(BuildModel.xcodeState(selected: nil, xcodeApp: nil, iosSDK: false), .notInstalled)
+ }
+
+ func testACommandLineToolsThatAnswersTheSDKIsUsable() {
+ XCTAssertEqual(BuildModel.xcodeState(selected: "/Library/Developer/CommandLineTools",
+ xcodeApp: nil, iosSDK: true), .ready)
+ }
+
+ func testTheTwoXcodeStringsHaveBothLanguages() {
+ for key in ["dep.xcode.notSelected", "dep.xcode.fix"] {
+ let row = L10n.table[key]
+ XCTAssertNotNil(row, key)
+ XCTAssertFalse(row?.0.isEmpty ?? true, key)
+ XCTAssertFalse(row?.1.isEmpty ?? true, key)
+ XCTAssertNotEqual(row?.0, row?.1, "\(key) has the English text twice")
+ }
+ }
+}