Skip to content

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

Move the build log into the BuildLog store (#387)

v0lumehi5add68aa640e
diff --git a/frontend/ARCHITECTURE.md b/frontend/ARCHITECTURE.md
index 41863c48..bfbe6481 100644
--- a/frontend/ARCHITECTURE.md
+++ b/frontend/ARCHITECTURE.md
@@ -13,11 +13,12 @@ The plan and the steps are in [LNPANDY/NX2Apple#387](https://github.com/LNPANDY/
## Where it stands
-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. The rest still lives in the app target and moves with the later steps.
+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. 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.
-- `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. No other store exists yet.
+- `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`.
- `NXLauncher` holds the views and styles that do not need `BuildModel`: `LauncherPalette`, `Typography`, `LauncherStyles`, `LauncherDropdown`, `PadFocus` and `EmbeddedFonts`.
- `NX2Apple` holds everything else, including `BuildModel`.
diff --git a/frontend/Sources/NX2Apple/ContentView.swift b/frontend/Sources/NX2Apple/ContentView.swift
index 6c161fc8..12fedce3 100644
--- a/frontend/Sources/NX2Apple/ContentView.swift
+++ b/frontend/Sources/NX2Apple/ContentView.swift
@@ -123,7 +123,12 @@ final class BuildModel: ObservableObject {
@Published var staleFrameworks: [StaleFrameworks.Entry] = [] // the prompt of StaleFrameworksPrompt.swift (#449)
// The shell folder (shellCachePath, shellRoot): ShellRoot.swift.
@Published var stopping = false
- @Published var logTail = ""
+ /// The build log. Not published: only `BuildLogView` observes it (#387), so a log line redraws the log and nothing else.
+ let log = BuildLog()
+ var logTail: String {
+ get { BuildLog.onMain { log.text } }
+ set { BuildLog.onMain { log.set(newValue) } }
+ }
@Published var done = false
@Published var doneNote = ""
/// What the NSP script stopped on, when it is one of the known early stops (BuildFailure.swift). nil for a build that
@@ -223,7 +228,6 @@ final class BuildModel: ObservableObject {
init(localizer: Localizer = Localizer()) { self.localizer = localizer }
private var proc: Process?
- private var lineBuf = ""
private var staging: String?
// ---- Process groups: Stop and quitting end the whole build ----
@@ -1244,7 +1248,7 @@ final class BuildModel: ObservableObject {
shellDeviceChecked = false
if target != BuildTarget.mac, askAboutStaleFrameworks() { return } // StaleFrameworksPrompt.swift (#449)
stopping = false
- running = true; done = false; doneNote = ""; stages = []; logTail = ""; lineBuf = ""; chainNoteShown = false
+ running = true; done = false; doneNote = ""; stages = []; BuildLog.onMain { log.clear() }; chainNoteShown = false
failure = nil; confirmingOverride = nil
let override = takeOverrideFlag() // for this build only: the next one asks again
installedOn = ""; installedAppPath = ""
@@ -1677,7 +1681,7 @@ final class BuildModel: ObservableObject {
}
/// A stopped build leaves no half-finished stages behind: the progress starts empty again.
- private func resetProgress() { stages = []; logTail = ""; lineBuf = "" }
+ private func resetProgress() { stages = []; BuildLog.onMain { log.clear() } }
private func markAllDone() { for i in stages.indices where stages[i].state != .failed { stages[i].state = .done } }
@@ -1695,10 +1699,7 @@ final class BuildModel: ObservableObject {
}
private func say(_ s: String) { append(s + "\n") }
- private func append(_ s: String) {
- logTail += s
- if logTail.count > 12000 { logTail = String(logTail.suffix(9000)) }
- }
+ private func append(_ s: String) { BuildLog.onMain { log.append(s) } }
/// Split into lines (the pipe delivers arbitrary chunks) and turn the script's progress into the stage list.
/// `--progress json` writes one event per line (stage, step, progress with done / total / frac / eta, stage_end,
@@ -1706,18 +1707,12 @@ final class BuildModel: ObservableObject {
/// The older `--progress lines` form is still understood:
/// [12:00:01] [2/7] extract v720896 …
/// [12:00:20] ✓ [2/7] extract v720896 (0:19): 30,000 files, 3.4 GB
- private func ingest(_ chunk: String) {
- lineBuf += chunk
- while let i = lineBuf.firstIndex(of: "\n") {
- let line = String(lineBuf[..<i]).replacingOccurrences(of: "\r", with: "")
- lineBuf = String(lineBuf[lineBuf.index(after: i)...])
- handle(line)
- }
+ func ingest(_ chunk: String) {
+ for line in BuildLog.onMain({ log.feed(chunk) }) { handle(line) }
}
private func flushLines() {
- guard !lineBuf.isEmpty else { return }
- handle(lineBuf); lineBuf = ""
+ if let rest = BuildLog.onMain({ log.flush() }) { handle(rest) }
}
private func handle(_ line: String) {
@@ -3394,21 +3389,7 @@ struct BuilderView: View {
}
private var logView: some View {
- ScrollViewReader { sp in
- ScrollView {
- Text(m.logTail.isEmpty ? loc.t("log.placeholder") : m.logTail)
- .font(.mono(11))
- .textSelection(.enabled)
- .frame(maxWidth: .infinity, alignment: .leading)
- .foregroundStyle(.secondary)
- .padding(12)
- .id("end")
- }
- .frame(height: 220)
- .background(Color.black.opacity(0.35), in: RoundedRectangle(cornerRadius: 10))
- .onChange(of: m.logTail) { _ in sp.scrollTo("end", anchor: .bottom) }
- .onAppear { sp.scrollTo("end", anchor: .bottom) }
- }
+ BuildLogView(log: m.log,placeholder: loc.t("log.placeholder"))
}
/// The page's conclusion, at the end of its column: what is still missing, and the button.
diff --git a/frontend/Sources/NXLauncher/BuildLogView.swift b/frontend/Sources/NXLauncher/BuildLogView.swift
new file mode 100644
index 00000000..6745b800
--- /dev/null
+++ b/frontend/Sources/NXLauncher/BuildLogView.swift
@@ -0,0 +1,32 @@
+import SwiftUI
+import NXModel
+
+/// The build log as a scrolling text. It is the only view that observes `BuildLog`, so a log line redraws this and not the
+/// launcher around it (LNPANDY/NX2Apple#387).
+package struct BuildLogView: View {
+ @ObservedObject var log: BuildLog
+ let placeholder: String
+
+ package init(log: BuildLog, placeholder: String) {
+ self.log = log
+ self.placeholder = placeholder
+ }
+
+ package var body: some View {
+ ScrollViewReader { sp in
+ ScrollView {
+ Text(log.text.isEmpty ? placeholder : log.text)
+ .font(.mono(11))
+ .textSelection(.enabled)
+ .frame(maxWidth: .infinity, alignment: .leading)
+ .foregroundStyle(.secondary)
+ .padding(12)
+ .id("end")
+ }
+ .frame(height: 220)
+ .background(Color.black.opacity(0.35), in: RoundedRectangle(cornerRadius: 10))
+ .onChange(of: log.text) { _ in sp.scrollTo("end", anchor: .bottom) }
+ .onAppear { sp.scrollTo("end", anchor: .bottom) }
+ }
+ }
+}
diff --git a/frontend/Sources/NXModel/BuildLog.swift b/frontend/Sources/NXModel/BuildLog.swift
new file mode 100644
index 00000000..935b552f
--- /dev/null
+++ b/frontend/Sources/NXModel/BuildLog.swift
@@ -0,0 +1,61 @@
+import Foundation
+import Combine
+
+/// The text of the build log (LNPANDY/NX2Apple#387). It is a store of its own, so a line from the build script invalidates
+/// the views that show the log and nothing else: `BuildModel` holds it in a `let` and does not publish it, and no view above
+/// the log observes it. The failure classifier reads `text` once, when a build stops.
+///
+/// `text` keeps the tail: past `limit` characters it shrinks to the last `keep`. The pipe delivers arbitrary chunks, so
+/// `feed` also splits them into lines and holds back a partial last line until its newline arrives.
+@MainActor package final class BuildLog: ObservableObject {
+ package static let limit = 12000
+ package static let keep = 9000
+
+ @Published package private(set) var text = ""
+ private var partial = ""
+
+ nonisolated package init() {}
+
+ /// Adds text to the log. It is not split into lines: the caller ends a line itself.
+ package func append(_ s: String) {
+ text += s
+ if text.count > Self.limit { text = String(text.suffix(Self.keep)) }
+ }
+
+ /// Takes a chunk of the script's output and returns the lines it completes, without their line ends. The rest waits
+ /// for the next chunk. It does not add to `text`: the caller decides what of each line is log text.
+ package func feed(_ chunk: String) -> [String] {
+ partial += chunk
+ var lines: [String] = []
+ while let i = partial.firstIndex(of: "\n") {
+ lines.append(String(partial[..<i]).replacingOccurrences(of: "\r", with: ""))
+ partial = String(partial[partial.index(after: i)...])
+ }
+ return lines
+ }
+
+ /// The partial last line, if any, when the output ends without a newline.
+ package func flush() -> String? {
+ guard !partial.isEmpty else { return nil }
+ defer { partial = "" }
+ return partial
+ }
+
+ /// Empties the log and drops a partial line: a new build or a stopped one starts clean.
+ package func clear() {
+ text = ""
+ partial = ""
+ }
+
+ /// Replaces the text (tests set a log to classify).
+ package func set(_ s: String) { text = s }
+
+ /// Runs `body` as the main actor's. `BuildModel` is not `@MainActor` yet and every call that reaches the log runs on the
+ /// main queue already (pipe output is hopped over, the rest is UI actions). This is `MainActor.assumeIsolated` for
+ /// macOS 13. Drop it when `BuildModel` is `@MainActor`.
+ nonisolated package static func onMain<T>(_ body: @MainActor () -> T) -> T {
+ dispatchPrecondition(condition: .onQueue(.main))
+ typealias Plain = () -> T
+ return withoutActuallyEscaping(body) { unsafeBitCast($0, to: Plain.self)() }
+ }
+}
diff --git a/frontend/Tests/NX2AppleTests/BuildLogTests.swift b/frontend/Tests/NX2AppleTests/BuildLogTests.swift
new file mode 100644
index 00000000..3b533aab
--- /dev/null
+++ b/frontend/Tests/NX2AppleTests/BuildLogTests.swift
@@ -0,0 +1,84 @@
+import Combine
+import XCTest
+@testable import NXModel
+
+/// LNPANDY/NX2Apple#387, step 3: the build log is a store of its own and needs no SwiftUI.
+@MainActor final class BuildLogTests: XCTestCase {
+ func testAppendAddsText() {
+ let log = BuildLog()
+ log.append("one\n")
+ log.append("two\n")
+ XCTAssertEqual(log.text, "one\ntwo\n")
+ }
+
+ func testTheTextKeepsItsTail() {
+ let log = BuildLog()
+ let line = String(repeating: "x", count: 99) + "\n" // 100 characters
+ for n in 0..<150 { log.append(n == 149 ? "last\n" : line) }
+ XCTAssertLessThanOrEqual(log.text.count, BuildLog.limit)
+ XCTAssertGreaterThanOrEqual(log.text.count, BuildLog.keep)
+ XCTAssertTrue(log.text.hasSuffix("last\n"), "the newest text stays")
+ }
+
+ func testTextUnderTheLimitIsKept() {
+ let log = BuildLog()
+ log.append(String(repeating: "y", count: BuildLog.limit))
+ XCTAssertEqual(log.text.count, BuildLog.limit)
+ }
+
+ func testFeedReturnsCompleteLines() {
+ let log = BuildLog()
+ XCTAssertEqual(log.feed("a\nb\n"), ["a", "b"])
+ XCTAssertNil(log.flush())
+ }
+
+ func testALineAcrossChunkBoundariesIsJoined() {
+ let log = BuildLog()
+ XCTAssertEqual(log.feed("hel"), [])
+ XCTAssertEqual(log.feed("lo wor"), [])
+ XCTAssertEqual(log.feed("ld\nnext"), ["hello world"])
+ XCTAssertEqual(log.feed("\n"), ["next"])
+ }
+
+ func testCarriageReturnsAreDropped() {
+ let log = BuildLog()
+ // A "\r" inside a line goes, as before. ("\r\n" is one Character in Swift, so it never matches "\n": the old code
+ // had the same gap, and the script's output has plain "\n".)
+ XCTAssertEqual(log.feed("a\rb\nc"), ["ab"])
+ XCTAssertEqual(log.flush(), "c")
+ }
+
+ func testFlushReturnsThePartialLineOnce() {
+ let log = BuildLog()
+ _ = log.feed("done\nno newline")
+ XCTAssertEqual(log.flush(), "no newline")
+ XCTAssertNil(log.flush())
+ }
+
+ func testFeedDoesNotTouchTheText() {
+ let log = BuildLog()
+ _ = log.feed("a\nb\n")
+ XCTAssertEqual(log.text, "")
+ }
+
+ func testClearEmptiesTheTextAndThePartialLine() {
+ let log = BuildLog()
+ log.append("old\n")
+ _ = log.feed("half")
+ log.clear()
+ XCTAssertEqual(log.text, "")
+ XCTAssertNil(log.flush())
+ XCTAssertEqual(log.feed("new\n"), ["new"])
+ }
+
+ func testOnlyTheTextPublishes() {
+ let log = BuildLog()
+ var changes = 0
+ let sink = log.objectWillChange.sink { changes += 1 }
+ _ = log.feed("a\nb")
+ XCTAssertEqual(changes, 0, "splitting lines is not a change of the text")
+ log.append("a\n")
+ XCTAssertEqual(changes, 1)
+ sink.cancel()
+ }
+}
diff --git a/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift b/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
index 08067467..e3ae297a 100644
--- a/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
+++ b/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
@@ -161,6 +161,14 @@ final class LauncherPerformanceTests: XCTestCase {
cards: 120, fonts: 20, plistReads: 110, busyMs: 800),
Interaction(name: "open Manage", action: { pad.perform(.y) }, undo: { pad.perform(.b) },
cards: 180, fonts: 20, plistReads: 180, busyMs: 400),
+ // A build writes its log while the player looks at the library. The log is its own store (#387), so no game card
+ // is built for it. Each line is its own turn of the run loop, as the pipe delivers them.
+ Interaction(name: "1000 log lines in a build", action: {
+ for n in 0..<1000 {
+ model.ingest("[12:00:\(String(format: "%02d", n % 60))] line \(n) of the build script's output\n")
+ RunLoop.main.run(until: Date().addingTimeInterval(0.0005))
+ }
+ }, cards: 0, fonts: 20, plistReads: 10, busyMs: 1500),
]
let meter = MainThreadMeter()
defer { meter.stop() }
@@ -204,6 +212,7 @@ final class LauncherPerformanceTests: XCTestCase {
+ "then profile the release build with the Time Profiler.", step.name, busy, step.busyMs))
}
}
+ XCTAssertTrue(model.log.text.contains("line 999 of the build"), "the lines reached the log")
print(report.joined(separator: "\n"))
for failure in failures { XCTFail(failure) }
#endif
diff --git a/frontend/Tests/NX2AppleTests/SourceSizeTests.swift b/frontend/Tests/NX2AppleTests/SourceSizeTests.swift
index bab1296e..e384f448 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": 3749,
+ "NX2Apple/ContentView.swift": 3730,
"NX2Apple/LauncherView.swift": 973,
"NXCore/Localization.swift": 4237,
]