Skip to content

Commit

Permalink
Merge branch 'deploy/3.6.0' into productive
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Dec 25, 2016
2 parents eb4770f + a4d6b5f commit 9403514
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 64 deletions.
4 changes: 0 additions & 4 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# some rules are only opt-in
opt_in_rules:
- empty_count

disabled_rules:
- vertical_whitespace # Turn off until configurable to 2 newlines
- cyclomatic_complexity

# paths to include during linting. `--path` is ignored if present.
included:
- Sources
- Tests

# paths to ignore during linting. Takes precedence over `included`.
excluded:
- Carthage
- Sources/Constants

# configurable rules can be customized from this configuration file
line_length: 180
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>1.44</real>
<real>1.45</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
<key>maxPercentRelativeStandardDeviation</key>
<real>20</real>
</dict>
</dict>
<key>testInitPerformance()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.953</real>
<real>0.056</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
<key>maxPercentRelativeStandardDeviation</key>
<real>20</real>
</dict>
</dict>
</dict>
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
alt="codebeat badge">
</a>
<a href="https://github.com/Flinesoft/BartyCrouch/releases">
<img src="https://img.shields.io/badge/Version-3.5.1-blue.svg"
alt="Version: 3.5.1">
<img src="https://img.shields.io/badge/Version-3.6.0-blue.svg"
alt="Version: 3.6.0">
</a>
<img src="https://img.shields.io/badge/Swift-3-FFAC45.svg"
alt="Swift: 3">
Expand Down Expand Up @@ -292,7 +292,7 @@ if which bartycrouch > /dev/null; then
bartycrouch interfaces -p "$PROJECT_DIR"

# Add new keys to Localizable.strings files from NSLocalizedString in code
bartycrouch code -p "$PROJECT_DIR" -l "$PROJECT_DIR" -a
bartycrouch code -p "$PROJECT_DIR" -l "$PROJECT_DIR" -a -s
else
echo "warning: BartyCrouch not installed, download it from https://github.com/Flinesoft/BartyCrouch"
fi
Expand All @@ -310,7 +310,7 @@ if which bartycrouch > /dev/null; then
bartycrouch interfaces -p "$PROJECT_DIR"

# Add new keys to Localizable.strings files from NSLocalizedString in code
bartycrouch code -p "$PROJECT_DIR" -l "$PROJECT_DIR" -a
bartycrouch code -p "$PROJECT_DIR" -l "$PROJECT_DIR" -a -s

