Skip to content

Commit bf9728a

Browse files
authored
Merge pull request #107 from ludvigeriksson/ignore-empty-strings
Added ability to ignore empty strings
2 parents c529a7e + f12a7a8 commit bf9728a

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Here's an overview of all options available for the sub command `interfaces`:
190190
- `path` (required), `override` and `verbose` (see [Options for all Sub Commands](#options-for-all-sub-commands) above)
191191
- `default-to-base`
192192
- `unstripped`
193+
- `ignore-empty-strings`
193194

194195
#### Default to Base (aka `-b`, `--default-to-base`) <small>*optional*</small>
195196

@@ -211,6 +212,16 @@ Example:
211212
$ bartycrouch interfaces -p "/path/to/project" -u
212213
```
213214

215+
#### Ignore empty strings (aka `-i`, `--ignore-empty-strings`) <small>*optional*</small>
216+
217+
With this options set, strings that are empty, or only contain whitespace, will not appear in the localization files.
218+
219+
Example:
220+
221+
```shell
222+
$ bartycrouch interfaces -p "/path/to/project" -i
223+
```
224+
214225
---
215226

216227
### Options for `code`

Sources/Code/CommandLineActor.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class CommandLineActor {
3434
customLocalizableName: customLocalizableName.value
3535
)
3636

37-
case let .interfacesOptions(defaultToBaseOption, unstripped):
38-
self.actOnInterfaces(path: path, override: override, verbose: verbose, defaultToBase: defaultToBaseOption.value, unstripped: unstripped.value)
37+
case let .interfacesOptions(defaultToBaseOption, unstripped, ignoreEmptyStrings):
38+
self.actOnInterfaces(path: path, override: override, verbose: verbose, defaultToBase: defaultToBaseOption.value, unstripped: unstripped.value, ignoreEmptyStrings: ignoreEmptyStrings.value)
3939

4040
case let .translateOptions(idOption, secretOption, localeOption):
4141
guard let id = idOption.value else {
@@ -90,7 +90,7 @@ public class CommandLineActor {
9090
)
9191
}
9292

93-
private func actOnInterfaces(path: String, override: Bool, verbose: Bool, defaultToBase: Bool, unstripped: Bool) {
93+
private func actOnInterfaces(path: String, override: Bool, verbose: Bool, defaultToBase: Bool, unstripped: Bool, ignoreEmptyStrings: Bool) {
9494
let inputFilePaths = StringsFilesSearch.shared.findAllIBFiles(within: path, withLocale: "Base")
9595

9696
guard !inputFilePaths.isEmpty else { print("No input files found.", level: .warning); exit(EX_OK) }
@@ -109,7 +109,7 @@ public class CommandLineActor {
109109
}
110110

111111
self.incrementalInterfacesUpdate(
112-
inputFilePath, outputStringsFilePaths, override: override, verbose: verbose, defaultToBase: defaultToBase, unstripped: unstripped
112+
inputFilePath, outputStringsFilePaths, override: override, verbose: verbose, defaultToBase: defaultToBase, unstripped: unstripped, ignoreEmptyStrings: ignoreEmptyStrings
113113
)
114114
}
115115
}
@@ -320,7 +320,7 @@ public class CommandLineActor {
320320
}
321321

322322
private func incrementalInterfacesUpdate(
323-
_ inputFilePath: String, _ outputStringsFilePaths: [String], override: Bool, verbose: Bool, defaultToBase: Bool, unstripped: Bool
323+
_ inputFilePath: String, _ outputStringsFilePaths: [String], override: Bool, verbose: Bool, defaultToBase: Bool, unstripped: Bool, ignoreEmptyStrings: Bool
324324
) {
325325
let extractedStringsFilePath = inputFilePath + ".tmpstrings"
326326

@@ -339,7 +339,8 @@ public class CommandLineActor {
339339
withStringsFileAtPath: extractedStringsFilePath,
340340
addNewValuesAsEmpty: !defaultToBase,
341341
override: override,
342-
keepWhitespaceSurroundings: unstripped
342+
keepWhitespaceSurroundings: unstripped,
343+
ignoreEmptyStrings: ignoreEmptyStrings
343344
)
344345

345346
if verbose {

Sources/Code/CommandLineParser.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class CommandLineParser {
3434
customFunction: StringOption,
3535
customLocalizableName: StringOption
3636
)
37-
case interfacesOptions(defaultToBase: BoolOption, unstripped: BoolOption)
37+
case interfacesOptions(defaultToBase: BoolOption, unstripped: BoolOption, ignoreEmptyStrings: BoolOption)
3838
case translateOptions(id: StringOption, secret: StringOption, locale: StringOption)
3939
case normalizeOptions(
4040
locale: StringOption,
@@ -143,6 +143,13 @@ public class CommandLineParser {
143143
required: false,
144144
helpMessage: "Fails if Strings files contain duplicate keys. Designed to be used as part of a CI service."
145145
)
146+
147+
private let ignoreEmptyStrings = BoolOption(
148+
shortFlag: "i",
149+
longFlag: "ignore-empty-strings",
150+
required: false,
151+
helpMessage: "Ignores empty strings and strings consisting of whitespace only."
152+
)
146153

147154
// MARK: - Initializers
148155
public init(arguments: [String] = CommandLine.arguments) {
@@ -260,9 +267,9 @@ public class CommandLineParser {
260267
)
261268

262269
let commonOptions: CommonOptions = (path: path, override: override, verbose: verbose)
263-
let subCommandOptions = SubCommandOptions.interfacesOptions(defaultToBase: defaultToBase, unstripped: unstripped)
270+
let subCommandOptions = SubCommandOptions.interfacesOptions(defaultToBase: defaultToBase, unstripped: unstripped, ignoreEmptyStrings: ignoreEmptyStrings)
264271

265-
commandLine.addOptions(path, override, verbose, defaultToBase, unstripped)
272+
commandLine.addOptions(path, override, verbose, defaultToBase, unstripped, ignoreEmptyStrings)
266273
return (commandLine, commonOptions, subCommandOptions)
267274
}
268275

Sources/Code/StringsFileUpdater.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class StringsFileUpdater {
3535
withStringsFileAtPath otherStringFilePath: String,
3636
addNewValuesAsEmpty: Bool, ignoreBaseKeysAndComment ignores: [String] = defaultIgnoreKeys,
3737
override: Bool = false, updateCommentWithBase: Bool = true, keepExistingKeys: Bool = false,
38-
overrideComments: Bool = false, sortByKeys: Bool = false, keepWhitespaceSurroundings: Bool = false
38+
overrideComments: Bool = false, sortByKeys: Bool = false, keepWhitespaceSurroundings: Bool = false,
39+
ignoreEmptyStrings: Bool = false
3940
) {
4041
do {
4142
let newContentString = try String(contentsOfFile: otherStringFilePath)
@@ -59,6 +60,7 @@ public class StringsFileUpdater {
5960
for newTranslation in newTranslations {
6061
// skip keys marked for ignore
6162
guard !newTranslation.value.containsAny(of: ignores) else { continue }
63+
if ignoreEmptyStrings && newTranslation.value.isBlank { continue }
6264

6365
// Skip keys that have been marked for ignore in comment
6466
if let newComment = newTranslation.comment, newComment.containsAny(of: ignores) { continue }
198 Bytes
Binary file not shown.
198 Bytes
Binary file not shown.

Tests/Code/StringsFileUpdaterTests.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class StringsFileUpdaterTests: XCTestCase {
6767
("cHL-Zc-L39.normalTitle", "Example Button 3", " Class = \"UIButton\"; normalTitle = \"Example Button 3\"; ObjectID = \"cHL-Zc-L39\"; "),
6868
("test.key", "This is a test key", " Completely custom comment structure in one line "),
6969
("test.key.ignored", "This is a test key to be ignored #bc-ignore!", " Completely custom comment structure in one line to be ignored "),
70+
("test.key.ignoreEmptyStrings", " ", " This key should be ignored when ignoreEmptyKeys is set "),
7071
("abc-12-345.normalTitle", "😀", " Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; "),
7172
("em1-3S-vgp.text", "Refrakční vzdálenost v metrech",
7273
" Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; ")
@@ -120,6 +121,8 @@ class StringsFileUpdaterTests: XCTestCase {
120121
"\"test.key\" = \"This is a test key\";", "",
121122
"/* Completely custom comment structure in one line to be ignored */",
122123
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
124+
"/* This key should be ignored when ignoreEmptyKeys is set */",
125+
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
123126
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
124127
"\"abc-12-345.normalTitle\" = \"😀\";", "",
125128
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
@@ -133,7 +136,7 @@ class StringsFileUpdaterTests: XCTestCase {
133136
XCTAssertEqual(oldLinesInFile[index], expectedLine)
134137
}
135138

136-
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: true, updateCommentWithBase: false)
139+
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: true, updateCommentWithBase: false, ignoreEmptyStrings: true)
137140

138141
let expectedLinesAfterIncrementalUpdate = [
139142
"", "/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
@@ -180,6 +183,8 @@ class StringsFileUpdaterTests: XCTestCase {
180183
"\"test.key\" = \"This is a test key\";", "",
181184
"/* Completely custom comment structure in one line to be ignored */",
182185
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
186+
"/* This key should be ignored when ignoreEmptyKeys is set */",
187+
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
183188
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
184189
"\"abc-12-345.normalTitle\" = \"😀\";", "",
185190
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
@@ -197,7 +202,8 @@ class StringsFileUpdaterTests: XCTestCase {
197202
withStringsFileAtPath: newStringsFilePath,
198203
addNewValuesAsEmpty: true,
199204
updateCommentWithBase: false,
200-
keepWhitespaceSurroundings: true
205+
keepWhitespaceSurroundings: true,
206+
ignoreEmptyStrings: true
201207
)
202208

203209
let expectedLinesAfterIncrementalUpdate = [
@@ -245,6 +251,8 @@ class StringsFileUpdaterTests: XCTestCase {
245251
"\"test.key\" = \"This is a test key\";", "",
246252
"/* Completely custom comment structure in one line to be ignored */",
247253
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
254+
"/* This key should be ignored when ignoreEmptyKeys is set */",
255+
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
248256
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
249257
"\"abc-12-345.normalTitle\" = \"😀\";", "",
250258
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
@@ -258,7 +266,7 @@ class StringsFileUpdaterTests: XCTestCase {
258266
XCTAssertEqual(oldLinesInFile[index], expectedLine)
259267
}
260268

261-
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: false)
269+
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: false, ignoreEmptyStrings: true)
262270

263271
let expectedLinesAfterIncrementalUpdate = [
264272
"", "/* Class = \"UIButton\"; normalTitle = \"New Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",

0 commit comments

Comments
 (0)