Skip to content

Instantly share code, notes, and snippets.

@shotastage
Created February 8, 2024 08:48
Show Gist options
  • Save shotastage/727ee599d952d4ac53ccc913ce242c98 to your computer and use it in GitHub Desktop.
Save shotastage/727ee599d952d4ac53ccc913ce242c98 to your computer and use it in GitHub Desktop.
Easy Pkl configuration language installer for Mac.
#!/usr/bin/env swift
import Foundation
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
func pklSetup() {
shell("curl", "-L", "-o", "pkl", "https://github.com/apple/pkl/releases/download/0.25.1/pkl-macos-aarch64")
shell("chmod", "+x", "pkl")
let pklToolchainDir = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".pkl_toolchain").path
if !FileManager.default.fileExists(atPath: pklToolchainDir) {
do {
try FileManager.default.createDirectory(atPath: pklToolchainDir, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Failed to create directory: \(error.localizedDescription)")
}
}
shell("mv", "pkl", "\(pklToolchainDir)/pkl")
updatePath(pklToolchainDir)
}
func updatePath(_ toolchainDir: String) {
let profilePath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".zshenv").path
if FileManager.default.fileExists(atPath: profilePath) {
let exportCmd = "\nexport PATH=\"\(toolchainDir):$PATH\"\n"
do {
let fileHandle = try FileHandle(forWritingTo: URL(fileURLWithPath: profilePath))
fileHandle.seekToEndOfFile()
if let exportData = exportCmd.data(using: .utf8) {
fileHandle.write(exportData)
}
fileHandle.closeFile()
print("PATH updated in .zshenv")
} catch {
print("Failed to update .zshenv: \(error.localizedDescription)")
}
} else {
print(".zshenv not found, unable to update PATH")
}
}
#if os(macOS)
pklSetup()
#else
print("This script is only for macOS")
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment