File Coverage

blib/lib/DBIx/Changeset/App/Command/create.pm
Criterion Covered Total %
statement 42 73 57.5
branch 8 30 26.6
condition 11 24 45.8
subroutine 12 12 100.0
pod 4 4 100.0
total 77 143 53.8


line stmt bran cond sub pod time code
1             package DBIx::Changeset::App::Command::create;
2              
3 2     2   4503 use warnings;
  2         4  
  2         65  
4 2     2   12 use strict;
  2         5  
  2         69  
5              
6 2     2   10 use base qw/DBIx::Changeset::App::BaseCommand/;
  2         4  
  2         175  
7 2     2   10 use DBIx::Changeset::Collection;
  2         5  
  2         13  
8 2     2   56 use DBIx::Changeset::Exception;
  2         4  
  2         39  
9 2     2   9 use Term::Prompt;
  2         4  
  2         113  
10              
11 2     2   8 use vars qw{$VERSION};
  2         5  
  2         81  
12             BEGIN {
13 2     2   1341 $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             sub run {
28 1     1 1 89 my ($self, $opt, $args) = @_;
29              
30 1         21 my $coll = DBIx::Changeset::Collection->new('disk', {
31             changeset_location => $opt->{'location'},
32             create_template => $opt->{'template'},
33             });
34 1         63 my $tag = $args->[0];
35 1         2 my $filename;
36             ### add via our collection
37 1         2 eval { $filename = $coll->add_changeset($tag); };
  1         8  
38              
39 1         14 my $e;
40 1 50 33     9 if ( ($e = Exception::Class->caught('DBIx::Changeset::Exception::DuplicateRecordNameException')) && ( defined $opt->{'prompt'} ) ) {
    50          
41             ### ok we have a duplicate record name
42             # prompt for overwrite
43 0         0 my $overwrite_flag = &prompt("y", "File ".$e->filename." already exists, overwrite ?", "y/N", "N");
44 0 0       0 if ( $overwrite_flag == 1 ) {
45             # remove existing file
46 0         0 eval { unlink $e->filename };
  0         0  
47 0 0       0 if ( $@ ) {
48 0         0 warn "Could not unlink ".$e->filename." to overwrite it because: $@";
49 0         0 exit;
50             } else {
51 0         0 eval { $filename = $coll->add_changeset($tag); };
  0         0  
52 0 0       0 if ( my $e = Exception::Class->caught() ) {
53 0         0 warn $e->error, "\n";
54 0 0       0 warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
55 0         0 exit;
56             }
57             }
58             } else {
59             # prompt for new filename suggest givenname + _time()
60 0         0 my $new_file = &prompt("x", "Ok then what shall we call it ?", "valid filename", $tag . "_" . time() );
61 0         0 eval { $filename = $coll->add_changeset($new_file); };
  0         0  
62 0 0       0 if ( my $e = Exception::Class->caught() ) {
63 0         0 warn $e->error, "\n";
64 0 0       0 warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
65 0         0 exit;
66             }
67             }
68             } elsif ( $e = Exception::Class->caught() ) {
69 0         0 warn $e->error, "\n";
70 0 0       0 warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
71 0         0 exit;
72             }
73              
74             ### get the record to get the proper filename
75 1         25 my $rec = $coll->next();
76              
77 1         38 printf "Changeset file created: %s\n", $filename;
78              
79 1 50       5 if ( $opt->{'edit'} ) {
80 0   0     0 my $editor = $opt->{'editor'} || $ENV{'EDITOR'};
81 0 0       0 if ($editor) {
82 0         0 system($editor, $filename);
83             }
84             }
85            
86 1 50       4 if ( $opt->{'vcs'} ) {
87 0 0       0 if ($opt->{'vcsadd'} ) {
88 0         0 my $cmd = sprintf("%s %s", $opt->{'vcs'}, $filename);
89 0         0 system($cmd);
90             } else {
91 0         0 warn qq{--vcs option has no effect without also specifying --vcsadd\n};
92 0         0 warn qq{ eg: --vcs --vcsadd="svn add"\n};
93             }
94             }
95              
96 1         55 return;
97             }
98              
99             =head2 options
100              
101             define the options for the create command
102              
103             =cut
104              
105             sub options {
106 4     4 1 6 my ($self, $app) = @_;
107             return (
108 4   50     125 [ 'edit' => 'Call editor', { default => $app->{'config'}->{'create'}->{'edit'} || undef } ],
      50        
      50        
      50        
      50        
      50        
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             [ 'template=s' => 'Path to changeset template', { default => $app->{'config'}->{'create'}->{'template'} || 'template.txt', required => 1 } ],
114             );
115             }
116              
117             =head2 validate
118              
119             define the options validation for the create command
120              
121             =cut
122              
123             sub validate {
124 3     3 1 7 my ($self,$opt,$args) = @_;
125 3 100 66     86 $self->usage_error('This command requires a valid changeset location') unless ( ( defined $opt->{'location'} ) && ( -d $opt->{'location'} ) );
126 2 100 66     14 $self->usage_error('This command requires a valid changeset name') unless ( ( defined $args->[0] ) && ( length $args->[0] > 0 ) );
127 1         3 return;
128             }
129              
130              
131             =head2 usage_desc
132              
133             Override to show usage of changeset_name
134              
135             =cut
136              
137             sub usage_desc {
138 4     4 1 46780 return "%c create %o ";
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             1; # End of DBIx::Changeset