From 937f5d0c8c203b0ff447df47a406fc46c3c9cd34 Mon Sep 17 00:00:00 2001 From: Petr Písař Date: Jun 28 2017 11:04:35 +0000 Subject: Implement renaming dependencies --- diff --git a/MANIFEST b/MANIFEST index ef159d7..2b5275f 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,6 +14,7 @@ t/parse.t t/rse.t t/rse_inline_first.t t/rse_multiple_add.t +t/rse_rename_dep.t t/save.t t/single_provides_with_a_trailing_space.t t/tokenize_dependency.t diff --git a/bin/rsemodernizeperl b/bin/rsemodernizeperl index 58ab1fe..393a7ee 100755 --- a/bin/rsemodernizeperl +++ b/bin/rsemodernizeperl @@ -110,10 +110,30 @@ sub add_br_perl_dash { # Or report an error die "I don't know where to insert `BuildRequires: $what'!\n"; } + +# Renames BuildRequires and Requires dependencies, it preserves versions +sub rename_deps { + my ($dom, $from, $to) = @_; + my $what = qr/^(?:Build)?Requires$/; + for ($dom->findDependencies($what, $from)) { + $_->symbol($to); + } +} + + # Parse arguments my @add_br_perl_dash; -GetOptions('add-br-perl-dash=s' => \@add_br_perl_dash) or - die "Wrong invocation!\n"; +my ($rename_dep_from, $rename_dep_to); +GetOptions( + 'add-br-perl-dash=s' => \@add_br_perl_dash, + 'rename-dep-from=s' => \$rename_dep_from, + 'rename-dep-to=s' => \$rename_dep_to, +) or die "Wrong invocation!\n"; + +if ($rename_dep_from xor $rename_dep_to) { + die "You need to specify both --rename-dep-from and --rename-dep-to options!\n" +} + my $file = $ARGV[0]; # Parse input spec @@ -125,6 +145,10 @@ for (@add_br_perl_dash) { add_br_perl_dash($spec->dom, $_); } +if ($rename_dep_from) { + rename_deps($spec->dom, $rename_dep_from, $rename_dep_to); +} + # Save modifed spec $spec->save(defined $file ? (file => $file) : (handle => \*STDOUT) ); @@ -156,6 +180,13 @@ It adds a C> tag into a proper place in the specification. The symbol must be C or start with C. You can use this option multiple times to instert different build-requires. +=head2 --rename-dep-from=I + +=head2 --rename-dep-to=I + +This replaces all occurrences of I build or run-time dependencies +with I. Both option must be specified at the same time. + =head1 EXIT CODE Returns zero, if no error occurred. Otherwise non-zero code is returned. @@ -172,7 +203,7 @@ Petr Písař =head1 COPYING -Copyright (C) 2016 Petr Písař +Copyright (C) 2016, 2017 Petr Písař This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/t/rse_rename_dep.t b/t/rse_rename_dep.t new file mode 100644 index 0000000..ce5897c --- /dev/null +++ b/t/rse_rename_dep.t @@ -0,0 +1,43 @@ +use Test::More tests => 6; + +use strict; +use warnings; +use Test::Script; + +script_runs(['bin/rsemodernizeperl', '--rename-dep-from=A', '--rename-dep-to=B'], { stdin => \< \< \<= 1 +EOM +script_stdout_is(<= 1 +EOM +