forked from glebm/i18n-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usages.rb
81 lines (67 loc) · 2.74 KB
/
usages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
module I18n::Tasks
module Command
module Commands
module Usages
include Command::Collection
arg :strict,
'--[no-]strict',
t('i18n_tasks.cmd.args.desc.strict')
arg :keep_order,
'-k',
'--keep-order',
t('i18n_tasks.cmd.args.desc.keep_order')
cmd :find,
pos: '[pattern]',
desc: t('i18n_tasks.cmd.desc.find'),
args: %i[out_format pattern strict]
def find(opt = {})
opt[:filter] ||= opt.delete(:pattern) || opt[:arguments].try(:first)
result = i18n.used_tree(strict: opt[:strict], key_filter: opt[:filter].presence, include_raw_references: true)
print_forest result, opt, :used_keys
end
cmd :unused,
pos: '[locale ...]',
desc: t('i18n_tasks.cmd.desc.unused'),
args: %i[locales out_format strict]
def unused(opt = {})
forest = i18n.unused_keys(**opt.slice(:locales, :strict))
print_forest forest, opt, :unused_keys
:exit1 unless forest.empty?
end
cmd :remove_unused,
pos: '[locale ...]',
desc: t('i18n_tasks.cmd.desc.remove_unused'),
args: %i[locales out_format strict keep_order confirm pattern]
def remove_unused(opt = {}) # rubocop:disable Metrics/AbcSize
unused_keys = i18n.unused_keys(**opt.slice(:locales, :strict))
if opt[:pattern]
pattern_re = i18n.compile_key_pattern(opt[:pattern])
unused_keys = unused_keys.select_keys { |full_key, _node| full_key =~ pattern_re }
end
if unused_keys.present?
terminal_report.unused_keys(unused_keys)
confirm_remove_unused!(unused_keys, opt)
i18n.data.config = i18n.data.config.merge(sort: false) if opt[:'keep-order']
removed = i18n.data.remove_by_key!(unused_keys)
log_stderr t('i18n_tasks.remove_unused.removed', count: unused_keys.leaves.count)
print_forest removed, opt
else
log_stderr Rainbow(t('i18n_tasks.remove_unused.noop')).green.bright
end
end
private
def confirm_remove_unused!(unused_keys, opt)
return if ENV['CONFIRM'] || opt[:confirm]
locales = Rainbow(unused_keys.flat_map { |root| root.key.split('+') }.sort.uniq * ', ').bright
msg = [
Rainbow(t('i18n_tasks.remove_unused.confirm', count: unused_keys.leaves.count, locales: locales)).red,
Rainbow(t('i18n_tasks.common.continue_q')).yellow,
Rainbow('(yes/no)').yellow
].join(' ')
exit 1 unless agree msg
end
end
end
end
end