Repository navigation
swb: support String Catalogs (.xcstrings) on Linux #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import Foundation | ||
|
|
||
| /// Emulates `xcstringstool`, which swift-build uses to compile String Catalogs | ||
| /// (`.xcstrings`) but which only ships with Xcode. | ||
| /// | ||
| /// Supports the two invocations swift-build makes (see `XCStringsCompilerSpec`): | ||
| /// | ||
| /// - `xcstringstool compile [--dry-run] [-l <lang>]... [--force-build-all-strings] | ||
| /// --output-directory <dir> <file.xcstrings>` | ||
| /// During planning, `--dry-run` prints the absolute paths of the files that will be | ||
| /// produced, one per line; the real run writes them. | ||
| /// - `xcstringstool generate-symbols --language swift --output-directory <dir> <file.xcstrings>` | ||
| /// (with `STRING_CATALOG_GENERATE_SYMBOLS = YES`, the default for localized packages). | ||
| /// Symbol generation isn't supported yet, so this writes an empty source file, and a | ||
| /// warning if the catalog has manually managed strings (the only ones with symbols). | ||
| struct XCStringsTool: EmulatedTool { | ||
| var name: String { "xcstringstool" } | ||
|
|
||
| struct UsageError: Error, CustomStringConvertible { | ||
| var description: String | ||
| } | ||
|
|
||
| func execute( | ||
| commandLine: [String], | ||
| workingDirectory: String?, | ||
| environment: [String: String] | ||
| ) async throws -> EmulatedToolResult { | ||
| let directory = URL( | ||
| fileURLWithPath: workingDirectory ?? FileManager.default.currentDirectoryPath, | ||
| isDirectory: true | ||
| ) | ||
| var input: String? | ||
| do { | ||
| let arguments = try Arguments(Array(commandLine.dropFirst())) | ||
| input = arguments.input | ||
| let output = try run(arguments, in: directory) | ||
| return .result(status: .exit(0), stdout: Data(output.stdout.utf8), stderr: Data(output.stderr.utf8)) | ||
| } catch { | ||
| // swift-build scrapes `[<file>: ]error: <message>` lines from stderr. | ||
| let prefix = input.map { "\($0): " } ?? "xcstringstool: " | ||
| return .result(status: .exit(1), stdout: Data(), stderr: Data("\(prefix)error: \(error)\n".utf8)) | ||
| } | ||
| } | ||
|
|
||
| private func run(_ arguments: Arguments, in directory: URL) throws -> (stdout: String, stderr: String) { | ||
| let input = URL(fileURLWithPath: arguments.input, relativeTo: directory).standardizedFileURL | ||
| let outputDirectory = URL( | ||
| fileURLWithPath: arguments.outputDirectory, | ||
| isDirectory: true, | ||
| relativeTo: directory | ||
| ).standardizedFileURL | ||
| let tableName = input.deletingPathExtension().lastPathComponent | ||
|
|
||
| switch arguments.command { | ||
| case .compile: | ||
| var compiler = StringCatalogCompiler( | ||
| languages: arguments.languages.isEmpty ? nil : Set(arguments.languages), | ||
| forceBuildAllStrings: arguments.forceBuildAllStrings | ||
| ) | ||
| let outputs = try compiler.compile(catalog: Data(contentsOf: input), tableName: tableName) | ||
| let urls = outputs.map { outputDirectory.appending(path: $0.path) } | ||
| if arguments.dryRun { | ||
| return (urls.map { "\($0.path)\n" }.joined(), "") | ||
| } | ||
| for (output, url) in zip(outputs, urls) { | ||
| try FileManager.default.createDirectory( | ||
| at: url.deletingLastPathComponent(), | ||
| withIntermediateDirectories: true | ||
| ) | ||
| try output.contents.write(to: url, options: .atomic) | ||
| } | ||
| let warnings = compiler.warnings.map { "\(input.path): warning: \($0)\n" }.joined() | ||
| return ("", warnings) | ||
|
|
||
| case .generateSymbols: | ||
| let url = outputDirectory.appending(path: "GeneratedStringSymbols_\(tableName).swift") | ||
| try FileManager.default.createDirectory(at: outputDirectory, withIntermediateDirectories: true) | ||
| try Data("// String Catalog symbol generation is not supported by xtool yet.\n".utf8) | ||
| .write(to: url, options: .atomic) | ||
| // swift-build turns symbol generation on for every localized package, but only | ||
| // manually managed strings get symbols, so most catalogs lose nothing. | ||
| guard try StringCatalogCompiler.hasManualStrings(catalog: Data(contentsOf: input)) else { | ||
| return ("", "") | ||
| } | ||
| return ("", """ | ||
| \(input.path): warning: symbols for manually managed strings are not generated \ | ||
| on this platform\n | ||
| """) | ||
| } | ||
| } | ||
|
|
||
| struct Arguments { | ||
| enum Command { | ||
| case compile | ||
| case generateSymbols | ||
| } | ||
|
|
||
| var command: Command | ||
| var dryRun = false | ||
| var forceBuildAllStrings = false | ||
| var languages: [String] = [] | ||
| var outputDirectory: String | ||
| var input: String | ||
|
|
||
| init(_ arguments: [String]) throws { | ||
| guard let first = arguments.first else { | ||
| throw UsageError(description: "missing subcommand") | ||
| } | ||
| switch first { | ||
| case "compile": command = .compile | ||
| case "generate-symbols": command = .generateSymbols | ||
| default: throw UsageError(description: "unsupported subcommand '\(first)'") | ||
| } | ||
|
|
||
| var options = Options() | ||
| var remaining = arguments.dropFirst()[...] | ||
| while let argument = remaining.popFirst() { | ||
| try options.parse(argument, command: command, remaining: &remaining) | ||
| } | ||
| dryRun = options.dryRun | ||
| forceBuildAllStrings = options.forceBuildAllStrings | ||
| languages = options.languages | ||
| guard let outputDirectory = options.outputDirectory else { | ||
| throw UsageError(description: "missing --output-directory") | ||
| } | ||
| guard options.inputs.count == 1, let input = options.inputs.first else { | ||
| throw UsageError(description: "expected exactly one .xcstrings file, got \(options.inputs.count)") | ||
| } | ||
| self.outputDirectory = outputDirectory | ||
| self.input = input | ||
| } | ||
| } | ||
|
|
||
| private struct Options { | ||
| var dryRun = false | ||
| var forceBuildAllStrings = false | ||
| var languages: [String] = [] | ||
| var outputDirectory: String? | ||
| var inputs: [String] = [] | ||
|
|
||
| mutating func parse( | ||
| _ argument: String, | ||
| command: Arguments.Command, | ||
| remaining: inout ArraySlice<String> | ||
| ) throws { | ||
| func value() throws -> String { | ||
| guard let value = remaining.popFirst() else { | ||
| throw UsageError(description: "missing value for \(argument)") | ||
| } | ||
| return value | ||
| } | ||
| switch argument { | ||
| case "--dry-run": | ||
| dryRun = true | ||
| case "-l": | ||
| languages.append(try value()) | ||
| case "--language" where command == .generateSymbols: | ||
| let language = try value() | ||
| guard language == "swift" else { | ||
| throw UsageError(description: "unsupported symbol language '\(language)'") | ||
| } | ||
| case "--output-directory": | ||
| outputDirectory = try value() | ||
| case "--force-build-all-strings": | ||
| forceBuildAllStrings = true | ||
| case _ where argument.hasPrefix("-"): | ||
| throw UsageError(description: "unsupported option '\(argument)'") | ||
| default: | ||
| inputs.append(argument) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.