grepo 0.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/grepo.rb +214 -0
  3. metadata +49 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 375e6bcd160f07c851d5511894f91cc7fe837b44
4
+ data.tar.gz: 6619813c6d37f0d8c5c375714cfd077082bdfdce
5
+ SHA512:
6
+ metadata.gz: bdee6c5e354f3106c2479fcfe55f231f343e9aaeff701759620610adb0d21730fe0401530474f29bff9b9003f63a115b839bcebce86c55f7182c471c05d1fb6f
7
+ data.tar.gz: 3582a6188fb5c738cfe74904193e4a0dc6a8fdb194baa4f25d7e6fde43a5bf016167ae7d6a92a7086cf9b0219ba981fd2f4a772493a98d333a6aaffb070e3e62
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json'
3
+ require 'yaml'
4
+ require_relative 'extend_json'
5
+
6
+ # grepo find config/locales/hr.yml app/
7
+ # grepo compare config/locales/hr.yml config/locales/en.yml
8
+ # grepo duplicates config/locales/hr.yml
9
+ #
10
+
11
+ class Grepo
12
+ def find(str_located_in='config/locales/en.yml', search_in='app/', options={})
13
+ @target = search_in
14
+ file = str_located_in
15
+ @text = File.read(file)
16
+ @type = str_located_in.split(".").last || "yml"
17
+
18
+ # for rails it is 1 bc 0 would search hr.profile.errors.title, depth = 2 would search for errors.title
19
+ @depth = options[:depth] || 1
20
+
21
+ # for example profile.new_post.title - this will be used to grep
22
+ @grep_for = []
23
+
24
+ if @type == "yml"
25
+ whole_file = YAML.load(@text)
26
+ elsif @type == "json"
27
+ whole_file = JSON.parse(@text)
28
+ end
29
+
30
+ whole_file.each do |key, value|
31
+ if value.is_a? String
32
+ @grep_for << "#{key}"
33
+ else
34
+ value.each do |k, v|
35
+ go_deeper(1, key, {key: k, value: v})
36
+ end
37
+ end
38
+ end
39
+
40
+ puts "In #{@target} there are no:"
41
+ @grep_for.each do |l|
42
+ if `grep -r "t('#{l}')" #{@target}`.length == 0 and `grep -r 't(\"#{l}\")' #{@target}`.length == 0
43
+ puts " #{l}"
44
+ end
45
+ end
46
+ end
47
+
48
+ def compare(file_one, file_two='config/locales/en.yml', options={})
49
+ @for_compare_one = []
50
+ @for_compare_two = []
51
+ # for rails it is 1 bc 0 would compare hr.profile.errors.title and en.profile.errors.title - there are not same
52
+ # profile.errors.title and profile.errors.title without en and hr are the same
53
+ @depth = options[:depth] || 1
54
+
55
+ make_file(file_one, @for_compare_one)
56
+ make_file(file_two, @for_compare_two)
57
+
58
+ # what IS NOT in en.yml
59
+ difference = @for_compare_two - @for_compare_one
60
+ if difference.count > 0
61
+ puts "Strings that are in #{file_two} and not in #{file_one} are:"
62
+ difference.each do |dif|
63
+ puts " #{dif}"
64
+ end
65
+ else
66
+ puts "Same strings in #{file_one} and #{file_two}"
67
+ end
68
+ end
69
+
70
+ def find_duplicates(file='config/locales/en.yml')
71
+ @type = file.split(".").last || "yml"
72
+ if @type == "yml"
73
+ formated_lines = []
74
+ current_yaml_keys = {}
75
+ line_number = 0;
76
+ previous_indentation_level = -1
77
+
78
+ # go through each line of the file
79
+ File.readlines(file).map do |line|
80
+ line_number += 1
81
+ # letters without strings that come after ':'
82
+ letters = line.split(":")[0].split("") # =~ /\A\s*\Z/
83
+
84
+ indentation_level=0
85
+ letters.each do |letter|
86
+ # if letter is an empty string count the indentation
87
+ if letter =~ /\A\s*\Z/
88
+ indentation_level += 1
89
+ else
90
+ # break if there is an error in key - an empty space in key
91
+ break
92
+ end
93
+ end
94
+
95
+ # check if there are any indentation errors - all keys are even
96
+ if indentation_level % 2 != 0
97
+ puts "There is an indentation error on line #{line_number} "
98
+ break
99
+ end
100
+
101
+ # if the previous indentation level is bigger than current than it is a new key that is NOT nested in the last one
102
+ # if it is smaller then it is a new key but nested in the last
103
+ if previous_indentation_level > indentation_level
104
+ # remove key in that the new key is not nested
105
+ current_yaml_keys.delete_if{ |key, value| key.to_i >= indentation_level }
106
+ end
107
+
108
+ # add the key to and his indentation_level
109
+ current_yaml_keys["#{indentation_level}"] = letters.delete_if{|l| l == " "}.join("")
110
+
111
+ # formated line is for example hr.our_homepage.title
112
+ formated_line = ""
113
+ current_yaml_keys.each do |key, value|
114
+ formated_line += "." unless key=="0"
115
+ formated_line += value
116
+ end
117
+ formated_lines << formated_line
118
+
119
+ # indentation_level will be previous_indentation_level in next iterration
120
+ previous_indentation_level=indentation_level
121
+ end
122
+
123
+ # report duplicated
124
+ duplicated_lines = formated_lines.find_all { |x| formated_lines.count(x) > 1 }.uniq
125
+ if duplicated_lines.count > 0
126
+ puts "There are some duplicated: "
127
+ duplicated_lines.each do |line|
128
+ puts " #{line}"
129
+ end
130
+ else
131
+ puts "There are no duplicates!"
132
+ end
133
+ elsif @type == "json"
134
+ JSON.parser = JSON::Ext::Parser
135
+ text = File.read(file)
136
+ whole_file = JSON.parse(text)
137
+
138
+ # DuplicateKeyChecker will find double keys and values while json is parsing
139
+ duplicated_keys = JSON.parse(text, { object_class:DuplicateKeyChecker }).get_duplicates
140
+
141
+ # now find keys in format: hr.form.something
142
+ @grep_for = []
143
+ @depth = 0
144
+ whole_file.each do |key, value|
145
+ if value.is_a? String
146
+ @grep_for << "#{key}"
147
+ else
148
+ value.each do |k, v|
149
+ go_deeper(1, key, {key: k, value: v, add_value: true})
150
+ end
151
+ end
152
+ end
153
+
154
+ # now find out which keys are repeating and print them out like: hr.form.whatever
155
+ duplicated_keys.map do |dk|
156
+ puts "There are some duplicated:"
157
+ if dk[:value].is_a? String
158
+ # print double keys that haw no more nested keys
159
+ look_for = @grep_for.find{|str| str.include? "#{dk[:key]}:#{dk[:value].delete(" ")}"}
160
+
161
+ puts " #{look_for.split(":")[0]}" if look_for
162
+ else
163
+ # print double keys that have nested keys
164
+ dk[:value].keys.map{|v| "#{dk[:key]}.#{v}"}.each do |sub_str|
165
+ look_for = @grep_for.find{|str| str.include? sub_str}
166
+ puts " #{look_for.split(":")[0]}" if look_for
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def go_deeper(level, parents, options = {}, push_to=@grep_for)
176
+ if options[:value].is_a? String
177
+ # value is needed for comparing json duplicates - must be without white spaces
178
+ value = ":#{options[:value].delete(" ")}"
179
+ push_to << "#{parents}.#{options[:key]}#{options[:add_value] ? value : "" }".split(".")[@depth..-1].join(".")
180
+ else
181
+ level += 1
182
+ options[:value].each do |k, v|
183
+ new_parents = "#{parents}.#{options[:key]}"
184
+ go_deeper(level, new_parents, {key: k, value: v, add_value: options[:add_value]}, push_to)
185
+ end
186
+ end
187
+ end
188
+
189
+ def make_file(file, compare)
190
+ text = File.read(file)
191
+ type = file.split(".").last || "yml"
192
+ if type == "yml"
193
+ whole_file = YAML.load(text)
194
+ elsif type == "json"
195
+ whole_file = JSON.parse(text)
196
+ end
197
+
198
+ whole_file.each do |key, value|
199
+ if value.is_a? String
200
+ compare << "#{key}"
201
+ else
202
+ value.each do |k, v|
203
+ go_deeper(1, key, {key: k, value: v}, compare)
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
209
+
210
+ #Grepo.new.find("config/locales/en.yml", "app/", {depth: 1} )
211
+ #Grepo.new.find("config/locales/en.yml", "umye_cms/app/", {depth: 0})
212
+ #Grepo.new.compare("config/locales/hr.yml", "config/locales/en.yml", {depth: 1})
213
+ #Grepo.new.compare("config/locales/hr.yml")
214
+ Grepo.new.find_duplicates("config/locales/en.yml")
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grepo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mislav Kvesić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Grepo is a small gem that helps you to clean up your yaml and json files.
14
+ It's usefull if you are having your translations in yaml or json files especially
15
+ if the files are numerous and have a lot of lines. It can detect keys you aren’t
16
+ using, can find duplicated keys and compere two files to find the missing keys.
17
+ It won’t remove the targeted keys neither it will alter your files in any way, just
18
+ grep for possible problems.
19
+ email: kvesic.mislav@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/grepo.rb
25
+ homepage: http://rubygems.org/gems/grepo
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.14
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Grepo will grep for yaml and json keys
49
+ test_files: []