Class: Tmsync::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tmsync/cli.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tmsync/cli.rb', line 13

def execute
  matching_regex = -> {
    case options[:platform].downcase
    when 'ios'
      Tmsync::Constants::IOS_MATCHING_REGEX
    when 'android'
      Tmsync::Constants::ANDROID_MATCHING_REGEX
    else
      options[:matching_regex]
    end
  }.call

  exclude_regex = -> {
    case options[:platform].downcase
    when 'ios'
      Tmsync::Constants::IOS_EXCLUDE_REGEX
    when 'android'
      Tmsync::Constants::ANDROID_EXCLUDE_REGEX
    else
      options[:exclude_regex]
    end
  }.call

  file_search = Tmsync::FileSearch.new(
    base_path: options[:path],
    matching_regex: matching_regex,
    exclude_regex: exclude_regex
  )

  result = file_search.find_all_grouped_by_language

  if result.empty?
    puts "No translation files found."
  else
    ([options[:source_language]] | result.keys).each do |language|
      result[language].each do |file_path|
        file_directory = File.dirname(file_path).gsub(options[:path], '')
        file_directory[0] = '' if file_directory[0] == '/'
        file_extension = File.extname(file_path)
        file_name = File.basename(file_path, file_extension)
        android_module = ''
        android_module = file_path.match(/([^\/]+)\/src\/main\/res/).captures.first if options[:platform] == 'android'


        command = options[:command]
          .gsub('<LANGUAGE>', language)
          .gsub('<FILE_PATH>', file_path)
          .gsub('<FILE_DIR>', file_directory)
          .gsub('<FILE_EXT>', file_extension)
          .gsub('<FILE_NAME>', file_name)
          .gsub('<ANDROID_MODULE>', android_module)

        puts "Executing: '#{command}'"
        output = %x(#{command})

        if output.nil? || output.empty?
          puts "Output: n/a"
        else
          puts "Output: #{output}"
        end
      end
    end
  end
end