Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Last active January 16, 2017 05:48
Show Gist options
  • Save NigelThorne/dc40741a4a57dda56a12e4b797c70884 to your computer and use it in GitHub Desktop.
Save NigelThorne/dc40741a4a57dda56a12e4b797c70884 to your computer and use it in GitHub Desktop.
AdHoc Scripts to compare regex files and look for strings that were not translated correctly.
if(ARGV.count < 2)
puts "Usage: #{__FILE__} file1.resx file2.resx"
exit -1
end
if(`gem list`.scan(/nokogiri/).count == 0)
puts "You need to install nokogiri. Type 'gem install nokogiri' at a command prompt"
exit -1
end
require 'nokogiri'
file1, file2 = *ARGV
def checkFile(file)
if(!File.exist?(file))
puts "File #{file} is missing."
exit -1
end
end
def to_hashs(filename)
Nokogiri::XML(File.read(filename)).xpath("/root/data").to_a.each_with_object(Hash.new) do
|node, hash|
hash[node.attr("name")] = {name: node.attr("name"), value: node.css("value").text, comment: node.css("comment").text}
end
end
def title(str)
puts
puts str
puts ("=" * str.length)
end
def keys(key_values)
puts key_values
end
def records(recs)
if(recs.empty?)
puts "-- nothing --"
else
recs.each{|r| puts "#{r[:name]}\n\tvalue: '#{r[:value]}'\n\tcomment: '#{r[:comment]}'\n"}
end
end
def records_compare(recs)
if(recs.empty?)
puts "-- nothing --"
else
recs.each{|r| puts "#{r[:name]}\n\tvalue: '#{r[:value]}'\n\tcomment: '#{r[:comment]}'\n"}
end
end
def can_ignore_node(node)
!!(node[:comment] =~ /Unused./i) ||
!!(node[:comment] =~ /english only/i)
end
def keys_to_records(keys, hash)
keys.sort.map{|k| hash[k] }.reject{|node| can_ignore_node(node)}
end
# validation
checkFile(file1)
checkFile(file2)
# calculation
hash1 = to_hashs(file1)
hash2 = to_hashs(file2)
only_in_1 = hash1.keys - hash2.keys
only_in_2 = hash2.keys - hash1.keys
matching_in_both = (hash1.keys & hash2.keys).select{|k| hash1[k][:value] == hash2[k][:value]}
# output
title "Comparing #{file1} to #{file2}"
title "Records only in #{file1}"
records (keys_to_records only_in_1, hash1)
title "Records only in #{file2}"
records (keys_to_records only_in_2, hash2)
title "Records that match in #{file1} and #{file2}"
records (keys_to_records matching_in_both, hash1)
if(ARGV.count < 2)
puts "Usage: #{__FILE__} file1.resx file2.resx"
exit -1
end
if(`gem list`.scan(/nokogiri/).count == 0)
puts "You need to install nokogiri. Type 'gem install nokogiri' at a command prompt"
exit -1
end
require 'nokogiri'
file1, file2, file3 = *ARGV
def checkFile(file)
if(!File.exist?(file))
puts "File #{file} is missing."
exit -1
end
end
def to_hashs(filename)
Nokogiri::XML(File.read(filename)).xpath("/root/data").to_a.each_with_object(Hash.new) do
|node, hash|
hash[node.attr("name")] = {name: node.attr("name"), value: node.css("value").text, comment: node.css("comment").text}
end
end
@output= ""
def puts(string)
val = string
val += "\n" unless string[-1] == "\n"
@output += val
end
def title(str)
puts
puts str
puts ("=" * str.length)
end
def keys(key_values)
puts key_values
end
def csv_val(val)
"\"#{val.gsub(/"/, '""')}\""
end
def records(recs)
if(recs.empty?)
puts "-- nothing --"
else
puts "Key, Value, Comment"
recs.each{|r| puts "#{csv_val r[:name]},#{csv_val r[:value]},#{csv_val r[:comment]}"}
end
end
def records_compare(recs)
if(recs.empty?)
puts "-- nothing --"
else
recs.each{|r| puts "#{r[:name]}\n\tvalue: '#{r[:value]}'\n\tcomment: '#{r[:comment]}'\n"}
end
end
def can_ignore_node(node)
!!(node[:comment] =~ /Unused./i) ||
!!(node[:comment] =~ /english only/i)
end
def keys_to_records(keys, hash)
keys.sort.map{|k| hash[k] }.reject{|node| can_ignore_node(node)}
end
# validation
checkFile(file1)
checkFile(file2)
# calculation
hash1 = to_hashs(file1)
hash2 = to_hashs(file2)
only_in_1 = hash1.keys - hash2.keys
only_in_2 = hash2.keys - hash1.keys
matching_in_both = (hash1.keys & hash2.keys).select{|k| hash1[k][:value] == hash2[k][:value]}
# output
#title "Comparing #{file1} to #{file2}"
#title "Records only in #{file1}"
records (keys_to_records only_in_1, hash1)
#title "Records only in #{file2}"
#records (keys_to_records only_in_2, hash2)
#title "Records that match in #{file1} and #{file2}"
#records (keys_to_records matching_in_both, hash1)
File.open(file3, "w:utf-8") { |file| file.write(@output) }
if(ARGV.count != 1)
puts "Usage: #{__FILE__} file.resx"
exit -1
end
if(`gem list`.scan(/nokogiri/).count == 0)
puts "You need to install nokogiri. Type 'gem install nokogiri' at a command prompt"
exit -1
end
require 'nokogiri'
file1, rest = *ARGV
def checkFile(file)
if(!File.exist?(file))
puts "File #{file} is missing."
exit -1
end
end
def to_hashs(filename)
Nokogiri::XML(File.read(filename)).xpath("/root/data").to_a.each_with_object(Hash.new) do
|node, hash|
hash[node.attr("name")] = {name: node.attr("name"), value: node.css("value").text, comment: node.css("comment").text}
end
end
def title(str)
puts
puts str
puts ("=" * str.length)
end
def keys(key_values)
puts key_values
end
def records(recs)
if(recs.empty?)
puts "-- nothing --"
else
recs.each{|r| puts "#{r[:name]}\n\tvalue: '#{r[:value]}'\n\tcomment: '#{r[:comment]}'\n"}
end
end
def records_compare(recs)
if(recs.empty?)
puts "-- nothing --"
else
recs.each{|r| puts "#{r[:name]}\n\tvalue: '#{r[:value]}'\n\tcomment: '#{r[:comment]}'\n"}
end
end
def can_ignore_node(node)
!!(node[:comment] =~ /Unused./i) ||
!!(node[:comment] =~ /english only/i)
end
def keys_to_records(keys, hash)
keys.sort.map{|k| hash[k] }.reject{|node| can_ignore_node(node)}
end
# validation
checkFile(file1)
# calculation
hash1 = to_hashs(file1)
results = hash1.keys.select{|k| hash1[k][:value] =~ /III/i || hash1[k][:value] =~ /MAX/i }
# output
title "Checking #{file1}"
records (keys_to_records results, hash1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment