diff --git a/bin/yamlook b/bin/yamlook index 177e9d2..aac440f 100755 --- a/bin/yamlook +++ b/bin/yamlook @@ -7,4 +7,4 @@ require 'yamlook' cli_parser = Yamlook::Cli.new(ARGV) -Yamlook::Search.perform(cli_parser.argument.split('.')) +Yamlook::Search.perform(cli_parser.argument.split(Yamlook::SEPARATOR)) diff --git a/lib/yamlook.rb b/lib/yamlook.rb index 1940ae8..f2c52ff 100644 --- a/lib/yamlook.rb +++ b/lib/yamlook.rb @@ -2,12 +2,14 @@ require 'psych' +require 'yamlook/iteration' require 'yamlook/handler' require 'yamlook/file' require 'yamlook/search' require 'yamlook/cli' module Yamlook + SEPARATOR = '.' LOCALES_FILEPATH = ::File.join(::File.expand_path('..', __dir__), 'locales.yaml') LOCALES = Psych.load_file(LOCALES_FILEPATH)['locales'].freeze end diff --git a/lib/yamlook/handler.rb b/lib/yamlook/handler.rb index 07308af..eee715d 100644 --- a/lib/yamlook/handler.rb +++ b/lib/yamlook/handler.rb @@ -1,66 +1,66 @@ +# frozen_string_literal: true + require 'psych' module Yamlook # Handler for Psych::Parser class Handler < ::Psych::Handler attr_reader :found - + def initialize(keys:, locales: []) + super() @keys = keys @locales = locales @found = [] - @level = -1 - @active = @prev_active = @locale_active = false - @start_line = @start_column = nil - @offset = 0 + + @iterations = [] + @current_iteration = Iteration.new(active: true) end - + def event_location(start_line, start_column, _end_line, _end_column) @start_line = start_line @start_column = start_column end - + def start_mapping(_anchor, _tag, _implicit, _style) - @level += 1 - @prev_active = @active || top_level? - @active = false + @iterations.push(@current_iteration.dup) + @current_iteration.reset! end - + def end_mapping - @level -= 1 - @level -= @offset - @offset = 0 - @locale_active = false if top_level? + @iterations.pop + @current_iteration.reset! end - - def scalar(value, _anchor, _tag, _plain, _quoted, _style) - if current_level == @keys.size.pred && @active && (current_level.zero? || @prev_active) - @found << [value, @start_line.next, @start_column.next] - end - - @active, @offset = match(value) - @level += @offset - @locale_active ||= top_level? && @locales.include?(value) + + def scalar(value, _anchor, _tag, _plain, _quoted, _style) # rubocop:disable Metrics/ParameterLists + @found << [value, @start_line.next, @start_column.next] if keys_out? && all_active? + + refresh_current_interation!(value) end - - def top_level? - @level.zero? + + def refresh_current_interation!(value) + value_keys = value.split(SEPARATOR) + + value_keys.shift if current_offset.zero? && LOCALES.include?(value_keys.first) + + @current_iteration.offset = value_keys.count + @current_iteration.active = current_keys == value_keys end - - def current_level - @locale_active ? @level.pred : @level + + def current_offset + @iterations.sum(&:offset) end - def match(value) - return [false, 0] unless @keys[current_level] + def current_keys + @keys.drop(current_offset).take(@current_iteration.offset) + end - @keys[current_level..-1].each_with_index do |key, index| - if value == @keys[current_level..index+current_level].join('.') - return [true, index] - end - end + def keys_out? + current_offset + @current_iteration.offset == @keys.size + end - [false, 0] + def all_active? + @iterations.any? && @iterations.all?(&:active) && @current_iteration.active end end -end \ No newline at end of file +end diff --git a/lib/yamlook/iteration.rb b/lib/yamlook/iteration.rb new file mode 100644 index 0000000..160c55c --- /dev/null +++ b/lib/yamlook/iteration.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Yamlook + # Holds information for iteration over Psych::Handler iterating through mappings + class Iteration + attr_accessor :active, :offset + + def initialize(active: false, offset: 0) + @active = active + @offset = offset + end + + def reset! + @active = false + @offset = 0 + end + end +end diff --git a/lib/yamlook/search.rb b/lib/yamlook/search.rb index 076c10e..48571f9 100644 --- a/lib/yamlook/search.rb +++ b/lib/yamlook/search.rb @@ -14,7 +14,7 @@ module Search def perform(keys) raise NoArgumentsError, "Nothing to search for.\n" if keys.empty? - findings = Dir.glob(PATTERN).map do |filename| + findings = Dir.glob(PATTERN).flat_map do |filename| result = File.new(filename).search(keys) print_progress(result) result diff --git a/lib/yamlook/version.rb b/lib/yamlook/version.rb index d6a3fcf..43a2383 100644 --- a/lib/yamlook/version.rb +++ b/lib/yamlook/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Yamlook - VERSION = '0.4.0' + VERSION = '0.4.1' end diff --git a/locales.yaml b/locales.yaml index 76742c6..f03ef9a 100644 --- a/locales.yaml +++ b/locales.yaml @@ -1,176 +1,176 @@ locales: -- af -- sq -- am -- ar -- ar-dz -- ar-bh -- ar-eg -- ar-iq -- ar-jo -- ar-kw -- ar-lb -- ar-ly -- ar-ma -- ar-om -- ar-qa -- ar-sa -- ar-sy -- ar-tn -- ar-ae -- ar-ye -- hy -- as -- az -- az-az -- eu -- be -- bn -- bs -- bg -- my -- ca -- zh -- zh-cn -- zh-hk -- zh-mo -- zh-sg -- zh-tw -- hr -- cs -- da -- dv -- nl -- nl-be -- nl-nl -- en -- en-au -- en-bz -- en-ca -- en-cb -- en-gb -- en-in -- en-ie -- en-jm -- en-nz -- en-ph -- en-za -- en-tt -- en-us -- et -- mk -- fo -- fa -- fi -- fr -- fr-be -- fr-ca -- fr-fr -- fr-lu -- fr-ch -- gd -- gd-ie -- gl -- ka -- de -- de-at -- de-de -- de-li -- de-lu -- de-ch -- el -- gn -- gu -- he -- hi -- hu -- is -- id -- it -- it-it -- it-ch -- ja -- kn -- ks -- kk -- km -- ko -- lo -- la -- lv -- lt -- ms -- ms-bn -- ms-my -- ml -- mt -- mi -- mr -- mn -- ne -- nb -- no-no -- nn -- or -- pl -- pt -- pt-br -- pt-pt -- pa -- rm -- ro -- ro-mo -- ru -- ru-mo -- sa -- sr -- sr-sp -- tn -- sd -- si -- sk -- sl -- so -- sb -- es -- es-ar -- es-bo -- es-cl -- es-co -- es-cr -- es-do -- es-ec -- es-sv -- es-gt -- es-hn -- es-mx -- es-ni -- es-pa -- es-py -- es-pe -- es-pr -- es-es -- es-uy -- es-ve -- sw -- sv -- sv-fi -- sv-se -- tg -- ta -- tt -- te -- th -- bo -- ts -- tr -- tk -- uk -- ur -- uz -- uz-uz -- vi -- cy -- xh -- yi -- zu \ No newline at end of file + - af + - sq + - am + - ar + - ar-dz + - ar-bh + - ar-eg + - ar-iq + - ar-jo + - ar-kw + - ar-lb + - ar-ly + - ar-ma + - ar-om + - ar-qa + - ar-sa + - ar-sy + - ar-tn + - ar-ae + - ar-ye + - hy + - as + - az + - az-az + - eu + - be + - bn + - bs + - bg + - my + - ca + - zh + - zh-cn + - zh-hk + - zh-mo + - zh-sg + - zh-tw + - hr + - cs + - da + - dv + - nl + - nl-be + - nl-nl + - en + - en-au + - en-bz + - en-ca + - en-cb + - en-gb + - en-in + - en-ie + - en-jm + - en-nz + - en-ph + - en-za + - en-tt + - en-us + - et + - mk + - fo + - fa + - fi + - fr + - fr-be + - fr-ca + - fr-fr + - fr-lu + - fr-ch + - gd + - gd-ie + - gl + - ka + - de + - de-at + - de-de + - de-li + - de-lu + - de-ch + - el + - gn + - gu + - he + - hi + - hu + - is + - id + - it + - it-it + - it-ch + - ja + - kn + - ks + - kk + - km + - ko + - lo + - la + - lv + - lt + - ms + - ms-bn + - ms-my + - ml + - mt + - mi + - mr + - mn + - ne + - nb + - no-no + - nn + - or + - pl + - pt + - pt-br + - pt-pt + - pa + - rm + - ro + - ro-mo + - ru + - ru-mo + - sa + - sr + - sr-sp + - tn + - sd + - si + - sk + - sl + - so + - sb + - es + - es-ar + - es-bo + - es-cl + - es-co + - es-cr + - es-do + - es-ec + - es-sv + - es-gt + - es-hn + - es-mx + - es-ni + - es-pa + - es-py + - es-pe + - es-pr + - es-es + - es-uy + - es-ve + - sw + - sv + - sv-fi + - sv-se + - tg + - ta + - tt + - te + - th + - bo + - ts + - tr + - tk + - uk + - ur + - uz + - uz-uz + - vi + - cy + - xh + - yi + - zu \ No newline at end of file diff --git a/spec/smoke_tests/dummy/test.yaml b/spec/smoke_tests/dummy/test.yaml index dce47d3..870620b 100644 --- a/spec/smoke_tests/dummy/test.yaml +++ b/spec/smoke_tests/dummy/test.yaml @@ -1,4 +1,23 @@ +wubba.lubba.dub.dub: bar wubba.lubba: - dub.dub: bar -test: - duplicate: true + go.go: + sha: meh + dub.dub: bar1 +wubba.lubba: + dub.dub: bar2 +net: + to: 1 +wubba: + lubba: + dub: + dub: bar3 +en: + wubba.lubba.dub.dub: bar4 +de: + wubba.lubba: + dub.dub: bar5 +nope: + en: + wubba.lubba.dub.dub: nope +en.wubba.lubba.dub.dub: bar6 +test.duplicate: true diff --git a/spec/smoke_tests/search_spec.rb b/spec/smoke_tests/search_spec.rb index d7e23a7..2ecc834 100644 --- a/spec/smoke_tests/search_spec.rb +++ b/spec/smoke_tests/search_spec.rb @@ -30,9 +30,27 @@ it 'searches correctly for dot-notated yaml keys' do assert_output(%r{ - Found\s1\soccurrences:\n - .+dummy/test\.yaml:2:12\n - bar + Found\s7\soccurrences:\n + .+dummy/test.yaml:1:22\n + bar\n + \n + .+dummy/test.yaml:5:12\n + bar1\n + \n + .+dummy/test.yaml:7:12\n + bar2\n + \n + .+dummy/test.yaml:13:12\n + bar3\n + \n + .+dummy/test.yaml:15:24\n + bar4\n + \n + .+dummy/test.yaml:18:14\n + bar5\n + \n + .+dummy/test.yaml:22:25\n + bar6\n }x) do Yamlook::Search.perform(%w[wubba lubba dub dub]) end @@ -45,7 +63,7 @@ .+dummy/test\.yml:7:14\n true\n \n - .+dummy/test\.yaml:4:14\n + .+dummy/test\.yaml:23:17\n true }x ) do