Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Sources/XcodeMCPNativeRuntime/NativeObjCGetters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ABIBridge
import ABIBridgeCore
import ABIBridgeCore.Backend
import Foundation

/// Reads Objective-C properties of native Xcode objects through their getters.
@safe
@MainActor
enum NativeObjCGetters {
static func object(_ receiver: AnyObject, selector: String) throws -> AnyObject? {
let get = try ABIRuntime.shared.object(receiver).resolveAndWait(.objc(.method(selector, as: (() -> AnyObject?).self)))
return try unsafe get.unsafeInvoke()
}

static func boolean(_ receiver: AnyObject, selector: String) throws -> Bool {
let get = try ABIRuntime.shared.object(receiver).resolveAndWait(.objc(.method(selector, as: (() -> Bool).self)))
return try unsafe get.unsafeInvoke()
}

static func array<Value>(_ receiver: AnyObject, selector: String) throws -> [Value] {
guard let value = try Self.object(receiver, selector: selector) else { return [] }
guard let values = value as? [Value] else {
throw NativeRuntimeError.unsupportedContract("The native \(selector) value is not an array of \(Value.self)")
}
return values
}

static func optionalString(_ receiver: AnyObject?, selector: String) throws -> String? {
guard let receiver, let object = receiver as? NSObject,
object.responds(to: NSSelectorFromString(selector)) else { return nil }
let get = try ABIRuntime.shared.object(receiver).resolveAndWait(.objc(.method(selector, as: (() -> String?).self)))
return try unsafe get.unsafeInvoke()
}
}
1,272 changes: 41 additions & 1,231 deletions Sources/XcodeMCPNativeRuntime/NativePreviewBuild.swift

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion Sources/XcodeMCPNativeRuntime/NativePreviewDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import Foundation
struct NativePreviewDefinitions {
let installation: NativeXcodeInstallation

/// A parsed preview definition and the translation unit it came from.
struct Source {
let translationUnit: NativeSwiftValue
let definition: NativeSwiftValue
}

func sourceURL(_ path: String, workspace: AnyObject) async throws -> URL {
do {
let action = try await ABIRuntime.shared.resolve(
Expand Down Expand Up @@ -36,7 +42,7 @@ struct NativePreviewDefinitions {
let displayName: String
let providerPath: NativeSwiftValue?
let registry: NativeSwiftValue?
let source: NativePreviewBuild.Source
let source: NativePreviewDefinitions.Source
}

func definition(in source: URL, at index: Int) async throws -> Definition {
Expand Down
891 changes: 891 additions & 0 deletions Sources/XcodeMCPNativeRuntime/NativePreviewDestinations.swift

Large diffs are not rendered by default.

300 changes: 300 additions & 0 deletions Sources/XcodeMCPNativeRuntime/NativePreviewProjectInputs.swift

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Sources/XcodeMCPNativeRuntime/NativePreviewSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class NativePreviewSession {
systemVersion = build.systemVersion
}

init(_ destination: NativePreviewBuild.Destination) {
init(_ destination: NativePreviewDestinations.Destination) {
identifier = destination.identifier
systemVersion = destination.systemVersion
}
Expand All @@ -43,8 +43,8 @@ final class NativePreviewSession {
let location: NativePreviewDefinitions.Definition
let index: Int
let localization: String?
let context: NativePreviewBuild.PlanningContext
let destination: NativePreviewBuild.DestinationSelection
let context: NativePreviewDestinations.PlanningContext
let destination: NativePreviewDestinations.Selection
var certificate: BuildCertificate?
}

Expand Down Expand Up @@ -234,7 +234,7 @@ final class NativePreviewSession {
}
}

func prepareSimulator(destination: NativePreviewBuild.Destination,
func prepareSimulator(destination: NativePreviewDestinations.Destination,
process: NativePreviewProcess) async throws {
guard destination.previewPlatform?.isSimulator == true else { return }
try await prepareSimulator(destination: SimulatorDestination(destination),
Expand Down
67 changes: 35 additions & 32 deletions Sources/XcodeMCPNativeRuntime/NativePreviewTool.swift

Large diffs are not rendered by default.

46 changes: 26 additions & 20 deletions Tests/XcodeMCPNativeRuntimeTests/NativePreviewBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Testing
@MainActor
struct NativePreviewBuildTests {
@Test func sourceSelectionsWaitForRestorationAndCancelQueuedOperations() async throws {
let (builder, root) = try sourceSelectionBuilder()
let (destinations, root) = try sourceSelectionDestinations()
defer { try? FileManager.default.removeItem(at: root) }
let workspace = NSObject()
let entered = AsyncStream<Void>.makeStream()
Expand All @@ -15,7 +15,7 @@ struct NativePreviewBuildTests {
var input = "original"
var observed: [String] = []
let first = Task { @MainActor in
try await builder.withSourceSelection(workspace: workspace) {
try await destinations.withSourceSelection(workspace: workspace) {
input = "first"
entered.continuation.yield(())
defer { input = "original"; observed.append("restored") }
Expand All @@ -27,15 +27,15 @@ struct NativePreviewBuildTests {
_ = await ready.next()
let second = Task { @MainActor in
attempted.continuation.yield(())
return try await builder.withSourceSelection(workspace: workspace) {
return try await destinations.withSourceSelection(workspace: workspace) {
observed.append(input)
return input
}
}
var pending = attempted.stream.makeAsyncIterator()
_ = await pending.next()
let cancelled = Task { @MainActor in
try await builder.withSourceSelection(workspace: workspace) {
try await destinations.withSourceSelection(workspace: workspace) {
observed.append("cancelled operation ran")
}
}
Expand All @@ -52,7 +52,7 @@ struct NativePreviewBuildTests {
}

@Test func sourceSelectionDoesNotBlockAnotherWorkspace() async throws {
let (builder, root) = try sourceSelectionBuilder()
let (destinations, root) = try sourceSelectionDestinations()
defer { try? FileManager.default.removeItem(at: root) }
let firstWorkspace = NSObject()
let secondWorkspace = NSObject()
Expand All @@ -61,7 +61,7 @@ struct NativePreviewBuildTests {
let attempted = AsyncStream<Void>.makeStream()
var secondEntered = false
let first = Task { @MainActor in
try await builder.withSourceSelection(workspace: firstWorkspace) {
try await destinations.withSourceSelection(workspace: firstWorkspace) {
entered.continuation.yield(())
for await _ in release.stream { break }
}
Expand All @@ -70,7 +70,7 @@ struct NativePreviewBuildTests {
_ = await ready.next()
let second = Task { @MainActor in
attempted.continuation.yield(())
try await builder.withSourceSelection(workspace: secondWorkspace) { secondEntered = true }
try await destinations.withSourceSelection(workspace: secondWorkspace) { secondEntered = true }
}
var pending = attempted.stream.makeAsyncIterator()
_ = await pending.next()
Expand All @@ -81,16 +81,22 @@ struct NativePreviewBuildTests {
try await second.value
}

private func sourceSelectionBuilder() throws -> (NativePreviewBuild, URL) {
private func sourceSelectionDestinations() throws -> (NativePreviewDestinations, URL) {
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let app = root.appendingPathComponent("Xcode.app")
try FileManager.default.createDirectory(at: app.appendingPathComponent("Contents/Frameworks/IDEFoundation.framework"),
withIntermediateDirectories: true)
return (NativePreviewBuild(installation: try NativeXcodeInstallation(developerDirectory: app)), root)
return (NativePreviewDestinations(installation: try NativeXcodeInstallation(developerDirectory: app)), root)
}

private func projectInputs() throws -> (NativePreviewProjectInputs, URL) {
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
return (NativePreviewProjectInputs(), root)
}

@Test func inputRootsDetectReferencedProjectConfigurationChangesOutsideWorkspace() throws {
let (builder, root) = try sourceSelectionBuilder()
let (inputs, root) = try projectInputs()
defer { try? FileManager.default.removeItem(at: root) }
let workspaceURL = root.appendingPathComponent("Workspace/Fixture.xcworkspace")
let projectFile = root.appendingPathComponent("Project/Fixture.xcodeproj/project.pbxproj")
Expand All @@ -106,7 +112,7 @@ struct NativePreviewBuildTests {
let build = NativePreviewBuild.Result(targets: [], previewRoots: [], destinationIdentifier: "destination",
previewDestinationIdentifier: "preview", platformIdentifier: nil, previewPlatform: nil, platformName: nil,
deviceModelName: nil, systemVersion: nil, declaredInputFiles: [])
let roots = try #require(try builder.inputRoots(workspace: workspace, build: build, source: source))
let roots = try #require(try inputs.inputRoots(workspace: workspace, build: build, source: source))
#expect(roots.missing == [alternateProjectFile])
let before = try NativePreviewInputSnapshot.capture(roots: roots.roots, missingInputs: roots.missing)
#expect(try before.matchesCurrentFiles())
Expand All @@ -124,7 +130,7 @@ struct NativePreviewBuildTests {

@Test(arguments: [false, true])
func projectSynchronizationSkipsSourceChangesButRefreshesSavedProjects(legacy: Bool) async throws {
let (builder, root) = try sourceSelectionBuilder()
let (inputs, root) = try projectInputs()
defer { try? FileManager.default.removeItem(at: root) }
let projectFile = root.appendingPathComponent("Fixture.xcodeproj/project.pbxproj")
let alternate = projectFile.deletingLastPathComponent().appendingPathComponent("project.xcproj")
Expand All @@ -137,36 +143,36 @@ struct NativePreviewBuildTests {
? LegacyPreviewBuildProjectContainer(dataFiles: files) : PreviewBuildProjectContainer(dataFiles: files)
let workspace = PreviewBuildWorkspace(url: root.appendingPathComponent("Fixture.xcworkspace"), containers: [container])

try await builder.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)
#expect(container.synchronizationCount == 1)
try Data("edited view".utf8).write(to: source)
try await builder.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)
#expect(container.synchronizationCount == 1)

try Data("edited project".utf8).write(to: projectFile)
try await builder.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)
#expect(container.synchronizationCount == 2)
try Data("alternate project".utf8).write(to: alternate)
try await builder.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)
#expect(container.synchronizationCount == 3)

// A new workspace must refresh even when it opens the same files.
let replacement = PreviewBuildWorkspace(url: workspace.representingFilePath.fileURL, containers: [container])
try await builder.synchronizeProjectContainers(in: replacement)
try await inputs.synchronizeProjectContainers(in: replacement)
#expect(container.synchronizationCount == 4)
}

@Test func untrackableProjectSettingsContinueRefreshing() async throws {
let (builder, root) = try sourceSelectionBuilder()
let (inputs, root) = try projectInputs()
defer { try? FileManager.default.removeItem(at: root) }
let projectFile = root.appendingPathComponent("project.pbxproj")
try Data("project".utf8).write(to: projectFile)
let container = PreviewBuildProjectContainer(dataFiles: [PreviewBuildFilePath(url: projectFile)])
container.pbxProject.targets = nil
let workspace = PreviewBuildWorkspace(url: root.appendingPathComponent("Fixture.xcworkspace"), containers: [container])

try await builder.synchronizeProjectContainers(in: workspace)
try await builder.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)
try await inputs.synchronizeProjectContainers(in: workspace)

#expect(container.synchronizationCount == 2)
}
Expand Down