| 1 | package Carton::Builder; | ||
| 2 | use strict; | ||
| 3 | use Class::Tiny { | ||
| 4 | mirror => undef, | ||
| 5 | index => undef, | ||
| 6 | cascade => sub { 1 }, | ||
| 7 | without => sub { [] }, | ||
| 8 | cpanfile => undef, | ||
| 9 | }; | ||
| 10 | |||
| 11 | sub effective_mirrors { | ||
| 12 | my $self = shift; | ||
| 13 | |||
| 14 | # push default CPAN mirror always, as a fallback | ||
| 15 | # TODO don't pass fallback if --cached is set? | ||
| 16 | |||
| 17 | my @mirrors = ($self->mirror); | ||
| 18 | push @mirrors, Carton::Mirror->default if $self->custom_mirror; | ||
| 19 | push @mirrors, Carton::Mirror->new('http://backpan.perl.org/'); | ||
| 20 | |||
| 21 | @mirrors; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub custom_mirror { | ||
| 25 | my $self = shift; | ||
| 26 | ! $self->mirror->is_default; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub bundle { | ||
| 30 | my($self, $path, $cache_path, $snapshot) = @_; | ||
| 31 | |||
| 32 | for my $dist ($snapshot->distributions) { | ||
| 33 | my $source = $path->child("cache/authors/id/" . $dist->pathname); | ||
| 34 | my $target = $cache_path->child("authors/id/" . $dist->pathname); | ||
| 35 | |||
| 36 | if ($source->exists) { | ||
| 37 | warn "Copying ", $dist->pathname, "\n"; | ||
| 38 | $target->parent->mkpath; | ||
| 39 | $source->copy($target) or warn "$target: $!"; | ||
| 40 | } else { | ||
| 41 | warn "Couldn't find @{[ $dist->pathname ]}\n"; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | my $has_io_gzip = eval { require IO::Compress::Gzip; 1 }; | ||
| 46 | |||
| 47 | my $ext = $has_io_gzip ? ".txt.gz" : ".txt"; | ||
| 48 | my $index = $cache_path->child("modules/02packages.details$ext"); | ||
| 49 | $index->parent->mkpath; | ||
| 50 | |||
| 51 | warn "Writing $index\n"; | ||
| 52 | |||
| 53 | my $out = $index->openw; | ||
| 54 | if ($has_io_gzip) { | ||
| 55 | $out = IO::Compress::Gzip->new($out) | ||
| 56 | or die "gzip failed: $IO::Compress::Gzip::GzipError"; | ||
| 57 | } | ||
| 58 | |||
| 59 | $snapshot->index->write($out); | ||
| 60 | close $out; | ||
| 61 | |||
| 62 | unless ($has_io_gzip) { | ||
| 63 | unlink "$index.gz"; | ||
| 64 | !system 'gzip', $index | ||
| 65 | or die "Running gzip command failed: $!"; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | sub install { | ||
| 70 | my($self, $path) = @_; | ||
| 71 | |||
| 72 | $self->run_install( | ||
| 73 | "-L", $path, | ||
| 74 | (map { ("--mirror", $_->url) } $self->effective_mirrors), | ||
| 75 | ( $self->index ? ("--mirror-index", $self->index) : () ), | ||
| 76 | ( $self->cascade ? "--cascade-search" : () ), | ||
| 77 | ( $self->custom_mirror ? "--mirror-only" : () ), | ||
| 78 | "--save-dists", "$path/cache", | ||
| 79 | $self->groups, | ||
| 80 | "--cpanfile", $self->cpanfile, | ||
| 81 | "--installdeps", $self->cpanfile->dirname, | ||
| 82 | ) or die "Installing modules failed\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub groups { | ||
| 86 | my $self = shift; | ||
| 87 | |||
| 88 | # TODO support --without test (don't need test on deployment) | ||
| 89 | my @options = ('--with-all-features', '--with-develop'); | ||
| 90 | |||
| 91 | for my $group (@{$self->without}) { | ||
| 92 | push @options, '--without-develop' if $group eq 'develop'; | ||
| 93 | push @options, "--without-feature=$group"; | ||
| 94 | } | ||
| 95 | |||
| 96 | return @options; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub update { | ||
| 100 | my($self, $path, @modules) = @_; | ||
| 101 | |||
| 102 | $self->run_install( | ||
| 103 | "-L", $path, | ||
| 104 | (map { ("--mirror", $_->url) } $self->effective_mirrors), | ||
| 105 | ( $self->custom_mirror ? "--mirror-only" : () ), | ||
| 106 | "--save-dists", "$path/cache", | ||
| 107 | @modules | ||
| 108 | ) or die "Updating modules failed\n"; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub run_install { | ||
| 112 | my($self, @args) = @_; | ||
| 113 | |||
| 114 | require Menlo::CLI::Compat; | ||
| 115 | local $ENV{PERL_CPANM_OPT}; | ||
| 116 | |||
| 117 | my $cli = Menlo::CLI::Compat->new; | ||
| 118 | $cli->parse_options("--quiet", "--notest", @args); | ||
| 119 | $cli->run; | ||
| 120 | |||
| 121 | !$cli->status; | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; |