Sorauta-SVN-AutoCommit

 view release on metacpan or  search on metacpan

lib/Sorauta/SVN/AutoCommit.pm  view on Meta::CPAN

#============================================
# SVN自動コミットクラス
#
# -------------------------------------------
# アクセサ
# svn_mode         String         SVNのコミットモード
#                                       commit ... 自動コミットモード
#                                       update ... アップデート実行
# work_dir_path    String         作業フォルダまでのパス
#                                       ex)c:\projects\hoge_svn_dir
# debug            Integer        デバッグモード(コミット防止)
#                                       0 ... コミットする(デフォルト)
#                                       1 ... アップデートされたファイル一覧の列挙などはやるが、AddやCommit自体はしない
# thumbnail_width  Integer        サムネイル横幅
#                                       デフォルトは$THUMBNAI_WIDTHの値
# thumbnail_height Integer        サムネイル縦幅
#                                       デフォルトは$THUMBNAI_HEIGHTの値
#
# changes          Integer        ローカルのSVNに更新があったか
#                                       0 ... 更新なし(デフォルト)
#                                       1 ... 更新あり
# commit_comment   String         コミット時のコメント
#                                       デフォルトは$COMMIT_COMMENTの値
#
#============================================
package Sorauta::SVN::AutoCommit;
use base qw/Class::Accessor::Fast/;

use 5.012003;
use strict;
use warnings;
use utf8;
use CGI::Carp qw/fatalsToBrowser/;
use Data::Dumper;
use SVN::Agent;
use SVN::Agent::Dummy;
use Image::Magick;
use Sorauta::Utility;

our $VERSION = '0.02';

# サムネイルの縦横
our($THUMBNAIL_WIDTH, $THUMBNAIL_HEIGHT) = (160, 90);

# コミット時のコメント
our $COMMIT_COMMENT = "auto commit from Sorauta::SVN::AutoCommit";

# コミットするか否か
our $DEBUG = 0;

__PACKAGE__->mk_accessors(
  qw/svn_mode work_dir_path debug thumbnail_width thumbnail_height changes commit_comment/);

#==========================================
# コミットを実行
# req:
# res:
#==========================================
sub execute {
  my $self = shift;

  if (!$self->thumbnail_width) {
    $self->thumbnail_width($THUMBNAIL_WIDTH);
  }
  if (!$self->thumbnail_height) {
    $self->thumbnail_height($THUMBNAIL_HEIGHT);
  }
  if (!$self->commit_comment) {
    $self->commit_comment($COMMIT_COMMENT);
  }

  # must be defined accessors
  if (!$self->svn_mode || !$self->work_dir_path) {
    die 'must be define accessor svn_mode(auto_commit|update), work_dir_path(/Users/user/Desktop/svn_work_folder)';
  }

  # set svn agent
  my $sa;
  if ($self->debug) {
    $sa = SVN::Agent::Dummy->load({
      path => $self->work_dir_path
    });
  }
  else {
    $sa = SVN::Agent->load({
      path => $self->work_dir_path
    });
  }
  #print Dumper($sa);

  # execute command
  if ($self->svn_mode eq 'auto_commit') {
    $self->_auto_commit($sa);
  }
  elsif ($self->svn_mode eq 'update') {
    $self->update($sa);
  }

lib/Sorauta/SVN/AutoCommit.pm  view on Meta::CPAN

    else {
      # add thumbnail image
      if ($unknown_file =~ /^([\w\_\-]+)\\([0-9]+)\\([\w\_\.]+)/) {
          my($type, $id_or_jan_code, $file_name) = ($1, $2, $3);
          my $abs_dir_path = cat($$self->work_dir_path, 'thumbnail', $type, $id_or_jan_code);

          $self->_create_thumbnail_image(
            $sa,
            cat($self->work_dir_path, $unknown_file),
            cat($abs_dir_path, $file_name));
      }
    }
  }
}

#==========================================
# SVNに登録済みだがローカルに存在しない場合削除するようにスケジューリング
# req:
#   sa: SVN::Agentのインスタンス
# res:
#   result: null
#==========================================
sub _remove_missing_files {
  my($self, $sa) = @_;

  my @missing_files = @{$sa->missing};
  foreach my $missing_file(@missing_files) {
    next if is_hidden_file($missing_file);

    # ファイルパスをエスケープ
    $missing_file = _escape($missing_file);

    $sa->remove($missing_file);

    # サムネイル画像も削除
    if ($missing_file =~ /^([\w\_\-]+)\\([0-9]+)\\([\w\_\.]+)/) {
      my($type, $id_or_jan_code, $file_name) = ($1, $2, $3);

      # ファイルパスをエスケープ
      $file_name = _escape($file_name);

      my $dir_path = cat($$self->work_dir_path, 'thumbnail', $type, $id_or_jan_code);
      if (-e cat($dir_path, $file_name)) {
        $sa->remove(cat($dir_path, $file_name));
      }
    }
  }
}

#==========================================
# サムネイルを生成する
# req:
#   sa: SVN::Agentのインスタンス
#   original_file_path: 元ファイルのパス
#   thumbnail_file_path: サムネイルファイルのパス
# res:
#   result: null
#==========================================
sub _create_thumbnail_image {
  my($self, $sa, $original_file_path, $thumbnail_file_path) = @_;
  my $image = Image::Magick->new;

  # 元画像を読み込む
  print $image->Read(
    $original_file_path
  );

  # タテヨコ比率指定
  print $image->Resize(
    geometry => $self->thumbnail_width.'x'.$self->thumbnail_height
  );

  # ファイル保存
  print $image->Write(
    filename    => $thumbnail_file_path,
    compression => 'None'
  );

  # 追加する
  print $sa->add($thumbnail_file_path);
}

#==========================================
# ファイル等をエスケープ
# req:
#   file_str: ファイル文字列
# res:
#   file_str: エスケープ後のファイル文字列
#==========================================
sub _escape {
  return quotemeta(shift);
}

1;

__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Sorauta::SVN::AutoCommit - auto recognize new file and deleted file, and commit those files

=head1 SYNOPSIS

  use Sorauta::SVN::AutoCommit;

  my $SVN_MODE = "auto_commit";
  my $SVN_WORK_DIR = "/Users/user/Desktop/svn_dir";
  my $DEBUG = 0;

  my $ssa = Sorauta::SVN::AutoCommit->new({
    svn_mode      => $SVN_MODE,
    work_dir_path => $SVN_WORK_DIR,
    debug         => $DEBUG,
  });

  #print $ssa;
  $ssa->execute();

=head1 DESCRIPTION



( run in 2.555 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )