Skip to content
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

Add select --latest and select --latest-prerelease #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion Sources/xcodes/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ struct Xcodes: AsyncParsableCommand {
completion: .custom { _ in Current.files.installedXcodes(getDirectory(possibleDirectory: nil)).sorted { $0.version < $1.version }.map { $0.version.appleDescription } })
var versionOrPath: [String] = []

@Flag(help: "Select the latest installed version of Xcode, excluding prerelease versions")
var latest = false

@Flag(help: "Select the latest installed version of Xcode, including prerelease versions")
var latestPrerelease = false

@OptionGroup
var globalDirectory: GlobalDirectoryOption

Expand All @@ -504,12 +510,33 @@ struct Xcodes: AsyncParsableCommand {

let directory = getDirectory(possibleDirectory: globalDirectory.directory)

selectXcode(shouldPrint: print, pathOrVersion: versionOrPath.joined(separator: " "), directory: directory)
var versionOrPath = versionOrPath.joined(separator: " ")
if latest || latestPrerelease {
guard let latest = latestInstalledXcode else {
Current.logging.log("No versions of Xcode are installed")
return
}

versionOrPath = latest.path.string
}

selectXcode(shouldPrint: print, pathOrVersion: versionOrPath, directory: directory)
.done { Select.exit() }
.catch { error in Select.exit(withLegibleError: error) }

RunLoop.current.run()
}

var latestInstalledXcode: InstalledXcode? {
var installedXcodes = Current.files.installedXcodes(getDirectory(possibleDirectory: nil))
.sorted { $0.version < $1.version }

if !latestPrerelease {
installedXcodes.removeAll { $0.version.isPrerelease }
}

return installedXcodes.last
}
}

struct Uninstall: ParsableCommand {
Expand Down