blib/lib/Shipwright/Backend/FS.pm

filter: Statementstmt 56.0% 70 / 125 Branchbran 27.7% 15 / 54 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 59.2% 16 / 27 Podpod 100% 4 / 4 totaltotal 50.0% 113 / 226 CC 33 SCAR 36.8
CC
33
Cov
48%
_cmd
129.9
__ANON__
20.0
__ANON__
20.0
CRAP 39.5
« blib/lib/Shipwright/Script/Requires.pm ↑ Summary blib/lib/Shipwright/Source/Directory.pm »
1 package Shipwright::Backend::FS;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Spec::Functions qw/catfile splitdir catdir rel2abs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Shipwright::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Copy::Recursive qw/rcopy rmove/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Path qw/remove_tree make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our %REQUIRE_OPTIONS = ( import => [qw/source/] );
11  
12 3 ▶use base qw/Shipwright::Backend::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Shipwright::Backend::FS - File System backend
17  
18 =head1 SYNOPSIS
19  
20 shipwright create -r fs:/home/me/shipwright/my_project
21  
22 =head1 DESCRIPTION
23  
24 This module implements file system based backend with version control
25 for Shipwright L<repository|Shipwright::Manual::Glossary/repository>.
26  
27 =head1 METHODS
28  
29 =cut
30  
31 =over 4
32  
33 =item build
34  
35 =cut
36  
37 6 ▶sub build {
Subroutine build: called CC=2 SCAR=7.1
Pod documented
38 6 my $self = shift;
39 6 $self->strip_repository;
40  
41 6 my $repo = $self->repository;
42 0 $repo =~ s/^~/user_home/e;
43 6 my $abs_path = rel2abs($repo);
44 6 ▶ $repo = $abs_path if $abs_path;
Branch: 1 branch
BranchTrueFalseTotal
if $abs_path 6 0 6
45 6 $self->repository($repo);
46  
47 6 $self->SUPER::build(@_);
48 }
49  
50 =item initialize
51  
52 Initialize a project.
53  
54 =cut
55  
56 2 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=7.0
Pod documented
57 2 my $self = shift;
58  
59 2 my $dir = $self->SUPER::initialize(@_);
60  
61 2 $self->delete; # clean repository in case it exists
62  
63 2 ▶ rcopy( $dir, $self->repository )
Branch: 1 branch
BranchTrueFalseTotal
rcopy($dir, $self->repository) or confess_or_die("can't copy $dir to " . $self->repository . ": $!") 2 0 2
64 or confess_or_die "can't copy $dir to " . $self->repository . ": $!";
65 }
66  
67 # a cmd generating factory
68 6 ▶sub _cmd {
Subroutine _cmd: called CC=16 SCAR=48.7
69 6 my $self = shift;
70 6 my $type = shift;
71 6 my %args = @_;
72 6 ▶ $args{path} ||= '';
Condition100%2/2
$args{"path"} ||= ""
l!l
24
MC/DC100%1/1
$args{"path"} ||= ""
$args{"path"}
Truth table100%2/2
A
A= $args{"path"}
Aresult
11
00
73  
74 6 for ( @{ $REQUIRE_OPTIONS{$type} } ) {
75 0 ▶ confess_or_die "$type need option $_" unless $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} 0 0 0
76 }
77  
78 6 my @cmd;
79  
80 6 ▶ if ( $type eq 'checkout' || $type eq 'export' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'checkout' or $type eq 'export') { } 0 6 6
elsif ($type eq 'import') { } 0 6 6
elsif ($type eq 'delete') { } 2 4 6
elsif ($type eq 'move') { } 0 4 4
elsif ($type eq 'info') { } 4 0 4
elsif ($type eq 'list') { } 0 0 0
elsif ($type eq 'cat') { } 0 0 0
Condition33%1/3
$type eq "checkout" or $type eq "export"
l!l&&r!l&&!r
006
MC/DC0%0/2
$type eq "checkout" or $type eq "export"
$type eq "checkout" $type eq "export"
Truth table33%1/3
A or B
A= $type eq "checkout"B= $type eq "export"
ABresult
1X1
011
000
81 0 ▶ @cmd = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 rcopy( $self->repository . $args{path}, $args{target} );
83 0 };
84 }
85 elsif ( $type eq 'import' ) {
86 0 ▶ if ( $args{_extra_tests} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'_extra_tests'}) { } 0 0 0
87 0 ▶ @cmd = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 rcopy( $args{source},
89 0 catdir( $self->repository, 't', 'extra' ) );
90 0 };
91 }
92 else {
93 0 ▶ if ( my $script_dir = $args{build_script} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $script_dir = $args{'build_script'}) { } 0 0 0
94 0 ▶ push @cmd, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 rcopy( catdir($script_dir),
96 0 catdir( $self->repository, 'scripts', $args{name} ) );
97 0 };
98 }
99 else {
100 0 ▶ if ( $self->has_branch_support ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_branch_support) { } 0 0 0
101 0 my @dirs = splitdir( $args{as} );
102 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (-e catdir($self->repository, "sources", $args{"name"}, @dirs[0 .. $#dirs - 1])) 0 0 0
103 -e catdir(
104 $self->repository, 'sources',
105 $args{name}, @dirs[ 0 .. $#dirs - 1 ]
106 )
107 )
108 {
109 0 ▶ push @cmd, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 make_path(
111 catdir(
112 $self->repository,
113 'sources',
114 $args{name},
115 0 @dirs[ 0 .. $#dirs - 1 ]
116 )
117 );
118 0 };
119 }
120  
121 0 ▶ push @cmd, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 rcopy(
123 catdir( $args{source} ),
124 catdir(
125 $self->repository, 'sources',
126 $args{name}, $args{as}
127 )
128 0 );
129 0 };
130 }
131 else {
132 0 ▶ push @cmd, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 rcopy( catdir( $args{source} ),
134 0 catdir( $self->repository, 'dists', $args{name} ) );
135 0 };
136 }
137 }
138 }
139 }
140 elsif ( $type eq 'delete' ) {
141 2 ▶ @cmd = sub { remove_tree( $self->repository . $args{path} ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
142 }
143 elsif ( $type eq 'move' ) {
144 0 ▶ @cmd = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 rmove(
146 $self->repository . $args{path},
147 $self->repository . $args{new_path}
148 0 );
149 0 };
150 }
151 elsif ( $type eq 'info' ) {
152 4 ▶ @cmd = sub { -e $self->repository . $args{path} };
Subroutine __ANON__: called CC=1 SCAR=0.0
153 }
154 elsif ( $type eq 'list' ) {
155 0 ▶ @cmd = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
156 0 my $path = $self->repository . $args{path};
157 0 ▶ return 'No such file or directory' unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
158  
159 0 ▶ if ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
160 0 my $dh;
161 0 ▶ opendir $dh, $path or confess_or_die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $path or confess_or_die($!) 0 0 0
162 0 my $dirs = join "\t", grep { /^[^.]/ } readdir $dh;
163 0 return $dirs;
164 }
165 else {
166 0 return $path;
167 }
168 0 };
169 }
170 elsif ( $type eq 'cat' ) {
171 0 ▶ @cmd = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
172 0 my $path = $self->repository . $args{path};
173 0 ▶ return ( 'No such file or directory' ) unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
174 0 ▶ return ( '', 'Is a directory' ) unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
175 0 local $/;
176 0 ▶ open my $fh, '<', $path or confess_or_die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or confess_or_die($!) 0 0 0
177 0 my $c = <$fh>;
178 0 return $c;
179 0 };
180 }
181 else {
182 0 confess_or_die "invalid command: $type";
183 }
184  
185 6 return @cmd;
186 }
187  
188 =item _yml
189  
190  
191 =cut
192  
193 5 ▶sub _yml {
Subroutine _yml: called CC=2 SCAR=6.9
194 5 my $self = shift;
195 5 my $path = shift;
196 5 my $yml = shift;
197  
198 5 my $file = catfile( $self->repository, $path );
199 5 ▶ if ($yml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yml) { } 2 3 5
200  
201 2 dump_yaml_file( $file, $yml );
202 }
203 else {
204 3 load_yaml_file($file);
205 }
206 }
207  
208 =item info
209  
210  
211 =cut
212  
213 4 ▶sub info {
Subroutine info: called CC=3 SCAR=11.2
Pod documented
214 4 my $self = shift;
215 4 my %args = @_;
216 4 ▶ my $path = $args{path} || '';
Condition100%2/2
$args{'path'} || ''
l!l
22
MC/DC100%1/1
$args{'path'} || ''
$args{'path'}
Truth table100%2/2
A
A= $args{'path'}
Aresult
11
00
217  
218 4 my ( $info, $err ) = $self->SUPER::info( path => $path );
219  
220 4 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 4 4
221 0 return $info, $err;
222 }
223 else {
224 4 return $info;
225 }
226 }
227  
228 =item check_repository
229  
230 Check if the given repository is valid.
231  
232 =cut
233  
234 4 ▶sub check_repository {
Subroutine check_repository: called CC=4 SCAR=15.0
Pod documented
235 4 my $self = shift;
236 4 my %args = @_;
237 4 ▶ if ( $args{action} eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'action'} eq 'create') { } 2 2 4
238 2 my $repo = $self->repository;
239 2 ▶ if ( $args{force} || !-e $repo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"force"} or not -e $repo) 2 0 2
Condition33%1/3
$args{"force"} or not -e $repo
l!l&&r!l&&!r
200
MC/DC0%0/2
$args{"force"} or not -e $repo
$args{"force"} not -e $repo
Truth table33%1/3
A or not(B)
A= $args{"force"}B= not -e $repo
ABresult
1X1
011
000
240 2 return 1;
241 }
242 0 $self->log->fatal("$repo exists already");
243 0 return;
244 }
245 else {
246 2 return $self->SUPER::check_repository(@_);
247 }
248 }
249  
250 0 ▶sub _update_file {
Subroutine _update_file: not called CC=2 SCAR=17.9
251 0 my $self = shift;
252 0 my $path = shift;
253 0 my $latest = shift;
254  
255 0 my $file = catfile( $self->repository, $path );
256 0 unlink $file;
257 0 ▶ rcopy( $latest, $file ) or confess_or_die "can't copy $latest to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rcopy($latest, $file) or confess_or_die("can't copy $latest to ${file}: $!") 0 0 0
258 }
259  
260 0 ▶sub _update_dir {
Subroutine _update_dir: not called CC=2 SCAR=17.9
261 0 my $self = shift;
262 0 my $path = shift;
263 0 my $latest = shift;
264  
265 0 my $dir = catfile( $self->repository, $path );
266 0 ▶ rcopy( $latest, $dir ) or confess_or_die "can't copy $latest to $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
rcopy($latest, $dir) or confess_or_die("can't copy $latest to ${dir}: $!") 0 0 0
267 }
268  
269 =item import
270  
271 =cut
272  
273 2 ▶sub import {
Subroutine import: called CC=2 SCAR=8.1
274 2 my $self = shift;
275 2 ▶ return unless ref $self; # get rid of class->import
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 0 2
276 0 return $self->SUPER::import( @_, delete => 1 );
277 }
278  
279 =back
280  
281 =cut
282  
283 3 1;
284  
285 __END__
« blib/lib/Shipwright/Script/Requires.pm ↑ Summary blib/lib/Shipwright/Source/Directory.pm »