[carton] 335/472: Add Lockfile class. Refactored cpanfile/carton.lock locator to Environments

Lucas Kanashiro kanashiro-guest at moszumanska.debian.org
Fri Jul 24 00:39:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

kanashiro-guest pushed a commit to branch master
in repository carton.

commit 277c543e4052405a4c87d847d2e15ce13a0359c6
Author: Tatsuhiko Miyagawa <miyagawa at bulknews.net>
Date:   Mon Jul 22 12:43:13 2013 -0700

    Add Lockfile class. Refactored cpanfile/carton.lock locator to Environments
---
 lib/Carton/CLI.pm         | 70 +++++++++++++----------------------------------
 lib/Carton/Environment.pm |  3 +-
 lib/Carton/Error.pm       |  2 ++
 lib/Carton/Lock.pm        |  6 +++-
 lib/Carton/Lockfile.pm    | 30 ++++++++++++++++++++
 5 files changed, 58 insertions(+), 53 deletions(-)

diff --git a/lib/Carton/CLI.pm b/lib/Carton/CLI.pm
index c9ecf29..833d386 100644
--- a/lib/Carton/CLI.pm
+++ b/lib/Carton/CLI.pm
@@ -76,7 +76,7 @@ sub run {
         if ($_->isa('Carton::Error::CommandExit')) {
             return $_->code || 255;
         } elsif ($_->isa('Carton::Error')) {
-            warn $_->error;
+            warn $_->error, "\n";
             return 255;
         }
     };
