Skip to content

Commit 0233b5b

Browse files
Merge pull request #254 from FlineDev/wip/with-new-line
Add new option 'separateWithEmptyLine' to allow removing newline
2 parents c0e26a4 + d26f95d commit 0233b5b

File tree

14 files changed

+141
-118
lines changed

14 files changed

+141
-118
lines changed

CHANGELOG.md

Lines changed: 39 additions & 38 deletions
Large diffs are not rendered by default.

Sources/BartyCrouchConfiguration/Options/UpdateOptions/NormalizeOptions.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public struct NormalizeOptions {
88
public let sourceLocale: String
99
public let harmonizeWithSource: Bool
1010
public let sortByKeys: Bool
11+
public let separateWithEmptyLine: Bool
1112
}
1213

1314
extension NormalizeOptions: TomlCodable {
@@ -20,18 +21,20 @@ extension NormalizeOptions: TomlCodable {
2021
subpathsToIgnore: toml.array(update, normalize, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
2122
sourceLocale: toml.string(update, normalize, "sourceLocale") ?? "en",
2223
harmonizeWithSource: toml.bool(update, normalize, "harmonizeWithSource") ?? true,
23-
sortByKeys: toml.bool(update, normalize, "sortByKeys") ?? true
24+
sortByKeys: toml.bool(update, normalize, "sortByKeys") ?? true,
25+
separateWithEmptyLine: toml.bool(update, normalize, "separateWithEmptyLine") ?? true
2426
)
2527
}
2628

2729
func tomlContents() -> String {
2830
var lines: [String] = ["[update.normalize]"]
2931

30-
lines.append("paths = \(paths)")
31-
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
32-
lines.append("sourceLocale = \"\(sourceLocale)\"")
33-
lines.append("harmonizeWithSource = \(harmonizeWithSource)")
34-
lines.append("sortByKeys = \(sortByKeys)")
32+
lines.append("paths = \(self.paths)")
33+
lines.append("subpathsToIgnore = \(self.subpathsToIgnore)")
34+
lines.append("sourceLocale = \"\(self.sourceLocale)\"")
35+
lines.append("harmonizeWithSource = \(self.harmonizeWithSource)")
36+
lines.append("sortByKeys = \(self.sortByKeys)")
37+
lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")
3538

3639
return lines.joined(separator: "\n")
3740
}

Sources/BartyCrouchConfiguration/Options/UpdateOptions/TransformOptions.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct TransformOptions {
1212
public let typeName: String
1313
public let translateMethodName: String
1414
public let customLocalizableName: String?
15+
public let separateWithEmptyLine: Bool
1516
}
1617

1718
extension TransformOptions: TomlCodable {
@@ -43,25 +44,28 @@ extension TransformOptions: TomlCodable {
4344
supportedLanguageEnumPath: toml.string(update, transform, "supportedLanguageEnumPath") ?? ".",
4445
typeName: toml.string(update, transform, "typeName") ?? "BartyCrouch",
4546
translateMethodName: toml.string(update, transform, "translateMethodName") ?? "translate",
46-
customLocalizableName: toml.string(update, transform, "customLocalizableName")
47+
customLocalizableName: toml.string(update, transform, "customLocalizableName"),
48+
separateWithEmptyLine: toml.bool(update, transform, "separateWithEmptyLine") ?? true
4749
)
4850
}
4951

5052
func tomlContents() -> String {
5153
var lines: [String] = ["[update.transform]"]
5254

53-
lines.append("codePaths = \(codePaths)")
54-
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
55-
lines.append("localizablePaths = \(localizablePaths)")
56-
lines.append("transformer = \"\(transformer.rawValue)\"")
57-
lines.append("supportedLanguageEnumPath = \"\(supportedLanguageEnumPath)\"")
58-
lines.append("typeName = \"\(typeName)\"")
59-
lines.append("translateMethodName = \"\(translateMethodName)\"")
55+
lines.append("codePaths = \(self.codePaths)")
56+
lines.append("subpathsToIgnore = \(self.subpathsToIgnore)")
57+
lines.append("localizablePaths = \(self.localizablePaths)")
58+
lines.append(#"transformer = "\#(self.transformer.rawValue)""#)
59+
lines.append(#"supportedLanguageEnumPath = "\#(self.supportedLanguageEnumPath)""#)
60+
lines.append(#"typeName = "\#(self.typeName)""#)
61+
lines.append(#"translateMethodName = "\#(self.translateMethodName)""#)
6062

6163
if let customLocalizableName = customLocalizableName {
62-
lines.append("customLocalizableName = \"\(customLocalizableName)\"")
64+
lines.append(#"customLocalizableName = "\#(customLocalizableName)""#)
6365
}
6466

67+
lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")
68+
6569
return lines.joined(separator: "\n")
6670
}
6771
}

Sources/BartyCrouchConfiguration/Options/UpdateOptions/TranslateOptions.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct TranslateOptions {
1313
public let subpathsToIgnore: [String]
1414
public let secret: Secret
1515
public let sourceLocale: String
16+
public let separateWithEmptyLine: Bool
1617
}
1718

1819
extension TranslateOptions: TomlCodable {
@@ -25,6 +26,7 @@ extension TranslateOptions: TomlCodable {
2526
let paths = toml.filePaths(update, translate, singularKey: "path", pluralKey: "paths")
2627
let subpathsToIgnore = toml.array(update, translate, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore
2728
let sourceLocale: String = toml.string(update, translate, "sourceLocale") ?? "en"
29+
let separateWithEmptyLine = toml.bool(update, translate, "separateWithEmptyLine") ?? true
2830
let secret: Secret
2931
switch Translator(rawValue: translator) {
3032
case .microsoftTranslator, .none:
@@ -38,7 +40,8 @@ extension TranslateOptions: TomlCodable {
3840
paths: paths,
3941
subpathsToIgnore: subpathsToIgnore,
4042
secret: secret,
41-
sourceLocale: sourceLocale
43+
sourceLocale: sourceLocale,
44+
separateWithEmptyLine: separateWithEmptyLine
4245
)
4346
}
4447
else {
@@ -56,13 +59,14 @@ extension TranslateOptions: TomlCodable {
5659
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
5760
switch secret {
5861
case let .deepL(secret):
59-
lines.append("secret = \"\(secret)\"")
62+
lines.append(#"secret = "\#(secret)""#)
6063

6164
case let .microsoftTranslator(secret):
62-
lines.append("secret = \"\(secret)\"")
65+
lines.append(#"secret = "\#(secret)""#)
6366
}
6467

65-
lines.append("sourceLocale = \"\(sourceLocale)\"")
68+
lines.append(#"sourceLocale = "\#(sourceLocale)""#)
69+
lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")
6670

6771
return lines.joined(separator: "\n")
6872
}

Sources/BartyCrouchKit/FileHandling/StringsFileUpdater.swift

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public class StringsFileUpdater {
4646
keepExistingKeys: Bool = false,
4747
overrideComments: Bool = false,
4848
keepWhitespaceSurroundings: Bool = false,
49-
ignoreEmptyStrings: Bool = false
49+
ignoreEmptyStrings: Bool = false,
50+
separateWithEmptyLine: Bool = true
5051
) {
5152
do {
5253
let newContentString = try String(contentsOfFile: otherStringFilePath)
@@ -132,14 +133,18 @@ public class StringsFileUpdater {
132133
return translations.sorted(by: sortingClosure)
133134
}()
134135

135-
rewriteFile(with: updatedTranslations, keepWhitespaceSurroundings: keepWhitespaceSurroundings)
136+
rewriteFile(
137+
with: updatedTranslations,
138+
keepWhitespaceSurroundings: keepWhitespaceSurroundings,
139+
separateWithEmptyLine: separateWithEmptyLine
140+
)
136141
}
137142
catch {
138143
print(error.localizedDescription, level: .error, file: path)
139144
}
140145
}
141146

142-
func insert(translateEntries: [CodeFileHandler.TranslateEntry]) {
147+
func insert(translateEntries: [CodeFileHandler.TranslateEntry], separateWithEmptyLine: Bool) {
143148
guard let langCode = extractLangCode(fromPath: path) else {
144149
print("Could not extract langCode from file.", level: .warning, file: path)
145150
return
@@ -151,14 +156,22 @@ public class StringsFileUpdater {
151156
}
152157
let newTranslations: [TranslationEntry] = translateEntries.map { ($0.key, getTranslation($0), $0.comment, 0) }
153158

154-
rewriteFile(with: oldTranslations + newTranslations, keepWhitespaceSurroundings: true)
159+
rewriteFile(
160+
with: oldTranslations + newTranslations,
161+
keepWhitespaceSurroundings: true,
162+
separateWithEmptyLine: separateWithEmptyLine
163+
)
155164
}
156165

157-
public func sortByKeys(keepWhitespaceSurroundings: Bool = false) {
166+
public func sortByKeys(separateWithEmptyLine: Bool, keepWhitespaceSurroundings: Bool = false) {
158167
let translations = findTranslations(inString: oldContentString)
159168
let sortedTranslations = translations.sorted(by: translationEntrySortingClosure(lhs:rhs:), stable: true)
160169

161-
rewriteFile(with: sortedTranslations, keepWhitespaceSurroundings: false)
170+
rewriteFile(
171+
with: sortedTranslations,
172+
keepWhitespaceSurroundings: false,
173+
separateWithEmptyLine: separateWithEmptyLine
174+
)
162175
}
163176

164177
private func translationEntrySortingClosure(lhs: TranslationEntry, rhs: TranslationEntry) -> Bool {
@@ -181,9 +194,13 @@ public class StringsFileUpdater {
181194
}
182195

183196
// Rewrites file with specified translations and reloads lines from new file.
184-
func rewriteFile(with translations: [TranslationEntry], keepWhitespaceSurroundings: Bool) {
197+
func rewriteFile(with translations: [TranslationEntry], keepWhitespaceSurroundings: Bool, separateWithEmptyLine: Bool)
198+
{
185199
do {
186-
var newContentsOfFile = stringFromTranslations(translations: translations)
200+
var newContentsOfFile = stringFromTranslations(
201+
translations: translations,
202+
separateWithEmptyLine: separateWithEmptyLine
203+
)
187204

188205
if keepWhitespaceSurroundings {
189206
var whitespacesOrNewlinesAtEnd = ""
@@ -239,6 +256,7 @@ public class StringsFileUpdater {
239256
public func translateEmptyValues(
240257
usingValuesFromStringsFile sourceStringsFilePath: String,
241258
clientSecret: Secret,
259+
separateWithEmptyLine: Bool,
242260
override: Bool = false
243261
) throws -> Int {
244262
guard let (sourceLanguage, sourceRegion) = extractLocale(fromPath: sourceStringsFilePath) else {
@@ -356,7 +374,13 @@ public class StringsFileUpdater {
356374
}
357375
}
358376

359-
if translatedValuesCount > 0 { rewriteFile(with: updatedTargetTranslations, keepWhitespaceSurroundings: false) }
377+
if translatedValuesCount > 0 {
378+
rewriteFile(
379+
with: updatedTargetTranslations,
380+
keepWhitespaceSurroundings: false,
381+
separateWithEmptyLine: separateWithEmptyLine
382+
)
383+
}
360384

361385
return translatedValuesCount
362386
}
@@ -408,14 +432,14 @@ public class StringsFileUpdater {
408432
return translations
409433
}
410434

411-
func stringFromTranslations(translations: [TranslationEntry]) -> String {
435+
func stringFromTranslations(translations: [TranslationEntry], separateWithEmptyLine: Bool) -> String {
412436
return
413437
translations.map { key, value, comment, _ -> String in
414438
let translationLine = "\"\(key)\" = \"\(value)\";"
415439
if let comment = comment { return "/*\(comment)*/\n" + translationLine }
416440
return translationLine
417441
}
418-
.joined(separator: "\n\n") + "\n"
442+
.joined(separator: separateWithEmptyLine ? "\n\n" : "\n") + "\n"
419443
}
420444

421445
/// Extracts locale from a path containing substring `{language}-{region}.lproj` or `{language}.lproj`.
@@ -465,7 +489,7 @@ public class StringsFileUpdater {
465489
return translations.filter { $0.value.isEmpty }
466490
}
467491

468-
func harmonizeKeys(withSource sourceFilePath: String) throws {
492+
func harmonizeKeys(withSource sourceFilePath: String, separateWithEmptyLine: Bool) throws {
469493
let sourceFileContentString = try String(contentsOfFile: sourceFilePath)
470494

471495
let sourceTranslations = findTranslations(inString: sourceFileContentString)
@@ -501,7 +525,7 @@ public class StringsFileUpdater {
501525
fixedTranslations = fixedTranslations.filter { $0.key != keyToRemove }
502526
}
503527

504-
rewriteFile(with: fixedTranslations, keepWhitespaceSurroundings: true)
528+
rewriteFile(with: fixedTranslations, keepWhitespaceSurroundings: true, separateWithEmptyLine: separateWithEmptyLine)
505529
}
506530
}
507531

Sources/BartyCrouchKit/OldCommandLine/CommandLineActor.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public class CommandLineActor {
111111
override: Bool,
112112
verbose: Bool,
113113
secret: Secret,
114-
locale: String
114+
locale: String,
115+
separateWithEmptyLine: Bool
115116
) {
116117
let inputFilePaths =
117118
paths.flatMap {
@@ -132,7 +133,14 @@ public class CommandLineActor {
132133

133134
let outputStringsFilePaths = StringsFilesSearch.shared.findAllLocalesForStringsFile(sourceFilePath: inputFilePath)
134135
.filter { $0 != inputFilePath }
135-
self.translate(secret: secret, inputFilePath, outputStringsFilePaths, override: override, verbose: verbose)
136+
self.translate(
137+
secret: secret,
138+
inputFilePath,
139+
outputStringsFilePaths,
140+
override: override,
141+
verbose: verbose,
142+
separateWithEmptyLine: separateWithEmptyLine
143+
)
136144
}
137145
}
138146

@@ -143,7 +151,8 @@ public class CommandLineActor {
143151
verbose: Bool,
144152
locale: String,
145153
sortByKeys: Bool,
146-
harmonizeWithSource: Bool
154+
harmonizeWithSource: Bool,
155+
separateWithEmptyLine: Bool
147156
) {
148157
let sourceFilePaths =
149158
paths.flatMap {
@@ -176,7 +185,8 @@ public class CommandLineActor {
176185
for filePath in targetStringsFilePaths {
177186
let stringsFileUpdater = StringsFileUpdater(path: filePath)
178187
do {
179-
try stringsFileUpdater?.harmonizeKeys(withSource: sourceFilePath)
188+
try stringsFileUpdater?
189+
.harmonizeKeys(withSource: sourceFilePath, separateWithEmptyLine: separateWithEmptyLine)
180190
}
181191
catch {
182192
print("Could not harmonize keys with source file at path \(sourceFilePath).", level: .error)
@@ -188,7 +198,7 @@ public class CommandLineActor {
188198
if sortByKeys {
189199
for filePath in allStringsFilePaths {
190200
let stringsFileUpdater = StringsFileUpdater(path: filePath)
191-
stringsFileUpdater?.sortByKeys()
201+
stringsFileUpdater?.sortByKeys(separateWithEmptyLine: separateWithEmptyLine)
192202
}
193203
}
194204
}
@@ -418,7 +428,8 @@ public class CommandLineActor {
418428
_ inputFilePath: String,
419429
_ outputStringsFilePaths: [String],
420430
override: Bool,
421-
verbose: Bool
431+
verbose: Bool,
432+
separateWithEmptyLine: Bool
422433
) {
423434
var overallTranslatedValuesCount = 0
424435
var filesWithTranslatedValuesCount = 0
@@ -430,6 +441,7 @@ public class CommandLineActor {
430441
let translationsCount = try stringsFileUpdater.translateEmptyValues(
431442
usingValuesFromStringsFile: inputFilePath,
432443
clientSecret: secret,
444+
separateWithEmptyLine: separateWithEmptyLine,
433445
override: override
434446
)
435447

Sources/BartyCrouchKit/TaskHandlers/CodeTaskHandler.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import Foundation
33

44
struct CodeTaskHandler {
55
let options: CodeOptions
6-
7-
init(
8-
options: CodeOptions
9-
) {
10-
self.options = options
11-
}
126
}
137

148
extension CodeTaskHandler: TaskHandler {

Sources/BartyCrouchKit/TaskHandlers/InterfacesTaskHandler.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import Foundation
33

44
struct InterfacesTaskHandler {
55
let options: InterfacesOptions
6-
7-
init(
8-
options: InterfacesOptions
9-
) {
10-
self.options = options
11-
}
126
}
137

148
extension InterfacesTaskHandler: TaskHandler {

Sources/BartyCrouchKit/TaskHandlers/LintTaskHandler.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import Foundation
33

44
struct LintTaskHandler {
55
let options: LintOptions
6-
7-
init(
8-
options: LintOptions
9-
) {
10-
self.options = options
11-
}
126
}
137

148
extension LintTaskHandler: TaskHandler {

Sources/BartyCrouchKit/TaskHandlers/NormalizeTaskHandler.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import Foundation
33

44
struct NormalizeTaskHandler {
55
let options: NormalizeOptions
6-
7-
init(
8-
options: NormalizeOptions
9-
) {
10-
self.options = options
11-
}
126
}
137

148
extension NormalizeTaskHandler: TaskHandler {
@@ -23,7 +17,8 @@ extension NormalizeTaskHandler: TaskHandler {
2317
verbose: GlobalOptions.verbose.value,
2418
locale: options.sourceLocale,
2519
sortByKeys: options.sortByKeys,
26-
harmonizeWithSource: options.harmonizeWithSource
20+
harmonizeWithSource: options.harmonizeWithSource,
21+
separateWithEmptyLine: options.separateWithEmptyLine
2722
)
2823
}
2924
}

0 commit comments

Comments
 (0)