This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
367 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import class Foundation.Bundle | ||
import XCTest | ||
|
||
extension XCTestCase { | ||
public func execute(product: String) throws -> String { | ||
guard #available(macOS 10.13, *) else { | ||
fatalError("Some of the APIs that we use in the test are available in macOS 10.13 and above.") | ||
} | ||
|
||
let fooBinary = productsDirectory.appendingPathComponent(product) | ||
|
||
let process = Process() | ||
process.executableURL = fooBinary | ||
|
||
let pipe = Pipe() | ||
process.standardOutput = pipe | ||
|
||
try process.run() | ||
process.waitUntilExit() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
guard let output = String(data: data, encoding: .utf8) else { fatalError("output cannot be nil") } | ||
|
||
return output | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/CLITestFramework/XCTest/XCTestCase+productsDirectory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import class Foundation.Bundle | ||
import XCTest | ||
|
||
extension XCTestCase { | ||
/// Returns path to the built products directory. | ||
public var productsDirectory: URL { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
} | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...gify/Foundation/String+containsHTML.swift → ...work/Foundation/String+containsHTML.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Foundation | ||
|
||
extension String { | ||
func containsHTML() throws -> Bool { | ||
public func containsHTML() throws -> Bool { | ||
return try !matches(regex: "\\<([a-zA-Z0-9]+)\\>.*?\\<\\/\\1\\>").isEmpty | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../Foundation/String+formatSpecifiers.swift → .../Foundation/String+formatSpecifiers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ndation/String+isInfoPlistValueLine.swift → ...ndation/String+isInfoPlistValueLine.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...fy/Foundation/String+isKeyValueLine.swift → ...rk/Foundation/String+isKeyValueLine.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...fy/Foundation/String+localizableKey.swift → ...rk/Foundation/String+localizableKey.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...stringify/Foundation/String+matches.swift → ...Framework/Foundation/String+matches.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Foundation/String+removingCharacter.swift → ...Foundation/String+removingCharacter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
|
||
public func findFilesRecursively(rootPath: String, extensions: [String], fileManager: FileManager = FileManager.default) throws -> [String] { | ||
let directoryPath = rootPath | ||
.appending("/\n") | ||
.replacingOccurrences(of: "//\n", with: "/") | ||
.replacingOccurrences(of: "/\n", with: "/") | ||
let enumerator = fileManager.enumerator(atPath: directoryPath) | ||
var filePaths = Set<String>() | ||
while let filePath = enumerator?.nextObject() as? String { | ||
if extensions.contains(URL(fileURLWithPath: filePath).pathExtension) { | ||
filePaths.insert(directoryPath.appending(filePath)) | ||
} | ||
} | ||
return Array(filePaths) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
public func parseKeys(_ content: String) throws -> [String] { | ||
let keyValueLines = content.components(separatedBy: "\n").filter { $0.isKeyValueLine } | ||
var keys = Set<String>() | ||
for line in keyValueLines { | ||
guard let key = line.localizableKey else { continue } | ||
keys.insert(key) | ||
} | ||
return Array(keys) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
public func readFile(at path: String) throws -> String { | ||
let fileURL = URL(fileURLWithPath: path) | ||
return try String(contentsOf: fileURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Foundation | ||
import UnstringifyFramework | ||
|
||
func generateReport(files: [String], keys: [String], outputPath: String, excludedFile: String) throws { | ||
var matches = [String: [String]]() | ||
for filePath in files where !filePath.hasSuffix(excludedFile) { | ||
let contents = try readFile(at: filePath) | ||
for key in keys { | ||
if contents.range(of: "@string/\(key)") != nil || contents.range(of: "R.string.\(key)") != nil { | ||
var matchesForKey = matches[key] ?? [] | ||
matchesForKey.append(filePath) | ||
matches[key] = matchesForKey | ||
} else { | ||
matches[key] = matches[key] ?? [] | ||
} | ||
} | ||
} | ||
let csv = matches.reduce(["Keys;File\n"]) { return $0 + ["\"\($1.key)\"; \($1.value.map { "\($0)" }.joined(separator: "; "))"] }.joined(separator: "\n") | ||
let data = csv.data(using: String.Encoding.utf8, allowLossyConversion: false) | ||
FileManager.default.createFile(atPath: outputPath, contents: data, attributes: nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
|
||
typealias Arguments = (localizablePath: String, rootPath: String, outputPath: String, excludedFile: String) | ||
|
||
func parseArguments(from arguments: [String] = CommandLine.arguments) throws -> Arguments { | ||
enum Error: Swift.Error { | ||
case tooFewArguments | ||
case tooManyArguments | ||
} | ||
|
||
func printUsage() { | ||
print("\nUsage: \(arguments[0]) localizable_strings_path android_project_root_path csv_output_path [excluded_file]\n") | ||
} | ||
|
||
if arguments.count < 4 { | ||
printUsage() | ||
throw Error.tooFewArguments | ||
} else if arguments.count > 5 { | ||
printUsage() | ||
throw Error.tooManyArguments | ||
} | ||
|
||
if arguments.count == 5 { | ||
return (arguments[1], arguments[2], arguments[3], arguments[4]) | ||
} else { | ||
return (arguments[1], arguments[2], arguments[3], "gen-strings.xml") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
import UnstringifyFramework | ||
|
||
let arguments = try parseArguments() | ||
let stringsFile = try readFile(at: arguments.localizablePath) | ||
print("Finding localizable keys...") | ||
let keys = try parseKeys(stringsFile) | ||
print("Gathering android files...") | ||
let files = try findFilesRecursively(rootPath: arguments.rootPath, extensions: ["kt", "xml"]) | ||
print("Generating report...") | ||
try generateReport(files: files, keys: keys, outputPath: arguments.outputPath, excludedFile: arguments.excludedFile) | ||
|
||
print("Done!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Foundation | ||
import UnstringifyFramework | ||
|
||
func generateReport(files: [String], keys: [String], outputPath: String, excludedFile: String) throws { | ||
var matches = [String: [String]]() | ||
for filePath in files where !filePath.hasSuffix(excludedFile) { | ||
let contents = try readFile(at: filePath) | ||
for key in keys { | ||
if contents.range(of: key) != nil { | ||
var matchesForKey = matches[key] ?? [] | ||
matchesForKey.append(filePath) | ||
matches[key] = matchesForKey | ||
} else { | ||
matches[key] = matches[key] ?? [] | ||
} | ||
} | ||
} | ||
let csv = matches.reduce(["Keys;File\n"]) { return $0 + ["\"\($1.key)\"; \($1.value.map { "\($0)" }.joined(separator: "; "))"] }.joined(separator: "\n") | ||
let data = csv.data(using: String.Encoding.utf8, allowLossyConversion: false) | ||
FileManager.default.createFile(atPath: outputPath, contents: data, attributes: nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
typealias Arguments = (rootPath: String, localizablePath: String, outputPath: String, excludedFile: String) | ||
|
||
func parseArguments(from arguments: [String] = CommandLine.arguments) throws -> Arguments { | ||
enum Error: Swift.Error { | ||
case tooFewArguments | ||
case tooManyArguments | ||
} | ||
|
||
func printUsage() { | ||
print("\nUsage: \(arguments[0]) root_path localizable_path output_path [excluded_file]\n") | ||
} | ||
|
||
if arguments.count < 4 { | ||
printUsage() | ||
throw Error.tooFewArguments | ||
} else if arguments.count > 5 { | ||
printUsage() | ||
throw Error.tooManyArguments | ||
} | ||
|
||
if arguments.count == 5 { | ||
return (arguments[1], arguments[2], arguments[3], arguments[4]) | ||
} else { | ||
return (arguments[1], arguments[2], arguments[3], "Unstringify.generated.swift") | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
import UnstringifyFramework | ||
|
||
let arguments = try parseArguments() | ||
let stringsFile = try readFile(at: arguments.localizablePath) | ||
print("Finding localizable keys...") | ||
let keys = try parseKeys(stringsFile) | ||
print("Gathering swift files...") | ||
let files = try findFilesRecursively(rootPath: arguments.rootPath, extensions: ["swift"]) | ||
print("Generating report...") | ||
try generateReport(files: files, keys: keys, outputPath: arguments.outputPath, excludedFile: arguments.excludedFile) | ||
|
||
print("Done!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import Foundation | ||
import UnstringifyFramework | ||
|
||
func generateKeys(localizablePath: String) throws -> Keys { | ||
let content = try readFile(at: localizablePath) | ||
return try parseKeys(content) | ||
} | ||
|
||
private func readFile(at path: String) throws -> String { | ||
let fileURL = URL(fileURLWithPath: path) | ||
return try String(contentsOf: fileURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Tests/FindAndroidStringsTests/FindAndroidStringsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import XCTest | ||
import CLITestFramework | ||
|
||
final class FindAndroidStringsTests: XCTestCase { | ||
func testUsage() throws { | ||
let output = try execute(product: "findAndroidStrings") | ||
XCTAssertTrue(output.hasPrefix("\nUsage: ")) | ||
XCTAssertTrue(output.hasSuffix("/findAndroidStrings localizable_strings_path android_project_root_path csv_output_path [excluded_file]\n\n")) | ||
} | ||
|
||
static var allTests = [ | ||
("testUsage", testUsage), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if !canImport(ObjectiveC) | ||
import XCTest | ||
|
||
extension FindAndroidStringsTests { | ||
// DO NOT MODIFY: This is autogenerated, use: | ||
// `swift test --generate-linuxmain` | ||
// to regenerate. | ||
static let __allTests__FindAndroidStringsTests = [ | ||
("testUsage", testUsage), | ||
] | ||
} | ||
|
||
public func __allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(FindAndroidStringsTests.__allTests__FindAndroidStringsTests), | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import XCTest | ||
import CLITestFramework | ||
|
||
final class FindStringsTests: XCTestCase { | ||
func testUsage() throws { | ||
let output = try execute(product: "findStrings") | ||
XCTAssertTrue(output.hasPrefix("\nUsage: ")) | ||
XCTAssertTrue(output.hasSuffix("/findStrings root_path localizable_path output_path [excluded_file]\n\n")) | ||
} | ||
|
||
static var allTests = [ | ||
("testUsage", testUsage), | ||
] | ||
} |
Oops, something went wrong.