Last active
May 30, 2019 05:46
-
-
Save bill350/9e779b0fc57789562dc33c045b4c2d54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Foundation | |
var baseLanguage = "fr" | |
var managedLanguages = ["fr": "π«π·"] | |
let languageFolderPath = "AppFrameworks/Localization/Localization/Resources" | |
guard CommandLine.arguments.count > 2 else { | |
print("β - No argument received, please put the Lokalise [1:Project ID] and your [2:API Token] as args") | |
exit(1) | |
} | |
let lokaliseProjectId = CommandLine.arguments[1] | |
let lokaliseApiToken = CommandLine.arguments[2] | |
print("π - Using ProjectID: \(lokaliseProjectId) || APIToken: \(lokaliseApiToken)") | |
//print("π - Using ProjectID: [Censored] || APIToken: [Censored]") | |
// Use it for asynchronous code | |
var sema = DispatchSemaphore(value: 0) | |
// MARK:- Scripting | |
func shell(launchPath: String, arguments: [String]) -> String | |
{ | |
let task = Process() | |
task.launchPath = launchPath | |
task.arguments = arguments | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output = String(data: data, encoding: String.Encoding.utf8)! | |
if output.count > 0 { | |
let lastIndex = output.index(before: output.endIndex) | |
return String(output[output.startIndex ..< lastIndex]) | |
} | |
return output | |
} | |
@discardableResult | |
func bash(command: String, arguments: [String]) -> String { | |
let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ]) | |
return shell(launchPath: whichPathForCommand, arguments: arguments) | |
} | |
func unzip(archive archiveName: String) { | |
print("π¦ - Unzip archive : \(archiveName)") | |
bash(command: "unzip", arguments: [archiveName]) | |
} | |
func copyLokalizableStrings() { | |
print("π―ββοΈ - Copy .strings / .stringsdict files") | |
managedLanguages.forEach { (key, value) in | |
if key == baseLanguage { | |
print("Copying Localizables for \(value) [Base language]") | |
bash(command: "cp", arguments: ["./\(key).lproj/Localizable.strings", "../\(languageFolderPath)/Base.lproj/"]) | |
bash(command: "cp", arguments: ["./\(key).lproj/Localizable.stringsdict", "../\(languageFolderPath)/Base.lproj/"]) | |
} else { | |
print("Copying Localizables for \(value)") | |
bash(command: "cp", arguments: ["./\(key).lproj/Localizable.strings", "../\(languageFolderPath)/\(key).lproj/"]) | |
bash(command: "cp", arguments: ["./\(key).lproj/Localizable.stringsdict", "../\(languageFolderPath)/\(key).lproj/"]) | |
} | |
} | |
} | |
func clean(archive archiveName: String) { | |
print("π§Ή - Cleaning files & folders") | |
bash(command: "rm", arguments: ["\(archiveName).zip"]) | |
managedLanguages.forEach({ (key, value) in | |
bash(command: "rm", arguments: ["-rf", "\(key).lproj"]) | |
}) | |
} | |
func writeData(_ data: Data?, archiveName: String) { | |
print("π - Writing Lokalise.co zip file") | |
guard let data = data else { | |
print("π€ - No data to write") | |
return | |
} | |
do { | |
let current = FileManager.default.currentDirectoryPath | |
let url = URL(string: "file://\(current)")! | |
let fileURL = url.appendingPathComponent("\(archiveName).zip") | |
try data.write(to: fileURL) | |
} catch { | |
print("β - Cannot write file : \(error)") | |
} | |
} | |
// MARK: Lokalise downloading and copying | |
// Lokalise Constants | |
let LokalisePOSTUrl = "https://api.lokalise.co/api2/projects/\(lokaliseProjectId)/files/download" | |
let LokalizableZipName = "Mobile-Localizable" | |
struct LokaliseRequestBody: Codable { | |
let format = "ios_sdk" | |
let original_filenames = true | |
} | |
struct LokaliseFileDownloadResponse: Codable { | |
let project_id: String | |
let bundle_url: String | |
} | |
let configuration = URLSessionConfiguration.default | |
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData | |
let urlSession = URLSession(configuration: configuration) | |
var lokaliseRequest = URLRequest(url: URL(string: LokalisePOSTUrl)!) | |
lokaliseRequest.httpMethod = "POST" | |
lokaliseRequest.httpBody = try { | |
let jsonEncoder = JSONEncoder() | |
return try jsonEncoder.encode(LokaliseRequestBody()) | |
}() | |
lokaliseRequest.allHTTPHeaderFields = { | |
let headers = ["x-api-token" : lokaliseApiToken, | |
"Content-Type": "application/json"] | |
return headers | |
}() | |
func downloadLastLokaliseStrings() { | |
print("β¬οΈ - Downloading last localizable strings") | |
let lokaliseTask = urlSession.dataTask(with: lokaliseRequest) { (data, response, error) in | |
if let error = error { | |
print("β - Cannot retrieve file : \(error)") | |
sema.signal() | |
} else { | |
guard let data = data else { | |
print("β - No data received") | |
sema.signal() | |
return | |
} | |
do { | |
let lokaliseResponse = try JSONDecoder().decode(LokaliseFileDownloadResponse.self, from: data) | |
let url = URL(string: lokaliseResponse.bundle_url)! | |
let downloadDataTask = urlSession.dataTask(with: url, completionHandler: { (data, response, error) in | |
if let error = error { | |
print("β - Cannot retrieve file :\(error)") | |
sema.signal() | |
} else { | |
writeData(data, archiveName: LokalizableZipName) | |
unzip(archive: LokalizableZipName) | |
copyLokalizableStrings() | |
clean(archive: LokalizableZipName) | |
print("β - Successfully updated Localizable.strings, please build the project for SwiftGen generation") | |
sema.signal() | |
} | |
}) | |
downloadDataTask.resume() | |
} catch { | |
print("β - Cannot retrieve file :\(error)") | |
sema.signal() | |
} | |
} | |
} | |
lokaliseTask.resume() | |
} | |
downloadLastLokaliseStrings() | |
sema.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment