|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# |
| 3 | +# Copyright (c) 2011 Yazz D. Atlas |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +# a copy of this software and associated documentation files (the |
| 7 | +# "Software"), to deal in the Software without restriction, including |
| 8 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +# permit persons to whom the Software is furnished to do so, subject to |
| 11 | +# the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be |
| 14 | +# included in all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +require 'rubygems' |
| 25 | +require "bundler/setup" |
| 26 | +require 'json' |
| 27 | +require 'yaml' |
| 28 | + |
| 29 | +class Hash |
| 30 | + # Replacing the to_yaml function so it'll serialize hashes sorted (by their keys) |
| 31 | + # |
| 32 | + # Original function is in /usr/lib/ruby/1.8/yaml/rubytypes.rb |
| 33 | + # |
| 34 | + # Credit goes to Dov Murik http://snippets.dzone.com/user/dubek |
| 35 | + # |
| 36 | + def to_yaml( opts = {} ) |
| 37 | + YAML::quick_emit( object_id, opts ) do |out| |
| 38 | + out.map( taguri, to_yaml_style ) do |map| |
| 39 | + sort.each do |k, v| # <-- here's my addition (the 'sort') |
| 40 | + map.add( k, v ) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +raise Exception, 'you must provide a json files' unless ARGV[1] |
| 48 | + |
| 49 | +my_hash_1 = JSON.parse(File.open(ARGV[0]).read) |
| 50 | +my_hash_2 = JSON.parse(File.open(ARGV[1]).read) |
| 51 | + |
| 52 | +# All of this need to be cleaned up and make it work. |
| 53 | +# |
| 54 | +#def orderit(my_hash) |
| 55 | +# ordered_yaml = #{my_hash}.to_yaml |
| 56 | +# yaml_hash = YAML::load(ordered_yaml) |
| 57 | +# new_json = yaml_hash.to_json |
| 58 | +# my_new_hash = JSON.parse(new_json) |
| 59 | +# my_pretty_json = JSON.pretty_generate(my_new_hash) |
| 60 | +# puts my_pretty_json |
| 61 | +#end |
| 62 | + |
| 63 | +ordered_yaml_1 = my_hash_1.to_yaml |
| 64 | +ordered_yaml_2 = my_hash_2.to_yaml |
| 65 | + |
| 66 | +yaml_1_hash = YAML::load(ordered_yaml_1) |
| 67 | +yaml_2_hash = YAML::load(ordered_yaml_2) |
| 68 | + |
| 69 | +new_json_1 = yaml_1_hash.to_json |
| 70 | +new_json_2 = yaml_2_hash.to_json |
| 71 | + |
| 72 | +my_new_hash_1 = JSON.parse(new_json_1) |
| 73 | +my_new_hash_2 = JSON.parse(new_json_2) |
| 74 | + |
| 75 | +my_pretty_json_1 = JSON.pretty_generate(my_new_hash_1) |
| 76 | +my_pretty_json_2 = JSON.pretty_generate(my_new_hash_2) |
| 77 | + |
| 78 | +# Compare and tell me they match. |
| 79 | +whats_different_hash = Hash[*((my_new_hash_2.size > my_new_hash_1.size) ? \ |
| 80 | + my_new_hash_2.to_a - my_new_hash_1.to_a : \ |
| 81 | + my_new_hash_1.to_a - my_new_hash_2.to_a).flatten] |
| 82 | + |
| 83 | +if whats_different_hash == {} |
| 84 | + print "Yea The files match\n" |
| 85 | +else |
| 86 | + print "You have a problem and the files do not match\n" |
| 87 | + # Need to add logic to write the files out so I can run diff -u |
| 88 | + # on them by hand |
| 89 | +end |
| 90 | + |
| 91 | +#puts orderit(my_hash_2) |
0 commit comments