Class: ObjCLocalizableConstReplace

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjCLocalizableConstReplace.rb

Class Method Summary collapse

Class Method Details

.getMatchConstObject



28
29
30
31
32
# File 'lib/ObjCLocalizableConstReplace.rb', line 28

def self.getMatchConst
  content = File.read(@constantFile)
  regx = /static NSString \* const (.+) = (@".+");/
  return content.scan(regx).map{|e| {e[0] => e[1]}}.inject(:merge)
end

.replaceInFiles(files) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ObjCLocalizableConstReplace.rb', line 34

def self.replaceInFiles(files)
  files.delete(@constantFile)
  files.each do |file|
    if File.file?(file)
      content = File.read(file)
      new_content = content.dup
      @constants.each do |key, value|
        if content.include?(value) 
          new_content.gsub!(value, key)
          regex = /(^.*?(\w+)?\s*=\s*#{key};.*$)/
          const_to_const = new_content.scan(regex)
          unless const_to_const.empty?
            new_content.gsub!(const_to_const[0][0], "")
            new_content.gsub!(/(\W)#{const_to_const[0][1]}(\W)/){"#{$1}#{key}#{$2}"}
            new_content.gsub!(/^$\n{2,}/, "\n")
          end
        end
      end
      if content != new_content
        File.open(file, "w") {|file| file.puts new_content }
        puts "[Alterado] - #{file}"
      else
        puts "[SEM Alteração] - #{file}"
      end
    end
  end
end

.running(constantFile, path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ObjCLocalizableConstReplace.rb', line 2

def self.running(constantFile, path)
  if constantFile.nil? || !File.file?(constantFile)
    fail "É necessário informar do caminho do ClasseDeConstants.h Localize Válido"
  end
  
  @constantFile = constantFile
  @constants = getMatchConst
  
  if path.nil?
    puts "É necessário informar o caminho dos arquivos que deseja trocar para Constant"
    return false
  end
  
  path = path.chomp("/").chomp("\\")
  
  if File.directory?(path)
    replaceInFiles(Dir["#{path}/**/*.*"])
  elsif File.file?(path)
    replaceInFiles([path])
  else
    puts "Pasta ou arquivo inválido"
    return false
  end
  
end