blib/lib/DBIx/Changeset/App/Command/create.pm

filter: Statementstmt 58.1% 43 / 74 Branchbran 26.6% 8 / 30 Conditioncond 44.4% 12 / 27 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 50.0% 81 / 162 CC 27 SCAR 40.7
CC
27
Cov
48%
run
129.8
options
14.7
validate
5.1
CRAP 58.6
« blib/lib/DBIx/Changeset/App/Command/update.pm ↑ Summary blib/lib/DBIx/Changeset/App/BaseCommand.pm »
1 package DBIx::Changeset::App::Command::create;
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  
6 3 ▶use base qw/DBIx::Changeset::App::BaseCommand/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBIx::Changeset::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DBIx::Changeset::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Term::Prompt;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 $VERSION = '1.11';
14 }
15  
16 =head1 NAME
17  
18 DBIx::Changeset::App::Command::create - command module used to create a new blank changeset
19  
20 =head1 SYNOPSIS
21  
22 =head1 METHODS
23  
24 =head2 run
25  
26 =cut
27 1 ▶sub run {
Subroutine run: called CC=16 SCAR=48.7
Pod documented
28 1 my ($self, $opt, $args) = @_;
29  
30 my $coll = DBIx::Changeset::Collection->new('disk', {
31 changeset_location => $opt->{'location'},
32 1 create_template => $opt->{'template'},
33 });
34 1 my $tag = $args->[0];
35 1 my $filename;
36 ### add via our collection
37 1 eval { $filename = $coll->add_changeset($tag); };
38  
39 1 my $e;
40 1 ▶ if ( ($e = Exception::Class->caught('DBIx::Changeset::Exception::DuplicateRecordNameException')) && ( defined $opt->{'prompt'} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e = 'Exception::Class'->caught('DBIx::Changeset::Exception::DuplicateRecordNameException') and defined $opt->{'prompt'}) { } 0 1 1
elsif ($e = 'Exception::Class'->caught) { } 0 1 1
Condition33%1/3
$e = "Exception::Class"->caught("DBIx::Changeset::Exception::DuplicateRecordNameException") and defined $opt->{"prompt"}
!ll&&!rl&&r
100
MC/DC0%0/2
$e = "Exception::Class"->caught("DBIx::Changeset::Exception::DuplicateRecordNameException") and defined $opt->{"prompt"}
$e = "Exception::Class"->caught("DBIx::Changeset::Exception::DuplicateRecordNameException") defined $opt->{"prompt"}
Truth table33%1/3
A and B
A= $e = "Exception::Class"->caught("DBIx::Changeset::Exception::DuplicateRecordNameException")B= defined $opt->{"prompt"}
ABresult
0X0
100
111
41 ### ok we have a duplicate record name
42 # prompt for overwrite
43 0 my $overwrite_flag = &prompt("y", "File ".$e->filename." already exists, overwrite ?", "y/N", "N");
44 0 ▶ if ( $overwrite_flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overwrite_flag == 1) { } 0 0 0
45 # remove existing file
46 0 eval { unlink $e->filename };
47 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
48 0 warn "Could not unlink ".$e->filename." to overwrite it because: $@";
49 0 exit;
50 } else {
51 0 eval { $filename = $coll->add_changeset($tag); };
52 0 ▶ if ( my $e = Exception::Class->caught() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Exception::Class"->caught) 0 0 0
53 0 warn $e->error, "\n";
54 0 ▶ warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"debug"} 0 0 0
55 0 exit;
56 }
57 }
58 } else {
59 # prompt for new filename suggest givenname + _time()
60 0 my $new_file = &prompt("x", "Ok then what shall we call it ?", "valid filename", $tag . "_" . time() );
61 0 eval { $filename = $coll->add_changeset($new_file); };
62 0 ▶ if ( my $e = Exception::Class->caught() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Exception::Class"->caught) 0 0 0
63 0 warn $e->error, "\n";
64 0 ▶ warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"debug"} 0 0 0
65 0 exit;
66 }
67 }
68 } elsif ( $e = Exception::Class->caught() ) {
69 0 warn $e->error, "\n";
70 0 ▶ warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"debug"} 0 0 0
71 0 exit;
72 }
73  
74 ### get the record to get the proper filename
75 1 my $rec = $coll->next();
76  
77 1 printf "Changeset file created: %s\n", $filename;
78  
79 1 ▶ if ( $opt->{'edit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"edit"}) 0 1 1
80 0 ▶ my $editor = $opt->{'editor'} || $ENV{'EDITOR'};
Condition0%0/3
$opt->{'editor'} || $ENV{'EDITOR'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{'editor'} || $ENV{'EDITOR'}
$opt->{'editor'} $ENV{'EDITOR'}
Truth table0%0/3
A || B
A= $opt->{'editor'}B= $ENV{'EDITOR'}
ABresult
1X1
011
000
81 0 ▶ if ($editor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($editor) 0 0 0
82 0 system($editor, $filename);
83 }
84 }
85
86 1 ▶ if ( $opt->{'vcs'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"vcs"}) 0 1 1
87 0 ▶ if ($opt->{'vcsadd'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'vcsadd'}) { } 0 0 0
88 0 my $cmd = sprintf("%s %s", $opt->{'vcs'}, $filename);
89 0 system($cmd);
90 } else {
91 0 warn qq{--vcs option has no effect without also specifying --vcsadd\n};
92 0 warn qq{ eg: --vcs --vcsadd="svn add"\n};
93 }
94 }
95  
96 1 return;
97 }
98  
99 =head2 options
100  
101 define the options for the create command
102  
103 =cut
104  
105 4 ▶sub options {
Subroutine options: called CC=8 SCAR=26.9
Pod documented
106 4 my ($self, $app) = @_;
107 return (
108 [ 'edit' => 'Call editor', { default => $app->{'config'}->{'create'}->{'edit'} || undef } ],
109 [ 'editor=s' => 'Path to Editor', { default => $app->{'config'}->{'create'}->{'edit'} || undef } ],
110 [ 'location=s' => 'Path to changeset files', { default => $app->{'config'}->{'location'} || $app->{'config'}->{'create'}->{'location'} || undef, required => 1 } ],
111 [ 'vcs' => 'Add to version control', { default => $app->{'config'}->{'create'}->{'vcs'} || undef } ],
112 [ 'vcsadd=s' => 'Command to add to version control', { default => $app->{'config'}->{'create'}->{'vcsadd'} || undef } ],
113 4 ▶ [ 'template=s' => 'Path to changeset template', { default => $app->{'config'}->{'create'}->{'template'} || 'template.txt', required => 1 } ],
Condition46%7/15
$app->{'config'}{'create'}{'edit'} || undef
l!l
04
$app->{'config'}{'create'}{'edit'} || undef
l!l
04
$app->{'config'}{'location'} || $app->{'config'}{'create'}{'location'} || undef
l!l
04
$app->{"config"}{"location"} or $app->{"config"}{"create"}{"location"}
l!l&&r!l&&!r
004
$app->{'config'}{'create'}{'vcs'} || undef
l!l
04
$app->{'config'}{'create'}{'vcsadd'} || undef
l!l
04
$app->{'config'}{'create'}{'template'} || 'template.txt'
l!l
04
MC/DC0%0/1
$app->{'config'}{'create'}{'edit'} || undef
$app->{'config'}{'create'}{'edit'}
MC/DC0%0/1
$app->{'config'}{'create'}{'edit'} || undef
$app->{'config'}{'create'}{'edit'}
MC/DC0%0/2
$app->{'config'}{'location'} || $app->{'config'}{'create'}{'location'} || undef
$app->{"config"}{"location"} $app->{"config"}{"create"}{"location"}
MC/DC0%0/1
$app->{'config'}{'create'}{'vcs'} || undef
$app->{'config'}{'create'}{'vcs'}
MC/DC0%0/1
$app->{'config'}{'create'}{'vcsadd'} || undef
$app->{'config'}{'create'}{'vcsadd'}
MC/DC0%0/1
$app->{'config'}{'create'}{'template'} || 'template.txt'
$app->{'config'}{'create'}{'template'}
Truth table50%1/2
A
A= $app->{'config'}{'create'}{'edit'}
Aresult
11
00
Truth table50%1/2
A
A= $app->{'config'}{'create'}{'edit'}
Aresult
11
00
Truth table33%1/3
A or B
A= $app->{"config"}{"location"}B= $app->{"config"}{"create"}{"location"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $app->{'config'}{'create'}{'vcs'}
Aresult
11
00
Truth table50%1/2
A
A= $app->{'config'}{'create'}{'vcsadd'}
Aresult
11
00
Truth table50%1/2
A
A= $app->{'config'}{'create'}{'template'}
Aresult
11
00
114 );
115 }
116  
117 =head2 validate
118  
119 define the options validation for the create command
120  
121 =cut
122  
123 3 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.2
Pod documented
124 3 my ($self,$opt,$args) = @_;
125 3 ▶ $self->usage_error('This command requires a valid changeset location') unless ( ( defined $opt->{'location'} ) && ( -d $opt->{'location'} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"location"} and -d $opt->{"location"} 1 2 3
Condition66%2/3
defined $opt->{"location"} and -d $opt->{"location"}
!ll&&!rl&&r
102
MC/DC50%1/2
defined $opt->{"location"} and -d $opt->{"location"}
defined $opt->{"location"} -d $opt->{"location"}
Truth table66%2/3
A and B
A= defined $opt->{"location"}B= -d $opt->{"location"}
ABresult
0X0
100
111
126 2 ▶ $self->usage_error('This command requires a valid changeset name') unless ( ( defined $args->[0] ) && ( length $args->[0] > 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->[0] and length $args->[0] > 0 1 1 2
Condition66%2/3
defined $args->[0] and length $args->[0] > 0
!ll&&!rl&&r
101
MC/DC50%1/2
defined $args->[0] and length $args->[0] > 0
defined $args->[0] length $args->[0] > 0
Truth table66%2/3
A and B
A= defined $args->[0]B= length $args->[0] > 0
ABresult
0X0
100
111
127 1 return;
128 }
129  
130  
131 =head2 usage_desc
132  
133 Override to show usage of changeset_name
134  
135 =cut
136  
137 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
138 4 return "%c create %o <changeset_name>";
139 }
140  
141 =head1 COPYRIGHT & LICENSE
142  
143 Copyright 2004-2008 Grox Pty Ltd.
144  
145 This program is free software; you can redistribute it and/or modify it
146 under the same terms as Perl itself.
147  
148 The full text of the license can be found in the LICENSE file included with this module.
149  
150 =cut
151  
152 3 1; # End of DBIx::Changeset
« blib/lib/DBIx/Changeset/App/Command/update.pm ↑ Summary blib/lib/DBIx/Changeset/App/BaseCommand.pm »