133 lines
2.8 KiB
Perl
133 lines
2.8 KiB
Perl
#! /usr/bin/env perl
|
|
|
|
use v5.10.1;
|
|
use strict;
|
|
use warnings;
|
|
use Getopt::Long;
|
|
use Pod::Usage;
|
|
use XML::LibXML;
|
|
|
|
# Parse arguments
|
|
my $input; my $current; my $output; my $version; my $next;
|
|
my $help; my $man;
|
|
GetOptions ("output|O=s" => \$output,
|
|
"current|c=s" => \$current,
|
|
"version|V=i" => \$version,
|
|
"next|n=i" => \$next,
|
|
"help|h|?" => \$help,
|
|
"man" => \$man,
|
|
"" => \$input)
|
|
or pod2usage(2);
|
|
pod2usage(1) if $help;
|
|
pod2usage(-exitval => 0, -verbose => 2) if $man;
|
|
|
|
# Get the current version
|
|
if (defined $current && defined $version) {
|
|
say "Error: Cannot have both -current and -version, choose the one!";
|
|
pod2usage(1);
|
|
}
|
|
elsif (defined $current) {
|
|
open my $xmlcur, "<", $current or die $!;
|
|
binmode $xmlcur;
|
|
my $dom = XML::LibXML->load_xml(IO => $xmlcur);
|
|
close $xmlcur;
|
|
|
|
$version = $dom->documentElement()->getAttribute("version");
|
|
}
|
|
|
|
if(defined $next) {
|
|
$version = $next;
|
|
}
|
|
elsif (!$version) {
|
|
say "Warning: Assume this is the first version.";
|
|
$version = 1;
|
|
}
|
|
else {
|
|
$version += 1;
|
|
}
|
|
|
|
# Open versioned XML file
|
|
my $xmlin;
|
|
if (defined $input || $#ARGV == -1) {
|
|
$xmlin = *STDIN;
|
|
}
|
|
else {
|
|
open $xmlin, "<", $ARGV[0] or die $!;
|
|
}
|
|
|
|
binmode $xmlin;
|
|
my $dom = XML::LibXML->load_xml(IO => $xmlin);
|
|
close $xmlin unless $xmlin eq *STDIN;
|
|
|
|
# Check the version
|
|
my $my_version = $dom->documentElement()->getAttribute("version");
|
|
if (!defined $my_version || $my_version < $version) {
|
|
$dom->documentElement()->setAttribute("version", $version);
|
|
}
|
|
|
|
# Save versioned XML file
|
|
my $xmlout;
|
|
if (defined $output) {
|
|
open $xmlout, '>', $output;
|
|
binmode $xmlout;
|
|
}
|
|
else {
|
|
$xmlout = *STDOUT;
|
|
}
|
|
print {$xmlout} $dom->toString();
|
|
close $xmlout unless $xmlout eq *STDOUT;
|
|
|
|
__END__
|
|
|
|
=head1 NAME
|
|
|
|
new_version.pl - Check if the new file version is greater than current file version
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
new_version.pl [options] [file]
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
Parse the XML file given (or stdin if no file is given) and print a valid versionned XML with a valid version (strictly greater than current version)
|
|
|
|
Options:
|
|
-output=file.xml save prepared XML to this location
|
|
-current=file.xml location of the current XML file
|
|
-version=X raw version to use at least
|
|
-next=X next version to be used
|
|
-help brief help message
|
|
-man full documentation
|
|
|
|
=head1 OPTIONS
|
|
|
|
=over 8
|
|
|
|
=item B<-output=file.xml>
|
|
|
|
Save the prepared XML to a file instead of printing it on standard output.
|
|
|
|
=item B<-current=file.xml>
|
|
|
|
Path to the current XML file, where extract the current version.
|
|
|
|
=item B<-version=X>
|
|
|
|
The last known version. Used when you haven't the file.
|
|
|
|
=item B<-next=X>
|
|
|
|
The version to write into the file.
|
|
|
|
=item B<-help>
|
|
|
|
Print a brief help message and exits.
|
|
|
|
=item B<-man>
|
|
|
|
Prints the manual page and exits.
|
|
|
|
=back
|
|
|
|
=cut
|