# Translate all empty values using the Microsoft Translator API
bartycrouch translate -p "$PROJECT_DIR" -l en -i "<API_ID>" -s "<API_SECRET>"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Code/CommandLineActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class CommandLineActor {
let allLocalizableStringsFilePaths = StringsFilesSearch.shared.findAllStringsFiles(within: localizable, withFileName: "Localizable")

guard !allLocalizableStringsFilePaths.isEmpty else {
printError("No `Localizable.strings` file found for output.\nTo fix this, please add a `Localizable.strings` file to your project and click the localize button for the file in Xcode. Alternatively remove the line beginning with `bartycrouch code` in you build script to remove this feature entirely if you don't need it.\nSee https://github.com/Flinesoft/BartyCrouch/issues/11 for further information.") // swiftlint:disable:this line_length
printError("No `Localizable.strings` file found for output.\nTo fix this, please add a `Localizable.strings` file to your project and click the localize button for the file in Xcode. Alternatively remove the line beginning with `bartycrouch code` in your build script to remove this feature entirely if you don't need it.\nSee https://github.com/Flinesoft/BartyCrouch/issues/11 for further information.") // swiftlint:disable:this line_length
exit(EX_USAGE)
}

Expand Down
60 changes: 28 additions & 32 deletions Sources/Code/StringsFileUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ public class StringsFileUpdater {
// MARK: - Stored Instance Properties

let path: String
var linesInFile: [String]
var oldContentString: String = ""


// MARK: - Initializers

public init?(path: String) {
self.path = path
do {
let contentString = try String(contentsOfFile: path)
self.linesInFile = contentString.components(separatedBy: .newlines)
self.oldContentString = try String(contentsOfFile: path)
} catch {
print(error.localizedDescription)
self.linesInFile = []
return nil
}
}
Expand All @@ -49,10 +47,9 @@ public class StringsFileUpdater {
overrideComments: Bool = false, sortByKeys: Bool = false) {
do {
let newContentString = try String(contentsOfFile: otherStringFilePath)
let linesInNewFile = newContentString.components(separatedBy: .newlines)

let oldTranslations = findTranslations(inLines: self.linesInFile)
var newTranslations = findTranslations(inLines: linesInNewFile)
let oldTranslations = findTranslations(inString: oldContentString)
var newTranslations = findTranslations(inString: newContentString)

if let lastOldTranslation = oldTranslations.last {
newTranslations = newTranslations.map { ($0.0, $0.1, $0.2, $0.3+lastOldTranslation.line+1) }
Expand Down Expand Up @@ -161,8 +158,7 @@ public class StringsFileUpdater {
try FileManager.default.removeItem(atPath: path)
try newContentsOfFile.write(toFile: path, atomically: true, encoding: .utf8)

let contentString = try String(contentsOfFile: path)
linesInFile = contentString.components(separatedBy: .newlines)
self.oldContentString = try String(contentsOfFile: path)
} catch {
print(error.localizedDescription)
}
Expand Down Expand Up @@ -210,7 +206,6 @@ public class StringsFileUpdater {

do {
let sourceContentString = try String(contentsOfFile: sourceStringsFilePath)
let linesInSourceFile = sourceContentString.components(separatedBy: .newlines)

let translator = Polyglot(clientId: clientId, clientSecret: clientSecret)

Expand All @@ -220,8 +215,8 @@ public class StringsFileUpdater {
var translatedValuesCount = 0
var awaitingTranslationRequestsCount = 0

let sourceTranslations = findTranslations(inLines: linesInSourceFile)
let existingTargetTranslations = findTranslations(inLines: linesInFile)
let sourceTranslations = findTranslations(inString: sourceContentString)
let existingTargetTranslations = findTranslations(inString: oldContentString)
var updatedTargetTranslations: [TranslationEntry] = []

for sourceTranslation in sourceTranslations {
Expand Down Expand Up @@ -275,32 +270,33 @@ public class StringsFileUpdater {
}

// - Returns: An array containing all found translations as tuples in the format `(key, value, comment?)`.
func findTranslations(inLines lines: [String]) -> [TranslationEntry] {
var foundTranslations: [TranslationEntry] = []
var lastCommentLine: String?
func findTranslations(inString string: String) -> [TranslationEntry] {
let translationRegexString = "(?:\\s*/\\*([^(/\\*|\\*/)]*)\\*/\\s*)?\\s*(?:^\\s*\"([\\S ]*)\"\\s*=\\s*\"([\\S ]*)\"\\s*;\\s*$)"

// swiftlint:disable force_try
let commentLineRegex = try! NSRegularExpression(pattern: "^\\s*/\\*(.*)\\*/\\s*$", options: .caseInsensitive)
let keyValueLineRegex = try! NSRegularExpression(pattern: "^\\s*\"(.*)\"\\s*=\\s*\"(.*)\"\\s*;$", options: .caseInsensitive)
let translationRegex = try! NSRegularExpression(pattern: translationRegexString, options: [.dotMatchesLineSeparators, .anchorsMatchLines])
let newlineRegex = try! NSRegularExpression(pattern: "(\\n)", options: .useUnixLineSeparators)
// swiftlint:enable force_try

lines.enumerated().forEach { i, line in
if let commentLineMatch = commentLineRegex.firstMatch(in: line, options: .reportCompletion, range: line.fullRange) {
lastCommentLine = (line as NSString).substring(with: commentLineMatch.rangeAt(1))
}

if let keyValueLineMatch = keyValueLineRegex.firstMatch(in: line, options: .reportCompletion, range: line.fullRange) {
let key = (line as NSString).substring(with: keyValueLineMatch.rangeAt(1))
let value = (line as NSString).substring(with: keyValueLineMatch.rangeAt(2))

let foundTranslation = (key, value, lastCommentLine, i)
foundTranslations.append(foundTranslation)

lastCommentLine = nil
let positionsOfNewlines = SortedArray(array: newlineRegex.matches(in: string, options: .reportCompletion, range: string.fullRange).map { $0.rangeAt(1).location })

let matches = translationRegex.matches(in: string, options: .reportCompletion, range: string.fullRange)
var translations: [TranslationEntry] = []
autoreleasepool {
translations = matches.map { match in
let valueRange = match.rangeAt(match.numberOfRanges - 1)
let value: String = (string as NSString).substring(with: valueRange)
let key = (string as NSString).substring(with: match.rangeAt(match.numberOfRanges - 2))
var comment: String?
if match.numberOfRanges >= 4 {
let range = match.rangeAt(match.numberOfRanges - 3)
if range.location != NSNotFound && range.length > 0 { comment = (string as NSString).substring(with: range) }
}
let numberOfNewlines = positionsOfNewlines.firstMatchingIndex { $0 > valueRange.location + valueRange.length } ?? positionsOfNewlines.array.count
return TranslationEntry(key: key, value: value, comment: comment, line: numberOfNewlines - 1)
}
}

return foundTranslations
return translations
}

func stringFromTranslations(translations: [TranslationEntry]) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.5.1</string>
<string>3.6.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
3 changes: 3 additions & 0 deletions Tests/Assets/Code Files/SwiftExample1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ class SwiftExample1 {
func exampleFunction1() {
NSLocalizedString("TestKey1", comment: "Comment for TestKey1")
String(format: NSLocalizedString("%@ and %.2f", comment: ""), "SomeString", 25.7528938)

let s1 = NSLocalizedString("test.multiline_comment", comment: "test comment 1")
let s2 = NSLocalizedString("test.multiline_comment", comment: "test comment 2")
}
}
Binary file modified Tests/Assets/Strings Files/NewExample.strings
Binary file not shown.
4 changes: 2 additions & 2 deletions Tests/Code/CommandLineActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CommandLineActorTests: XCTestCase {
return
}

let resultingKeys = updater.findTranslations(inLines: updater.linesInFile).map { $0.key }
let resultingKeys = updater.findTranslations(inString: updater.oldContentString).map { $0.key }
let expectedKeys = ["DDD", "ggg", "BBB", "aaa", "FFF", "eee", "ccc"]

XCTAssertEqual(resultingKeys, expectedKeys)
Expand All @@ -67,7 +67,7 @@ class CommandLineActorTests: XCTestCase {
return
}

let resultingKeys = updater.findTranslations(inLines: updater.linesInFile).map { $0.key }
let resultingKeys = updater.findTranslations(inString: updater.oldContentString).map { $0.key }
let expectedKeys = ["aaa", "BBB", "eee", "FFF", "ggg", "ccc", "DDD"]

XCTAssertEqual(resultingKeys, expectedKeys)
Expand Down
6 changes: 5 additions & 1 deletion Tests/Code/GenStringsCommanderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GenStringsCommanderTests: XCTestCase {
do {
let contentsOfStringsFile = try String(contentsOfFile: exampleCodeFilesDirectoryPath + "/Localizable.strings")

let linesInStringsFile = contentsOfStringsFile.components(separatedBy: CharacterSet.newlines)
let linesInStringsFile = contentsOfStringsFile.components(separatedBy: .newlines)
XCTAssertEqual(linesInStringsFile, [
"/* No comment provided by engineer. */",
"\"%010d and %03.f\" = \"%1$d and %2$.f\";",
Expand All @@ -48,6 +48,10 @@ class GenStringsCommanderTests: XCTestCase {
"/* No comment provided by engineer. */",
"\"ccc\" = \"ccc\";",
"",
"/* test comment 1",
" test comment 2 */",
"\"test.multiline_comment\" = \"test.multiline_comment\";",
"",
"/* Comment for TestKey1 */",
"\"TestKey1\" = \"TestKey1\";",
"",
Expand Down
Loading

0 comments on commit 9403514

Please sign in to comment.