File: | blib/lib/Git/ReleaseRepo/Command/deploy.pm |
Coverage: | 87.2% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Git::ReleaseRepo::Command::deploy; | ||||||
2 | # ABSTRACT: Deploy a release repository | ||||||
3 | |||||||
4 | 2 2 2 | 1618 2 31 | use strict; | ||||
5 | 2 2 2 | 4 2 23 | use warnings; | ||||
6 | 2 2 2 | 4 5 5 | use Moose; | ||||
7 | 2 2 2 | 7141 2 53 | use File::Spec::Functions qw( catdir ); | ||||
8 | 2 2 2 | 5398 2824 81 | use File::Copy qw( move ); | ||||
9 | 2 2 2 | 5 2 703 | use Cwd qw( getcwd ); | ||||
10 | |||||||
11 | extends 'Git::ReleaseRepo::CreateCommand'; | ||||||
12 | |||||||
13 | override usage_desc => sub { | ||||||
14 | my ( $self ) = @_; | ||||||
15 | return super() . " <repo_url> [<repo_name>]"; | ||||||
16 | }; | ||||||
17 | |||||||
18 | sub description { | ||||||
19 | 0 | 1 | 0 | return 'Deploy a release repository'; | |||
20 | } | ||||||
21 | |||||||
22 | sub validate_args { | ||||||
23 | 3 | 1 | 1751 | my ( $self, $opt, $args ) = @_; | |||
24 | 3 | 9 | return $self->usage_error( "Repository URL is required" ) if ( @$args < 1 ); | ||||
25 | 3 | 6 | return $self->usage_error( "Too many arguments" ) if ( @$args > 2 ); | ||||
26 | 3 | 8 | return $self->usage_error( 'Must specify --version_prefix' ) unless $opt->{version_prefix}; | ||||
27 | } | ||||||
28 | |||||||
29 | around opt_spec => sub { | ||||||
30 | my ( $orig, $self ) = @_; | ||||||
31 | return ( | ||||||
32 | $self->$orig, | ||||||
33 | [ 'branch=s' => 'Specify the release branch to deploy. Defaults to the latest release branch.' ], | ||||||
34 | [ 'master' => 'Deploy the "master" version of the repository and all submodules, for testing.' ], | ||||||
35 | ); | ||||||
36 | }; | ||||||
37 | |||||||
38 | augment execute => sub { | ||||||
39 | my ( $self, $opt, $args ) = @_; | ||||||
40 | my $repo_name = $args->[1]; | ||||||
41 | my $rename_repo = 0; | ||||||
42 | if ( !$repo_name ) { | ||||||
43 | # The automatic name will come from the release branch of the deployed repository, which | ||||||
44 | # we won't have until we actually clone the repository, so create a temporary | ||||||
45 | # directory instead | ||||||
46 | $rename_repo = 1; | ||||||
47 | $repo_name = join "-", $self->repo_name_from_url( $args->[0] ), 'deploy', time; | ||||||
48 | } | ||||||
49 | my $repo_dir = catdir( getcwd, $repo_name ); | ||||||
50 | my $cmd = Git::Repository->command( clone => $args->[0], $repo_dir ); | ||||||
51 | my @stderr = readline $cmd->stderr; | ||||||
52 | my @stdout = readline $cmd->stdout; | ||||||
53 | $cmd->close; | ||||||
54 | if ( $cmd->exit != 0 ) { | ||||||
55 | die "Could not clone '$args->[0]'.\nEXIT: " . $cmd->exit . "\nSTDERR: " . ( join "\n", @stderr ) | ||||||
56 | . "\nSTDOUT: " . ( join "\n", @stdout ); | ||||||
57 | } | ||||||
58 | my $repo = Git::Repository->new( work_tree => $repo_dir ); | ||||||
59 | $repo->release_prefix( $opt->{version_prefix} ); | ||||||
60 | my $version = $opt->{master} ? "master" | ||||||
61 | : $opt->{branch} ? $repo->latest_version( $opt->{branch} ) | ||||||
62 | : $repo->latest_version; | ||||||
63 | my $branch = $opt->{master} ? "master" | ||||||
64 | : $opt->{branch} ? $opt->{branch} | ||||||
65 | : $repo->latest_release_branch; | ||||||
66 | $cmd = $repo->command( checkout => $version ); | ||||||
67 | @stderr = readline $cmd->stderr; | ||||||
68 | @stdout = readline $cmd->stdout; | ||||||
69 | $cmd->close; | ||||||
70 | if ( $cmd->exit != 0 ) { | ||||||
71 | die "Could not checkout '$version'.\nEXIT: " . $cmd->exit . "\nSTDERR: " . ( join "\n", @stderr ) | ||||||
72 | . "\nSTDOUT: " . ( join "\n", @stdout ); | ||||||
73 | } | ||||||
74 | $repo->run( submodule => 'update', '--init' ); | ||||||
75 | if ( $opt->{master} ) { | ||||||
76 | my $cmd = $repo->command( submodule => 'foreach', 'git checkout master && git pull origin master' ); | ||||||
77 | my @stderr = readline $cmd->stderr; | ||||||
78 | my @stdout = readline $cmd->stdout; | ||||||
79 | $cmd->close; | ||||||
80 | if ( $cmd->exit != 0 ) { | ||||||
81 | die "Could not checkout master\nEXIT: " . $cmd->exit . "\nSTDERR: " . ( join "\n", @stderr ) | ||||||
82 | . "\nSTDOUT: " . ( join "\n", @stdout ); | ||||||
83 | } | ||||||
84 | } | ||||||
85 | if ( $rename_repo ) { | ||||||
86 | $repo_name = join "-", $self->repo_name_from_url( $args->[0] ), $branch; | ||||||
87 | my $new_repo_dir = catdir( getcwd, $repo_name ); | ||||||
88 | move( $repo_dir, $new_repo_dir ); | ||||||
89 | $repo = Git::Repository->new( work_tree => $new_repo_dir ); | ||||||
90 | } | ||||||
91 | # Set new default repo and configuration | ||||||
92 | # Deploy creates a detatched HEAD, so we need to know what branch we're | ||||||
93 | # tracking | ||||||
94 | $self->update_config( $opt, $repo, { track => $branch } ); | ||||||
95 | }; | ||||||
96 | |||||||
97 | 1; |