Skip to content

Model remaining commands #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 2, 2025
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
27 changes: 12 additions & 15 deletions Sources/LinuxPlatform/Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,11 @@ public struct Linux: Platform {
try await fs.withTemporary(files: tmpFile) {
try await ctx.httpClient.getGpgKeys().download(to: tmpFile)
if let mockedHomeDir = ctx.mockedHomeDir {
try self.runProgram(
"gpg", "--import", "\(tmpFile)", quiet: true,
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
)
var env = ProcessInfo.processInfo.environment
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
try await sys.gpg()._import(keys: tmpFile).run(self, env: env, quiet: true)
} else {
try self.runProgram("gpg", "--import", "\(tmpFile)", quiet: true)
try await sys.gpg()._import(keys: tmpFile).run(self, quiet: true)
}
}
}
Expand Down Expand Up @@ -417,12 +416,11 @@ public struct Linux: Platform {
await ctx.print("Verifying toolchain signature...")
do {
if let mockedHomeDir = ctx.mockedHomeDir {
try self.runProgram(
"gpg", "--verify", "\(sigFile)", "\(archive)", quiet: false,
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
)
var env = ProcessInfo.processInfo.environment
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, env: env, quiet: false)
} else {
try self.runProgram("gpg", "--verify", "\(sigFile)", "\(archive)", quiet: !verbose)
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, quiet: !verbose)
}
} catch {
throw SwiftlyError(message: "Signature verification failed: \(error).")
Expand All @@ -447,12 +445,11 @@ public struct Linux: Platform {
await ctx.print("Verifying swiftly signature...")
do {
if let mockedHomeDir = ctx.mockedHomeDir {
try self.runProgram(
"gpg", "--verify", "\(sigFile)", "\(archive)", quiet: false,
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
)
var env = ProcessInfo.processInfo.environment
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, env: env, quiet: false)
} else {
try self.runProgram("gpg", "--verify", "\(sigFile)", "\(archive)", quiet: !verbose)
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, quiet: !verbose)
}
} catch {
throw SwiftlyError(message: "Signature verification failed: \(error).")
Expand Down
27 changes: 13 additions & 14 deletions Sources/MacOSPlatform/MacOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ public struct MacOS: Platform {
if toolchainsDir == self.defaultToolchainsDirectory {
// If the toolchains go into the default user location then we use the installer to install them
await ctx.print("Installing package in user home directory...")
try runProgram(
"installer", "-verbose", "-pkg", "\(tmpFile)", "-target", "CurrentUserHomeDirectory",
quiet: !verbose
)

try await sys.installer(.verbose, pkg: tmpFile, target: "CurrentUserHomeDirectory").run(self, quiet: !verbose)
} else {
// Otherwise, we extract the pkg into the requested toolchains directory.
await ctx.print("Expanding pkg...")
Expand All @@ -86,7 +84,7 @@ public struct MacOS: Platform {

await ctx.print("Checking package signature...")
do {
try runProgram("pkgutil", "--check-signature", "\(tmpFile)", quiet: !verbose)
try await sys.pkgutil().checkSignature(pkgPath: tmpFile).run(self, quiet: !verbose)
} catch {
// If this is not a test that uses mocked toolchains then we must throw this error and abort installation
guard ctx.mockedHomeDir != nil else {
Expand All @@ -96,7 +94,7 @@ public struct MacOS: Platform {
// We permit the signature verification to fail during testing
await ctx.print("Signature verification failed, which is allowable during testing with mocked toolchains")
}
try runProgram("pkgutil", "--verbose", "--expand", "\(tmpFile)", "\(tmpDir)", quiet: !verbose)
try await sys.pkgutil(.verbose).expand(pkgPath: tmpFile, dirPath: tmpDir).run(self, quiet: !verbose)

// There's a slight difference in the location of the special Payload file between official swift packages
// and the ones that are mocked here in the test framework.
Expand All @@ -106,7 +104,7 @@ public struct MacOS: Platform {
}

await ctx.print("Untarring pkg Payload...")
try runProgram("tar", "-C", "\(toolchainDir)", "-xvf", "\(payload)", quiet: !verbose)
try await sys.tar(.directory(toolchainDir)).extract(.verbose, .archive(payload)).run(self, quiet: !verbose)
}
}

Expand All @@ -119,16 +117,19 @@ public struct MacOS: Platform {

if ctx.mockedHomeDir == nil {
await ctx.print("Extracting the swiftly package...")
try runProgram("installer", "-pkg", "\(archive)", "-target", "CurrentUserHomeDirectory")
try? runProgram("pkgutil", "--volume", "\(userHomeDir)", "--forget", "org.swift.swiftly")
try await sys.installer(
pkg: archive,
target: "CurrentUserHomeDirectory"
)
try? await sys.pkgutil(.volume(userHomeDir)).forget(packageId: "org.swift.swiftly").run(self)
} else {
let installDir = userHomeDir / ".swiftly"
try await fs.mkdir(.parents, atPath: installDir)

// In the case of a mock for testing purposes we won't use the installer, perferring a manual process because
// the installer will not install to an arbitrary path, only a volume or user home directory.
let tmpDir = fs.mktemp()
try runProgram("pkgutil", "--expand", "\(archive)", "\(tmpDir)")
try await sys.pkgutil().expand(pkgPath: archive, dirPath: tmpDir).run(self)

// There's a slight difference in the location of the special Payload file between official swift packages
// and the ones that are mocked here in the test framework.
Expand All @@ -138,7 +139,7 @@ public struct MacOS: Platform {
}

await ctx.print("Extracting the swiftly package into \(installDir)...")
try runProgram("tar", "-C", "\(installDir)", "-xvf", "\(payload)", quiet: false)
try await sys.tar(.directory(installDir)).extract(.verbose, .archive(payload)).run(self, quiet: false)
}

try self.runProgram((userHomeDir / ".swiftly/bin/swiftly").string, "init")
Expand All @@ -161,9 +162,7 @@ public struct MacOS: Platform {

try await fs.remove(atPath: toolchainDir)

try? runProgram(
"pkgutil", "--volume", "\(fs.home)", "--forget", pkgInfo.CFBundleIdentifier, quiet: !verbose
)
try? await sys.pkgutil(.volume(fs.home)).forget(packageId: pkgInfo.CFBundleIdentifier).run(self, quiet: !verbose)
}

public func getExecutableName() -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Swiftly/Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ struct Install: SwiftlyCommand {

await ctx.print("Installing \(version)")

let tmpFile = fs.mktemp()
let tmpFile = fs.mktemp(ext: ".\(Swiftly.currentPlatform.toolchainFileExtension)")
try await fs.create(file: tmpFile, contents: nil)
return try await fs.withTemporary(files: tmpFile) {
var platformString = config.platform.name
Expand Down
Loading