| 1 | package Dist::Zilla::Plugin::SvnObtain; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $Dist::Zilla::Plugin::SvnObtain::VERSION = '0.02'; | |
| 4 | } | ||
| 5 | # ABSTRACT: obtain files from a subversion repository before building a distribution | ||
| 6 | |||
| 7 | 0 | ▶ | use SVN::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use File::Path qw/ make_path remove_tree /; | ||
| 9 | use Cwd; | ||
| 10 | use Moose; | ||
| 11 | use namespace::autoclean; | ||
| 12 | |||
| 13 | with 'Dist::Zilla::Role::Plugin'; | ||
| 14 | with 'Dist::Zilla::Role::BeforeBuild'; | ||
| 15 | |||
| 16 | has 'svn_dir' => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'Str', | ||
| 19 | required => 1, | ||
| 20 | default => 'src', | ||
| 21 | ); | ||
| 22 | |||
| 23 | has _repos => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'HashRef', | ||
| 26 | required => 1, | ||
| 27 | default => sub { {} }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub BUILDARGS { | ||
| 31 | my $class = shift; | ||
| 32 | my %repos = ref($_[0]) ? %{$_[0]} : @_; | ||
| 33 | |||
| 34 | my $zilla = delete $repos{zilla}; | ||
| 35 | my $svn_dir = delete $repos{plugin_name}; | ||
| 36 | $svn_dir = '.' if $svn_dir eq 'SvnObtain'; | ||
| 37 | |||
| 38 | my %args; | ||
| 39 | for my $project (keys %repos) { | ||
| 40 | if ($project =~ /^--/) { | ||
| 41 | (my $arg = $project) =~ s/^--//; $args{$arg} = delete $repos{$project}; next; | ||
| 42 | } | ||
| 43 | my ($url,$rev) = split ' ', $repos{$project}; | ||
| 44 | $rev = 'HEAD' unless $rev; | ||
| 45 | $repos{$project} = { url => $url, rev => $rev }; | ||
| 46 | } | ||
| 47 | |||
| 48 | return { | ||
| 49 | zilla => $zilla, | ||
| 50 | plugin_name => 'SvnObtain', | ||
| 51 | _repos => \%repos, | ||
| 52 | svn_dir => $svn_dir, | ||
| 53 | %args, | ||
| 54 | }; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub before_build { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my $svn = SVN::Client->new; | ||
| 61 | if (-d $self->svn_dir) { | ||
| 62 | $self->log("using existing directory " . $self->svn_dir); | ||
| 63 | } else { | ||
| 64 | $self->log("creating directory " . $self->svn_dir); | ||
| 65 | make_path($self->svn_dir); | ||
| 66 | } | ||
| 67 | my $prev_dir = getcwd; | ||
| 68 | chdir($self->svn_dir) or die "Can't change to the " . $self->svn_dir . " directory -- $!"; | ||
| 69 | for my $project (keys %{$self->_repos}) { | ||
| 70 | my ($url,$rev) = map { $self->_repos->{$project}{$_} } qw/url rev/; | ||
| 71 | if (-d $project) { | ||
| 72 | if (-e "$project/.svn") { | ||
| 73 | my $wc_info; | ||
| 74 | $svn->info($project, undef, undef, sub { $wc_info = $_[1] }, 0); | ||
| 75 | if ($wc_info->URL eq $url) { | ||
| 76 | $self->log("updating $project to revision $rev"); | ||
| 77 | $svn->update($project,$rev,1); | ||
| 78 | } else { | ||
| 79 | die "$project directory is not an SVN repository for $url (" .$wc_info->URL . ")"; | ||
| 80 | } | ||
| 81 | } else { | ||
| 82 | die "$project directory already exists and is not an SVN repository"; | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | $self->log("checking out $project revision $rev"); | ||
| 86 | $svn->checkout($url, $project, $rev, 1); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | chdir($prev_dir) or die "Can't change back to the $prev_dir directory -- $!"; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | __PACKAGE__->meta->make_immutable; | ||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ | ||