yamlook 0.1.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8d60c103a1ccc580ea4ef6e0d3e2f5f053caae8a43cf43938faa120b953a5c6
4
- data.tar.gz: 7056f0a884fd99fc881360bd6c2945554699995546e2de3e81c73187613d4994
3
+ metadata.gz: 4ff790519841c559a55d2502b075037d0c6f8e9c7b204ddccc24a7940bd9c64c
4
+ data.tar.gz: c3de76314369e83a5bce053c194108987fbe3cb09884325552b3cdfcecec932a
5
5
  SHA512:
6
- metadata.gz: 1589f85031a782dc1e269d857f77f4b2ef686fb34823334cc0bf0a722bfc48545b69f8b121822e20855948bfd34180394743c691286bc63574ef47f9ff6bafdb
7
- data.tar.gz: 76fedaacd5a13e16ef5d1feab2604c91b275047873e80e303242c9444cced2b04e2a51e3c77d3ddd9e1f8af8dac8b774122d0aecdd2e084edbd0aeb804026a9b
6
+ metadata.gz: c92be1c326364a5e48bfe927738e6fab7c1ebc7b1621f7a29388eb9addf9b8de75fe55c5a234f9a280d61b640f66890ffd11784d67ab6c00e20b4d38b82d266c
7
+ data.tar.gz: b7efd21a23b6bee5624b07a7eb4ca69c6f3096e8f5b0c626f8a962c6be81b48cc6910a105d4e956b58aa102e2618bae58f54e66a719d6fac0fae8562eacc6678
data/README.md CHANGED
@@ -5,7 +5,7 @@ or deep configs and you don't know where one or another value comes from.
5
5
 
6
6
  For instance you have such code:
7
7
  ```
8
- t("admin.marketing.reports.some_report.title"), some_report_path(format: "csv")
8
+ <%= link_to t("admin.marketing.reports.some_report.title"), some_report_path(format: "csv") %>
9
9
  ```
10
10
  Run `yamlook admin.marketing.reports.some_report.title` in terminal and it will show up all occurrences of that value
11
11
  in your internationalization yaml files. If you have all the internationalization in one yaml file, you will likely
@@ -26,7 +26,7 @@ $ yamlook some.deep.key.in.you.yaml.file
26
26
 
27
27
  ## Contributing
28
28
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[sl4vr]/yamlook.
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sl4vr/yamlook.
30
30
 
31
31
 
32
32
  ## License
data/bin/yamlook CHANGED
@@ -5,4 +5,6 @@ $LOAD_PATH.unshift("#{__dir__}/../lib")
5
5
 
6
6
  require 'yamlook'
7
7
 
8
- Yamlook::Search.perform(ARGV[0].split('.'))
8
+ cli_parser = Yamlook::Cli.new(ARGV)
9
+
10
+ Yamlook::Search.perform(cli_parser.argument.split(Yamlook::SEPARATOR))
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require 'yamlook/version'
5
+
6
+ module Yamlook
7
+ # Yamlook CLI
8
+ class Cli
9
+ attr_reader :arguments, :options
10
+
11
+ def initialize(arguments)
12
+ @arguments = arguments
13
+ @options = {}
14
+
15
+ opt_parser.parse!(@arguments)
16
+
17
+ show_help! if @arguments.empty?
18
+ rescue OptionParser::InvalidOption => e
19
+ puts e
20
+ exit 1
21
+ end
22
+
23
+ def argument
24
+ @arguments.first
25
+ end
26
+
27
+ private
28
+
29
+ # rubocop:disable Metrics/MethodLength
30
+ def opt_parser
31
+ @opt_parser ||= OptionParser.new do |opts|
32
+ opts.banner = 'Usage: yamlook KEYS'
33
+ opts.separator ''
34
+ opts.separator 'Example: yamlook some.deep.key.in.you.yaml.file'
35
+
36
+ opts.separator ''
37
+ opts.separator 'Options:'
38
+ opts.on_tail('-h', '--help', 'Show this message') do
39
+ puts opts.help
40
+ exit
41
+ end
42
+
43
+ opts.on_tail('--version', 'Show version') do
44
+ puts Yamlook::VERSION
45
+ exit
46
+ end
47
+ end
48
+ end
49
+ # rubocop:enable Metrics/MethodLength
50
+
51
+ def show_help!
52
+ puts opt_parser.help
53
+ exit
54
+ end
55
+ end
56
+ end
data/lib/yamlook/file.rb CHANGED
@@ -10,34 +10,17 @@ module Yamlook
10
10
  end
11
11
 
12
12
  def search(keys)
13
- return unless yaml
13
+ file = ::File.read(filename)
14
+ handler = Handler.new(keys: keys, locales: LOCALES)
15
+ parser = ::Psych::Parser.new(handler)
14
16
 
15
- findings = root_node_list.search(keys).flatten.compact.map do |finding|
16
- finding.filename = filename
17
- finding
17
+ findings = parser.parse(file).handler.found.map do |value, line, column|
18
+ "#{filename}:#{line}:#{column}\n#{value}"
18
19
  end
19
20
 
20
21
  findings if findings.any?
21
- end
22
-
23
- def yaml
24
- @yaml ||= parse_file(filename)
25
- end
26
-
27
- private
28
-
29
- def parse_file(filename)
30
- ::Psych.parse_file(filename)
31
22
  rescue ::Psych::Exception
32
23
  nil
33
24
  end
34
-
35
- def root_node_list
36
- NodeList.new(root_mapping.children)
37
- end
38
-
39
- def root_mapping
40
- yaml.children.first
41
- end
42
25
  end
43
26
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+
5
+ module Yamlook
6
+ # Handler for Psych::Parser
7
+ class Handler < ::Psych::Handler
8
+ attr_reader :found
9
+
10
+ def initialize(keys:, locales: [])
11
+ super()
12
+ @keys = keys
13
+ @locales = locales
14
+ @found = []
15
+
16
+ @iterations = []
17
+ @current_iteration = Iteration.new(active: true)
18
+ end
19
+
20
+ def event_location(start_line, start_column, _end_line, _end_column)
21
+ @start_line = start_line
22
+ @start_column = start_column
23
+ end
24
+
25
+ def start_mapping(_anchor, _tag, _implicit, _style)
26
+ @iterations.push(@current_iteration.dup)
27
+ @current_iteration.reset!
28
+ end
29
+
30
+ def end_mapping
31
+ @iterations.pop
32
+ @current_iteration.reset!
33
+ end
34
+
35
+ def scalar(value, _anchor, _tag, _plain, _quoted, _style) # rubocop:disable Metrics/ParameterLists
36
+ @found << [value, @start_line.next, @start_column.next] if keys_out? && all_active?
37
+
38
+ refresh_current_interation!(value)
39
+ end
40
+
41
+ def refresh_current_interation!(value)
42
+ value_keys = value.split(SEPARATOR)
43
+
44
+ value_keys.shift if current_offset.zero? && LOCALES.include?(value_keys.first)
45
+
46
+ @current_iteration.offset = value_keys.count
47
+ @current_iteration.active = current_keys == value_keys
48
+ end
49
+
50
+ def current_offset
51
+ @iterations.sum(&:offset)
52
+ end
53
+
54
+ def current_keys
55
+ @keys.drop(current_offset).take(@current_iteration.offset)
56
+ end
57
+
58
+ def keys_out?
59
+ current_offset + @current_iteration.offset == @keys.size
60
+ end
61
+
62
+ def all_active?
63
+ @iterations.any? && @iterations.all?(&:active) && @current_iteration.active
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yamlook
4
+ # Holds information for iteration over Psych::Handler iterating through mappings
5
+ class Iteration
6
+ attr_accessor :active, :offset
7
+
8
+ def initialize(active: false, offset: 0)
9
+ @active = active
10
+ @offset = offset
11
+ end
12
+
13
+ def reset!
14
+ @active = false
15
+ @offset = 0
16
+ end
17
+ end
18
+ end
@@ -4,19 +4,25 @@ module Yamlook
4
4
  # Searches for occurrences of dot-notated keys in all yaml files
5
5
  # of current directory
6
6
  module Search
7
+ NoArgumentsError = Class.new(ArgumentError)
8
+
7
9
  EXTENSIONS = %w[yml yaml].freeze
8
10
  PATTERN = EXTENSIONS.map { |ext| ::File.join('**', "*.#{ext}") }.freeze
