| 1 | package Carton::Environment; | ||
| 2 | use strict; | ||
| 3 | use Carton::CPANfile; | ||
| 4 | use Carton::Snapshot; | ||
| 5 | use Carton::Error; | ||
| 6 | use Carton::Tree; | ||
| 7 | use Path::Tiny; | ||
| 8 | |||
| 9 | use Class::Tiny { | ||
| 10 | cpanfile => undef, | ||
| 11 | snapshot => sub { $_[0]->_build_snapshot }, | ||
| 12 | install_path => sub { $_[0]->_build_install_path }, | ||
| 13 | vendor_cache => sub { $_[0]->_build_vendor_cache }, | ||
| 14 | tree => sub { $_[0]->_build_tree }, | ||
| 15 | }; | ||
| 16 | |||
| 17 | sub _build_snapshot { | ||
| 18 | my $self = shift; | ||
| 19 | Carton::Snapshot->new(path => $self->cpanfile . ".snapshot"); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _build_install_path { | ||
| 23 | my $self = shift; | ||
| 24 | if ($ENV{PERL_CARTON_PATH}) { | ||
| 25 | return Path::Tiny->new($ENV{PERL_CARTON_PATH}); | ||
| 26 | } else { | ||
| 27 | return $self->cpanfile->path->parent->child("local"); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _build_vendor_cache { | ||
| 32 | my $self = shift; | ||
| 33 | Path::Tiny->new($self->install_path->dirname . "/vendor/cache"); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _build_tree { | ||
| 37 | my $self = shift; | ||
| 38 | Carton::Tree->new(cpanfile => $self->cpanfile, snapshot => $self->snapshot); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub vendor_bin { | ||
| 42 | my $self = shift; | ||
| 43 | $self->vendor_cache->parent->child('bin'); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub build_with { | ||
| 47 | my($class, $cpanfile) = @_; | ||
| 48 | |||
| 49 | $cpanfile = Path::Tiny->new($cpanfile)->absolute; | ||
| 50 | if ($cpanfile->is_file) { | ||
| 51 | return $class->new(cpanfile => Carton::CPANfile->new(path => $cpanfile)); | ||
| 52 | } else { | ||
| 53 | Carton::Error::CPANfileNotFound->throw(error => "Can't locate cpanfile: $cpanfile"); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub build { | ||
| 58 | my($class, $cpanfile_path, $install_path) = @_; | ||
| 59 | |||
| 60 | my $self = $class->new; | ||
| 61 | |||
| 62 | $cpanfile_path &&= Path::Tiny->new($cpanfile_path)->absolute; | ||
| 63 | |||
| 64 | my $cpanfile = $self->locate_cpanfile($cpanfile_path || $ENV{PERL_CARTON_CPANFILE}); | ||
| 65 | if ($cpanfile && $cpanfile->is_file) { | ||
| 66 | $self->cpanfile( Carton::CPANfile->new(path => $cpanfile) ); | ||
| 67 | } else { | ||
| 68 | Carton::Error::CPANfileNotFound->throw(error => "Can't locate cpanfile: (@{[ $cpanfile_path || 'cpanfile' ]})"); | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->install_path( Path::Tiny->new($install_path)->absolute ) if $install_path; | ||
| 72 | |||
| 73 | $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub locate_cpanfile { | ||
| 77 | my($self, $path) = @_; | ||
| 78 | |||
| 79 | if ($path) { | ||
| 80 | return Path::Tiny->new($path)->absolute; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $current = Path::Tiny->cwd; | ||
| 84 | my $previous = ''; | ||
| 85 | |||
| 86 | until ($current eq '/' or $current eq $previous) { | ||
| 87 | # TODO support PERL_CARTON_CPANFILE | ||
| 88 | my $try = $current->child('cpanfile'); | ||
| 89 | if ($try->is_file) { | ||
| 90 | return $try->absolute; | ||
| 91 | } | ||
| 92 | |||
| 93 | ($previous, $current) = ($current, $current->parent); | ||
| 94 | } | ||
| 95 | |||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 |