diff --git a/Sources/commandService/commandLineService.swift b/Sources/commandService/commandLineService.swift index 454ea16..3c6adab 100644 --- a/Sources/commandService/commandLineService.swift +++ b/Sources/commandService/commandLineService.swift @@ -27,7 +27,7 @@ public struct localizationCommand :RegexStringsSearcher,RegexStringsWriter{ func search(in content: String) {} - public mutating func findTargetFiles(){ + public mutating func findTargetPath(){ let optonalPaths = try? projectPath.recursiveChildren() guard let paths = optonalPaths else {return} @@ -42,34 +42,32 @@ public struct localizationCommand :RegexStringsSearcher,RegexStringsWriter{ ocPath.append(item) } } - + return search() + } + + mutating func search() { for item in Progress(swiftPath) { patterns = [SWIFT_REGEX] search(in: item) } - // test -// DataHandleManager.defaltManager.mapSwfit() - for item in Progress(ocPath) { patterns = [OC_REGEX] search(in: item) } - - // test -// DataHandleManager.defaltManager.mapOC() - + return write() + } + + func write () { for path in findAllLocalizable(with: projectPath, excluded: exceptPath){ if path.lastComponentWithoutExtension == "Localizable" && path.parent().lastComponent == "zh-Hans.lproj" { -// print(path.parent().lastComponent.red + path.lastComponentWithoutExtension.yellow + path.description.blue) writeToLocalizable(to: path) } } - defer { - DataHandleManager.defaltManager.mapError() - } + DataHandleManager.defaltManager.mapError() } + } diff --git a/Sources/commandService/commandTool.swift b/Sources/commandService/commandTool.swift index d68d960..ba80265 100644 --- a/Sources/commandService/commandTool.swift +++ b/Sources/commandService/commandTool.swift @@ -60,7 +60,6 @@ extension RegexStringsSearcher { /// over here, if localizedString is isAmbiguous,we analysis error if localizedString.isAmbiguous { - print(path.description.red) errors.append(value) }else{ extracts.append(value) @@ -94,27 +93,32 @@ extension RegexStringsWriter { var content = !writeAppend ? "" : { return try? path.read(.utf16) }() ?? "" + + let contentArr = contentRegex(content: content) - print(content.red + " \(writeAppend)".red) - let swift = DataHandleManager.defaltManager.swift_listNode?.head content += "//-------------------swfit-------------------" + let swift = DataHandleManager.defaltManager.swift_listNode?.head DataHandleManager.defaltManager.outPutLinkNode(root: swift, action: { valuesOptial in if let values = valuesOptial { for value in values { - content += "\n\(value.comment)\n\(value.localizedString) = \(value.localizedString);\n" + if !contentArr.contains(value.localizedString){ + content += "\n\(value.comment)\n\(value.localizedString) = \(value.localizedString);\n" + } } } }) content += "\n//\(NSDate())\n" - let objc = DataHandleManager.defaltManager.oc_listNode?.head content += "//-------------------objc-------------------" + let objc = DataHandleManager.defaltManager.oc_listNode?.head DataHandleManager.defaltManager.outPutLinkNode(root: objc, action: { valuesOptial in if let values = valuesOptial { for value in values { - content += "\n\(value.comment)\n\(value.localizedString) = \(value.localizedString);\n" + if !contentArr.contains(value.localizedString){ + content += "\n\(value.comment)\n\(value.localizedString) = \(value.localizedString);\n" + } } } }) @@ -125,6 +129,14 @@ extension RegexStringsWriter { } +func contentRegex(content:String)->[String]{ + guard let regex = try? NSRegularExpression(pattern: "\".+?\"", options: []) else { + print("Failed to create regular expression.".red) + return [] + } + let matches = regex.matches(in: content, options: [], range: content.fullRange) + return matches.map{NSString(string: content).substring(with: $0.rangeAt(0))} +} func findAllLocalizable(with path:Path,excluded:[Path]) -> [Path]{ diff --git a/Sources/localizationCommand/main.swift b/Sources/localizationCommand/main.swift index 8fe2e35..1dae291 100644 --- a/Sources/localizationCommand/main.swift +++ b/Sources/localizationCommand/main.swift @@ -28,7 +28,7 @@ let oc = BoolOption(shortFlag: "m", longFlag: "oc", let appendOpt = BoolOption(shortFlag: "a", longFlag: "append", helpMessage: "append to the file context.") let replaceOpt = BoolOption(shortFlag: "r", longFlag: "replace", helpMessage: "replace to the file context.") -cli.setOptions(help,exceptPath,projectPath,version,swift,oc) +cli.setOptions(help,exceptPath,projectPath,version,swift,oc,appendOpt,replaceOpt) cli.formatOutput = { s,type in var str: String @@ -90,7 +90,7 @@ if exceptPath.value == nil { var commandService = localizationCommand.init(projPath: projectPath.value ?? FileManager.default.currentDirectoryPath, except: exceptPath.value ?? []) writeAppend = appendOpt.value writeReplace = replaceOpt.value -commandService.findTargetFiles() +commandService.findTargetPath()