File Coverage

blib/lib/Giblog/Command/publish.pm
Criterion Covered Total %
statement 21 55 38.1
branch 0 18 0.0
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 29 82 35.3


line stmt bran cond sub pod time code
1             package Giblog::Command::publish;
2              
3 1     1   8950 use base 'Giblog::Command';
  1         3  
  1         145  
4              
5 1     1   7 use strict;
  1         2  
  1         26  
6 1     1   5 use warnings;
  1         2  
  1         40  
7 1     1   6 use Mojolicious;
  1         2  
  1         10  
8 1     1   40 use Time::Piece 'localtime';
  1         1  
  1         10  
9 1     1   75 use Getopt::Long 'GetOptions';
  1         1  
  1         9  
10              
11 1     1   153 use Carp 'confess';
  1         2  
  1         488  
12              
13             sub run {
14 0     0 1   my ($self, @argv) = @_;
15            
16 0           my $has_build_option;
17             my $has_deploy_option;
18             {
19 0           local @ARGV = @argv;
  0            
20 0           my $getopt_option_all = Getopt::Long::Configure(qw(default no_auto_abbrev no_ignore_case));
21 0           GetOptions(
22             'build' => \$has_build_option,
23             'deploy' => \$has_deploy_option,
24             );
25 0           Getopt::Long::Configure($getopt_option_all);
26 0           @argv = @ARGV;
27             }
28 0           my ($remote_rep, $branch) = @argv;
29            
30 0           my $api = $self->api;
31              
32 0           my $home_dir = $api->rel_file('.');
33 0           my $public_dir = $api->rel_file('public');
34              
35 0 0         unless (defined $remote_rep) {
36 0           confess 'Must be specify remote repository name';
37             }
38              
39 0 0         unless (defined $branch) {
40 0           confess 'Must be specify branch name';
41             }
42            
43 0 0         if ($has_build_option) {
44 0           my @giblog_build_command = ('giblog', '-C', $home_dir, 'build');
45 0 0         if (system(@giblog_build_command) == -1) {
46 0           confess "Fail giblog publish command with --build option. Command is @giblog_build_command: $?";
47             }
48             }
49            
50 0           my @git_add_command = ('git', '-C', $public_dir, 'add', '--all');
51 0 0         if (system(@git_add_command) == -1) {
52 0           confess "Fail giblog publish command. Command is @git_add_command: $?";
53             }
54 0           my $now_tp = Time::Piece::localtime;
55 0           my @git_commit_command = ('git', '-C', $public_dir, 'commit', '-m', '"Published by Giblog at ' . $now_tp->strftime('%Y-%m-%d %H:%M:%S') . '"');
56 0 0         if(system(@git_commit_command) == -1) {
57 0           confess "Fail giblog publish command. Command is @git_commit_command : $?";
58             }
59 0           my @git_push_command = ('git', '-C', $public_dir, 'push', '-f', $remote_rep, $branch);
60 0 0         if (system(@git_push_command) == -1) {
61 0           confess "Fail giblog publish command. Command is @git_push_command : $?";
62             }
63              
64 0 0         if ($has_deploy_option) {
65 0           my @giblog_deploy_command = ('giblog', '-C', $home_dir, 'deploy');
66 0 0         if (system(@giblog_deploy_command) == -1) {
67 0           confess "Fail giblog publish command with --deploy option. Command is @giblog_deploy_command: $?";
68             }
69             }
70             }
71              
72             1;
73              
74             =encoding utf8
75              
76             =head1 NAME
77              
78             Giblog::Command::publish - Website publish command
79              
80             =head1 DESCRIPTION
81              
82             L is website publish command.
83              
84             =head1 USAGE
85              
86             giblog publish REMOTE_REPOSITORY BRANCH
87            
88             giblog publish --build REMOTE_REPOSITORY BRANCH
89              
90             =head1 METHODS
91              
92             L inherits all methods from L and
93             implements the following new ones.
94              
95             =head2 run
96              
97             $command->run($remote_repository, $branch);
98             $command->run('--build', $remote_repository, $branch);
99              
100             Publish your website by specifing remote repository name and branch name.
101              
102             This is the same as the following command. In this example, the repository name is origin and the branch name is main. YY-mm-dd HH:MM:SS is current date and time.
103              
104             git -C public add --all
105             git -C public commit -m "Published by Giblog at YY-mm-dd HH:MM:SS"
106             git -C public push -f origin main
107              
108             When you deploy this on the production environment, you can use the following command.
109            
110             # Deployment on production environment
111             git fetch
112             git reset --hard origin/main
113              
114             If C<--build> option is specified, "giblog build" is executed before publishing.
115              
116             If C<--deploy> option is specified, "giblog deploy" is executed after publishing.