Sha256: f031a5c5432a09385f5cee6eb4d92515f0a82ac7613e1aec64e31e44402e890d

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

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

    def initialize(opts = {})
      @output_file = opts[:'output-file']
      @input_directory = opts[:'input-directory']
      @locale = opts[:locale]
      @ignore = opts[:ignore]
    end

    def process!
      puts "Generating Accent JSON file `#{@output_file}` based on Core Java Enum files in `#{@input_directory}` directory"

      if @ignore
        print 'Ignoring '
        puts @ignore.join(', ')
      end

      puts '---'

      build_output
      write_json_file

      puts '---'
      puts 'Done'
    end

  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

3 entries across 3 versions & 1 rubygems

Version Path
aigu-0.4.2 lib/aigu/core_exporter.rb
aigu-0.4.1 lib/aigu/core_exporter.rb
aigu-0.4 lib/aigu/core_exporter.rb