openstreetmap-website/script/locale/diff
Ævar Arnfjörð Bjarmason d3dd0229cb Don't use Data::Walk and instead construct a flattened list of hash
values using a custom recursive walker (stolen from yaml2po).

This makes reading the output a whole lot easier.
2009-06-22 16:05:28 +00:00

109 lines
2 KiB
Perl
Executable file

#!/usr/bin/env perl
use strict;
use warnings;
use YAML::Syck qw(LoadFile);
use Test::Differences;
use Pod::Usage ();
use Getopt::Long ();
=head1 NAME
locale-diff - Compare two YAML files and print how their datastructures differ
=head1 SYNOPSIS
locale-diff en.yml is.yml
locale-diff en.yml is.yml | grep '*'
=head1 DESCRIPTION
This utility prints the differences between two YAML files using
L<Test::Differences>. The purpose of it is to diff the files is
F<config/locales> to find out what keys need to be added to the
translated files when F<en.yml> changes.
=head1 OPTIONS
=over
=item -h, --help
Print this help message.
=back
=head1 AUTHOR
E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@f-prot.com>
=cut
# Get the command-line options
Getopt::Long::Parser->new(
config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
)->getoptions(
'h|help' => \my $help,
) or help();
# On --help
help() if $help;
# If we're not given two .yml files
help() if @ARGV != 2 or (!-f $ARGV[0] or !-f $ARGV[1]);
my ($from, $to) = @ARGV;
my $from_data = LoadFile($from);
my $to_data = LoadFile($to);
my $from_parsed = { iterate($from_data->{basename($from)}) };
my $to_parsed = { iterate($to_data->{basename($to)}) };
# Delete hash values
#walkdepth \&delete_hash_values, $_ for $from_data, $to_data;
# Hack around Test::Differences wanting a Test::* module loaded
$INC{"Test.pm"} = 1;
sub Test::ok { print shift }
# Diff the tree
eq_or_diff([ sort keys %$from_parsed ], [ sort keys %$to_parsed ]);
exit 0;
sub iterate
{
my ($hash, @path) = @_;
my @ret;
while (my ($k, $v) = each %$hash)
{
if (ref $v eq 'HASH')
{
push @ret => iterate($v, @path, $k);
}
else
{
push @ret => join(".",@path, $k), $v;
}
}
return @ret;
}
sub basename
{
my $name = shift;
$name =~ s[\..*?$][];
$name;
}
sub help
{
my %arg = @_;
Pod::Usage::pod2usage(
-verbose => $arg{ verbose },
-exitval => $arg{ exitval } || 0,
);
}