Class: LocalizedScan

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-scancode/trig.rb

Constant Summary collapse

@@defineKeyDic =
Hash.new
@@undefineKeyDic =
Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalizedScan

把资源包中的key都load进内存



53
54
55
# File 'lib/cocoapods-scancode/trig.rb', line 53

def initialize
  
end

Class Method Details

.checkKey(key: nil, filePath: "") ⇒ Object



174
175
176
177
178
179
180
# File 'lib/cocoapods-scancode/trig.rb', line 174

def self.checkKey(key:nil,filePath:"")
  if @@defineKeyDic[key] == nil
    # print '未定义的key:',key,"\n"
    item = UnDefineKeyItem.new(key,filePath)
    @@undefineKeyDic[key] = item
  end
end

.filterKeyInBLock(block, filePath) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cocoapods-scancode/trig.rb', line 133

def self.filterKeyInBLock(block,filePath)

      #localizedString(forKey: "common_guide_board_edit1" .swift
      # localizedStringForKey  : @ "home_switch_position" comment:@"切换家庭" .m
      block.scan(/localizedString\(?forkey\s*:\s*@?\s*"(.+?)",?\s*comment/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)
        # p matched[0] +":" + filePath
      end
      # swift:LMBundleNSLocalizedString("abc", "测试")
      # oc : LMBundleNSLocalizedString(@"voice_guide_title", @"Hi,我是小乔")
      block.scan(/LMBundleNSLocalizedString\s*\(\s*@?\s*"(.+?)",\s*/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)
      end

      block.scan(/LMCALanuage\(\s*@\s*?"(.*?)"\s*,/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)
      end

      block.scan(/LMCTLanuage\(\s*@\s*?"(.*?)"\s*,/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)
      end

      block.scan(/LMIrcodeLocalizedString\(\s*@\s*?"(.*?)"\s*,/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)

        # p matched[0] +":" + filePath
      end

      block.scan(/LMLTLanuage\(\s*@\s*?"(.*?)"\s*,/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)

        # p matched[0] +":" + filePath
      end

      block.scan(/kLMPositionFrameworkNSLocalized\(\s*@\s*?"(.*?)"\s*,/im) do |matched|
        self.checkKey(:key=>matched[0],:filePath=>filePath)
        # p matched[0] +":" + filePath
      end

end

.findKey(filePath) ⇒ Object

提取出key



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cocoapods-scancode/trig.rb', line 76

def self.findKey(filePath)
    if File.exist?(filePath) == false
      puts "#{filePath} 不存在-"
      return
    end
    str = filePath.split('/')[-2]
    lang =  str.split('.')[0]
    # puts  "lang:#{lang}"
    block = ""
    File.open(filePath,"r").each_line{|line|
      block += line
      if line.end_with?("\";\n") and line.end_with?("\\\";\n") == false
        left = block.index('"')
        right = block.index('"',left+1)
        key = block[left+1...right]
        left = block.index('"',right+1)
        right = block.rindex('"')
        value = block[left+1...right]
        if @@defineKeyDic[key] == nil
          model = LanguageModel.new
          model.setKey key
          @@defineKeyDic[key] = model
        else
          # puts lang,value
          model = @@defineKeyDic[key]
          model.setLanguageValue(lang,value)
        end
        block = ""
      end
    }
end

.loadBundleKeyObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-scancode/trig.rb', line 57

def self.loadBundleKey
  bundPath = nil
  #查找bundle路径
  Find.find("./") do |filePath|
    if  filePath.end_with?("LMFramework.bundle")
      bundPath = filePath
      break
    end
  end
  puts "\e[34m app里所有模块的多语言都在这里#{bundPath}\e[0m"

  Find.find(bundPath) do |filePath|
    if filePath.end_with?("Localizable.strings")
      self.findKey(filePath)
    end
  end
end

.pipLineObject



217
218
219
220
221
# File 'lib/cocoapods-scancode/trig.rb', line 217

def self.pipLine
  self.loadBundleKey
  self.scan
  self.printUndefineKey
end

.printUndefineKeyObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/cocoapods-scancode/trig.rb', line 182

def self.printUndefineKey
  resultHas = {}
  @@undefineKeyDic.each_value do |item|
    path_list =item.printFilePath.split("/")
    if path_list[1] == "Pods"
      module_name = path_list[2]
      if resultHas.has_key? module_name
        list = resultHas[module_name]
        list << item
      else
        list = [item]
        resultHas[module_name] = list
      end
    else
      module_name = path_list[1]
      if resultHas.has_key? module_name
        list = resultHas[module_name]
        list << item
      else
        list = [item]
        resultHas[module_name] = list
      end
    end
  end
  resultHas.each do |module_name,list|
    puts "\e[32m 模块名称:#{module_name}\e[0m"
    list.each_with_index do |item,index|
      print "#{index+1}"
      puts "\e[31m #{item.printKey}\e[0m"
      puts " 文件路径:#{item.printFilePath}"
    end
  end
  puts "\e[32m 未识别key总数:#{@@undefineKeyDic.values.size}\e[0m"
end

.scanObject



108
109
110
111
112
113
114
# File 'lib/cocoapods-scancode/trig.rb', line 108

def self.scan
  Find.find ("./") do |fileName|
    if fileName.end_with?(".m") or fileName.end_with?(".swift")
      self.scanKeyInOcCode(fileName,:endWithSwift=> fileName.end_with?(".swift"))
    end
  end
end

.scanKeyInOcCode(path = nil, endWithSwift: false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cocoapods-scancode/trig.rb', line 116

def self.scanKeyInOcCode(path=nil,endWithSwift:false)

  file = File.open(path,"r:utf-8") do |file|
    block = ""
    file.each_line do |line|
      block += line
      if line.end_with?(endWithSwift ? "\n":";\n")
        #处理block
        self.filterKeyInBLock(block,path)
        block = ""
      end
    end
  end
  file.close

end