9
11
 
10
12
  module_function
11
13
 
12
14
  def perform(keys)
13
- findings = Dir.glob(PATTERN).map do |filename|
15
+ raise NoArgumentsError, "Nothing to search for.\n" if keys.empty?
16
+
17
+ findings = Dir.glob(PATTERN).flat_map do |filename|
14
18
  result = File.new(filename).search(keys)
15
19
  print_progress(result)
16
20
  result
17
21
  end
18
22
 
19
23
  print_result(findings.compact)
24
+ rescue NoArgumentsError => e
25
+ puts e.message
20
26
  end
21
27
 
22
28
  def print_progress(result)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yamlook
4
- VERSION = '0.1.1'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/yamlook.rb CHANGED
@@ -2,7 +2,14 @@
2
2
 
3
3
  require 'psych'
4
4
 
5
- require 'yamlook/node'
6
- require 'yamlook/node_list'
5
+ require 'yamlook/iteration'
6
+ require 'yamlook/handler'
7
7
  require 'yamlook/file'
8
8
  require 'yamlook/search'
9
+ require 'yamlook/cli'
10
+
11
+ module Yamlook
12
+ SEPARATOR = '.'
13
+ LOCALES_FILEPATH = ::File.join(::File.expand_path('..', __dir__), 'locales.yaml')
14
+ LOCALES = Psych.load_file(LOCALES_FILEPATH)['locales'].freeze
15
+ end
data/locales.yaml ADDED
@@ -0,0 +1,176 @@
1
+ locales:
2
+ - af
3
+ - sq
4
+ - am
5
+ - ar
6
+ - ar-dz
7
+ - ar-bh
8
+ - ar-eg
9
+ - ar-iq
10
+ - ar-jo
11
+ - ar-kw
12
+ - ar-lb
13
+ - ar-ly
14
+ - ar-ma
15
+ - ar-om
16
+ - ar-qa
17
+ - ar-sa
18
+ - ar-sy
19
+ - ar-tn
20
+ - ar-ae
21
+ - ar-ye
22
+ - hy
23
+ - as
24
+ - az
25
+ - az-az
26
+ - eu
27
+ - be
28
+ - bn
29
+ - bs
30
+ - bg
31
+ - my
32
+ - ca
33
+ - zh
34
+ - zh-cn
35
+ - zh-hk
36
+ - zh-mo
37
+ - zh-sg
38
+ - zh-tw
39
+ - hr
40
+ - cs
41
+ - da
42
+ - dv
43
+ - nl
44
+ - nl-be
45
+ - nl-nl
46
+ - en
47
+ - en-au
48
+ - en-bz
49
+ - en-ca
50
+ - en-cb
51
+ - en-gb
52
+ - en-in
53
+ - en-ie
54
+ - en-jm
55
+ - en-nz
56
+ - en-ph
57
+ - en-za
58
+ - en-tt
59
+ - en-us
60
+ - et
61
+ - mk
62
+ - fo
63
+ - fa
64
+ - fi
65
+ - fr
66
+ - fr-be
67
+ - fr-ca
68
+ - fr-fr
69
+ - fr-lu
70
+ - fr-ch
71
+ - gd
72
+ - gd-ie
73
+ - gl
74
+ - ka
75
+ - de
76
+ - de-at
77
+ - de-de
78
+ - de-li
79
+ - de-lu
80
+ - de-ch
81
+ - el
82
+ - gn
83
+ - gu
84
+ - he
85
+ - hi
86
+ - hu
87
+ - is
88
+ - id
89
+ - it
90
+ - it-it
91
+ - it-ch
92
+ - ja
93
+ - kn
94
+ - ks
95
+ - kk
96
+ - km
97
+ - ko
98
+ - lo
99
+ - la
100
+ - lv
101
+ - lt
102
+ - ms
103
+ - ms-bn
104
+ - ms-my
105
+ - ml
106
+ - mt
107
+ - mi
108
+ - mr
109
+ - mn
110
+ - ne
111
+ - nb
112
+ - no-no
113
+ - nn
114
+ - or
115
+ - pl
116
+ - pt
117
+ - pt-br
118
+ - pt-pt
119
+ - pa
120
+ - rm
121
+ - ro
122
+ - ro-mo
123
+ - ru
124
+ - ru-mo
125
+ - sa
126
+ - sr
127
+ - sr-sp
128
+ - tn
129
+ - sd
130
+ - si
131
+ - sk
132
+ - sl
133
+ - so
134
+ - sb
135
+ - es
136
+ - es-ar
137
+ - es-bo
138
+ - es-cl
139
+ - es-co
140
+ - es-cr
141
+ - es-do
142
+ - es-ec
143
+ - es-sv
144
+ - es-gt
145
+ - es-hn
146
+ - es-mx
147
+ - es-ni
148
+ - es-pa
149
+ - es-py
150
+ - es-pe
151
+ - es-pr
152
+ - es-es
153
+ - es-uy
154
+ - es-ve
155
+ - sw
156
+ - sv
157
+ - sv-fi
158
+ - sv-se
159
+ - tg
160
+ - ta
161
+ - tt
162
+ - te
163
+ - th
164
+ - bo
165
+ - ts
166
+ - tr
167
+ - tk
168
+ - uk
169
+ - ur
170
+ - uz
171
+ - uz-uz
172
+ - vi
173
+ - cy
174
+ - xh
175
+ - yi
176
+ - zu
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yamlook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viacheslav Mefodin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-07 00:00:00.000000000 Z
11
+ date: 2021-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
@@ -72,11 +72,13 @@ files:
72
72
  - README.md
73
73
  - bin/yamlook
74
74
  - lib/yamlook.rb
75
+ - lib/yamlook/cli.rb
75
76
  - lib/yamlook/file.rb
76
- - lib/yamlook/node.rb
77
- - lib/yamlook/node_list.rb
77
+ - lib/yamlook/handler.rb
78
+ - lib/yamlook/iteration.rb
78
79
  - lib/yamlook/search.rb
79
80
  - lib/yamlook/version.rb
81
+ - locales.yaml
80
82
  homepage: https://github.com/sl4vr/yamlook
81
83
  licenses:
82
84
  - MIT
@@ -96,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  - !ruby/object:Gem::Version
97
99
  version: '0'
98
100
  requirements: []
99
- rubygems_version: 3.1.2
101
+ rubygems_version: 3.1.4
100
102
  signing_key:
101
103
  specification_version: 4
102
104
  summary: Search occurrences of dot-notated yaml keys.
data/lib/yamlook/node.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Yamlook
4
- # Result of search - scalar node
5
- class Node
6
- attr_reader :value, :line, :column
7
- attr_accessor :filename
8
-
9
- def self.from_scalar(scalar)
10
- return unless scalar
11
-
12
- new(
13
- value: scalar.value,
14
- line: scalar.start_line + 1,
15
- column: scalar.start_column + 1
16
- )
17
- end
18
-
19
- def initialize(value:, line:, column:, filename: nil)
20
- @value = value
21
- @line = line
22
- @column = column
23
- @filename = filename
24
- end
25
-
26
- def to_s
27
- "#{filename}:#{line}:#{column}\n#{value}"
28
- end
29
- end
30
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Yamlook
4
- Mapping = ::Psych::Nodes::Mapping
5
- Scalar = ::Psych::Nodes::Scalar
6
-
7
- # List of yaml nodes
8
- class NodeList
9
- attr_reader :nodes
10
-
11
- def self.from_mapping(mapping)
12
- new(mapping.children)
13
- end
14
-
15
- def initialize(nodes)
16
- @nodes = nodes
17
- end
18
-
19
- def search(keys)
20
- keys.each_index.map do |index|
21
- key = keys[0..index].join('.')
22
- rest_keys = keys[index + 1..-1]
23
- result = find(key)
24
-
25
- case result
26
- when Mapping then NodeList.from_mapping(result).search(rest_keys)
27
- when Scalar then Node.from_scalar(result) if rest_keys.empty?
28
- end
29
- end
30
- end
31
-
32
- private
33
-
34
- def find(key)
35
- key_node_index = find_key_node_index(key)
36
- nodes[key_node_index + 1] if key_node_index
37
- end
38
-
39
- def find_key_node_index(key)
40
- nodes.find_index { |node| node.is_a?(Scalar) && node.value == key }
41
- end
42
- end
43
- end