@@ -157,19 +157,15 @@ sub cmd_version {
 sub cmd_bundle {
     my($self, @args) = @_;
 
-    my $lock = $self->find_lock;
-    my $cpanfile = $self->find_cpanfile;
+    my $lock = $self->lockfile->load;
+    my $cpanfile = $self->cpanfile;
 
-    if ($lock) {
-        $self->print("Bundling modules using $cpanfile\n");
+    $self->print("Bundling modules using $cpanfile\n");
 
-        my $builder = Carton::Builder->new(
-            mirror => $self->mirror,
-        );
-        $builder->bundle($self->install_path, $self->vendor_cache, $lock);
-    } else {
-        $self->error("Can't locate carton.lock file. Run carton install first\n");
-    }
+    my $builder = Carton::Builder->new(
+        mirror => $self->mirror,
+    );
+    $builder->bundle($self->install_path, $self->vendor_cache, $lock);
 
     $self->printf("Complete! Modules were bundled into %s\n", $self->vendor_cache, SUCCESS);
 }
@@ -188,13 +184,13 @@ sub cmd_install {
         "cached!"     => \my $cached,
     );
 
-    my $lock = $self->find_lock;
+    my $lock = $self->lockfile->load_if_exists;
 
     if ($deployment && !$lock) {
         $self->error("--deployment requires carton.lock: Run `carton install` and make sure carton.lock is checked into your version control.\n");
     }
 
-    my $cpanfile = $self->find_cpanfile;
+    my $cpanfile = $self->cpanfile;
 
     my $builder = Carton::Builder->new(
         cascade => 1,
@@ -234,8 +230,7 @@ sub cmd_install {
 sub cmd_show {
     my($self, @args) = @_;
 
-    my $lock = $self->find_lock
-        or $self->error("Can't find carton.lock: Run `carton install`\n");
+    my $lock = $self->lockfile->load;
 
     for my $module (@args) {
         my $dist = $lock->find($module)
@@ -254,8 +249,7 @@ sub cmd_list {
         "distfile" => sub { $format = 'distfile' },
     );
 
-    my $lock = $self->find_lock
-        or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
+    my $lock = $self->lockfile->load;
 
     for my $dist ($lock->distributions) {
         $self->print($dist->$format . "\n");
@@ -265,10 +259,9 @@ sub cmd_list {
 sub cmd_tree {
     my($self, @args) = @_;
 
-    my $lock = $self->find_lock
-      or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
+    my $lock = $self->lockfile->load;
 
-    my $cpanfile = Module::CPANfile->load($self->find_cpanfile);
+    my $cpanfile = Module::CPANfile->load($self->cpanfile);
     my $requirements = Carton::Requirements->new(lock => $lock, prereqs => $cpanfile->prereqs);
 
     my %seen;
@@ -284,10 +277,9 @@ sub cmd_tree {
 sub cmd_check {
     my($self, @args) = @_;
 
-    my $lock = $self->find_lock
-      or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
+    my $lock = $self->lockfile->load;
 
-    my $prereqs = Module::CPANfile->load($self->find_cpanfile)->prereqs;
+    my $prereqs = Module::CPANfile->load($self->cpanfile)->prereqs;
 
     # TODO remove $lock
     # TODO pass git spec to Requirements?
@@ -328,7 +320,7 @@ sub cmd_check {
 sub cmd_update {
     my($self, @args) = @_;
 
-    my $cpanfile = Module::CPANfile->load($self->find_cpanfile);
+    my $cpanfile = Module::CPANfile->load($self->cpanfile);
     my $prereqs = $cpanfile->prereqs;
 
     my $reqs = CPAN::Meta::Requirements->new;
@@ -337,8 +329,7 @@ sub cmd_update {
 
     @args = grep { $_ ne 'perl' } $reqs->required_modules unless @args;
 
-    my $lock = $self->find_lock
-        or $self->error("Can't find carton.lock: Run `carton install` to build the lock file.\n");
+    my $lock = $self->lockfile->load;
 
     my @modules;
     for my $module (@args) {
@@ -359,8 +350,7 @@ sub cmd_update {
 sub cmd_exec {
     my($self, @args) = @_;
 
-    my $lock = $self->find_lock
-        or $self->error("Can't find carton.lock: Run `carton install` to build the lock file.\n");
+    my $lock = $self->lockfile->load;
 
     # allows -Ilib
     @args = map { /^(-[I])(.+)/ ? ($1,$2) : $_ } @args;
@@ -388,28 +378,6 @@ sub cmd_exec {
     $UseSystem ? system(@args) : exec(@args);
 }
 
-sub find_cpanfile {
-    my $self = shift;
-    $self->cpanfile;
-}
-
-sub find_lock {
-    my $self = shift;
-
-    if (-e $self->lockfile) {
-        my $lock;
-        try {
-            $lock = Carton::Lock->from_file($self->lockfile);
-        } catch {
-            $self->error("Can't parse carton.lock: $_\n");
-        };
-
-        return $lock;
-    }
-
-    return;
-}
-
 sub index_file {
     my $self = shift;
     $self->work_file("cache/modules/02packages.details.txt");
diff --git a/lib/Carton/Environment.pm b/lib/Carton/Environment.pm
index af39f4d..79aed97 100644
--- a/lib/Carton/Environment.pm
+++ b/lib/Carton/Environment.pm
@@ -2,6 +2,7 @@ package Carton::Environment;
 use strict;
 use Moo;
 
+use Carton::Lockfile;
 use Carton::Error;
 use Path::Tiny;
 
@@ -12,7 +13,7 @@ has vendor_cache  => (is => 'lazy');
 
 sub _build_lockfile {
     my $self = shift;
-    Path::Tiny->new($self->cpanfile->dirname . "/carton.lock");
+    Carton::Lockfile->new($self->cpanfile->dirname . "/carton.lock");
 }
 
 sub _build_install_path {
diff --git a/lib/Carton/Error.pm b/lib/Carton/Error.pm
index 24af647..bcc3bbd 100644
--- a/lib/Carton/Error.pm
+++ b/lib/Carton/Error.pm
@@ -5,6 +5,8 @@ use Exception::Class (
     'Carton::Error::CommandNotFound' => { isa => 'Carton::Error' },
     'Carton::Error::CommandExit' => { isa => 'Carton::Error', fields => [ 'code' ] },
     'Carton::Error::CPANfileNotFound' => { isa => 'Carton::Error' },
+    'Carton::Error::LockfileParseError' => { isa => 'Carton::Error', fields => [ 'path' ] },
+    'Carton::Error::LockfileNotFound' => { isa => 'Carton::Error', fields => [ 'path' ] },
 );
 
 1;
diff --git a/lib/Carton/Lock.pm b/lib/Carton/Lock.pm
index cb83025..0855df0 100644
--- a/lib/Carton/Lock.pm
+++ b/lib/Carton/Lock.pm
@@ -3,12 +3,14 @@ use strict;
 use Config;
 use Carton::Dist;
 use Carton::Dist::Core;
+use Carton::Error;
 use Carton::Package;
 use Carton::Index;
 use Carton::Util;
 use CPAN::Meta;
 use CPAN::Meta::Requirements;
 use File::Find ();
+use Try::Tiny;
 use Module::CoreList;
 use Moo;
 
@@ -20,7 +22,9 @@ use constant CARTON_LOCK_VERSION => '0.9';
 sub from_file {
     my($class, $file) = @_;
 
-    my $data = Carton::Util::load_json($file);
+    my $data = try { Carton::Util::load_json($file) }
+        catch { Carton::Error::LockfileParseError->throw(error => "Can't parse carton.lock", path => $file) };
+
     return $class->new($data);
 }
 
diff --git a/lib/Carton/Lockfile.pm b/lib/Carton/Lockfile.pm
new file mode 100644
index 0000000..f4ea3be
--- /dev/null
+++ b/lib/Carton/Lockfile.pm
@@ -0,0 +1,30 @@
+package Carton::Lockfile;
+use strict;
+use parent 'Path::Tiny';
+
+sub new {
+    my $class = shift;
+    my $self = Path::Tiny->new(@_);
+    bless $self, $class; # XXX: Path::Tiny doesn't allow subclasses. Should be via Role + handles?
+}
+
+sub load_if_exists {
+    my $self = shift;
+    Carton::Lock->from_file($self) if $self->exists;
+}
+
+sub load {
+    my $self = shift;
+
+    if ($self->exists) {
+        Carton::Lock->from_file($self);
+    } else {
+        Carton::Error::LockfileNotFound->throw(
+            error => "Can't find carton.lock: Run `carton install` to build the lock file.",
+            path => $self->stringify,
+        );
+    }
+}
+
+1;
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/carton.git



More information about the Pkg-perl-cvs-commits mailing list