Giblog

 view release on metacpan or  search on metacpan

lib/Giblog/Command/publish.pm  view on Meta::CPAN

package Giblog::Command::publish;

use base 'Giblog::Command';

use strict;
use warnings;
use Mojolicious;
use Time::Piece 'localtime';
use Getopt::Long 'GetOptions';

use Carp 'confess';

sub run {
  my ($self, @argv) = @_;
  
  my $has_build_option;
  my $has_deploy_option;
  {
    local @ARGV = @argv;
    my $getopt_option_all = Getopt::Long::Configure(qw(default no_auto_abbrev no_ignore_case));
    GetOptions(
      'build' => \$has_build_option,
      'deploy' => \$has_deploy_option,
    );
    Getopt::Long::Configure($getopt_option_all);
    @argv = @ARGV;
  }
  my ($remote_rep, $branch) = @argv;
  
  my $api = $self->api;

  my $home_dir = $api->rel_file('.');
  my $public_dir = $api->rel_file('public');

  unless (defined $remote_rep) {
    confess 'Must be specify remote repository name';
  }

  unless (defined $branch) {
    confess 'Must be specify branch name';
  }
  
  if ($has_build_option) {
    my @giblog_build_command = ('giblog', '-C', $home_dir, 'build');
    if (system(@giblog_build_command) == -1) {
      confess "Fail giblog publish command with --build option. Command is @giblog_build_command: $?";
    }
  }
  
  my @git_add_command = ('git', '-C', $public_dir, 'add', '--all');
  if (system(@git_add_command) == -1) {
    confess "Fail giblog publish command. Command is @git_add_command: $?";
  }
  my $now_tp = Time::Piece::localtime;
  my @git_commit_command = ('git', '-C', $public_dir, 'commit', '-m', '"Published by Giblog at ' . $now_tp->strftime('%Y-%m-%d %H:%M:%S') . '"');
  if(system(@git_commit_command) == -1) {
    confess "Fail giblog publish command. Command is @git_commit_command : $?";
  }
  my @git_push_command = ('git', '-C', $public_dir, 'push', '-f', $remote_rep, $branch);
  if (system(@git_push_command) == -1) {
    confess "Fail giblog publish command. Command is @git_push_command : $?";
  }

  if ($has_deploy_option) {
    my @giblog_deploy_command = ('giblog', '-C', $home_dir, 'deploy');
    if (system(@giblog_deploy_command) == -1) {
      confess "Fail giblog publish command with --deploy option. Command is @giblog_deploy_command: $?";
    }
  }
}

1;

=encoding utf8

=head1 NAME

Giblog::Command::publish - Website publish command



( run in 0.904 second using v1.01-cache-2.11-cpan-8dfa8b56332 )