Skip to content

Instantly share code, notes, and snippets.

@monoqlo
Last active March 3, 2019 00:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monoqlo/848caf689e74378acac8a3dd30320f41 to your computer and use it in GitHub Desktop.
Save monoqlo/848caf689e74378acac8a3dd30320f41 to your computer and use it in GitHub Desktop.
Download R.swift binary for your Cartfile.resolved
#!/usr/bin/swift
import Foundation
extension String {
func index(from: Int) -> Index {
return index(startIndex, offsetBy: from)
}
func substring(from: Int) -> String {
let fromIndex = index(from: from)
return String(self[fromIndex..<endIndex])
}
var nsString: NSString {
return self as NSString
}
}
let cartfileResolvedName = "Cartfile.resolved"
guard let rawCartfileResolvedPath = CommandLine.arguments.suffix(from: 1).first,
rawCartfileResolvedPath == cartfileResolvedName || rawCartfileResolvedPath.hasSuffix("/" + cartfileResolvedName) else {
print("Error: Cartfile.resolved path is not specified. \(CommandLine.arguments)")
print("Usage: \(#file) YOUR/DIRECTORY/Cartfile.resolved")
exit(-1)
}
let cartfileResolvedPath: String
let homePathPrefix = "~/"
let currentPathPrefix = "./"
let relativePathPrefix = "../"
if rawCartfileResolvedPath == cartfileResolvedName {
cartfileResolvedPath = FileManager.default.currentDirectoryPath.nsString.appendingPathComponent(cartfileResolvedName)
} else if rawCartfileResolvedPath.hasPrefix(homePathPrefix) {
let path = rawCartfileResolvedPath.substring(from: homePathPrefix.count)
cartfileResolvedPath = NSHomeDirectory().nsString.appendingPathComponent(path)
} else if rawCartfileResolvedPath.hasPrefix(currentPathPrefix) || rawCartfileResolvedPath.hasPrefix(relativePathPrefix) {
cartfileResolvedPath = FileManager.default.currentDirectoryPath.nsString.appendingPathComponent(rawCartfileResolvedPath)
} else {
cartfileResolvedPath = rawCartfileResolvedPath
}
let cartfileResolvedURL = URL(fileURLWithPath: cartfileResolvedPath)
let cartfileResolvedDirectoryPath = cartfileResolvedPath.nsString.deletingLastPathComponent
let cartfileResolvedDirectoryURL = URL(fileURLWithPath: cartfileResolvedDirectoryPath)
print("R.swift installer started!")
guard let cartfileResolvedString = try? String(contentsOf: cartfileResolvedURL, encoding: .utf8) else {
print("Error: No Cartfile.resolved at \(cartfileResolvedURL).")
exit(-1)
}
let pattern = "github \"mac-cain13/R.swift.Library\" \"v(\\d.\\d.\\d)\""
guard let regexp = try? NSRegularExpression(pattern: pattern, options: []) else {
print("Error: NSRegularExpression can't be created by this pattern \(pattern).")
exit(-1)
}
let matches = regexp.matches(in: cartfileResolvedString, options: [], range: NSRange(location: 0, length: cartfileResolvedString.count))
guard let match = matches.first else {
print("Error: R.swift.Library is not installed by Carthage.")
exit(-1)
}
let version = cartfileResolvedString.nsString.substring(with: match.range(at: 1))
let downloadURL = URL(string: "https://github.com/mac-cain13/R.swift/releases/download/v\(version)/rswift-\(version).zip")!
print("Downloading R.swift...")
guard let data = try? Data(contentsOf: downloadURL) else {
print("Error: R.swift can't be downloaded from \(downloadURL).")
exit(-1)
}
let directoryName = "rswift"
let directoryPath = cartfileResolvedDirectoryPath.nsString.appendingPathComponent(directoryName)
guard let _ = try? FileManager.default.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil) else {
print("Error: rswift Directory can't be created to \(directoryPath).")
exit(-1)
}
let fileName = "rswift-\(version).zip"
let storeURL = cartfileResolvedDirectoryURL.appendingPathComponent("\(directoryName)/\(fileName)")
guard let _ = try? data.write(to: storeURL) else {
print("Error: R.swift.Library can't be stored to \(storeURL).")
exit(-1)
}
let targetFileURL = directoryPath.nsString.appendingPathComponent(fileName)
let process = Process()
process.launchPath = "/usr/bin/unzip"
process.arguments = ["-o", "\(targetFileURL)", "-d", "\(directoryPath)"]
process.launch()
process.waitUntilExit()
try? FileManager.default.removeItem(atPath: targetFileURL)
print("R.swift installation completed. rswift is located at \(directoryPath).")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment