Skip to content

Instantly share code, notes, and snippets.

@monsieurp
Created March 31, 2015 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monsieurp/f9e7d67c8eeff75b0ca8 to your computer and use it in GitHub Desktop.
Save monsieurp/f9e7d67c8eeff75b0ca8 to your computer and use it in GitHub Desktop.
eapi5.pl
#!/usr/bin/perl
use warnings;
use strict;
use Term::ANSIScreen qw/cls/;
use POSIX qw/strftime/;
use feature 'say';
# Current year.
my $year = strftime '%Y', gmtime;
# Adjust the year accordingly.
sub adjust_year {
my $line = shift;
$line =~ s/(\A.*?-)\d{4}(.*?\Z)/$1$year$2/g;
return $line;
}
# Upgrade to the latest EAPI.
sub migrate_to_eapi5 {
return "EAPI=5\n";
}
# Drop all KEYWORDS.
sub drop_keywords {
my $line = shift;
(my $keywords = $line) =~ s/\AKEYWORDS="(.*?)"\Z/$1/;
my @kw = split / /, $keywords;
my @nkw = ( );
for my $kw (@kw) {
chomp $kw;
if ($kw =~ /\A\w/) {
$kw = "~$kw";
}
push @nkw, $kw;
}
return sprintf "KEYWORDS=\"%s\"\n", join ' ', @nkw;
}
# Print a diff.
sub print_diff {
my ($orig, $modified) = @_;
open my $fh, "diff -u $orig $modified|" or die "Can't launch diff command: $!\n";
while (<$fh>) {
chomp;
say;
}
close $fh;
say '';
}
# Return a nicely formatted revision bump.
sub revbump {
my $ebuild = shift;
open my $fh, "qatom $ebuild|" or die "Can't launch qatom command: $!";
my $qatom = <$fh>;
chomp $qatom;
close $fh;
my (undef, $pkg, $ver, $rev) = split / /, $qatom;
my $newrev;
if ($rev) {
if ($rev =~ /(r)(\d+)/) {
$newrev = $1 . ($2 + 1);
}
} else {
$newrev = 'r1';
}
return sprintf '%s-%s-%s.ebuild', $pkg, $ver, $newrev;
}
# Prompt user for an answer.
sub prompt_user {
my ($prompt, $default) = @_;
my $default_value = $default ? "[$default]" : "";
print "$prompt $default_value: ";
chomp (my $input = <STDIN>);
return $input ? $input : $default;
}
# Meat of the script.
my $ebuild = $ARGV[0];
if (not defined $ebuild or
not -f $ebuild or
$ebuild !~ /\A.*?\.ebuild\Z/) {
die 'You must specify an ebuild!';
}
# Create a backup file
$^I = '.orig';
my $orig = "$ebuild.orig";
# Read stdin (file)
while (<>) {
if (/\A# Copyright/) {
$_ = adjust_year($_);
}
if (/\AEAPI/) {
$_ = migrate_to_eapi5();
}
if (/\AKEYWORDS/) {
$_ = drop_keywords($_)
}
print;
}
# Clear screen.
cls();
# Print a diff.
print_diff($orig, $ebuild);
# Work out the ebuild new name.
my $revbump = revbump($ebuild);
# Ask the user what to do next.
my $answer = prompt_user("Rename $ebuild to $revbump? [y/n]");
if ($answer eq 'y') {
print "Renaming $ebuild to $revbump ...";
rename $ebuild, $revbump;
rename $orig, $ebuild;
print " OK!\n";
} else {
say 'Keeping things as is.';
}
say 'All done!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment