From 6f31bfb5489a6c454b6e263476ca330df69f593c Mon Sep 17 00:00:00 2001 From: Ken-ichi Mito Date: Sun, 18 Aug 2013 05:28:12 +0900 Subject: [PATCH] remove Class::Accessor::Fast * handle directory path correctly --- Dev/github/GitHubBackup.pm | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/Dev/github/GitHubBackup.pm b/Dev/github/GitHubBackup.pm index 34e40e6..130c6f6 100644 --- a/Dev/github/GitHubBackup.pm +++ b/Dev/github/GitHubBackup.pm @@ -23,28 +23,20 @@ sub json_api { package GitHubBackup; -use base qw(Class::Accessor::Fast); use strict; use warnings; use utf8; use Carp qw(croak); - -__PACKAGE__->mk_accessors( qw( - directory -)); +use File::Spec; # both hash and hashref are acceptable sub new { my $class = shift; - my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; - if (! $args->{directory}) { - $args->{directory} = "."; - } - return $class->SUPER::new($args); + return bless $args, $class; } sub account { @@ -71,6 +63,17 @@ sub repository { return $self->{repository}; } +sub directory { + my $self = shift; + my $args = shift; + + if (defined $args) { + $self->{directory} = File::Spec->rel2abs($args); + } + + return $self->{directory}; +} + sub repos { my $self = shift; return $self->{repos} if ($self->{repos}); @@ -125,7 +128,6 @@ sub backup { package GitHubBackup::Repository; -use base qw(Class::Accessor::Fast); use strict; use warnings; @@ -133,7 +135,8 @@ use utf8; use Carp qw(croak); use Git::Repository; use File::chdir; - +use File::Spec; +use File::Path qw(mkpath); sub new { my $class = shift; @@ -144,19 +147,24 @@ sub new { $args->{clone_url} = $result->{clone_url}; } - return $class->SUPER::new($args); + return bless $args, $class; } sub directory { my $self = shift; - return $self->{directory}->(); + my $path = $self->{full_name}; + if (my $base = $self->{directory}->()) { + $path = File::Spec->catfile($base, $path); + } + + return $path; } sub clone_git { my $self = shift; - my $dir = $self->directory .'/'. $self->{full_name}; + my $dir = $self->directory; if (-d "$dir") { local $CWD = $dir; print "fetch ", $dir, "\n"; @@ -165,6 +173,7 @@ sub clone_git { } print "clone ", $dir, "\n"; + mkpath $dir; Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir); return $self; } -- 1.7.9.5