Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import Foundationimport PackagePlugin
@mainstruct CompileStrings: BuildToolPlugin { func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { let resources = context.package.directoryURL.appending(path: "Sources/\(target.name)/Resources") let catalogs = try FileManager.default.contentsOfDirectory(at: resources, includingPropertiesForKeys: nil) .filter { $0.pathExtension == "xcstrings" } var languages: Set<String> = [] for url in catalogs { let catalog = try JSONDecoder().decode(Catalog.self, from: Data(contentsOf: url)) languages.insert(catalog.sourceLanguage) for entry in catalog.strings.values { languages.formUnion((entry.localizations ?? [:]).keys) } } let nativeBuild = ProcessInfo.processInfo.environment["PARCEL_BUNDLE_BUILD"] == "1" let developerTool: String if nativeBuild { guard let binary = ProcessInfo.processInfo.environment["PARCEL_DEV_BINARY"], FileManager.default.isExecutableFile(atPath: binary) else { throw MissingDeveloperTool() } developerTool = binary } else { developerTool = try context.tool(named: "parcel-dev").url.path } let environment = ["PARCEL_DEV_BINARY": developerTool] return try languages.sorted().map { language in let output = context.pluginWorkDirectoryURL.appending(path: language) let executable = context.package.directoryURL.appending(path: "Scripts/generate-config-strings.sh") let arguments = ["--language", language, resources.path, output.path] // SwiftBuild skips prebuild commands, so it needs resources staged during planning. if nativeBuild { let compiler = Process() compiler.executableURL = executable compiler.arguments = arguments compiler.environment = ProcessInfo.processInfo.environment.merging(environment) { _, value in value } try compiler.run() compiler.waitUntilExit() guard compiler.terminationReason == .exit, compiler.terminationStatus == 0 else { throw CompilationFailed(target: target.name, language: language) } } return .prebuildCommand( displayName: "Compile \(target.name) strings (\(language))", executable: executable, arguments: arguments, environment: environment, // SwiftBuild copies this directory; SwiftPM processes the files inside it. outputFilesDirectory: output.appending(path: "\(language).lproj") ) } }
private struct MissingDeveloperTool: Error, CustomStringConvertible { var description: String { "Native bundle builds require a prebuilt PARCEL_DEV_BINARY; use Scripts/bundle.sh." } }
private struct CompilationFailed: Error { let target: String let language: String }
private struct Catalog: Decodable { let sourceLanguage: String let strings: [String: Entry]
struct Entry: Decodable { let localizations: [String: Localization]? }
struct Localization: Decodable {} }}