|
| 1 | +/* |
| 2 | +Copyright 2020 happn |
| 3 | + |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. */ |
| 15 | + |
| 16 | +import Foundation |
| 17 | + |
| 18 | +import ArgumentParser |
| 19 | + |
| 20 | +import LocMapper |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +struct MergeStdrefloc : ParsableCommand { |
| 25 | + |
| 26 | + static var configuration = CommandConfiguration( |
| 27 | + commandName: "merge_stdrefloc", |
| 28 | + abstract: "Get ref loc from a given source and merge them in an lcm file, optionally converting them into the XibRefLoc format before merge." |
| 29 | + ) |
| 30 | + |
| 31 | + @OptionGroup var csvOptions: CSVOptions |
| 32 | + @OptionGroup var logOptions: LoggingOptions |
| 33 | + |
| 34 | + @Option |
| 35 | + var mergeStyle = LocFile.MergeStyle.add |
| 36 | + |
| 37 | + /* Only for Lokalize, at least for now. */ |
| 38 | + @Option |
| 39 | + var excludedTags = [String]() |
| 40 | + |
| 41 | + @Flag(inversion: .prefixedNo) |
| 42 | + var convertToXibrefloc: Bool = true /* Yes, I’m bitter, and I promote my format! The choice that would make the most sense would be no here… */ |
| 43 | + |
| 44 | + /* Either these three options must be set… */ |
| 45 | + @Option |
| 46 | + var lokalizeReadToken: String? |
| 47 | + @Option |
| 48 | + var lokalizeProjectID: String? |
| 49 | + @Option(help: "This is the type of the project as seen by Lokalise. Possible values are (at least now): ios, web, android, other") |
| 50 | + var lokalizeProjectType: String? |
| 51 | + /* … or this one must be. */ |
| 52 | + @Option |
| 53 | + var fileToMerge: String? |
| 54 | + |
| 55 | + @Argument |
| 56 | + var mergedFilePath: String |
| 57 | + |
| 58 | + /* What this contains depends on whether the source is lokalize or a ref loc file. |
| 59 | + * For Lokalize it’s a language mapping, for a ref loc file it’s a list of languages. */ |
| 60 | + @Argument |
| 61 | + var languageArgs = [String]() |
| 62 | + |
| 63 | + func run() throws { |
| 64 | + logOptions.bootstrapLogger() |
| 65 | + |
| 66 | + let csvSeparator = csvOptions.csvSeparator |
| 67 | + let excludedTags = Set(parseObsoleteOptionList(self.excludedTags) ?? []) |
| 68 | + guard !languageArgs.isEmpty else { |
| 69 | + /* Whatever the language args contains, it cannot be empty. */ |
| 70 | + throw ValidationError("The languages cannot be left empty") |
| 71 | + } |
| 72 | + |
| 73 | + let sourceError = ValidationError("Either the three Lokalize args or the file to merge path (the std ref loc to merge) must be set") |
| 74 | + |
| 75 | + print("Merging Lokalise Trads as StdRefLoc in LocFile...") |
| 76 | + let stdRefLoc: StdRefLocFile |
| 77 | + if let lokalizeReadToken, let lokalizeProjectID, let lokalizeProjectType { |
| 78 | + guard fileToMerge == nil else {throw sourceError} |
| 79 | + print(" Creating StdRefLoc from Lokalise...") |
| 80 | + stdRefLoc = try StdRefLocFile(token: lokalizeReadToken, projectId: lokalizeProjectID, lokaliseToReflocLanguageName: dictionaryOptionFromArray(languageArgs), keyType: lokalizeProjectType, excludedTags: excludedTags, logPrefix: " ") |
| 81 | + } else if let fileToMerge { |
| 82 | + guard lokalizeReadToken == nil, lokalizeProjectID == nil, lokalizeProjectType == nil else {throw sourceError} |
| 83 | + print(" Reading StdRefLoc from file...") |
| 84 | + stdRefLoc = try StdRefLocFile(fromURL: URL(fileURLWithPath: fileToMerge), languages: languageArgs, csvSeparator: csvSeparator) |
| 85 | + } else { |
| 86 | + throw sourceError |
| 87 | + } |
| 88 | + |
| 89 | + print(" Reading source LocFile...") |
| 90 | + let locFile = try LocFile(fromPath: mergedFilePath, withCSVSeparator: csvSeparator) |
| 91 | + if convertToXibrefloc { |
| 92 | + print(" Converting StdRefLoc to XibRefLoc...") |
| 93 | + let xibRefLoc = try XibRefLocFile(stdRefLoc: stdRefLoc) |
| 94 | + |
| 95 | + print(" Merging XibRefLoc...") |
| 96 | + locFile.mergeRefLocsWithXibRefLocFile(xibRefLoc, mergeStyle: mergeStyle) |
| 97 | + } else { |
| 98 | + print(" Merging StdRefLoc...") |
| 99 | + locFile.mergeRefLocsWithStdRefLocFile(stdRefLoc, mergeStyle: mergeStyle) |
| 100 | + } |
| 101 | + |
| 102 | + print(" Writing merged file...") |
| 103 | + var stream = try FileHandleOutputStream(forPath: mergedFilePath) |
| 104 | + print(locFile, terminator: "", to: &stream) |
| 105 | + print("Done") |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments