Last active
April 19, 2019 18:28
-
-
Save JohnSundell/2d4dd5e0e1eb20f26139e372889718d9 to your computer and use it in GitHub Desktop.
A Swift script that we use at Hyper to generate licenses for used 3rd party libraries, using https://github.com/mono0926/LicensePlist
This file contains hidden or 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
| import Foundation | |
| import Files // marathon:https://github.com/JohnSundell/Files.git | |
| import ShellOut // marathon:https://github.com/JohnSundell/ShellOut.git | |
| // MARK: - Generation | |
| guard let settingsBundle = try? Folder.current.subfolder(named: "Settings.bundle") else { | |
| print("Create a Settings.bundle in Xcode, then re-run this script") | |
| exit(1) | |
| } | |
| try shellOut(to: "license-plist --output-path Settings.bundle") | |
| var rootContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
| rootContents.append("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">") | |
| rootContents.append("<plist version=\"1.0\">") | |
| rootContents.append("<dict><key>StringsTable</key><string>Root</string><key>PreferenceSpecifiers</key><array><dict><key>Type</key>") | |
| rootContents.append("<string>PSChildPaneSpecifier</string><key>Title</key><string>Licenses</string><key>File</key>") | |
| rootContents.append("<string>com.mono0926.LicensePlist</string></dict></array></dict></plist>") | |
| let rootPlist = try settingsBundle.file(named: "Root.plist") | |
| try rootPlist.write(string: rootContents) | |
| // MARK: - Localization | |
| enum Language: String { | |
| case english = "en" | |
| case norwegian = "nb" | |
| case swedish = "sv" | |
| } | |
| extension Language { | |
| var licensesString: String { | |
| switch self { | |
| case .english: | |
| return "Licenses" | |
| case .norwegian: | |
| return "Lisenser" | |
| case .swedish: | |
| return "Licenser" | |
| } | |
| } | |
| } | |
| func writeLocalizedString(forLanguage language: Language) throws { | |
| let folder = try settingsBundle.createSubfolderIfNeeded(withName: language.rawValue + ".lproj") | |
| let stringsFile = try folder.createFileIfNeeded(withName: "Root.strings") | |
| try stringsFile.write(string: "\"Licenses\" = \"\(language.licensesString)\";") | |
| } | |
| try writeLocalizedString(forLanguage: .english) | |
| try writeLocalizedString(forLanguage: .norwegian) | |
| try writeLocalizedString(forLanguage: .swedish) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think we can write it like, right?