Class: Ftotr

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

Class Method Summary collapse

Class Method Details

.startObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ftotr.rb', line 7

def self.start

    # Output files path
    base_output_file_path = File.expand_path('~') + '/Desktop/'

    # Search for .strings files
    strings_file_paths = []
    xcodeproj_file_paths = []
    Find.find(Pathname.pwd.to_path) do |path|
        strings_file_paths << path if path.match(/.*\.strings$/)
        strings_file_paths = strings_file_paths.select{ |i| i[/^((?!Pods).)*$/] }
        xcodeproj_file_paths << path if path.match(/.*\.xcodeproj$/)
        xcodeproj_file_paths = xcodeproj_file_paths.select{ |i| i[/^((?!Pods).)*$/] }
    end

    # Write found '\\TOTR' to corresponding files
    for strings_file_path in strings_file_paths do
        file_created = false
        file_path = ''
        File.open(strings_file_path).each_line.with_index do |line, i|
            if !line.valid_encoding?
                line = line.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
            end
            if line.match('\/\/\s*TOTR')
                if !file_created
                    output = strings_file_path + " :("
                    puts output.red
                    language_name = URI(strings_file_path).path.split('/').last(2)[0].sub('.lproj', '')
                    project_file_name  = URI(xcodeproj_file_paths.last).path.split('/').last.sub('.xcodeproj', '')
                    file_name = URI(strings_file_path).path.split('/').last.sub('.strings', '')
                    output_file_name = language_name + "_" + project_file_name + "_" + file_name
                    output_file_name = output_file_name.downcase
                    file_path = base_output_file_path + output_file_name + ".txt"
                    File.delete(file_path) if File.exist?(file_path)
                    file_created = true
                end
                file = File.open(file_path, 'a+')
                file.puts(line)
                file.close
            end
        end
        if !file_created
            output = strings_file_path + " :)"
            puts output.green
        end
    end
end