File Coverage

lib/Sorauta/SVN/Updater.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             #============================================
2             # SVN自動アップデートクラス
3             # updateだと競合するので、毎度ファイルを削除してcheckoutするようにしている
4             # -------------------------------------------
5             # アクセサ
6             # repository_url String リポジトリへのURL
7             # http://svn_url/repository_path
8             # work_dir_path String 作業フォルダまでのパス
9             # ex)c:\batch\images
10             # tmp_dir_path String SVNの最新版をチェックアウトするテンポラリフォルダ
11             # os String OSの種類
12             # Win ... windows
13             # Mac ... mac os
14             # debug Integer デバッグモード
15             # 0 ... コミットする(デフォルト)
16             # 1 ... リストの列挙などはやるが、コミット自体はしない
17             #============================================
18             package Sorauta::SVN::Updater;
19 1     1   21092 use base qw/Class::Accessor::Fast/;
  1         3  
  1         739  
20            
21             use strict;
22             use warnings;
23             use utf8;
24             use CGI::Carp qw/fatalsToBrowser/;
25             use Data::Dumper;
26             use SVN::Agent;
27             use SVN::Agent::Dummy;
28             use File::Copy::Recursive qw/dircopy/;
29             use Sorauta::Utility;
30            
31             our $VERSION = '0.01';
32            
33             __PACKAGE__->mk_accessors(
34             qw/repository_url work_dir_path tmp_dir_path os debug/);
35            
36             #==========================================
37             # SVNのアップデートを実行
38             # req:
39             # res:
40             # result: 成功の時は1、失敗時はそれ以外
41             #==========================================
42             sub execute {
43             my $self = shift;
44            
45             # must be defined accessors
46             if (!$self->work_dir_path || !$self->os || !$self->repository_url || !$self->tmp_dir_path) {
47             die 'must be define accessor work_dir_path(/Users/yuki/Desktop/svn_work_folder), os(Win|Mac), '.
48             'repository_url(http://svn_url/repository_path/), tmp_dir_path(/tmp/dir_path/)';
49             }
50            
51             # set svn agent
52             my $sa;
53             my $tmp_dir_path = $self->tmp_dir_path;
54             if ($self->debug) {
55             $sa = SVN::Agent::Dummy->new({
56             path => $tmp_dir_path
57             });
58             }
59             else {
60             $sa = SVN::Agent->new({
61             path => $tmp_dir_path
62             });
63             }
64             #print Dumper($sa);
65            
66             # remove tmp_dir
67             if ($self->os eq 'Win') {
68             system("del", "/q", $tmp_dir_path);
69             }
70             elsif ($self->os eq 'Mac') {
71             my $cmd = "rm -rf $tmp_dir_path";
72             `$cmd`;
73             }
74            
75             # execute command
76             print $sa->checkout($self->repository_url);
77            
78             # remove .svn folders
79             if ($self->os eq 'Win') {
80             # TODO: system("del", "/q", $destination_path);
81             print "[warnings]remove .svn folders for Win";
82             }
83             elsif ($self->os eq 'Mac') {
84             my $cmd = "cd $tmp_dir_path; rm -rf \$(find ./ -name \".svn\")";
85             `$cmd`;
86             }
87            
88             # copy checkout files to www folder
89             $File::Copy::Recursive::CPRFComp = 1;
90             dircopy($tmp_dir_path.'*', $self->work_dir_path);
91            
92             return 1;
93             }
94            
95             1;
96             __END__