Skip to content

Commit

Permalink
Feature #8: Add --exclude-comments|-e flag to not include comments in… (
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-blahunka authored Apr 14, 2023
1 parent 016feb6 commit 3845735
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 42 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ The upstream issue is tracked [here](https://openradar.appspot.com/22133811).
## Usage

```
SwiftGenStrings [<files> ...] [-s <substitute>] [-o <output-directory>]
USAGE: SwiftGenStrings <files> ... [-s <s>] [-o <o>] [--exclude-comments]
ARGUMENTS:
<files> List of files, that are used as source of Localizable.strings generation.
<files> List of files, that are used as source of
Localizable.strings generation.
OPTIONS:
-s <substitute> (Optional) Substitute for NSLocalizedString, useful when different macro is used.
-o <output-directory> (Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output content
to standard output (console).
-s <s> (Optional) Substitute for NSLocalizedString, useful when different macro is used.
-o <o> (Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output
content to standard output (console).
-e, --exclude-comments (Optional) Formatted output does not include comments
--version Show the version.
-h, --help Show help information.
```
Expand Down
5 changes: 4 additions & 1 deletion Sources/SwiftGenStrings/SwiftGenStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct SwiftGenStrings: ParsableCommand {
@Option(name: .short, help: "(Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output content to standard output (console).")
var outputDirectory: URL?

@Flag(name: .shortAndLong, help: "(Optional) Formatted output does not include comments")
var excludeComments: Bool = false

// MARK: - Processing

func run() throws {
Expand Down Expand Up @@ -64,7 +67,7 @@ struct SwiftGenStrings: ParsableCommand {
throw NSError(description: errorMessage)
}

let output = finalStrings.formattedContent
let output = finalStrings.formattedContent(includeComments: !excludeComments)

if let outputDirectory = outputDirectory {
let outputFileURL = outputDirectory.appendingPathComponent("Localizable.strings")
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGenStringsCore/CharacterIterator.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class CharacterIterator {
public final class CharacterIterator {

private let characters: [Character]

Expand Down
24 changes: 8 additions & 16 deletions Sources/SwiftGenStringsCore/LocalizedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ private let formatSpecifierRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: pattern, options:[])
}()

public class LocalizedString: CustomStringConvertible, Equatable {
public struct LocalizedString: CustomStringConvertible, Equatable {

let key: String
let value: String
let comments: [String]

init(key: String, value: String, comments: [String]) {
self.key = key
self.value = value
self.comments = comments
}

var valueWithIndexedPlaceholders: String {
let matches = formatSpecifierRegex.matches(in: value, options:[], range: NSRange(location: 0, length: (value as NSString).length))
if matches.count <= 1 {
Expand All @@ -45,9 +39,13 @@ public class LocalizedString: CustomStringConvertible, Equatable {
return result as String
}

var formatted: String {
"/* \(formattedComments) */\n" +
"\"\(key)\" = \"\(valueWithIndexedPlaceholders)\";"
func formatted(includeComments: Bool) -> String {
var result = ""
if includeComments {
result = "/* \(formattedComments) */\n"
}
result += "\"\(key)\" = \"\(valueWithIndexedPlaceholders)\";"
return result
}

private var formattedComments: String {
Expand All @@ -60,10 +58,4 @@ public class LocalizedString: CustomStringConvertible, Equatable {
"LocalizedString(key: \(key), value: \(value), comments: \(comments))"
}

// MARK: - Equatable

public static func ==(lhs: LocalizedString, rhs: LocalizedString) -> Bool {
lhs.key == rhs.key && lhs.value == rhs.value && lhs.comments == rhs.comments
}

}
18 changes: 9 additions & 9 deletions Sources/SwiftGenStringsCore/LocalizedStringCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public protocol LocalizedStringCollectionErrorOutput {
func differentValues(forKey key: String, value1: String, value2: String)
}

public class LocalizedStringCollection {
public final class LocalizedStringCollection {

private let errorOutput: LocalizedStringCollectionErrorOutput?

Expand All @@ -19,6 +19,14 @@ public class LocalizedStringCollection {
merge(with: strings)
}

public func formattedContent(includeComments: Bool) -> String {
strings
.sorted(by: {$0.key < $1.key })
.reduce("") { result, string in
result + string.formatted(includeComments: includeComments) + "\n\n"
}
}

public func merge(with collection: LocalizedStringCollection) {
merge(with: collection.strings)
}
Expand All @@ -39,12 +47,4 @@ public class LocalizedStringCollection {
}
}

public var formattedContent: String {
strings
.sorted(by: {$0.key < $1.key })
.reduce("") { result, string in
result + string.formatted + "\n\n"
}
}

}
2 changes: 1 addition & 1 deletion Sources/SwiftGenStringsCore/LocalizedStringFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public protocol LocalizedStringFinderErrorOutput {

private let verifyUnicodeRegex = try! NSRegularExpression(pattern: "\\\\[uU]\\{[a-fA-F0-9]+\\}", options: [])

public class LocalizedStringFinder {
public final class LocalizedStringFinder {

private let routine: String
private let errorOutput: LocalizedStringFinderErrorOutput?
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGenStringsTests/CharacterIteratorTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import SwiftGenStringsCore
import XCTest

class CharacterIteratorTests: XCTestCase {
final class CharacterIteratorTests: XCTestCase {

// MARK: - Iteration tests

Expand Down
16 changes: 14 additions & 2 deletions Tests/SwiftGenStringsTests/LocalizedStringCollectionTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
@testable import SwiftGenStringsCore
import XCTest

class LocalizedStringCollectionTests: XCTestCase {
final class LocalizedStringCollectionTests: XCTestCase {

fileprivate var differentValuesReported: [String] = []
private var differentValuesReported: [String] = []

func testFormattedContentIncludingComments() {
let collection = LocalizedStringCollection(strings: [LocalizedString(key: "KEY", value: "VALUE", comments: ["Comment"])], errorOutput: self)
let output = collection.formattedContent(includeComments: true)
XCTAssertEqual(output, "/* Comment */\n\"KEY\" = \"VALUE\";\n\n")
}

func testFormattedContentExcludingComments() {
let collection = LocalizedStringCollection(strings: [LocalizedString(key: "KEY", value: "VALUE", comments: ["Comment will be ignored"])], errorOutput: self)
let output = collection.formattedContent(includeComments: false)
XCTAssertEqual(output, "\"KEY\" = \"VALUE\";\n\n")
}

func testMergeWithEmptyStrings() {
let collection = LocalizedStringCollection(strings: [], errorOutput: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import SwiftGenStringsCore
import XCTest

class LocalizedStringFinderTests: XCTestCase {
final class LocalizedStringFinderTests: XCTestCase {

func testFindStringsWithTableNameAndBundle() {
let finder = LocalizedStringFinder()
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGenStringsTests/LocalizedStringTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import SwiftGenStringsCore
import XCTest

class LocalizedStringTests: XCTestCase {
final class LocalizedStringTests: XCTestCase {

func testLocalizedStringWithPercent() {
let string = LocalizedString(key: "", value: "100% Swift", comments: [])
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftGenStringsTests/RealWorldStringsTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import SwiftGenStringsCore
import XCTest

class RealWorldStringsTests: XCTestCase {
final class RealWorldStringsTests: XCTestCase {

private let errorOutput = StubErrorOutput()

Expand Down Expand Up @@ -76,7 +76,7 @@ class RealWorldStringsTests: XCTestCase {

}

private class StubErrorOutput: LocalizedStringFinderErrorOutput {
private final class StubErrorOutput: LocalizedStringFinderErrorOutput {
var invalidIdentifiers: [String] = []
var invalidUnicodeCodePoints: [String] = []

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftGenStringsTests/SwiftTokenizerTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@testable import SwiftGenStringsCore
import XCTest

class SwiftTokenizerTests: XCTestCase {
final class SwiftTokenizerTests: XCTestCase {

let tokenizer = SwiftTokenizer()
private let tokenizer = SwiftTokenizer()

func testTokenizer() {
let string = "func something { return NSLocalizedString(\"KEY\", value: \"Quotes in \\\" the middle\", comment: \"COMMENT\") }"
Expand Down

0 comments on commit 3845735

Please sign in to comment.