diff --git a/Package.resolved b/Package.resolved index a0220dc1..794f1563 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "ec262a538fb7b5a8c615f4591b3605cc8c6203b3a08572c61cf0a4630bdeeb55", + "originHash" : "68528e9f8c4ceda631635ad30a77f74ce22d8c7f6059dbd88e8799bc98ddbf08", "pins" : [ { "identity" : "asynchttpnetworkservice", @@ -122,8 +122,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/XcodesOrg/XcodesKit.git", "state" : { - "revision" : "4d3c6093980978b74c3fa42fc1b90cd1c34ac911", - "version" : "1.1.0" + "revision" : "37617957ae33310455bfe7cf0835d69e0a6aa3c5", + "version" : "1.2.0" } }, { diff --git a/Package.swift b/Package.swift index b52c295b..b4555e53 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,7 @@ let package = Package( .package(url: "https://github.com/xcodereleases/data", revision: "fcf527b187817f67c05223676341f3ab69d4214d"), .package(url: "https://github.com/onevcat/Rainbow.git", .upToNextMinor(from: "3.2.0")), .package(url: "https://github.com/XcodesOrg/XcodesLoginKit.git", branch: "main"), - .package(url: "https://github.com/XcodesOrg/XcodesKit.git", .upToNextMinor(from: "1.1.0")) + .package(url: "https://github.com/XcodesOrg/XcodesKit.git", .upToNextMinor(from: "1.2.0")) ], targets: [ .executableTarget( diff --git a/README.md b/README.md index eb63e82c..593d0e05 100644 --- a/README.md +++ b/README.md @@ -73,11 +73,14 @@ xcodes install 10.2.1 xcodes install 11 Beta 7 xcodes install 11.2 GM seed xcodes install 9.0 --path ~/Archive/Xcode_9.xip +xcodes install 27.3 Beta --path ~/Archive/Xcode_27.3_beta.aea xcodes install --latest-prerelease xcodes install --latest --directory "/Volumes/Bag Of Holding/" xcodes install --latest --experimental-unxip ``` +Installing an `.aea` archive requires macOS with `archiveutil extractXcodeAEA`. If Xcode Releases has an outdated link, try installing with `--data-source apple`. + You'll then be prompted to enter your Apple ID username and password. You can also provide these with the `XCODES_USERNAME` and `XCODES_PASSWORD` environment variables. After successfully authenticating, xcodes will save your Apple ID password into the keychain and will remember your Apple ID for future use. If you need to use a different Apple ID than the one that's remembered, set the `XCODES_USERNAME` environment variable. diff --git a/Sources/XcodesKit/XcodeInstaller.swift b/Sources/XcodesKit/XcodeInstaller.swift index f829d219..58742cc6 100644 --- a/Sources/XcodesKit/XcodeInstaller.swift +++ b/Sources/XcodesKit/XcodeInstaller.swift @@ -87,6 +87,7 @@ public final class XcodeInstaller: Sendable { enum InstallationStep: CustomStringConvertible { case downloading(version: String, progress: String?, willInstall: Bool) case unarchiving(experimentalUnxip: Bool) + case extractingAEA case moving(destination: String) case cleaningArchive(archiveName: String, shouldDelete: Bool) case checkingSecurity @@ -118,6 +119,8 @@ public final class XcodeInstaller: Sendable { Unarchiving Xcode (This can take a while) \(hint) """ + case .extractingAEA: + return "Extracting Xcode with archiveutil (This can take a while)" case .moving(let destination): return "Moving Xcode to \(destination)" case .cleaningArchive(let archiveName, let shouldDelete): @@ -135,7 +138,7 @@ public final class XcodeInstaller: Sendable { var stepNumber: Int { switch self { case .downloading: return 1 - case .unarchiving: return 2 + case .unarchiving, .extractingAEA: return 2 case .moving: return 3 case .cleaningArchive: return 4 case .checkingSecurity: return 5 @@ -421,7 +424,9 @@ public final class XcodeInstaller: Sendable { ) { step in switch step { case .unarchive(.unarchiving): - Current.logging.log(InstallationStep.unarchiving(experimentalUnxip: experimentalUnxip).description) + let installationStep: InstallationStep = archiveURL.pathExtension.lowercased() == "aea" + ? .extractingAEA : .unarchiving(experimentalUnxip: experimentalUnxip) + Current.logging.log(installationStep.description) case let .unarchive(.moving(destination)): Current.logging.log(InstallationStep.moving(destination: destination).description) case let .cleaningArchive(archiveName): diff --git a/Sources/xcodes/App.swift b/Sources/xcodes/App.swift index 36ee7e5e..3fcd4ee8 100644 --- a/Sources/xcodes/App.swift +++ b/Sources/xcodes/App.swift @@ -168,7 +168,7 @@ struct Xcodes: AsyncParsableCommand { let versionString = version.joined(separator: " ") let installation: XcodeInstaller.InstallationType - // Deliberately not using InstallationType.path here as it doesn't make sense to download an Xcode from a .xip that's already on disk + // A local archive is already downloaded. if latest { installation = .latest } else if latestPrerelease { @@ -214,8 +214,8 @@ struct Xcodes: AsyncParsableCommand { var version: [String] = [] @Option(name: .customLong("path"), - help: "Local path to Xcode .xip", - completion: .file(extensions: ["xip"])) + help: "Local path to an Xcode .xip or .aea archive", + completion: .file(extensions: ["xip", "aea"])) var pathString: String? @Flag(help: "Update and then install the latest release version available.") @@ -240,13 +240,13 @@ struct Xcodes: AsyncParsableCommand { @ArgumentParser.Flag(name: [.customShort("p"), .customLong("print-path")], help: "Print the path of the selected Xcode") var print: Bool = false - @Flag(help: "Use the experimental unxip functionality. May speed up unarchiving by up to 2-3x.") + @Flag(help: "Use experimental unxip for .xip archives. May speed up unarchiving by up to 2-3x.") var experimentalUnxip: Bool = false @Flag(help: "Don't ask for superuser (root) permission. Some optional steps of the installation will be skipped.") var noSuperuser: Bool = false - @Flag(help: "Completely delete Xcode .xip after installation, instead of keeping it on the user's Trash.") + @Flag(help: "Permanently delete the Xcode archive after installation instead of moving it to the Trash.") var emptyTrash: Bool = false @Option(help: "The directory to install Xcode into. Defaults to /Applications.", diff --git a/Tests/XcodesKitTests/XcodesKitTests.swift b/Tests/XcodesKitTests/XcodesKitTests.swift index 900e15ed..5dcc43ad 100644 --- a/Tests/XcodesKitTests/XcodesKitTests.swift +++ b/Tests/XcodesKitTests/XcodesKitTests.swift @@ -51,6 +51,89 @@ final class XcodesKitTests: XCTestCase { XCTAssertEqual(info.bundleIdentifier, "com.apple.dt.Xcode") } + func test_InstallDownloadedAEAUsingAria2() async throws { + let environment = ProcessInfo.processInfo.environment + guard let aria2 = environment["XCODES_ARCHIVE_SMOKE_ARIA2"].flatMap(Path.init) else { + throw XCTSkip("Set XCODES_ARCHIVE_SMOKE_ARIA2 to the aria2c executable for this smoke test.") + } + try await installFromArchiveSmokeServer( + format: "aea", downloader: .aria2(aria2), experimentalUnxip: true + ) + } + + func test_InstallDownloadedXIPUsingURLSession() async throws { + try await installFromArchiveSmokeServer( + format: "xip", downloader: .urlSession, experimentalUnxip: false + ) + } + + private func installFromArchiveSmokeServer( + format: String, downloader: Downloader, experimentalUnxip: Bool + ) async throws { + let environment = ProcessInfo.processInfo.environment + guard let server = environment["XCODES_ARCHIVE_SMOKE_URL"].flatMap(URL.init(string:)), + server.scheme == "http", server.host == "127.0.0.1", + let home = environment["XCODES_ARCHIVE_SMOKE_HOME"].flatMap(Path.init), + home == Path.environmentHome, home.basename().hasPrefix("xcodes-cli-smoke.") else { + throw XCTSkip("Set XCODES_ARCHIVE_SMOKE_URL to the loopback fixture server and HOME and XCODES_ARCHIVE_SMOKE_HOME to a temporary xcodes-cli-smoke.* directory.") + } + let url = server.appendingPathComponent("cli/Xcode_27.3_beta.\(format)") + let version = try XCTUnwrap(Version("27.3.0-Beta")) + let destination = home/"Installed-\(format)" + let archive = Path.xcodesApplicationSupport/"Xcode-\(version).\(format)" + try FileManager.default.createDirectory(at: destination.url, withIntermediateDirectories: true) + try FileManager.default.createDirectory(at: Path.xcodesApplicationSupport.url, withIntermediateDirectories: true) + XCTAssertFalse(FileManager.default.fileExists(atPath: archive.string)) + + let metadata = Data(""" + [{ + "name": "Xcode", + "version": {"number": "27.3", "release": {"beta": 1}}, + "date": {"year": 2026, "month": 11, "day": 3}, + "requires": "15.0", + "links": {"download": {"url": "\(url.absoluteString)"}} + }] + """.utf8) + Current.files = Files() + Current.network = Network( + session: URLSession(configuration: .ephemeral), + loadData: { request in + let requestURL = try XCTUnwrap(request.url) + let data = requestURL == URLRequest.xcodeReleasesData.url ? metadata : Data() + return (data, try XCTUnwrap(HTTPURLResponse(url: requestURL, statusCode: 200, httpVersion: nil, headerFields: nil))) + }, + validateSession: {} + ) + Current.shell.spctlAssess = XcodesShell().spctlAssess + Current.shell.codesignVerify = XcodesShell().codesignVerify + if url.pathExtension == "xip" { + Current.shell.unxip = XcodesShell().unxip + } else { + Current.shell.unxip = { _ in throw XcodesKitError("AEA must bypass xip") } + } + Current.shell.isatty = { false } + let list = XcodeList() + // Both fixtures report the same version, so replace any cached URL from the other format. + _ = try await list.updateAvailableXcodes(dataSource: .xcodeReleases) + let installer = XcodeInstaller(xcodeList: list, sessionService: AppleSessionService(configuration: Configuration())) + let expectedApp = destination/"Xcode-\(version.descriptionWithoutBuildMetadata).app" + let expectDamagedXIP = url.pathExtension == "xip" && environment["XCODES_ARCHIVE_SMOKE_EXPECT_DAMAGED_XIP"] == "1" + do { + let installed = try await installer.install( + .version("27.3 Beta"), dataSource: .xcodeReleases, downloader: downloader, + destination: destination, experimentalUnxip: experimentalUnxip, emptyTrash: true, noSuperuser: true + ) + XCTAssertFalse(expectDamagedXIP, "Expected the damaged XIP to be rejected") + XCTAssertEqual(installed.path, expectedApp) + XCTAssertEqual(installed.version.buildMetadataIdentifiers, ["27C5020n"]) + XCTAssertFalse(FileManager.default.fileExists(atPath: archive.string)) + } catch let error as XcodeInstaller.Error { + guard expectDamagedXIP, case let .damagedXIP(rejectedArchive) = error else { throw error } + XCTAssertEqual(rejectedArchive.path, archive.string) + XCTAssertFalse(FileManager.default.fileExists(atPath: expectedApp.string)) + } + } + func test_DownloadOrUseExistingArchive_ReturnsExistingArchive() async throws { Current.files.fileExistsAtPath = { _ in return true } let xcodeDownloadURL = LockedBox(nil)