line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Git::Demo::Action::File; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
31
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
6
|
use File::Spec::Functions; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
114
|
|
5
|
1
|
|
|
1
|
|
7
|
use File::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
6
|
1
|
|
|
1
|
|
1054
|
use File::Copy; |
|
1
|
|
|
|
|
35946
|
|
|
1
|
|
|
|
|
124
|
|
7
|
1
|
|
|
1
|
|
12
|
use File::Basename; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2224
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new{ |
10
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
11
|
0
|
|
|
|
|
|
my $args = shift; |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
|
|
|
|
my $self = {}; |
14
|
0
|
|
|
|
|
|
my $logger = Log::Log4perl->get_logger( __PACKAGE__ ); |
15
|
0
|
|
|
|
|
|
$self->{logger} = $logger; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
bless $self, $class; |
18
|
0
|
|
|
|
|
|
return $self; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub run{ |
22
|
0
|
|
|
0
|
0
|
|
my( $self, $character, $event ) = @_; |
23
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
24
|
0
|
0
|
|
|
|
|
if( $event->action() eq 'touch' ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
return $self->_touch( $character, $event ); |
26
|
|
|
|
|
|
|
}elsif( $event->action() eq 'append' ){ |
27
|
0
|
|
|
|
|
|
return $self->_append( $character, $event ); |
28
|
|
|
|
|
|
|
}elsif( $event->action() eq 'copy' ){ |
29
|
0
|
|
|
|
|
|
return $self->_copy( $character, $event ); |
30
|
|
|
|
|
|
|
}elsif( $event->action() eq 'move' ){ |
31
|
0
|
|
|
|
|
|
return $self->_move( $character, $event ); |
32
|
|
|
|
|
|
|
}else{ |
33
|
0
|
|
|
|
|
|
die( "Unknown action: " . $event->action() ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _touch{ |
39
|
0
|
|
|
0
|
|
|
my( $self, $character, $event ) = @_; |
40
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
41
|
0
|
|
|
|
|
|
foreach my $arg( @{ $event->args() } ){ |
|
0
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $path = catfile( $character->dir(), $arg ); |
43
|
0
|
|
|
|
|
|
$logger->debug( "touching: $path" ); |
44
|
0
|
0
|
|
|
|
|
if( ! open( FH, ">", $path ) ){ |
45
|
0
|
|
|
|
|
|
die( "Could not open file ($path): $!" ); |
46
|
|
|
|
|
|
|
} |
47
|
0
|
|
|
|
|
|
close FH; |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Can accept absolute, or relative paths for the source |
53
|
|
|
|
|
|
|
# The target path will always be relative to the characters own directory |
54
|
|
|
|
|
|
|
sub _copy{ |
55
|
0
|
|
|
0
|
|
|
my( $self, $character, $event ) = @_; |
56
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my @args = @{ $event->args() }; |
|
0
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
if( scalar( @args ) < 2 ){ |
60
|
0
|
|
|
|
|
|
die( "need at least two paths for a copy" ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# The last will be the target |
64
|
0
|
|
|
|
|
|
my $target_rel = pop( @args ); |
65
|
0
|
|
|
|
|
|
my $target_abs = catdir( $character->dir(), $target_rel ); |
66
|
0
|
|
|
|
|
|
my $num_files = scalar( @args ); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# If there are more than one file to copy, the target must be a directory |
69
|
0
|
0
|
0
|
|
|
|
if( $num_files > 1 && -f $target_abs ){ |
70
|
0
|
|
|
|
|
|
die( "Cannot copy multiple files to one target file" ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
0
|
|
|
|
if( $num_files > 1 && ! -d $target_abs ){ |
74
|
0
|
|
|
|
|
|
my $f = File::Util->new(); |
75
|
0
|
0
|
|
|
|
|
if( ! $f->make_dir( $target_abs ) ){ |
76
|
0
|
|
|
|
|
|
die( "Could not create dir ($target_abs): $!" ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
foreach my $path( @args ){ |
81
|
0
|
|
|
|
|
|
my $source_path; |
82
|
0
|
0
|
|
|
|
|
if( file_name_is_absolute( $path ) ){ |
83
|
0
|
|
|
|
|
|
$source_path = $path; |
84
|
|
|
|
|
|
|
}else{ |
85
|
0
|
|
|
|
|
|
$source_path = catfile( $character->dir(), $path ); |
86
|
|
|
|
|
|
|
} |
87
|
0
|
|
|
|
|
|
my $target_path = undef; |
88
|
0
|
0
|
|
|
|
|
if( $num_files > 1 ){ |
89
|
0
|
|
|
|
|
|
$target_path = catfile( $target_abs, fileparse( $source_path ) ); |
90
|
|
|
|
|
|
|
}else{ |
91
|
0
|
|
|
|
|
|
$target_path = $target_abs; |
92
|
|
|
|
|
|
|
} |
93
|
0
|
0
|
|
|
|
|
if( -f $source_path ){ |
94
|
0
|
|
|
|
|
|
$self->output( $character, "Copying from/to\n\t$source_path\n\t$target_path" ); |
95
|
0
|
0
|
|
|
|
|
if( ! copy( $source_path, $target_path ) ){ |
96
|
0
|
|
|
|
|
|
die( "Could not copy from $source_path to $target_path: $!" ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
}else{ |
99
|
0
|
|
|
|
|
|
$logger->warn( "File does not exist: $source_path\n" ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Can accept absolute, or relative paths for the source |
107
|
|
|
|
|
|
|
# The target path will always be relative to the characters own directory |
108
|
|
|
|
|
|
|
sub _move{ |
109
|
0
|
|
|
0
|
|
|
my( $self, $character, $event ) = @_; |
110
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my @args = @{ $event->args() }; |
|
0
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
if( scalar( @args ) != 2 ){ |
114
|
0
|
|
|
|
|
|
die( "need at exactly two paths for a move" ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $source_abs; |
118
|
0
|
0
|
|
|
|
|
if( file_name_is_absolute( $args[0] ) ){ |
119
|
0
|
|
|
|
|
|
$source_abs = $args[0]; |
120
|
|
|
|
|
|
|
}else{ |
121
|
0
|
|
|
|
|
|
$source_abs = catdir( $character->dir(), $args[0] ); |
122
|
|
|
|
|
|
|
} |
123
|
0
|
|
|
|
|
|
my $target_abs = catdir( $character->dir(), $args[1] ); |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if( ! -f $source_abs ){ |
126
|
0
|
|
|
|
|
|
die( "Source file ($source_abs) does not exit" ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
$self->output( $character, "Moving from/to\n\t$args[0]\n\t$args[1]" ); |
130
|
0
|
0
|
|
|
|
|
if( ! rename( $source_abs, $target_abs ) ){ |
131
|
0
|
|
|
|
|
|
die( "Could not move from $source_abs to $target_abs: $!" ); |
132
|
|
|
|
|
|
|
} |
133
|
0
|
|
|
|
|
|
return; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub _append{ |
138
|
0
|
|
|
0
|
|
|
my( $self, $character, $event ) = @_; |
139
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
140
|
0
|
|
|
|
|
|
my @args = @{ $event->args() }; |
|
0
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
|
if( scalar( @args ) != 2 ){ |
142
|
0
|
|
|
|
|
|
die( "Incorrect number of arguments" ); |
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
|
my $path = catfile( $character->dir(), $args[0] ); |
145
|
0
|
|
|
|
|
|
my $text = $args[1]; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Some text replacements |
148
|
0
|
|
|
|
|
|
my $name = $character->name(); |
149
|
0
|
|
|
|
|
|
my $date = '' . localtime(); |
150
|
0
|
|
|
|
|
|
$text =~ s/\[% NAME %\]/$name/g; |
151
|
0
|
|
|
|
|
|
$text =~ s/\[% DATE %\]/$date/g; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
$self->output( $character, "appending to: $path" ); |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
if( ! open( FH, ">>", $path ) ){ |
156
|
0
|
|
|
|
|
|
die( "Could not open file ($path): $!" ); |
157
|
|
|
|
|
|
|
} |
158
|
0
|
|
|
|
|
|
print FH $text . "\n"; |
159
|
0
|
|
|
|
|
|
close FH; |
160
|
0
|
|
|
|
|
|
return; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub output{ |
164
|
0
|
|
|
0
|
0
|
|
my( $self, $character, $text ) = @_; |
165
|
0
|
|
|
|
|
|
my $logger = $self->{logger}; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
$logger->info( sprintf( "File (%s): %s\n", $character->name(), $text ) ); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |