Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
add append and replace model
Browse files Browse the repository at this point in the history
  • Loading branch information
yusanlou committed Mar 20, 2017
1 parent 0df3ad7 commit 247523a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
24 changes: 11 additions & 13 deletions Sources/commandService/commandLineService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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()
}


}

Expand Down
24 changes: 18 additions & 6 deletions Sources/commandService/commandTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
}
}
}
})
Expand All @@ -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]{

Expand Down
4 changes: 2 additions & 2 deletions Sources/localizationCommand/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()



Expand Down

0 comments on commit 247523a

Please sign in to comment.