Skip to content

Instantly share code, notes, and snippets.

@finestructure
Last active March 26, 2024 10:39
Show Gist options
  • Save finestructure/f4ce789d35bcde4c792f27f6e3776228 to your computer and use it in GitHub Desktop.
Save finestructure/f4ce789d35bcde4c792f27f6e3776228 to your computer and use it in GitHub Desktop.
List Swift toolchain IDs
#!/usr/bin/env swift
import Foundation
let toolchainsDir = URL(fileURLWithPath: "/Library/Developer/Toolchains")
struct ToolchainInfoPlist: Codable {
var bundleIdentifier: String
var createdDate: Date
var displayName: String
enum CodingKeys: String, CodingKey {
case bundleIdentifier = "CFBundleIdentifier"
case createdDate = "CreatedDate"
case displayName = "DisplayName"
}
}
func readPlist(url: URL) throws -> ToolchainInfoPlist {
try PropertyListDecoder().decode(ToolchainInfoPlist.self, from: Data(contentsOf: url))
}
func getToolchainInfo(toolchainsDir: URL) throws -> [ToolchainInfoPlist] {
try FileManager.default.contentsOfDirectory(atPath: toolchainsDir.path)
.map { toolchainsDir.appendingPathComponent($0) }
.filter(\.hasDirectoryPath)
.map { $0.appendingPathComponent("Info.plist") }
.map(readPlist(url:))
}
try getToolchainInfo(toolchainsDir: toolchainsDir)
.sorted(by: { $0.bundleIdentifier < $1.bundleIdentifier })
.forEach {
print("Name:", $0.displayName)
print("xcrun --toolchain \($0.bundleIdentifier) swift --version")
print()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment