Skip to content

Instantly share code, notes, and snippets.

@DominikBucher12
Last active October 15, 2018 14:05
Show Gist options
  • Save DominikBucher12/ce73d92c0f6411ca6ed42fd372d89667 to your computer and use it in GitHub Desktop.
Save DominikBucher12/ce73d92c0f6411ca6ed42fd372d89667 to your computer and use it in GitHub Desktop.
Gets easily version of given gem with Swifty and decodable.
#!/usr/bin/swift
// Use this script to get name and version of gem passed as first argument
//
// Example usage: `swift GetFastlaneVersion.swift fastlane 1.0.2`
//
// Arguments: - `gem` - The name of gem you want to inspect.
// - `version` - The version you want to compare against the repo
//
// Because whole script runs in one thread, we need to use Semaphore to hold the thread until
// It finishes the asynch URLRequest. No mutex sorry 😔
//
import Foundation
// MARK: Safe Bounds of array:
extension Array {
func at(index: Int) -> Element? {
guard (0...self.count - 1).contains(index) else { return nil }
return self[index]
}
}
// MARK: Gem struct
struct Gem: Decodable {
let name: String
let version: String
}
// MARK: GLobal properties
let semaphore = DispatchSemaphore(value: 0)
let gemName = CommandLine.arguments.at(index: 1)
let currentFastlaneVersion = CommandLine.arguments.at(index: 2)
// MARK: Implementation
if CommandLine.arguments.count > 1,
let gemName = gemName,
let currentFastlaneVersion = currentFastlaneVersion {
let url = URL(string: "https://rubygems.org/api/v1/gems/\(gemName).json")!
let req = URLSession.shared.dataTask(with: url) { data, _, error in
print("Getting request...")
if let error = error {
print("❌ An Error ocurred: \(error)")
semaphore.signal()
return
}
guard let data = data,
let gem = try? JSONDecoder().decode(Gem.self, from: data)
else { print("❌ Couldn't decode gem info."); semaphore.signal(); return }
if gem.version == currentFastlaneVersion {
print(" 🚀 You are all setup, your Fastlane is up to date")
} else {
print("❗️Your actual Fastlane installation is: \(currentFastlaneVersion), the most recent Fastlane version is \(gem.version)")
print("❗️Please consider update")
}
semaphore.signal()
}
req.resume()
} else {
print("❌ Please specify the gem name and current version as arguments.")
}
let timeout = DispatchTime.now() + DispatchTimeInterval.milliseconds(Int(60.0 * 1000.0)) // Overkill 😅
_ = semaphore.wait(timeout: timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment