Permalink
Browse files

Merge pull request #35 from masaki/master

bundle support
  • Loading branch information...
2 parents 00e13bb + 80c55a7 commit 3627125e14763678374177a84b560f05a0e06f56 @miyagawa miyagawa committed Mar 31, 2012
Showing with 107 additions and 4 deletions.
  1. +5 −0 Makefile.PL
  2. +76 −3 lib/Carton.pm
  3. +26 −1 lib/Carton/CLI.pm
View
@@ -23,6 +23,11 @@ requires 'ExtUtils::MakeMaker', 6.59;
requires 'Module::Build', 0.38;
requires 'CPAN::Meta', 2.112;
+# bundle DarkPAN support
+requires 'File::chdir';
+requires 'Dist::Metadata';
+requires 'IO::Compress::Gzip';
+
install_script 'bin/carton';
doc_to_pods();
View
@@ -9,7 +9,10 @@ use Cwd;
use Config qw(%Config);
use Carton::Util;
use CPAN::Meta;
-use File::Path;
+use File::Path ();
+use File::Basename ();
+use File::Spec ();
+use File::Temp ();
use Capture::Tiny 'capture';
use constant CARTON_LOCK_VERSION => '0.9';
@@ -30,6 +33,16 @@ sub configure {
sub lock { $_[0]->{lock} }
+sub local_mirror { File::Spec->rel2abs("$_[0]->{path}/cache") }
+
+sub download_from_build_file {
+ my($self, $build_file, $local_mirror) = @_;
+
+ my @modules = $self->list_dependencies;
+ $self->download_conservative(\@modules, $local_mirror, 1)
+ or die "Bundling modules failed\n";
+}
+
sub install_from_build_file {
my($self, $file) = @_;
@@ -85,6 +98,27 @@ sub dedupe_modules {
return [ reverse @result ];
}
+sub download_conservative {
+ my($self, $modules, $dir, $cascade) = @_;
+
+ $modules = $self->dedupe_modules($modules);
+
+ my $mirror = $self->{mirror} || $DefaultMirror;
+
+ local $self->{path} = File::Temp::tempdir(CLEANUP => 1); # ignore installed
+ $self->run_cpanm(
+ "--mirror", $mirror,
+ "--mirror", "http://backpan.perl.org/", # fallback
+ "--no-skip-satisfied",
+ ( $mirror ne $DefaultMirror ? "--mirror-only" : () ),
+ ( $cascade ? "--cascade-search" : () ),
+ "--scandeps",
+ "--format", "dists",
+ "--save-dists", $dir,
+ @$modules,
+ );
+}
+
sub install_conservative {
my($self, $modules, $cascade) = @_;
@@ -113,7 +147,13 @@ sub build_mirror_file {
my @packages = $self->build_packages($index);
- open my $fh, ">", $file or die $!;
+ my $fh;
+ if ($file =~ /\.gz$/i) {
+ require IO::Compress::Gzip;
+ $fh = IO::Compress::Gzip->new($file) or die $IO::Compress::Gzip::GzipError;
+ } else {
+ open $fh, ">", $file or die $!;
+ }
print $fh <<EOF;
File: 02packages.details.txt
@@ -170,6 +210,28 @@ sub build_index {
return $index;
}
+sub build_mirror_index {
+ my($self, $local_mirror) = @_;
+
+ require File::chdir;
+ require Dist::Metadata;
+
+ my $index = {};
+
+ local $File::chdir::CWD = "$local_mirror/authors/id";
+
+ for my $file (<*/*/*/*>) { # D/DU/DUMMY/Foo-Bar-0.01.tar.gz
+ my $dist = Dist::Metadata->new(file => $file);
+
+ my $provides = $dist->package_versions;
+ while (my($package, $version) = each %$provides) {
+ $index->{$package} = { version => $version, meta => { pathname => $file } };
+ }
+ };
+
+ return $index;
+}
+
sub is_core {
my($self, $module, $want_ver, $perl_version) = @_;
$perl_version ||= $];
@@ -279,6 +341,17 @@ sub run_cpanm {
!system "cpanm", "--quiet", "-L", $self->{path}, "--notest", @args;
}
+sub update_mirror_index {
+ my($self, $local_mirror) = @_;
+
+ my $index = $self->build_mirror_index($local_mirror);
+
+ my $file = "$local_mirror/modules/02packages.details.txt.gz";
+ File::Path::mkpath(File::Basename::dirname($file));
+ $self->build_mirror_file($index, $file)
+ or die "Bundling modules failed\n";
+}
+
sub update_lock_file {
my($self, $file) = @_;
@@ -317,7 +390,7 @@ sub find_installs {
return map {
my $module = Carton::Util::load_json($_->[0]);
- my $mymeta = CPAN::Meta->load_file($_->[1])->as_struct({ version => "2" });
+ my $mymeta = -f $_->[1] ? CPAN::Meta->load_file($_->[1])->as_struct({ version => "2" }) : {};
($module->{name} => { %$module, mymeta => $mymeta }) } @installs;
}
View
@@ -131,16 +131,41 @@ sub cmd_version {
$self->print("carton $Carton::VERSION\n");
}
+sub cmd_bundle {
+ my($self, @args) = @_;
+
+ $self->parse_options(\@args, "p|path=s" => sub { $self->carton->{path} = $_[1] });
+
+ my $local_mirror = $self->carton->local_mirror;
+
+ if (my $build_file = $self->has_build_file) {
+ $self->print("Bundling modules using $build_file\n");
+ $self->carton->download_from_build_file($build_file, $local_mirror);
+ $self->carton->update_mirror_index($local_mirror);
+ } else {
+ $self->error("Can't locate build file\n");
+ }
+
+ $self->printf("Complete! Modules were bundled into %s (DarkPAN)\n", $local_mirror, SUCCESS);
+}
+
sub cmd_install {
my($self, @args) = @_;
- $self->parse_options(\@args, "p|path=s", sub { $self->carton->{path} = $_[1] }, "deployment!" => \$self->{deployment});
+ $self->parse_options(
+ \@args,
+ "p|path=s" => sub { $self->carton->{path} = $_[1] },
+ "deployment!" => \$self->{deployment},
+ "cached!" => \$self->{use_local_mirror},
+ );
my $lock = $self->find_lock;
+ my $local_mirror = $self->carton->local_mirror;
$self->carton->configure(
lock => $lock,
mirror_file => $self->mirror_file, # $lock object?
+ ( $self->{use_local_mirror} && -d $local_mirror ? (mirror => $local_mirror) : () ),
);
my $build_file = $self->has_build_file;

0 comments on commit 3627125

Please sign in to comment.