Skip to content

Commit

Permalink
Added "repository_url" support for "cpan" PURL type
Browse files Browse the repository at this point in the history
  • Loading branch information
giterlizzi committed Sep 17, 2023
1 parent 6b96c6a commit 5f928b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/URI/PackageURL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use utf8;
use warnings;

use Carp;
use Exporter qw(import);
use Exporter qw(import);
use URI::PackageURL::Util qw(purl_to_urls);

use overload '""' => 'to_string', fallback => 1;

our $VERSION = '2.00';
our $VERSION = '2.01';
our @EXPORT = qw(encode_purl decode_purl);

my $PURL_REGEXP = qr{^pkg:[A-Za-z\\.\\-\\+][A-Za-z0-9\\.\\-\\+]*/.+};
Expand Down Expand Up @@ -309,21 +309,21 @@ URI::PackageURL - Perl extension for Package URL (aka "purl")
type => cpan,
namespace => 'GDT',
name => 'URI-PackageURL',
version => '2.00'
version => '2.01'
);
say $purl; # pkg:cpan/GDT/URI-PackageURL@2.00
say $purl; # pkg:cpan/GDT/URI-PackageURL@2.01
# Parse PackageURL string
$purl = URI::PackageURL->from_string('pkg:cpan/URI-PackageURL@2.00');
$purl = URI::PackageURL->from_string('pkg:cpan/URI-PackageURL@2.01');
# exported funtions
$purl = decode_purl('pkg:cpan/GDT/URI-PackageURL@2.00');
$purl = decode_purl('pkg:cpan/GDT/URI-PackageURL@2.01');
say $purl->type; # cpan
$purl_string = encode_purl(type => cpan, name => 'URI-PackageURL', version => '2.00');
say $purl_string; # pkg:cpan/URI-PackageURL@2.00
$purl_string = encode_purl(type => cpan, name => 'URI-PackageURL', version => '2.01');
say $purl_string; # pkg:cpan/URI-PackageURL@2.01
=head1 DESCRIPTION
Expand Down Expand Up @@ -439,7 +439,7 @@ Helper method for JSON modules (L<JSON>, L<JSON::PP>, L<JSON::XS>, L<Mojo::JSON>
use Mojo::JSON qw(encode_json);
say encode_json($purl); # {"name":"URI-PackageURL","namespace":"GDT","qualifiers":null,"subpath":null,"type":"cpan","version":"2.00"}
say encode_json($purl); # {"name":"URI-PackageURL","namespace":"GDT","qualifiers":null,"subpath":null,"type":"cpan","version":"2.01"}
=back
Expand Down
27 changes: 16 additions & 11 deletions lib/URI/PackageURL/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use warnings;
use Carp;
use Exporter qw(import);

our $VERSION = '2.00';
our $VERSION = '2.01';
our @EXPORT = qw(purl_to_urls);

sub purl_to_urls {
Expand Down Expand Up @@ -193,11 +193,16 @@ sub _cpan_urls {

my $purl = shift;

my $name = $purl->name;
my $version = $purl->version;
my $qualifiers = $purl->qualifiers;
my $author = $purl->namespace ? uc($purl->namespace) : undef;
my $file_ext = $qualifiers->{ext} || 'tar.gz';
my $name = $purl->name;
my $version = $purl->version;
my $qualifiers = $purl->qualifiers;
my $author = $purl->namespace ? uc($purl->namespace) : undef;
my $file_ext = $qualifiers->{ext} || 'tar.gz';
my $repository_url = $qualifiers->{repository_url} || 'https://www.cpan.org';

if ($repository_url !~ /^(http|https|file|ftp):\/\//) {
$repository_url = 'https://' . $repository_url;
}

$name =~ s/\:\:/-/g; # TODO

Expand All @@ -209,7 +214,7 @@ sub _cpan_urls {
my $author_2 = substr($author, 0, 2);

$urls->{repository} = "https://metacpan.org/release/$author/$name-$version";
$urls->{download} = "http://www.cpan.org/authors/id/$author_1/$author_2/$author/$name-$version.$file_ext";
$urls->{download} = "$repository_url/authors/id/$author_1/$author_2/$author/$name-$version.$file_ext";

}

Expand Down Expand Up @@ -287,7 +292,7 @@ URI::PackageURL::Util - Utility for URI::PackageURL
use URI::PackageURL::Util qw(purl_to_urls);
$urls = purl_to_urls('pkg:cpan/GDT/URI-PackageURL@2.00');
$urls = purl_to_urls('pkg:cpan/GDT/URI-PackageURL@2.01');
$filename = basename($urls->{download});
$ua->mirror($urls->{download}, "/tmp/$filename");
Expand Down Expand Up @@ -324,13 +329,13 @@ C<gem>, C<github>, C<gitlab>, C<maven>, C<npm>, C<nuget>, C<pypi>).
(*) Only with B<version> component
$urls = purl_to_urls('pkg:cpan/GDT/URI-PackageURL@2.00');
$urls = purl_to_urls('pkg:cpan/GDT/URI-PackageURL@2.01');
print Dumper($urls);
# $VAR1 = {
# 'repository' => 'https://metacpan.org/release/GDT/URI-PackageURL-2.00',
# 'download' => 'http://www.cpan.org/authors/id/G/GD/GDT/URI-PackageURL-2.00.tar.gz'
# 'repository' => 'https://metacpan.org/release/GDT/URI-PackageURL-2.01',
# 'download' => 'http://www.cpan.org/authors/id/G/GD/GDT/URI-PackageURL-2.01.tar.gz'
# };
=back
Expand Down
6 changes: 3 additions & 3 deletions t/30-util.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use URI::PackageURL::Util qw(purl_to_urls);

my @tests = (
{
purl => 'pkg:cpan/GDT/URI-PackageURL@2.00',
download_url => 'http://www.cpan.org/authors/id/G/GD/GDT/URI-PackageURL-2.00.tar.gz',
repository_url => 'https://metacpan.org/release/GDT/URI-PackageURL-2.00'
purl => 'pkg:cpan/GDT/URI-PackageURL@2.01',
download_url => 'https://www.cpan.org/authors/id/G/GD/GDT/URI-PackageURL-2.01.tar.gz',
repository_url => 'https://metacpan.org/release/GDT/URI-PackageURL-2.01'
},

{
Expand Down

0 comments on commit 5f928b6

Please sign in to comment.