Skip to content

Commit

Permalink
normalize: Remove emptied files
Browse files Browse the repository at this point in the history
Refs #263
  • Loading branch information
glebm committed Sep 24, 2017
1 parent ac56cf8 commit bc84a3b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Fixes support for calls such as `t @instance_variable, scope: :static_scope` in
Fixes `remove-unused` not removing entire files.
[#260](https://github.com/glebm/i18n-tasks/issues/260)

Fixes `normalize` nor removing emptied files.

This comment has been minimized.

Copy link
@vsakarov

vsakarov Sep 24, 2017

Typo

This comment has been minimized.

Copy link
@glebm

glebm Sep 24, 2017

Author Owner

Fixed in fdb2e1d

[#263](https://github.com/glebm/i18n-tasks/issues/263)

## v0.9.18

Fixes support for calls such as `t dynamic_key, scope: :static_scope` in the non-AST scanner.
Expand Down
35 changes: 26 additions & 9 deletions lib/i18n/tasks/data/file_system_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@ def get(locale)
alias [] get

# set locale tree
# @param [String, Symbol] locale
# @param [::I18n::Tasks::Data::Siblings] tree
def set(locale, tree)
locale = locale.to_s
router.route locale, tree do |path, tree_slice|
write_tree path, tree_slice
removing_emptied_files locale, tree do |&block|
router.route locale, tree do |path, tree_slice|
tree_slice.key_names(root: true).each { |key| block.call(key, path) }

This comment has been minimized.

Copy link
@vsakarov

vsakarov Sep 24, 2017

Do you need all this key/path mapping? It will be simpler if you just store all paths in array here.

This comment has been minimized.

Copy link
@glebm

glebm Sep 24, 2017

Author Owner

You're right, simplified in fdb2e1d

write_tree path, tree_slice
end
end
@trees.delete(locale) if @trees
@available_locales = nil
end

alias []= set

# @param [String] locale
# @return [Array<String>] paths to files that are not normalized
def non_normalized_paths(locale)
Expand All @@ -62,7 +69,7 @@ def non_normalized_paths(locale)
end

def write(forest)
forest.each { |root| set(root.key, root) }
forest.each { |root| set(root.key, root.to_siblings) }
end

def merge!(forest)
Expand All @@ -80,15 +87,10 @@ def remove_by_key!(forest)
locale_data = get(locale)
subtracted = locale_data.subtract_by_key(forest)
set locale, subtracted
(tree_paths(locale, forest) - tree_paths(locale, subtracted)).each do |path|
FileUtils.remove_file(path) if File.exist?(path)
end
removed.merge! locale_data.subtract_by_key(subtracted)
end
end

alias []= set

# @return self
def reload
@trees = nil
Expand Down Expand Up @@ -181,8 +183,23 @@ def filter_nil_keys!(path, data, suffix = [])
end
end

def removing_emptied_files(locale, tree, &block)
@trees.delete(locale) if @trees
paths_before = tree_paths(locale, get(locale))
new_paths = {}
block.call do |key, path| # rubocop:disable Performance/RedundantBlockCall
new_paths[key] = path
end
tree.keys(root: true) do |key, node|
node.data[:path] = new_paths[key]
end
(paths_before - tree_paths(locale, tree)).each do |path|
FileUtils.remove_file(path) if File.exist?(path)
end
end

def tree_paths(locale, forest)
Set.new(router.route(locale, forest).map { |path, _| path })
Set.new(forest[locale].leaves.map { |node| node.data[:path] })
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/tasks/data/router/pattern_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def initialize(_adapter, data_config)
end

# Route keys to destinations
# @param forest [I18n::Tasks::LocaleTree::Siblings] forest roots are locales.
# @param forest [I18n::Tasks::Data::Tree::Siblings] forest roots are locales.
# @yieldparam [String] dest_path
# @yieldparam [I18n::Tasks::Data::Tree::Siblings] tree_slice
# @return [Hash] mapping of destination => [ [key, value], ... ]
def route(locale, forest, &block)
return to_enum(:route, locale, forest) unless block
Expand Down
5 changes: 4 additions & 1 deletion spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@
expect(File).to_not exist 'config/locales/devise.en.yml'
run_cmd 'normalize', '--pattern_router'
expect(YAML.load_file('config/locales/devise.en.yml')['en']['devise']['a']).to eq 'EN_TEXT'
# Old value should be removed
expect(File).not_to exist 'config/locales/old_devise.en.yml'
end
end
end
Expand Down Expand Up @@ -325,7 +327,6 @@
},
'numeric' => { 'a' => v_num },
'plural' => { 'a' => { 'one' => v, 'other' => "%{count} #{v}s" } },
'devise' => { 'a' => v },
'scoped' => { 'x' => v },
'very' => { 'scoped' => { 'x' => v } },
'used' => { 'a' => v },
Expand Down Expand Up @@ -362,6 +363,8 @@
fs = fixtures_contents.merge(
'config/locales/en.yml' => { 'en' => en_data }.to_yaml,
'config/locales/es.yml' => { 'es' => es_data }.to_yaml,
'config/locales/old_devise.en.yml' => { 'en' => { 'devise' => { 'a' => 'EN_TEXT' } } }.to_yaml,
'config/locales/old_devise.es.yml' => { 'es' => { 'devise' => { 'a' => 'ES_TEXT' } } }.to_yaml,
'config/locales/unused.en.yml' => { 'en' => { 'unused' => { 'file' => 'EN_TEXT' } } }.to_yaml,
'config/locales/unused.es.yml' => { 'es' => { 'unused' => { 'file' => 'ES_TEXT' } } }.to_yaml,
'config/locales/not-in-write/unused.en.yml' =>
Expand Down

0 comments on commit bc84a3b

Please sign in to comment.