lib/aigu/exporter.rb in aigu-0.2.1 vs lib/aigu/exporter.rb in aigu-0.3

- old
+ new

@@ -1,15 +1,22 @@ module Aigu class Exporter - def initialize(output_file: nil, input_directory: nil, locale: nil) - @output_file = output_file - @input_directory = input_directory - @locale = locale + 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 YAML localization files in `#{@input_directory}` directory" + + if @ignore + print 'Ignoring ' + puts @ignore.join(', ') + end + puts '---' build_output write_json_file @@ -20,18 +27,26 @@ protected def build_output @output = {} - Dir[File.join(@input_directory, '**', "*.#{@locale}.yml")].each do |file| - content = YAML.load_file(file) - base_key = file.gsub(@input_directory, '').gsub(/^\/*/, '').gsub(/\.yml$/, '|') + pattern = File.join(@input_directory, '**', "*.#{@locale}.yml") + Dir[pattern].each do |file| + filepath = file.gsub(/\A#{@input_directory}\//, '') - content = flattenize_hash(content, base_key) - content = flattenize_content_values(content) - content = globalize_content_keys(content) - @output.merge! content + if ignored_filepath?(filepath) + puts "Ignoring #{filepath}" + else + puts "Processing #{filepath}" + content = YAML.load_file(file) + base_key = file.gsub(@input_directory, '').gsub(/^\/*/, '').gsub(/\.yml$/, '|') + + content = flattenize_hash(content, base_key) + content = flattenize_content_values(content) + content = globalize_content_keys(content) + @output.merge! content + end end end def write_json_file file_path = @output_file @@ -86,9 +101,15 @@ new_base_key = [base_key, key].join('.').gsub(/\|\.+/, '|') memo.merge! flattenize_hash(value, new_base_key) end else { base_key => hash } + end + end + + def ignored_filepath?(filepath) + @ignore && @ignore.any? do |pattern| + File.fnmatch(pattern, filepath, File::FNM_PATHNAME | File::FNM_DOTMATCH) end end end end