Sha256: c0a66edd7af2f8c23f74a510099219d6116d492f96ca2bbcb583b1ef221992ce

Contents?: true

Size: 1.48 KB

Versions: 12

Compression:

Stored size: 1.48 KB

Contents

module Aigu
  class CoreExporter < Exporter
    ENUM_LINE_REGEX = /^\s*(?<key>\w+)\s?\("(?<value_en>.*)",\s?"(?<value_fr>.*)"\)[,;]?\s$/

  protected

    def build_output
      @output = {}

      pattern = File.join(@input_directory, '**', 'CoreLocalizedStrings.java')
      Dir[pattern].each do |filepath|
        if ignored_filepath?(filepath)
          puts "Ignoring #{filepath}"
        else
          puts "Processing #{filepath}"

          content = File.open(filepath, 'rb:bom|utf-8').read

          content = parse_java_file(content, @locale)
          @output.merge! content
        end
      end

      @output
    end

    def write_json_file
      file_path = @output_file
      puts "Generating #{file_path}"
      FileUtils.mkdir_p(File.dirname(file_path))

      File.open(file_path, 'w+') do |file|
        file << JSON.pretty_generate(JSON.parse(@output.to_json))
      end
    end

    def parse_java_file(java_file_content, locale = '')
      string_hash = {}

      java_file_content.each_line do |line|
        next if line.include?('/* <-- !!LOCALIZED STRINGS!! */')

        match_data = line.match(ENUM_LINE_REGEX)
        if match_data
          string_hash[match_data[:key]] = locale == 'fr' ? match_data[:value_fr] : match_data[:value_en]
        end
      end

      string_hash
    end

    def ignored_filepath?(filepath)
      @ignore && @ignore.any? do |pattern|
        return File.fnmatch(pattern, filepath, File::FNM_PATHNAME | File::FNM_DOTMATCH)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
aigu-1.2 lib/aigu/core_exporter.rb
aigu-1.1.1 lib/aigu/core_exporter.rb
aigu-1.1 lib/aigu/core_exporter.rb
aigu-1.0.3 lib/aigu/core_exporter.rb
aigu-1.0.2 lib/aigu/core_exporter.rb
aigu-1.0.1 lib/aigu/core_exporter.rb
aigu-1.0 lib/aigu/core_exporter.rb
aigu-0.7 lib/aigu/core_exporter.rb
aigu-0.6.1 lib/aigu/core_exporter.rb
aigu-0.6 lib/aigu/core_exporter.rb
aigu-0.5.1 lib/aigu/core_exporter.rb
aigu-0.5 lib/aigu/core_exporter.rb