Class: FindStrKey::FindValueIOS

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/FindStrKey/find_str_key_ios.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proj_path, key) ⇒ FindValueIOS

Returns a new instance of FindValueIOS.



7
8
9
10
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 7

def initialize( proj_path, key)
  self.proj_path = proj_path
  self.key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 6

def key
  @key
end

#proj_pathObject

Returns the value of attribute proj_path.



6
7
8
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 6

def proj_path
  @proj_path
end

Instance Method Details

#findObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 12

def find()
  arrOfObj = Array.new
  files = Dir.glob("#{self.proj_path}/**/*.{swift}")
  files.each do |file|
    if /^Pods\/|\/Pods\//.match(file)
      next
    end
    arr = findInFile(file)
    arrOfObj.concat arr
  end
  resultInPureKey, result = groupResult(arrOfObj)

  keysarr = resultInPureKey.keys.map { |_key| {:key => _key.downcase, :realkey => _key}}
  keys_ordered = keysarr.sort_by {|value|  value[:key]}
  resultInPureKey_ordered = keys_ordered.map{|value| resultInPureKey[value[:realkey]]}

  keysarr = result.keys.map { |_key| {:key => _key.downcase, :realkey => _key}}
  keys_ordered = keysarr.sort_by {|value|  value[:key]}
  result_ordered = keys_ordered.map{|value| result[value[:realkey]]}
  return resultInPureKey_ordered, result_ordered
end

#findInFile(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 34

def findInFile(file)
  puts "processing #{file}"
  result = Array.new
  lineNum = 0
  lines = IO.readlines(file).map do |line|
    lineNum += 1
    arrOfValue = self.getValueWithInKey(line)
    if arrOfValue.length > 0
      arrOfObj = arrOfValue.map { |value| FindStrKeyObj.new(value,file,lineNum)}
      result.concat arrOfObj
    end
  end
  return result
end

#getValueWithInKey(orgline) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 49

def getValueWithInKey(orgline)
  result = Array.new
  regex = /((?:[^\w]+#{self.key}\()(.*)|^(?:#{self.key}\()(.*))/
  if orgline.match(regex)
    matchedArr = orgline.scan(regex)
    matchedArr.each do |matchedValue|
      if matchedValue[1].to_s.length > 0
        value, line = self.parse_token(matchedValue[1])
      else
        value, line = self.parse_token(matchedValue[2])
      end
      if value.strip.length > 0
        result.push(value.strip)
      end
      if line.length > 0
        arr = self.getValueWithInKey(line)
        if arr.length > 0
          result.concat arr
        end
      end
    end
  end
  return result
end

#groupResult(arrOfObj) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 130

def groupResult(arrOfObj)
  list = {}
  purekeylist = {}
  arrOfObj.each do |item|
    value = isPureValue(item.value)
    arr = Array.new
    if value.nil?
      value = item.value
      if !list[value].nil?
        arr = list[value]
      end
      arr.push(item)
      list[value] = arr
    else
      item.value = value
      if !purekeylist[value].nil?
        arr = purekeylist[value]
      end
      arr.push(item)
      purekeylist[value] = arr
    end
  end
  return purekeylist, list
end

#isPureValue(aline) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
206
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 155

def isPureValue(aline)
  n = 0
  in_value = false
  in_quote = false
  in_escape = false
  value = ""
  line = aline.strip
  for ch in line.chars
    prech = ""
    prech = line.chars[n-1] if n > 0
    n += 1

    if not in_value
      if ch == "\""
        in_quote = true
        in_value = true
      elsif ch != " " and ch != "\t"
        return nil
      end
      next
    end

    if in_escape
      value << prech
      value << ch
      in_escape = false
    elsif ch == "\\"
      in_escape = true
    elsif in_quote
      if ch == "\""
        break
      else
        value << ch
      end
    else
      value << ch
    end
  end
  remained = line[n..-1]
  if remained.strip.length > 0
    return nil
  else
    regex = /\\\(.*\)/
    if line.match(regex)
      return nil
    end
    if line.length > 2
      return line[1..n-2]
    end
  end
  return nil
end

#parse_token(line) ⇒ Object



74
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/applocale/Core/FindStrKey/find_str_key_ios.rb', line 74

def parse_token(line)
  n = 0
  in_value = false
  in_quote = false
  in_escape = false
  value = ""
  num_open = 0

  for ch in line.chars
    prech = ""
    prech = line.chars[n-1] if n > 0
    n += 1

    if not in_value
      if ch == "\""
        in_quote = true
        in_value = true
      elsif ch != " " and ch != "\t"
        in_value = true
        value << ch
      end
      next
    end

    if in_escape
      value << prech
      value << ch
      in_escape = false
    elsif ch == "\\"
      in_escape = true
    elsif in_quote
      if ch == "\""
        in_quote = false
      else
        value << ch
      end
    else
      if ch == "("
        num_open += 1
      elsif ch == ")"
        if num_open <= 0
          break
        end
        num_open -= 1
      end
    end
  end
  value = ""
  if line.chars.length > 1
    value = line[0..n-2]
  end
  return value, line[n..-1]
end