| 1 | use strict; | ||
| 2 | |||
| 3 | package Copy::From::Git; | ||
| 4 | |||
| 5 | use File::Basename; | ||
| 6 | |||
| 7 | our $VERSION = 0.0003_02; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $class = shift; | ||
| 11 | |||
| 12 | my $args = { | ||
| 13 | server => 'guthub.com', | ||
| 14 | path => undef, | ||
| 15 | repo => undef, | ||
| 16 | branch => 'master', | ||
| 17 | subdir => '.', | ||
| 18 | user => getlogin(), | ||
| 19 | @_, | ||
| 20 | }; | ||
| 21 | |||
| 22 | return bless $args => $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub clone { | ||
| 26 | my ($self, $target) = @_; | ||
| 27 | |||
| 28 | $target = '/tmp/testing/' unless $target; | ||
| 29 | |||
| 30 | unless( -d $target ){ | ||
| 31 | my $cmd; | ||
| 32 | if( $self->{http} ){ | ||
| 33 | $cmd = sprintf "git clone -b %s %s%s/%s %s", $self->{branch}, $self->{http}, ${self}->{server}, ${self}->{path}, $target ; | ||
| 34 | } else { | ||
| 35 | $cmd = sprintf "git clone -b %s %s\@%s:%s/%s %s", $self->{branch}, $self->{user}, ${self}->{server}, ${self}->{path}, ${self}->{repo}, $target ; | ||
| 36 | } | ||
| 37 | print "Executing: [$cmd]\n"; | ||
| 38 | system( $cmd ); | ||
| 39 | } else { | ||
| 40 | my $cmd = sprintf "cd %s && git pull", $target; | ||
| 41 | print "Executing: [$cmd]\n"; | ||
| 42 | system( $cmd ); | ||
| 43 | } | ||
| 44 | |||
| 45 | $self->{target} = $target; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | sub cleanup { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | print "Cleaning up $self->{target}..\n"; | ||
| 53 | system( "rm -rI $self->{target}" ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub copy { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | my @files = `find $self->{target} -type f`; | ||
| 60 | chomp for @files; | ||
| 61 | |||
| 62 | for my $k ( keys %{$self->{files}} ){ | ||
| 63 | print "Finding [$k]..\n"; | ||
| 64 | my $target = ${self}->{files}->{"$k"}; | ||
| 65 | my @f = grep { /$k/ } @files; | ||
| 66 | |||
| 67 | #print "Target: [$target]\n"; | ||
| 68 | |||
| 69 | # We need to ensure that the target dir | ||
| 70 | # exists for each of these files. | ||
| 71 | for ( @f ){ | ||
| 72 | my ($filename, $directories, $suffix) = fileparse($_); | ||
| 73 | #print "$filename => $directories .. "; | ||
| 74 | |||
| 75 | # Check that $directories exists | ||
| 76 | $directories =~ s/^$self->{target}//; | ||
| 77 | #print "$directories\n"; | ||
| 78 | |||
| 79 | `mkdir -p ${target}/${directories}` | ||
| 80 | unless -d "${target}/${directories}"; | ||
| 81 | |||
| 82 | `cp -v $_ ${target}/${directories}`; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub run { | ||
| 88 | my ($self) = @_; | ||
| 89 | $self->clone; | ||
| 90 | $self->copy; | ||
| 91 | $self->cleanup; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ |