-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/split into framework and executable (#4)
* Add framework * Move functionality to framework
- Loading branch information
1 parent
0dd4e68
commit 5028297
Showing
3 changed files
with
104 additions
and
81 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,9 @@ | ||
import Foundation | ||
import SwiftPolyglotCore | ||
|
||
guard CommandLine.arguments.count > 1 else { | ||
print("Usage: script.swift <language codes> [--errorOnMissing]") | ||
exit(1) | ||
} | ||
|
||
let isRunningFromGitHubActions = ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true" | ||
let languages = CommandLine.arguments[1].split(separator: ",").map(String.init) | ||
let errorOnMissing = CommandLine.arguments.contains("--errorOnMissing") | ||
|
||
let fileManager = FileManager.default | ||
let currentDirectoryPath = fileManager.currentDirectoryPath | ||
|
||
var missingTranslations = false | ||
|
||
func checkTranslations(in fileURL: URL, for languages: [String]) { | ||
guard let data = try? Data(contentsOf: fileURL), | ||
let jsonObject = try? JSONSerialization.jsonObject(with: data), | ||
let jsonDict = jsonObject as? [String: Any], | ||
let strings = jsonDict["strings"] as? [String: [String: Any]] | ||
else { | ||
if isRunningFromGitHubActions { | ||
print("::warning file=\(fileURL.path)::Could not process file at path: \(fileURL.path)") | ||
} else { | ||
print("Could not process file at path: \(fileURL.path)") | ||
} | ||
return | ||
} | ||
|
||
for (originalString, translations) in strings { | ||
guard let localizations = translations["localizations"] as? [String: [String: Any]] else { | ||
logWarning(file: fileURL.path, message: "'\(originalString)' is not translated in any language in file: \(fileURL.path)") | ||
missingTranslations = true | ||
continue | ||
} | ||
|
||
for lang in languages { | ||
if let langDict = localizations[lang], | ||
let stringUnit = langDict["stringUnit"] as? [String: Any], | ||
let state = stringUnit["state"] as? String, state == "translated" | ||
{ | ||
} else { | ||
logWarning(file: fileURL.path, message: "'\(originalString)' is missing or not translated in \(lang) in file: \(fileURL.path)") | ||
missingTranslations = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func searchDirectory(_ dirPath: String) { | ||
if let enumerator = fileManager.enumerator(atPath: dirPath) { | ||
for case let file as String in enumerator { | ||
if file.hasSuffix(".xcstrings") { | ||
let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(file) | ||
checkTranslations(in: fileURL, for: languages) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func logWarning(file: String, message: String) { | ||
if isRunningFromGitHubActions { | ||
if errorOnMissing { | ||
print("::error file=\(file)::\(message)") | ||
} else { | ||
print("::warning file=\(file)::\(message)") | ||
} | ||
} else { | ||
print(message) | ||
} | ||
} | ||
|
||
searchDirectory(currentDirectoryPath) | ||
let swiftPolyglot: SwiftPolyglot = .init(arguments: CommandLine.arguments) | ||
|
||
if missingTranslations, errorOnMissing { | ||
print("Error: One or more translations are missing.") | ||
exit(1) | ||
} else if missingTranslations { | ||
print("Completed with missing translations.") | ||
} else { | ||
print("All translations are present.") | ||
do { | ||
try swiftPolyglot.run() | ||
} catch { | ||
print(error) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import Foundation | ||
|
||
public struct SwiftPolyglot { | ||
private let arguments: [String] | ||
|
||
public init(arguments: [String]) { | ||
self.arguments = arguments | ||
} | ||
|
||
public func run() throws { | ||
guard CommandLine.arguments.count > 1 else { | ||
print("Usage: script.swift <language codes> [--errorOnMissing]") | ||
exit(1) | ||
} | ||
|
||
let isRunningFromGitHubActions = ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true" | ||
let languages = CommandLine.arguments[1].split(separator: ",").map(String.init) | ||
let errorOnMissing = CommandLine.arguments.contains("--errorOnMissing") | ||
|
||
let fileManager = FileManager.default | ||
let currentDirectoryPath = fileManager.currentDirectoryPath | ||
|
||
var missingTranslations = false | ||
|
||
func checkTranslations(in fileURL: URL, for languages: [String]) { | ||
guard let data = try? Data(contentsOf: fileURL), | ||
let jsonObject = try? JSONSerialization.jsonObject(with: data), | ||
let jsonDict = jsonObject as? [String: Any], | ||
let strings = jsonDict["strings"] as? [String: [String: Any]] | ||
else { | ||
if isRunningFromGitHubActions { | ||
print("::warning file=\(fileURL.path)::Could not process file at path: \(fileURL.path)") | ||
} else { | ||
print("Could not process file at path: \(fileURL.path)") | ||
} | ||
return | ||
} | ||
|
||
for (originalString, translations) in strings { | ||
guard let localizations = translations["localizations"] as? [String: [String: Any]] else { | ||
logWarning(file: fileURL.path, message: "'\(originalString)' is not translated in any language in file: \(fileURL.path)") | ||
missingTranslations = true | ||
continue | ||
} | ||
|
||
for lang in languages { | ||
if let langDict = localizations[lang], | ||
let stringUnit = langDict["stringUnit"] as? [String: Any], | ||
let state = stringUnit["state"] as? String, state == "translated" | ||
{ | ||
} else { | ||
logWarning(file: fileURL.path, message: "'\(originalString)' is missing or not translated in \(lang) in file: \(fileURL.path)") | ||
missingTranslations = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func searchDirectory(_ dirPath: String) { | ||
if let enumerator = fileManager.enumerator(atPath: dirPath) { | ||
for case let file as String in enumerator { | ||
if file.hasSuffix(".xcstrings") { | ||
let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(file) | ||
checkTranslations(in: fileURL, for: languages) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func logWarning(file: String, message: String) { | ||
if isRunningFromGitHubActions { | ||
if errorOnMissing { | ||
print("::error file=\(file)::\(message)") | ||
} else { | ||
print("::warning file=\(file)::\(message)") | ||
} | ||
} else { | ||
print(message) | ||
} | ||
} | ||
|
||
searchDirectory(currentDirectoryPath) | ||
|
||
if missingTranslations, errorOnMissing { | ||
print("Error: One or more translations are missing.") | ||
exit(1) | ||
} else if missingTranslations { | ||
print("Completed with missing translations.") | ||
} else { | ||
print("All translations are present.") | ||
} | ||
} | ||
} |