module Aigu class CoreExporter ENUM_LINE_REGEX = /^\s*(?\w+)\s?\("(?.*)",\s?"(?.*)"\),?\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