Jifty/Script/Script.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 57 CC 4 SCAR 18.3
CC
4
Cov
0%
_write
12.0
run
6.0
options
2.0
CRAP 6.2
1 use warnings;
2 use strict;
3  
4 package Jifty::Script::Script;
5 use base qw/ Jifty::Script /;
6  
7 =head1 NAME
8  
9 Jifty::Script::Script - Add a new Jifty script to your Jifty application
10  
11 =head1 SYNOPSIS
12  
13 jifty script --name my_new_script
14 jifty script --name my_new_script --force
15 jifty script --help
16 jifty script --man
17  
18 =head1 DESCRIPTION
19  
20 Add a skeleton command-line script file.
21  
22 =head2 options
23  
24 =over 8
25  
26 =item --name NAME (required)
27  
28 Name of the script to create.
29  
30 =item --force
31  
32 By default, this will stop and warn you if any of the files it is going to write already exist. Passing the --force flag will make it overwrite the files.
33  
34 =item B<--help>
35  
36 Print a brief help message and exits.
37  
38 =item B<--man>
39  
40 Prints the manual page and exits.
41  
42 =back
43  
44 =cut
45  
46 sub options {
47 my $self = shift;
48 return (
49 $self->SUPER::options,
50 'n|name=s' => 'name',
51 'force' => 'force',
52 )
53 }
54  
55 =head1 DESCRIPTION
56  
57 This creates a skeleton of a new script file for your Jifty application. Often such a script is needed for cron jobs, annual maintenance work, and any other server-side activity that would benefit from a command-line script.
58  
59 =head1 METHODS
60  
61 =head2 run
62  
63 Creates a skeleton file under C<bin/I<script>>.
64  
65 TODO Should this create skeleton test files too?
66  
67 =cut
68  
69 sub run {
70 my $self = shift;
71  
72 $self->print_help;
73  
74 my $script = $self->{name};
75 $self->print_help("You need to give your new script a --name")
76 unless defined $script;
77  
78 Jifty->new( no_handle => 1 );
79 my $root = Jifty::Util->app_root;
80  
81 my $script_file = <<"END_OF_SCRIPT";
82 #!/usr/bin/env perl
83 use strict;
84 use warnings;
85  
86 use Jifty;
87 BEGIN { Jifty->new }
88  
89 # Your script-specific code goes here.
90  
91 END_OF_SCRIPT
92  
93 $self->_write("$root/bin/$script" => $script_file);
94 }
95  
96 # TODO This should be moved to Jifty::Util or somewhere else so all these
97 # scripts don't duplicate it!
98 sub _write {
99 my $self = shift;
100 my %files = (@_);
101 my $halt;
102 for my $path (keys %files) {
103 my ($volume, $dir, $file) = File::Spec->splitpath($path);
104  
105 # Make sure the directories we need are there
106 Jifty::Util->make_path($dir);
107  
108 # If it already exists, bail
109 if (-e $path and not $self->{force}) {
110 print "File $path exists already; Use --force to overwrite\n";
111 $halt = 11;
112 }
113 }
114 exit if $halt;
115  
116 # Now that we've san-checked everything, we can write the files
117 for my $path (keys %files) {
118 print "Writing file $path\n";
119 # Actually write the file out
120 open(FILE, ">$path")
121 or die "Can't write to $path: $!";
122 print FILE $files{$path};
123 close FILE;
124 }
125 }
126  
127 1;