File Coverage

blib/lib/Drupal/Module/Starter.pm
Criterion Covered Total %
statement 67 68 98.5
branch 12 22 54.5
condition 1 3 33.3
subroutine 12 12 100.0
pod 7 7 100.0
total 99 112 88.3


line stmt bran cond sub pod time code
1             package Drupal::Module::Starter;
2              
3 3     3   60035 use warnings;
  3         8  
  3         113  
4 3     3   31 use strict;
  3         6  
  3         105  
5 3     3   2557 use YAML;
  3         47962  
  3         174  
6 3     3   2084 use Drupal::Module::Starter::4_7_3;
  3         10  
  3         92  
7 3     3   19 use File::Path;
  3         7  
  3         3289  
8              
9             =head1 NAME
10              
11             Drupal::Module::Starter - Create Drupal Module starter files
12              
13              
14             =head1 VERSION
15              
16             Version 0.05
17              
18             =cut
19              
20             our $VERSION = '0.06';
21              
22             =head1 SYNOPSIS
23              
24             You probably don't want to use this module directly - you want to use the drupal-module-starter script in the scripts directory of the distribution
25              
26             use Drupal::Module::Starter;
27              
28             my $foo = Drupal::Module::Starter->new('path/to/config.yaml');
29             $foo->generate;
30             ...
31              
32             =head1 FUNCTIONS
33              
34             =head2 new - constructor - requires a YAML file path
35              
36             =cut
37              
38             sub new {
39              
40 2     2 1 27 my ($self, $class) = ( {}, shift );
41 2         6 bless $self,$class;
42 2 50       18 $self->{config_file} = shift or die "No config.yaml passed to constructor";
43 2 50       13 $self->{cfg} = YAML::LoadFile($self->{config_file}) or die "Error loading YAML";
44 2 50       97695 $self->{cfg}->{drupal_version} = '4.7.3' unless $self->{cfg}->{drupal_version};
45 2 50       13 $self->{cfg}->{author} = 'Author not set' unless $self->{cfg}->{author};
46 2 50       12 $self->{cfg}->{email} = 'author@somesite.com' unless $self->{cfg}->{email};
47 2 50       11 die "No module set in config" unless $self->{cfg}->{module};
48 2         10 $self->{cfg}->{drupal_version} =~ s/\./_/g;
49 2         9 my $pkg = "Drupal::Module::Starter::".$self->{cfg}->{drupal_version};
50            
51 2         22 $self->{stubs} = $pkg->new;
52 2         17 return $self;
53             }
54              
55             =head2 sample_yaml - create a sample yaml file to use as a template
56              
57             =cut
58              
59             sub sample_yaml {
60 1     1 1 6 return qq!---
61             hook_access: 0
62             hook_auth: 0
63             hook_block: 0
64             hook_comment: 0
65             hook_cron: 0
66             hook_db_rewrite_sql: 0
67             hook_delete: 0
68             hook_elements: 0
69             hook_exit: 0
70             hook_file_download: 0
71             hook_filter: 0
72             hook_filter_tips: 0
73             hook_footer: 0
74             hook_form: 0
75             hook_form_alter: 0
76             hook_help: 0
77             hook_info: 0
78             hook_init: 0
79             hook_insert: 0
80             hook_install: 0
81             hook_link: 0
82             hook_load: 0
83             hook_menu: 0
84             hook_nodeapi: 0
85             hook_node_grants: 0
86             hook_node_info: 0
87             hook_perm : 0
88             hook_ping: 0
89             hook_prepare: 0
90             hook_search: 0
91             hook_search_item: 0
92             hook_search_preprocess: 0
93             hook_settings: 0
94             hook_submit: 0
95             hook_taxonomy: 0
96             hook_update: 0
97             hook_update_index: 0
98             hook_update_N: 0
99             hook_user: 0
100             hook_validate: 0
101             hook_view: 0
102             hook_xmlrpc: 0
103             !;
104              
105              
106             }
107              
108             =head2 generate_php - run through the requested module hooks and generate stubs
109              
110             =cut
111              
112             sub generate_php {
113 2     2 1 3 my $self = shift;
114 2         208 my $now = scalar(localtime);
115            
116 2         19 my $module = "
117            
118             /* $self->{cfg}->{module} - Version 0.1
119             * ----------------------------------------------------------
120             * Author: $self->{cfg}->{author} ($self->{cfg}->{email})
121             *
122             * Changelog:
123             * - Version: 0.1 - generated $now
124             *
125             */
126              
127             ";
128              
129             # add stub hooks
130 2         4 my @hooks = keys %{$self->{stubs}};
  2         32  
131 2         9 for my $hook (keys %{$self->{stubs}}) {
  2         12  
132            
133 84 100       205 next unless($self->{cfg}->{$hook});
134            
135 2         5 my $stub = $self->{stubs}->{$hook};
136            
137             # TODO -- add table name substitution support...
138            
139 2         19 $stub =~ s/MODULENAME/$self->{cfg}->{module}/g;
140 2         11 $module .= "\n$stub\n\n";
141             }
142              
143 2         9 $module .= "?>";
144 2         15 return $module;
145             }
146              
147             =head2 generate_readme - create a stub README.txt
148              
149             =cut
150              
151             sub generate_readme {
152 2     2 1 3 my $self = shift;
153 2         78 my $now = scalar(localtime);
154 2         37 my $year = (localtime)[5]+1900;
155 2         26 my $readme = qq!
156             ---------------------------------------------------------------------------
157             $self->{cfg}->{module} - Version 0.0.1
158             ---------------------------------------------------------------------------
159             Author: $self->{cfg}->{author} ($self->{cfg}->{email})
160              
161             (c) Copyright $year - $self->{cfg}->{author} - All Rights Reserved
162              
163              
164             Changelog - Version 0.0.1 (autogenerated $now)
165              
166             !;
167 2         11 return $readme;
168             }
169              
170             =head2 generate_license - create a stub license file
171              
172             =cut
173             sub generate_license {
174 2     2 1 3 my $self = shift;
175 2         3 my $lic = "License Terms go here";
176 2         27 return $lic;
177             }
178              
179             =head2 generate_install - create a stub INSTALL.txt
180              
181             =cut
182             sub generate_install {
183 2     2 1 3 my $self = shift;
184 2         15 my $install = qq!
185             Installation instructions for $self->{cfg}->{module} 0.1
186             ----------------------------------------------------------------------------------
187             !;
188              
189             }
190              
191             =head2 generate_files - actually do the work and create the files if they exist
192              
193             =cut
194             sub generate {
195 1     1 1 3 my $self = shift;
196 1         5 my $output_dir = $self->{cfg}->{dir}."/$self->{cfg}->{module}";
197 1 50       28 mkpath($output_dir,0777) unless -e $output_dir;
198 1         5 my $module_name = $self->{cfg}->{module}.'.module';
199 1         5 my $files = {
200             module => [ $module_name, $self->generate_php ],
201             readme => [ 'README.txt',$self->generate_readme ],
202             install => [ 'INSTALL.txt', $self->generate_install ],
203             license => [ 'LICENSE.txt',$self->generate_license]
204             };
205            
206             # do any files exist? are we in force mode?
207 1         5 for my $file (keys %$files) {
208 4         13 my $path = $output_dir.'/'.$files->{$file}[0];
209 4 50 33     85 if(-e $path and !$self->{cfg}->{force}) {
210 0         0 die "$path already exists. Use the 'force' directive to overwrite files";
211             }
212             }
213 1         5 for my $file (keys %$files) {
214 4         15 my $path = $output_dir.'/'.$files->{$file}[0];
215 4         7 my $data = $files->{$file}[1];
216 4 50       12 print "Opening file $file for write\n" if $self->{cfg}->{verbose};
217 4 50       276 open(F,">$path") or die "$path: $!";
218 4         27 print F $data;
219 4         221 close F;
220             }
221              
222 1         8 return 1;
223             }
224              
225             =head1 AUTHOR
226              
227             Steve McNabb, C<< >>
228             IT Director, F5 Site Design - http://www.f5sitedesign.com
229             Open Source Internet Application Development
230              
231             =cut
232              
233             =head1 BUGS
234              
235             Please report any bugs or feature requests to
236             C, or through the web interface at
237             L.
238             I will be notified, and then you'll automatically be notified of progress on
239             your bug as I make changes.
240              
241             =head1 ACKNOWLEDGEMENTS
242              
243             =head1 COPYRIGHT & LICENSE
244              
245             Copyright 2006 Steve McNabb, All Rights Reserved.
246              
247             This program is free software; you can redistribute it and/or modify it
248             under the same terms as Perl itself.
249              
250             =cut
251              
252             1; # End of Drupal::Module::Starter