-
Notifications
You must be signed in to change notification settings - Fork 50
Add an option to self-update to provide the swiftly version to update #370
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
base: main
Are you sure you want to change the base?
Changes from all commits
fee2de1
c827e6a
1d92457
f1542a6
eb3a02d
0a5cfd1
3d93a51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,23 @@ import SwiftlyWebsiteAPI | |
@preconcurrency import TSCBasic | ||
import TSCUtility | ||
|
||
extension SwiftlyVersion: ExpressibleByArgument { | ||
public init?(argument: String) { | ||
try? self.init(parsing: argument) | ||
} | ||
} | ||
|
||
struct SelfUpdate: SwiftlyCommand { | ||
public static let configuration = CommandConfiguration( | ||
abstract: "Update the version of swiftly itself." | ||
) | ||
|
||
@OptionGroup var root: GlobalOptions | ||
|
||
@Option(help: .hidden) var toVersion: SwiftlyVersion | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case root | ||
case root, toVersion | ||
} | ||
|
||
mutating func run() async throws { | ||
|
@@ -31,50 +39,80 @@ struct SelfUpdate: SwiftlyCommand { | |
) | ||
} | ||
|
||
let _ = try await Self.execute(ctx, verbose: self.root.verbose) | ||
let _ = try await Self.execute(ctx, verbose: self.root.verbose, version: self.toVersion) | ||
} | ||
|
||
public static func execute(_ ctx: SwiftlyCoreContext, verbose: Bool) async throws | ||
public static func execute(_ ctx: SwiftlyCoreContext, verbose: Bool, version swiftlyVersion: SwiftlyVersion?) async throws | ||
-> SwiftlyVersion | ||
{ | ||
await ctx.print("Checking for swiftly updates...") | ||
var downloadURL: Foundation.URL? | ||
var version: SwiftlyVersion? = swiftlyVersion | ||
|
||
let swiftlyRelease = try await ctx.httpClient.getCurrentSwiftlyRelease() | ||
if let version { | ||
#if os(macOS) | ||
downloadURL = URL(string: "https://download.swift.org/swiftly/darwin/swiftly-\(version).pkg") | ||
#endif | ||
|
||
guard try swiftlyRelease.swiftlyVersion > SwiftlyCore.version else { | ||
await ctx.print("Already up to date.") | ||
return SwiftlyCore.version | ||
#if os(Linux) | ||
#if arch(x86_64) | ||
downloadURL = URL(string: "https://download.swift.org/swiftly/linux/swiftly-\(version)-x86_64.tar.gz") | ||
#elseif arch(arm64) | ||
downloadURL = URL(string: "https://download.swift.org/swiftly/linux/swiftly-\(version)-aarch64.tar.gz") | ||
#else | ||
fatalError("Unsupported architecture") | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this throw an error if we somehow get here and don't know what we're downloading? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added fatal errors in the unsupported cases. |
||
#else | ||
fatalError("Unsupported OS") | ||
#endif | ||
|
||
guard version > SwiftlyCore.version else { | ||
await ctx.print("Self-update does not support downgrading to an older version or re-installing the current version. Current version is \(SwiftlyCore.version) and requested version is \(version).") | ||
return SwiftlyCore.version | ||
} | ||
|
||
await ctx.print("Self-update requested to swiftly version \(version)") | ||
} | ||
|
||
var downloadURL: Foundation.URL? | ||
for platform in swiftlyRelease.platforms { | ||
#if os(macOS) | ||
guard platform.isDarwin else { | ||
continue | ||
if downloadURL == nil { | ||
await ctx.print("Checking for swiftly updates...") | ||
|
||
let swiftlyRelease = try await ctx.httpClient.getCurrentSwiftlyRelease() | ||
|
||
guard try swiftlyRelease.swiftlyVersion > SwiftlyCore.version else { | ||
await ctx.print("Already up to date.") | ||
return SwiftlyCore.version | ||
} | ||
for platform in swiftlyRelease.platforms { | ||
#if os(macOS) | ||
guard platform.isDarwin else { | ||
continue | ||
} | ||
#elseif os(Linux) | ||
guard platform.isLinux else { | ||
continue | ||
} | ||
guard platform.isLinux else { | ||
continue | ||
} | ||
#endif | ||
|
||
#if arch(x86_64) | ||
downloadURL = try platform.x86_64URL | ||
downloadURL = try platform.x86_64URL | ||
#elseif arch(arm64) | ||
downloadURL = try platform.arm64URL | ||
downloadURL = try platform.arm64URL | ||
#endif | ||
} | ||
} | ||
|
||
guard let downloadURL else { | ||
throw SwiftlyError( | ||
message: | ||
"The newest release of swiftly is incompatible with your current OS and/or processor architecture." | ||
) | ||
} | ||
guard let downloadURL else { | ||
throw SwiftlyError( | ||
message: | ||
"The newest release of swiftly is incompatible with your current OS and/or processor architecture." | ||
) | ||
} | ||
|
||
version = try swiftlyRelease.swiftlyVersion | ||
|
||
let version = try swiftlyRelease.swiftlyVersion | ||
await ctx.print("A new version of swiftly is available: \(version!)") | ||
} | ||
|
||
await ctx.print("A new version is available: \(version)") | ||
guard let version, let downloadURL else { fatalError() } | ||
|
||
let tmpFile = fs.mktemp() | ||
try await fs.create(file: tmpFile, contents: nil) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space added here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the documentation generator is homegrown, and naive. This seems minor enough that I wanted to keep the change to another PR, or possibly adopt the revision of it that's now available in the swift argument parser project.