CPANPLUS-Dist-Arch

 view release on metacpan or  search on metacpan

script/cpan2aur  view on Meta::CPAN

    my %info     = get_pkgbuild_info( $pkgbuild );

    error( <<"END_ERROR" ) unless ( $info{dist_name} );
${pkg_path}'s PKGBUILD does not seem to be made by cpan2aur.
We are unable to extract the CPAN distribution name from it.
END_ERROR

    return %info;
}

sub by_version
{
    my ($aver) = $a =~ /-([^-]+)-\d+[.]src[.]tar[.]gz\z/
        or die "Failed to extract version from $a.";
    my ($bver) = $b =~ /-([^-]+)-\d+[.]src[.]tar[.]gz\z/
        or die "Failed to extract version from $b.";
    version->parse( $aver ) cmp version->parse( $bver );
}

sub pkgdir_srcpkg
{
    my ($pkg_dir) = @_;

    my ($pkg_name) = reverse splitdir( $pkg_dir )
        or error( "Failed to extract pkgname from dir $pkg_dir" );

    # Choose the package with the latest version number...
    my ($src_pkgpath) = reverse sort by_version
        glob "$pkg_dir/$pkg_name-*.src.tar.gz";

    return $src_pkgpath;
}

sub get_pkgdir_info
{
    my ($pkg_dir) = @_;

    # Return a source package file's info if one exists in the dir...
    my $srcpkg = pkgdir_srcpkg( $pkg_dir );
    return get_pkgfile_info( $srcpkg ) if ( $srcpkg );

    # Next is the PKGBUILD file itself...
    error( "$pkg_dir does not contain a PKGBUILD or source package file." )
        unless ( -f "$pkg_dir/PKGBUILD" );

    open my $pkgbuild_file, '<', "$pkg_dir/PKGBUILD"
        or die "open $pkg_dir/PKGBUILD: $!";
    my $pkgbuild = do { local $/; <$pkgbuild_file> }; # slurp!
    close $pkgbuild_file;

    my %info = get_pkgbuild_info( $pkgbuild );

    error( <<'END_ERROR' ) unless ( $info{dist_name} );
$pkg_dir/PKGBUILD does not seem to be made by cpan2aur.
We are unable to extract the CPAN distribution name from it.
END_ERROR

    return %info;
}

sub update_if_old
{
    my ($thing) = @_;

    status "Checking if $thing is up to date...";

    my $type = ( -f $thing ? 'file' : -d $thing ? 'dir' : undef );
    unless ( $type ) {
        error( <<"END_ERROR" );
$thing does not seem to be a source package file or directory!
END_ERROR
    }

    # Check if the "thing" has a newer version on CPAN...
    my %pkg_info = ( $type eq 'file' ? get_pkgfile_info( $thing ) :
                     $type eq 'dir'  ? get_pkgdir_info( $thing )  :
                     die );

    my $mod_obj  = find_module( $pkg_info{'dist_name'} );
    return unless ( $mod_obj );

    my $cpan_ver = version->new( dist_pkgver( $mod_obj->package_version ));
    my $dist_ver = version->new( $pkg_info{'dist_ver'} );

    if ( $cpan_ver < $dist_ver ) {
        error( <<"END_WARN" );
CPAN version $cpan_ver is less than package version $dist_ver!
END_WARN
    }
    elsif ( $cpan_ver == $dist_ver ) {
        msg "$thing is up to date.";
        return;
    }

    if ( $type eq 'file' ) {
        # If this is a source package file, make a new one!
        my $dist_obj = create_dist_arch( $mod_obj => 'create' );
        my $pkg_path = $dist_obj->status->dist
            or die 'Unable to find path of created source package';

        if ( prompt_yn( 'Delete the old package file?' => 'yes' )) {
            status "Deleting old package file: $thing";
            unlink $thing or warning( "Failed to delete $thing ($!)" );
        }
        upload_pkgfile( $pkg_path );
        return;
    }

    # upload_pkgdir() will automatically update the directory
    # TODO: perhaps we should turn force on?
    upload_pkgdir( $thing );
    return;
}

## SCRIPT START
##############################################################################

GetOptions( 'directory' => \$DIRECTORY,
            'verbose'   => \$VERBOSE,
            'reverse'   => \$REVERSE,
            'upload'    => \$UPLOAD,



( run in 1.307 second using v1.01-cache-2.11-cpan-7fcb06a456a )