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

.filterKeyInOcBLock(block, filePath) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cocoapods-scancode/trig.rb', line 177

def self.filterKeyInOcBLock(block,filePath)
  keywords = ['localizedStringForKey:@"',
              'LMCALanuage(@"',
              'LMCTLanuage(@"',
     'LMIrcodeLocalizedString(@"',
    'LMLTLanuage(@"',
     'LMMDLocalizedString(@"',
    'kLMPositionFrameworkNSLocalized(@"']
  keywords.each do |keyword|
    tempBlock = block
    while tempBlock.index(keyword) != nil do
      #左引号
      left = tempBlock.index(keyword) + keyword.length-1
      # puts left
      # puts tempBlock[left]
      right = tempBlock.index('"',left+1)
      # puts right
      # puts tempBlock[right]
      key = tempBlock[left+1...right]
      #
      if @@defineKeyDic[key] == nil
        # print '未定义的key:',key,"\n"
        item = UnDefineKeyItem.new(key,filePath)
        @@undefineKeyDic[key] = item
      end
      tempBlock = tempBlock[right+1..tempBlock.length-1]
    end
  end
end

.filterKeyInSwiftBLock(block, filePath) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cocoapods-scancode/trig.rb', line 153

def self.filterKeyInSwiftBLock(block,filePath)
  keywords = ['LMBundleNSLocalizedString(','LHLocalizedTool.localizedString(forKey:']
  keywords.each do |keyword|
    tempBlock = block
    while tempBlock.index(keyword) != nil do
      #左引号
      left = tempBlock.index(keyword)
      # puts left
      # puts tempBlock[left]
      left = tempBlock.index('"',left+1)
      right = tempBlock.index('"',left+1)
      # puts right
      # puts tempBlock[right]
      key = tempBlock[left+1...right]
      if @@defineKeyDic[key] == nil
        print 'swift未定义的key:',key,"\n"
        item = UnDefineKeyItem.new(key,filePath)
        @@undefineKeyDic[key] = item
      end
      tempBlock = tempBlock[right+1..tempBlock.length-1]
    end
  end
end

.findKey(filePath) ⇒ Object

提取出key



75
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
# File 'lib/cocoapods-scancode/trig.rb', line 75

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
# 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 bundPath
  Find.find(bundPath) do |filePath|
    if filePath.end_with?("Localizable.strings")
      self.findKey(filePath)
    end
  end
end

.pipLineObject



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cocoapods-scancode/trig.rb', line 207

def self.pipLine
  self.loadBundleKey
  self.scan
  # @@defineKeyDic.each_value do |model|
  #   puts model
  # end

  num = 0
  @@undefineKeyDic.each_value do |item|
    num += 1
    puts "#{num}:#{item.printKey} filepath:#{item.printFilePath}"
  end
end

.scanObject



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

def self.scan

  Find.find ("./") do |fileName|
#      puts fileName
     if fileName.end_with?".m"
       self.scanKeyInOcCode(fileName)
     elsif fileName.end_with?".swift"
       self.scanKeyInSwiftCode(fileName)
     end
  end
end

.scanKeyInOcCode(path) ⇒ Object



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

def self.scanKeyInOcCode(path)

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

end

.scanKeyInSwiftCode(path) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cocoapods-scancode/trig.rb', line 136

def self.scanKeyInSwiftCode(path)

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

end