Skip to content

Commit

Permalink
Feature/split into framework and executable (#4)
Browse files Browse the repository at this point in the history
* Add framework

* Move functionality to framework
  • Loading branch information
pereBohigas authored Mar 21, 2024
1 parent 0dd4e68 commit 5028297
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 81 deletions.
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ let package = Package(
.executable(name: "swiftpolyglot", targets: ["SwiftPolyglot"]),
],
targets: [
.executableTarget(name: "SwiftPolyglot"),
.executableTarget(
name: "SwiftPolyglot",
dependencies: ["SwiftPolyglotCore"]
),
.target(name: "SwiftPolyglotCore")
]
)
86 changes: 6 additions & 80 deletions Sources/SwiftPolyglot/main.swift
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)
}
93 changes: 93 additions & 0 deletions Sources/SwiftPolyglotCore/SwiftPolyglot.swift
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.")
}
}
}

0 comments on commit 5028297

Please sign in to comment.