Skip to content

Commit

Permalink
Add GitHub annotations if running from GitHub Actions (#2)
Browse files Browse the repository at this point in the history
* Add warnings

* Don't include github annotations if not using gha
  • Loading branch information
roddymunro authored Mar 15, 2024
1 parent aa2ca71 commit a60ada0
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Sources/SwiftPolyglot/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ guard CommandLine.arguments.count > 1 else {
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")

Expand All @@ -19,13 +20,17 @@ func checkTranslations(in fileURL: URL, for languages: [String]) {
let jsonDict = jsonObject as? [String: Any],
let strings = jsonDict["strings"] as? [String: [String: Any]]
else {
print("Could not process file: \(fileURL.path)")
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 {
print("Warning: '\(originalString)' is not translated in any language in file: \(fileURL.absoluteString)")
logWarning(file: fileURL.path, message: "'\(originalString)' is not translated in any language in file: \(fileURL.path)")
missingTranslations = true
continue
}
Expand All @@ -36,7 +41,7 @@ func checkTranslations(in fileURL: URL, for languages: [String]) {
let state = stringUnit["state"] as? String, state == "translated"
{
} else {
print("Warning: '\(originalString)' is missing or not translated in \(lang) in file: \(fileURL.absoluteString)")
logWarning(file: fileURL.path, message: "'\(originalString)' is missing or not translated in \(lang) in file: \(fileURL.path)")
missingTranslations = true
}
}
Expand All @@ -54,6 +59,18 @@ func searchDirectory(_ dirPath: String) {
}
}

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 {
Expand Down

0 comments on commit a60ada0

Please sign in to comment.