forked from glebm/i18n-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.rb
102 lines (85 loc) · 3.53 KB
/
tree.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
module I18n::Tasks
module Command
module Commands
module Tree
include Command::Collection
include I18n::Tasks::KeyPatternMatching
cmd :tree_translate,
pos: '[tree (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_translate'),
args: [:locale_to_translate_from, arg(:data_format).from(1), :translation_backend]
def tree_translate(opts = {})
forest = forest_pos_or_stdin!(opts)
print_forest i18n.translate_forest(forest, from: opts[:from], backend: opts[:backend].to_sym), opts
end
cmd :tree_merge,
pos: '[[tree] [tree] ... (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_merge'),
args: %i[data_format nostdin]
def tree_merge(opts = {})
print_forest merge_forests_stdin_and_pos!(opts), opts
end
cmd :tree_filter,
pos: '[pattern] [tree (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_filter'),
args: %i[data_format pattern]
def tree_filter(opts = {})
pattern = arg_or_pos! :pattern, opts
forest = forest_pos_or_stdin! opts
unless pattern.blank?
pattern_re = i18n.compile_key_pattern(pattern)
forest = forest.select_keys { |full_key, _node| full_key =~ pattern_re }
end
print_forest forest, opts
end
arg :all_locales,
'-a',
'--all-locales',
t('i18n_tasks.cmd.args.desc.all_locales')
cmd :tree_mv,
pos: 'FROM_KEY_PATTERN TO_KEY_PATTERN [tree (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_mv_key'),
args: %i[data_format all_locales]
def tree_mv(opt = {})
fail CommandError, 'requires FROM_KEY_PATTERN and TO_KEY_PATTERN' if opt[:arguments].size < 2
from_pattern = opt[:arguments].shift
to_pattern = opt[:arguments].shift
forest = forest_pos_or_stdin!(opt)
forest.mv_key!(compile_key_pattern(from_pattern), to_pattern, root: !opt[:'all-locales'])
print_forest forest, opt
end
cmd :tree_subtract,
pos: '[[tree] [tree] ... (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_subtract'),
args: %i[data_format nostdin]
def tree_subtract(opt = {})
forests = forests_stdin_and_pos! opt, 2
forest = forests.reduce(:subtract_by_key) || empty_forest
print_forest forest, opt
end
cmd :tree_set_value,
pos: '[VALUE] [tree (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_set_value'),
args: %i[value data_format nostdin pattern]
def tree_set_value(opt = {})
value = arg_or_pos! :value, opt
forest = forest_pos_or_stdin!(opt)
key_pattern = opt[:pattern]
fail CommandError, 'pass value (-v, --value)' if value.blank?
forest.set_each_value!(value, key_pattern)
print_forest forest, opt
end
cmd :tree_convert,
pos: '[tree (or stdin)]',
desc: t('i18n_tasks.cmd.desc.tree_convert'),
args: [arg(:data_format).dup.tap { |a| a[0..1] = ['-f', '--from FORMAT'] },
arg(:out_format).dup.tap { |a| a[0..1] = ['-t', '--to FORMAT'] }]
def tree_convert(opt = {})
forest = forest_pos_or_stdin! opt.merge(format: opt[:from])
print_forest forest, opt.merge(format: opt[:to])
end
end
end
end
end