| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Script::Plugin; | ||
| 5 | use base qw(Jifty::Script Class::Accessor::Fast); | ||
| 6 | |||
| 7 | use File::Copy; | ||
| 8 | use Jifty::Config; | ||
| 9 | use Jifty::YAML; | ||
| 10 | use File::Basename; | ||
| 11 | |||
| 12 | __PACKAGE__->mk_accessors(qw/prefix dist_name mod_name lib_dir/); | ||
| 13 | |||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Jifty::Script::Plugin - Create the skeleton of a Jifty plugin | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | jifty plugin --name NewPlugin | ||
| 22 | jifty plugin --help | ||
| 23 | jifty plugin --man | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Creates a skeleton of a new L<Jifty::Plugin>. | ||
| 28 | |||
| 29 | =head2 options | ||
| 30 | |||
| 31 | =over 8 | ||
| 32 | |||
| 33 | =item --name | ||
| 34 | |||
| 35 | This script only takes one option, C<--name>, which is required; it is | ||
| 36 | the name of the plugin to create; this will be prefixed with | ||
| 37 | C<Jifty::Plugin::> automatically. Jifty will create a directory with | ||
| 38 | that name, and place all of the files it creates inside that | ||
| 39 | directory. | ||
| 40 | |||
| 41 | =item B<--help> | ||
| 42 | |||
| 43 | Print a brief help message and exits. | ||
| 44 | |||
| 45 | =item B<--man> | ||
| 46 | |||
| 47 | Prints the manual page and exits. | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub options { | ||
| 54 | my $self = shift; | ||
| 55 | return ( | ||
| 56 | $self->SUPER::options, | ||
| 57 | 'n|name=s' => 'name', | ||
| 58 | ) | ||
| 59 | } | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =head2 run | ||
| 64 | |||
| 65 | Create a directory for the plugin, a skeleton directory structure, and | ||
| 66 | a C<Makefile.PL> for your plugin. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub run { | ||
| 71 | my $self = shift; | ||
| 72 | $self->print_help(); | ||
| 73 | |||
| 74 | $self->prefix( $self->{name} ||''); | ||
| 75 | |||
| 76 | unless ($self->prefix =~ /\w+/ ) { die "You need to give your new Jifty plugin a --name"."\n";} | ||
| 77 | $self->prefix( $self->prefix ); | ||
| 78 | |||
| 79 | # Turn my-plugin-name into My::Plugin::Name. | ||
| 80 | $self->mod_name ("Jifty::Plugin::" . join ("::", map { ucfirst } split (/\-/, $self->prefix))); | ||
| 81 | $self->dist_name("Jifty-Plugin-".$self->prefix); | ||
| 82 | $self->lib_dir(join("/",grep{$_} split '::', $self->mod_name)); | ||
| 83 | |||
| 84 | print("Creating new plugin ".$self->mod_name."\n"); | ||
| 85 | $self->_make_directories(); | ||
| 86 | $self->_write_makefile(); | ||
| 87 | $self->_write_default_files(); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _write_makefile { | ||
| 91 | my $self = shift; | ||
| 92 | my $prefix = $self->prefix; | ||
| 93 | # Write a makefile | ||
| 94 | open(MAKEFILE, ">$prefix/Makefile.PL") or die "Can't write Makefile.PL: $!"; | ||
| 95 | print MAKEFILE <<"EOT"; | ||
| 96 | use inc::Module::Install; | ||
| 97 | name('@{[$self->dist_name]}'); | ||
| 98 | version('0.01'); | ||
| 99 | requires('Jifty' => '@{[$Jifty::VERSION]}'); | ||
| 100 | |||
| 101 | install_share; | ||
| 102 | |||
| 103 | WriteAll; | ||
| 104 | EOT | ||
| 105 | close MAKEFILE; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _write_default_files { | ||
| 109 | my $self = shift; | ||
| 110 | my $mod_name = $self->mod_name; | ||
| 111 | my $prefix = $self->prefix; | ||
| 112 | my $lib = $self->lib_dir; | ||
| 113 | open(PLUGIN, ">$prefix/lib/$lib.pm") or die "Can't write $prefix/$lib.pm: $!"; | ||
| 114 | print PLUGIN <<"EOT"; | ||
| 115 | use strict; | ||
| 116 | use warnings; | ||
| 117 | |||
| 118 | package $mod_name; | ||
| 119 | use base qw/Jifty::Plugin/; | ||
| 120 | |||
| 121 | # Your plugin goes here. If it takes any configuration or arguments, you | ||
| 122 | # probably want to override L<Jifty::Plugin/init>. | ||
| 123 | |||
| 124 | 1; | ||
| 125 | EOT | ||
| 126 | close PLUGIN; | ||
| 127 | |||
| 128 | open(DISPATCHER, ">$prefix/lib/$lib/Dispatcher.pm") or die "Can't write $prefix/lib/$lib/Dispatcher.pm: $!"; | ||
| 129 | print DISPATCHER <<"EOT"; | ||
| 130 | use strict; | ||
| 131 | use warnings; | ||
| 132 | |||
| 133 | package @{[$mod_name]}::Dispatcher; | ||
| 134 | use Jifty::Dispatcher -base; | ||
| 135 | |||
| 136 | # Put any plugin-specific dispatcher rules here. | ||
| 137 | |||
| 138 | 1; | ||
| 139 | EOT | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _make_directories { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | mkdir($self->prefix); | ||
| 146 | my @dirs = qw( lib ); | ||
| 147 | my @dir_parts = split('/',$self->lib_dir); | ||
| 148 | push @dirs, join('/', 'lib', @dir_parts[0..$_]) for 0..@dir_parts-1; | ||
| 149 | |||
| 150 | @dirs = (@dirs, $self->_directories); | ||
| 151 | |||
| 152 | foreach my $dir (@dirs) { | ||
| 153 | $dir =~ s/__LIB__/$self->lib_dir/e; | ||
| 154 | print("Creating directory $dir\n"); | ||
| 155 | mkdir( $self->prefix."/$dir") or die "Can't create ". $self->prefix."/$dir: $!"; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _directories { | ||
| 160 | return qw( | ||
| 161 | doc | ||
| 162 | share | ||
| 163 | share/po | ||
| 164 | share/web | ||
| 165 | share/web/templates | ||
| 166 | share/web/static | ||
| 167 | lib/__LIB__/Model | ||
| 168 | lib/__LIB__/Action | ||
| 169 | t | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; |