view lib/Slideshow/Util.pm @ 15:80767afba59c

auto-Update generated slides by script
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 30 Jan 2018 19:09:40 +0900
parents 967fe50f1ef4
children 79a79bbc5c01
line wrap: on
line source

package Slideshow::Util;
use strict;
use warnings;
use utf8;

use Carp qw/ croak /;

use base 'Exporter';

use Time::Piece;
use feature 'say';
use Path::Tiny;
use File::chdir;
use Capture::Tiny qw/capture/;

our @EXPORT = qw/
    getopts
    new
    set_template
    build_recently 
    build_pinpoint 
    open_slide
    edit_slide
/;

sub getopts {
    my ($arg,$path)  = @_;

    unless (defined $arg){
        return { help => 1};
    }

    if ($arg eq "new") {
        return {new => 1};

    } elsif ( $arg eq "upload") {
       upload();
       exit;

    } elsif ( $arg eq "build") {

        if(defined $path){
            return { build_point=> $path};
        } else {
            return { build => "recent"};
        }


    } elsif ( $arg eq "build-open"){
        return { build_open => 1};

    } elsif ( $arg eq "open"){
        return { open => 1};

    } elsif ( $arg eq "edit"){
        return { edit=> 1};

    } else {
        return { help => 1};
    }

}

sub set_template {
    my $template = shift;
    my $file  = path($template);
    return $file;
}

sub new {
    my ($template,$root_directory_name) = @_;
    my $root_dir = path($root_directory_name);
    my $t = localtime;

    # ex... 2018/02/14
    my $slide = $root_dir->child($t->strftime('%Y%m%d'). '/'.'slide.md')->touchpath;
    $template->copy($slide);
}

sub _search_recently {
    my ($root_directory_name) = @_;
    my $root_dir = path($root_directory_name);
    my $t = localtime;

    my $recently = shift @{[sort { $b->stat->mtime <=> $a->stat->mtime } $root_dir->children]};

    return $recently;
}



sub build_recently {
    my $recently = _search_recently(shift);
    _build($recently);
}

sub build_pinpoint {
    my $target = shift;

    my $target_path = path($target);

    my $dir = $target_path->dirname;
    my $slide = $target_path->basename;

   _build($dir,$slide); 
}

sub edit_slide {
    my $recently = _search_recently(shift);
    my $target = $recently->child('slide.md');
    exec $ENV{EDITOR}, ($target->realpath);
}

sub open_slide {
    my $recently = _search_recently(shift);
    my $target = $recently->child('slide.html');

    if ( $target->realpath){
        system "open", ($target->realpath);
    }  else {
        say "didn't slide.html";
    }
}

sub _build {
    my ($dir,$target) = @_;
    use Capture::Tiny;

    $target //= 'slide.md';

    say "[AUTO] BUILD at $dir/$target";

    local $CWD = $dir;

    my ($stdout,$stderr,$exit) = capture {
        system(" slideshow build ${target} -t s6cr");
    };

    if ($stderr){
        croak "Perl can't build....";
    }
}


sub upload {

    say "[AUTO]hg addremove";
    my ($stdout,$stderr,$exit) = capture {
        system("hg addremove");
        system("hg add");
    };

    if ($stderr) {
        croak "didn't add";
    }

    say "[AUTO]hg commit -m auto-Update generated slides by script";

    ($stdout,$stderr,$exit) = capture {
        system('hg commit -m "auto-Update generated slides by script"');
    };

    if ($stderr) {
        say $stderr;
        croak "didn't commit";
    }
    
    say "[AUTO]hg push";

    ($stdout,$stderr,$exit) = capture {
        system('hg push');
    };

    if ( $stderr ) {
        say $stderr;
        croak "didn't commit";
    } else {
        say $stdout;
    }
}

1;