view lib/Slideshow/Util.pm @ 36:74859abd87cc

update cli tool
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 07 May 2018 12:59:27 +0900
parents 952136cc268f
children
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
    edit_memo
/;

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};
    } elsif ( $arg eq "memo"){
        return { memo => 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 ($y,$m,$d) = ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d'));
    my $slide = $root_dir->child($y .'/'. $m .'/'. $d .'/'.'slide.md')->touchpath;
    $template->copy($slide);
}

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

    # ex... 2018/02/14
    my ($y,$m,$d) = ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d'));
    my $root_dir = path($root_directory_name.'/'.$y.'/'.$m);

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

    return $date;
}

# sub zip {
#     my $recently = _search_recently(shift);
#     my $t = localtime;
# 
#     my ($y,$m,$d) = ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d'));
#     my $zip = $root_dir->child($y .'/'. $m .'/'. $d .'/'.'zip.txt')->touchpath;
# 
#     $t -= ONE_WEEK;
# 
#     for(1..7){
#         my ($y,$m,$d) = ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d'));
#         my $memo = $root_dir->child($y .'/'. $m .'/'. $d .'/'.'memo.txt')->touchpath;
#         $zip->append($memo->slurp);
#     }
# }
# 
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_memo {
    my $root_dir = path(shift);
    my $t = localtime;

    # ex... 2018/02/14
    my ($y,$m,$d) = ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d'));
    my $memo = $root_dir->child($y .'/'. $m .'/'. $d .'/'.'memo.txt')->touchpath;
    exec $ENV{EDITOR}, ($memo->realpath);
}

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;