diff --git a/README.md b/README.md index f729a08..0b9e07a 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ Arguments: Options: --swiftfile [default: ${SRCROOT}/${TARGET_NAME}/Literals.swift] - The output Swift file directory. --stringsfile [default: ${SRCROOT}/${TARGET_NAME}/Localizable.strings] - The output Strings file directory. + --access [default: public] - The access modifier. + --bundle [default: main] - The bundle modifier. It can be only `main` or `module`. ``` ## Authors & Collaborators diff --git a/Sources/POEditorParser/Lib/LiteralsParser.swift b/Sources/POEditorParser/Lib/LiteralsParser.swift index b0b9fdd..439fb24 100644 --- a/Sources/POEditorParser/Lib/LiteralsParser.swift +++ b/Sources/POEditorParser/Lib/LiteralsParser.swift @@ -29,7 +29,7 @@ struct Variable { - alreadyReadPages <- this should not be captialized (or we'll lose the Read and Pages capital letters) */ - let words = rawKey.split(separator: " ").map(String.init) + let words = rawKey.components(separatedBy: .alphanumerics.inverted) if words.count == 1 { return words[0].lowercaseFirst } @@ -111,37 +111,55 @@ public struct Translation { return rawKey.capitalized.replacingOccurrences(of: "_", with: "") } - var swiftCode: String { + func swiftCode(accessModifier: AccessModifier = .public, bundleModifier: BundleModifier = .main) -> String { if variables.isEmpty { - return generateVariableLessSwiftCode() + return generateVariableLessSwiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier) } else { - return generateVariableSwiftCode() + return generateVariableSwiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier) } } - private func generateVariableLessSwiftCode() -> String { + private func generateVariableLessSwiftCode( + accessModifier: AccessModifier, + bundleModifier: BundleModifier + ) -> String { /* static var Welcome: String { return NSLocalizedString() } */ - return "\tstatic var \(prettyKey): String {\n\t\treturn \(localizedStringFunction)(\"\(rawKey)\", comment: \"\")\n\t}\n" + return "\t\(accessModifier.rawValue) static var \(prettyKey): String {\n\t\treturn \(localizedStringFunction)(\"\(rawKey)\", bundle: .\(bundleModifier.rawValue), comment: \"\")\n\t}\n" } - private func generateVariableSwiftCode() -> String { + private func generateVariableSwiftCode( + accessModifier: AccessModifier, + bundleModifier: BundleModifier + ) -> String { /* static func ReadBooksKey(readNumber: Int) -> String { return "" } */ - let parameters = variables + let uniqueKeyVariables: [Variable] = { + guard variables.count > 1 else { + return variables + } + + return variables + .enumerated() + .map { (index, variable) in + return .init(rawKey: variable.rawKey + "_\(index)") + } + }() + + let parameters = uniqueKeyVariables .map { $0.type.swiftParameter(key: $0.parameterKey) } .joined(separator: ", ") - let localizedArguments = variables + let localizedArguments = uniqueKeyVariables .map { $0.parameterKey } .map { $0.snakeCased() } .joined(separator: ", ") - return "\tstatic func \(prettyKey)(\(parameters)) -> String {\n\t\treturn String(format: \(localizedStringFunction)(\"\(rawKey)\", comment: \"\"), \(localizedArguments))\n\t}" + return "\t\(accessModifier.rawValue) static func \(prettyKey)(\(parameters)) -> String {\n\t\treturn String(format: \(localizedStringFunction)(\"\(rawKey)\", bundle: .\(bundleModifier.rawValue), comment: \"\"), \(localizedArguments))\n\t}" } } @@ -156,17 +174,19 @@ enum SwiftCodeGeneratorConstants { return formatter }() - static let rootObjectHeader = """ - // Generated using POEditorParser - // DO NOT EDIT - // Generated: \(SwiftCodeGeneratorConstants.dateFormatter.string(from: Date())) - - // swiftlint:disable all + static func rootObjectHeader(accessModifier: AccessModifier = .public) -> String { + """ + // Generated using POEditorParser + // DO NOT EDIT + // Generated: \(SwiftCodeGeneratorConstants.dateFormatter.string(from: Date())) + + // swiftlint:disable all - import Foundation - - enum Literals { - """ + import Foundation + + \(accessModifier.rawValue) enum Literals { + """ + } static let rootObjectFooter = "\n}\n// swiftlint:enable all\n" static let methodOrVariableHeader = "\n" @@ -176,36 +196,28 @@ public protocol SwiftCodeGenerator { func generateCode(translations: [Translation]) } -class StringCodeGenerator: SwiftCodeGenerator { - - var generatedResult = "" - - func generateCode(translations: [Translation]) { - generatedResult += SwiftCodeGeneratorConstants.rootObjectHeader - - for t in translations { - generatedResult += SwiftCodeGeneratorConstants.methodOrVariableHeader - generatedResult += t.swiftCode - } - - generatedResult += SwiftCodeGeneratorConstants.rootObjectFooter - } -} - public class FileCodeGenerator: SwiftCodeGenerator { let fileHandle: FileHandle - public init(fileHandle: FileHandle) { + let accessModifier: AccessModifier + let bundleModifier: BundleModifier + public init( + fileHandle: FileHandle, + access: String, + bundle: String + ) { self.fileHandle = fileHandle + self.accessModifier = .init(accessString: access) + self.bundleModifier = .init(bundleName: bundle) } // TODO: Generalize!!! += (same code as in string) public func generateCode(translations: [Translation]) { - fileHandle += SwiftCodeGeneratorConstants.rootObjectHeader + fileHandle += SwiftCodeGeneratorConstants.rootObjectHeader(accessModifier: accessModifier) for t in translations { fileHandle += SwiftCodeGeneratorConstants.methodOrVariableHeader - fileHandle += t.swiftCode + fileHandle += t.swiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier) } fileHandle += SwiftCodeGeneratorConstants.rootObjectFooter @@ -365,6 +377,24 @@ enum TranslationValueParser { } +enum AccessModifier: String { + case `public`, `private`, `open`, `internal` + + init(accessString: String) { + self = AccessModifier(rawValue: accessString) ?? .public + } +} + +enum BundleModifier: String { + case main + case module + + init(bundleName: String) { + self = BundleModifier(rawValue: bundleName) ?? .main + } +} + + extension String { var first: String { return String(prefix(1)) diff --git a/Sources/POEditorParser/main.swift b/Sources/POEditorParser/main.swift index 09647cc..78856ca 100644 --- a/Sources/POEditorParser/main.swift +++ b/Sources/POEditorParser/main.swift @@ -10,8 +10,10 @@ command( Argument("id", description: "The id of the project"), Argument("language", description: "The language code"), Option("swiftfile", default: "${SRCROOT}/${TARGET_NAME}/Literals.swift", description: "The output Swift file directory."), - Option("stringsfile", default: "${SRCROOT}/${TARGET_NAME}/Localizable.strings", description: "The output Strings file directory.") -) { (token: String, id: Int, language: String, swiftfile: String, stringsfile: String) in + Option("stringsfile", default: "${SRCROOT}/${TARGET_NAME}/Localizable.strings", description: "The output Strings file directory."), + Option("access", default: "public", description: "The access modifier."), + Option("bundle", default: "main", description: "The bundle modifier.") +) { (token: String, id: Int, language: String, swiftfile: String, stringsfile: String, access: String, bundle: String) in print("Fetching contents of strings at POEditor...".blue) @@ -60,7 +62,7 @@ command( print("Fatal error: Couldn't write to file located at \(swiftfile)".red) return } - let fileCodeGenerator = FileCodeGenerator(fileHandle: swiftHandle) + let fileCodeGenerator = FileCodeGenerator(fileHandle: swiftHandle, access: access, bundle: bundle) fileCodeGenerator.generateCode(translations: translations) print("Success! Literals generated at \(swiftfile)".green) diff --git a/bin/poe b/bin/poe index a044eb9..dc6f564 100755 Binary files a/bin/poe and b/bin/poe differ