Skip to content

Commit 7a4f681

Browse files
committed
Merge the merge ref loc commands into one
1 parent 129673c commit 7a4f681

File tree

5 files changed

+114
-166
lines changed

5 files changed

+114
-166
lines changed

LocMapper CLI/Commands/MergeLokaliseTradsAsStdrefloc.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.

LocMapper CLI/Commands/MergeLokaliseTradsAsXibrefloc.swift

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

LocMapper CLI/main.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ struct LocMapperCLI : ParsableCommand {
4343
/* The two subcommands below should be deleted. */
4444
Help.self, Version.self,
4545

46-
MergeXcodeLocs.self, ExportToXcode.self,
46+
MergeXcodeLocs.self, ExportToXcode.self,
4747
MergeAndroidLocs.self, ExportToAndroid.self,
4848

49-
MergeLokaliseTradsAsXibrefloc.self,
50-
MergeLokaliseTradsAsStdrefloc.self,
49+
MergeStdrefloc.self,
5150

5251
Lint.self,
5352
StandardizeRefloc.self,

LocMapper.xcodeproj/project.pbxproj

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@
9999
7BB9051224800C1C00E3F5F5 /* ExportToXcode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB9051124800C1C00E3F5F5 /* ExportToXcode.swift */; };
100100
7BB90516248010C500E3F5F5 /* MergeAndroidLocs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB90515248010C500E3F5F5 /* MergeAndroidLocs.swift */; };
101101
7BB90518248013D500E3F5F5 /* ExportToAndroid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB90517248013D500E3F5F5 /* ExportToAndroid.swift */; };
102-
7BB9051A248017CE00E3F5F5 /* MergeLokaliseTradsAsXibrefloc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB90519248017CE00E3F5F5 /* MergeLokaliseTradsAsXibrefloc.swift */; };
103-
7BB9051C24801E2600E3F5F5 /* MergeLokaliseTradsAsStdrefloc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB9051B24801E2600E3F5F5 /* MergeLokaliseTradsAsStdrefloc.swift */; };
102+
7BB9051A248017CE00E3F5F5 /* MergeStdrefloc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB90519248017CE00E3F5F5 /* MergeStdrefloc.swift */; };
104103
7BB9051E2480203A00E3F5F5 /* Lint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB9051D2480203A00E3F5F5 /* Lint.swift */; };
105104
7BB90520248021B400E3F5F5 /* StandardizeRefloc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB9051F248021B400E3F5F5 /* StandardizeRefloc.swift */; };
106105
7BB905232480266100E3F5F5 /* Experimental.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BB905222480266100E3F5F5 /* Experimental.swift */; };
@@ -259,8 +258,7 @@
259258
7BB9051124800C1C00E3F5F5 /* ExportToXcode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportToXcode.swift; sourceTree = "<group>"; };
260259
7BB90515248010C500E3F5F5 /* MergeAndroidLocs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeAndroidLocs.swift; sourceTree = "<group>"; };
261260
7BB90517248013D500E3F5F5 /* ExportToAndroid.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportToAndroid.swift; sourceTree = "<group>"; };
262-
7BB90519248017CE00E3F5F5 /* MergeLokaliseTradsAsXibrefloc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeLokaliseTradsAsXibrefloc.swift; sourceTree = "<group>"; };
263-
7BB9051B24801E2600E3F5F5 /* MergeLokaliseTradsAsStdrefloc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeLokaliseTradsAsStdrefloc.swift; sourceTree = "<group>"; };
261+
7BB90519248017CE00E3F5F5 /* MergeStdrefloc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeStdrefloc.swift; sourceTree = "<group>"; };
264262
7BB9051D2480203A00E3F5F5 /* Lint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lint.swift; sourceTree = "<group>"; };
265263
7BB9051F248021B400E3F5F5 /* StandardizeRefloc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardizeRefloc.swift; sourceTree = "<group>"; };
266264
7BB905222480266100E3F5F5 /* Experimental.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Experimental.swift; sourceTree = "<group>"; };
@@ -688,8 +686,7 @@
688686
7BB9051124800C1C00E3F5F5 /* ExportToXcode.swift */,
689687
7BB90515248010C500E3F5F5 /* MergeAndroidLocs.swift */,
690688
7BB90517248013D500E3F5F5 /* ExportToAndroid.swift */,
691-
7BB90519248017CE00E3F5F5 /* MergeLokaliseTradsAsXibrefloc.swift */,
692-
7BB9051B24801E2600E3F5F5 /* MergeLokaliseTradsAsStdrefloc.swift */,
689+
7BB90519248017CE00E3F5F5 /* MergeStdrefloc.swift */,
693690
7BB9051D2480203A00E3F5F5 /* Lint.swift */,
694691
7BB9051F248021B400E3F5F5 /* StandardizeRefloc.swift */,
695692
7B20C7972480433600004824 /* UpdateXcodeStringsFromCode.swift */,
@@ -1046,8 +1043,7 @@
10461043
7BB9051E2480203A00E3F5F5 /* Lint.swift in Sources */,
10471044
7B6E25A319D4157A001934ED /* main.swift in Sources */,
10481045
7BB90516248010C500E3F5F5 /* MergeAndroidLocs.swift in Sources */,
1049-
7BB9051C24801E2600E3F5F5 /* MergeLokaliseTradsAsStdrefloc.swift in Sources */,
1050-
7BB9051A248017CE00E3F5F5 /* MergeLokaliseTradsAsXibrefloc.swift in Sources */,
1046+
7BB9051A248017CE00E3F5F5 /* MergeStdrefloc.swift in Sources */,
10511047
7B88B9DA247FE0DC00D48975 /* MergeXcodeLocs.swift in Sources */,
10521048
7B88B9D8247FDC2F00D48975 /* ObsoleteVerbs.swift in Sources */,
10531049
7B88B9DC247FE3FC00D48975 /* ParseUtils.swift in Sources */,

0 commit comments

Comments
 (0)