| 1 | package Padre::Plugin::Shell::Template; | ||
| 2 | 1 | ▶ | use base 'Padre::Plugin::Shell::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use 5.008; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Padre::Constant (); | ||
| 8 | use Padre::Current (); | ||
| 9 | use Padre::Util (); | ||
| 10 | use Padre::Wx (); | ||
| 11 | use File::Which; | ||
| 12 | |||
| 13 | our $VERSION = '0.1'; | ||
| 14 | |||
| 15 | ######################################################################## | ||
| 16 | # | ||
| 17 | sub plugin_menu { | ||
| 18 | my ($self) = @_; | ||
| 19 | my @menu = (); | ||
| 20 | my %config = $self->get_config(); | ||
| 21 | my @accel_keys = ( 0 .. 9, 'A' .. 'Z' ); | ||
| 22 | |||
| 23 | foreach my $template ( sort keys %config ) { | ||
| 24 | my $accel = ''; | ||
| 25 | if (@accel_keys) { | ||
| 26 | my $a = shift @accel_keys; | ||
| 27 | $accel = '&' . $a . ' - '; | ||
| 28 | } | ||
| 29 | push @menu, "$accel$template" => sub { $self->new_from_template($template) }; | ||
| 30 | } | ||
| 31 | push @menu, '---' => undef; | ||
| 32 | push @menu, Wx::gettext("&Configure Templates") => sub { $self->edit_config_file() },; | ||
| 33 | |||
| 34 | return @menu; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub example_config { | ||
| 38 | my ($self) = @_; | ||
| 39 | |||
| 40 | my $config = "---\n"; | ||
| 41 | |||
| 42 | # Start by loading the templates from Padre itself as: | ||
| 43 | # 1. they probably exist, and | ||
| 44 | # 2. they make good working examples | ||
| 45 | my %templates = ( | ||
| 46 | 'pl' => Wx::gettext('Perl 5 Script'), | ||
| 47 | 'pm' => Wx::gettext('Perl 5 Module'), | ||
| 48 | 'p6' => Wx::gettext('Perl 6 Script'), | ||
| 49 | 't' => Wx::gettext('Perl 5 Test'), | ||
| 50 | ); | ||
| 51 | |||
| 52 | # NOTE: if we have sh then we want to copy the file | ||
| 53 | # (and thus short circuit sh), otherwise leave the command blank. | ||
| 54 | my $cmd = (which ('sh')) ? 'cp [% IN %] [% OUT %]' : ''; | ||
| 55 | |||
| 56 | foreach my $extension ( sort keys %templates ) { | ||
| 57 | my $template_pathname = File::Spec->catfile( | ||
| 58 | Padre::Util::sharedir('templates'), "template.$extension" | ||
| 59 | ); | ||
| 60 | next unless ( -f $template_pathname ); | ||
| 61 | my $template_name = $templates{$extension}; | ||
| 62 | $config .= <<"EOT"; | ||
| 63 | $template_name: | ||
| 64 | command: '$cmd' | ||
| 65 | description: Create a new $template_name | ||
| 66 | mimetype: '' | ||
| 67 | source_file: $template_pathname | ||
| 68 | EOT | ||
| 69 | } | ||
| 70 | |||
| 71 | # Add something that is not perl/Padre | ||
| 72 | # TODO: example for systems without sh | ||
| 73 | # TODO: create the example template | ||
| 74 | if ( which('sh') ) { | ||
| 75 | my $template_pathname = File::Spec->catfile( | ||
| 76 | Padre::Constant::CONFIG_DIR, 'templates', "template.sh" | ||
| 77 | ); | ||
| 78 | |||
| 79 | $config .= <<"EOT"; | ||
| 80 | Shell Script: | ||
| 81 | command: '' | ||
| 82 | description: Create a new Shell Script | ||
| 83 | mimetype: 'application/x-shellscript' | ||
| 84 | source_file: $template_pathname | ||
| 85 | |||
| 86 | EOT | ||
| 87 | } | ||
| 88 | return $config; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub new_from_template { | ||
| 92 | my ( $self, $template ) = @_; | ||
| 93 | my %config = $self->get_config(); | ||
| 94 | if ( exists $config{$template} ) { | ||
| 95 | my $command = $config{$template}{command} || ''; | ||
| 96 | my $mimetype = $config{$template}{mimetype} || ''; | ||
| 97 | my $template_file = $config{$template}{source_file}; | ||
| 98 | |||
| 99 | ################################################################ | ||
| 100 | # Setup the environment | ||
| 101 | $self->update_environment_vars(); | ||
| 102 | |||
| 103 | my $in_filename = $self->get_temp_file(); | ||
| 104 | my $out_filename = $self->get_temp_file(); | ||
| 105 | |||
| 106 | # 1. If there is a command then | ||
| 107 | # - get a temporary output file | ||
| 108 | # - call the command with the template and temporary output file | ||
| 109 | # - create new from output file | ||
| 110 | # - rm out file | ||
| 111 | if ($command) { | ||
| 112 | $command =~ s/\[\% IN \%\]/$template_file/; | ||
| 113 | $command =~ s/\[\% OUT \%\]/$out_filename/; | ||
| 114 | my $rc = system($command); | ||
| 115 | if ( $rc == -1 ) { | ||
| 116 | $self->notify_of_error( | ||
| 117 | Wx::gettext("Failed to run template command.\n") . $! | ||
| 118 | ); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | $self->new_document_from_file( $out_filename, $mimetype ); | ||
| 122 | } | ||
| 123 | |||
| 124 | # 2. else If we have a shell & cat then: | ||
| 125 | # - get a temporary file | ||
| 126 | # - wrap things in cat and redirect the output to the temporary file | ||
| 127 | # - create new from temporary out file | ||
| 128 | # - rm out file | ||
| 129 | elsif ( which('cat') && which('sh') ) { | ||
| 130 | my $template_text = $self->slurp_file($template_file); | ||
| 131 | my $IN; | ||
| 132 | if ( open $IN, '>', $in_filename ) { | ||
| 133 | print $IN <<"EOT"; | ||
| 134 | cat <<PADRE_EOT > $out_filename | ||
| 135 | $template_text | ||
| 136 | PADRE_EOT | ||
| 137 | EOT | ||
| 138 | close $IN; | ||
| 139 | |||
| 140 | my $rc = system("sh $in_filename"); | ||
| 141 | if ( $rc == -1 ) { | ||
| 142 | $self->notify_of_error( | ||
| 143 | Wx::gettext("Failed to run template command.\n") . $! | ||
| 144 | ); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | $self->new_document_from_file( $out_filename, $mimetype ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | # 3. otherwise just create from the static template | ||
| 152 | else { | ||
| 153 | $self->new_document_from_file( $template_file, $mimetype ); | ||
| 154 | } | ||
| 155 | |||
| 156 | $self->delete_temp_file($out_filename); | ||
| 157 | $self->delete_temp_file($in_filename); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | 1; | ||
| 161 | |||
| 162 | =pod | ||
| 163 | |||
| 164 | =head1 NAME | ||
| 165 | |||
| 166 | Padre::Plugin::Shell::Template - Create new documents from templates. | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | Create new documents from a list of user defined templates. Optionally, | ||
| 171 | the template may be processed with an external command prior to creating | ||
| 172 | the new document. | ||
| 173 | |||
| 174 | =head1 CONFIGURATION | ||
| 175 | |||
| 176 | Template definitions are kept in a YAML formatted configuration file | ||
| 177 | in the user's Padre configuration directory (C<~/.padre>). | ||
| 178 | |||
| 179 | Each template is labeled with the name that is to be displayed in the | ||
| 180 | template menu. In addition to the name there are four attributes for | ||
| 181 | each template definition: | ||
| 182 | |||
| 183 | =over | ||
| 184 | |||
| 185 | =item B<command> (optional) - | ||
| 186 | |||
| 187 | The command to process the template through prior to creating a new document. | ||
| 188 | |||
| 189 | =item B<description> (optional) - | ||
| 190 | |||
| 191 | A description of the template. This is not [currently] used by the plugin | ||
| 192 | and is intended for the benefit of the individual maintaining the template | ||
| 193 | configuration. | ||
| 194 | |||
| 195 | =item B<mimetype> (optional) - | ||
| 196 | |||
| 197 | The mimetype of the created document. If no mimetype is specified the | ||
| 198 | Padre will be attempt to guess the mimetype. | ||
| 199 | |||
| 200 | =item B<source_file (required)> - | ||
| 201 | |||
| 202 | The full path and name of the template file. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =head1 ENVIRONMENT VARIABLES | ||
| 207 | |||
| 208 | To provide additional information for the templates, various | ||
| 209 | environment variables are set prior to processing the template. | ||
| 210 | These environment variables are covered in the | ||
| 211 | L<Padre::Plugin::Shell::Base> documentation. | ||
| 212 | |||
| 213 | =head1 METHODS | ||
| 214 | |||
| 215 | =head3 plugin_menu | ||
| 216 | |||
| 217 | Generates and returns a menu of the defined templates. | ||
| 218 | |||
| 219 | =head3 example_config | ||
| 220 | |||
| 221 | Returns an example configuration. This is the initial configuration | ||
| 222 | that gets created the first time that this plugin is used. | ||
| 223 | |||
| 224 | =head3 new_from_template ($template_name) | ||
| 225 | |||
| 226 | Searches the template configuration for a template definition that | ||
| 227 | matches the supplied $template_name. If a matching template is found | ||
| 228 | then one of three actions is performed. | ||
| 229 | |||
| 230 | =over | ||
| 231 | |||
| 232 | =item B<Use command> - | ||
| 233 | |||
| 234 | If a command is supplied as part of the template configuration then | ||
| 235 | the template is processed through that command and the output is used | ||
| 236 | to create the new document. | ||
| 237 | |||
| 238 | There are two rather important placeholders in the command string | ||
| 239 | C<[% IN %]> and C<[% OUT %]> for the input and output filenames used | ||
| 240 | in running the command. When the command is run, the C<[% IN %]> | ||
| 241 | placeholder is replaced with the template filename and the C<[% OUT %]> | ||
| 242 | placeholder is replaced by the name temporary file that the command | ||
| 243 | is to write to. | ||
| 244 | |||
| 245 | =item B<Use the shell> - | ||
| 246 | |||
| 247 | If no command is supplied and the system has both C<cat> and C<sh> | ||
| 248 | available then the template is wrapped in a C<cat> block and executed | ||
| 249 | using C<sh>. | ||
| 250 | |||
| 251 | By using this approach it is possible to include environment variables | ||
| 252 | and other commands within the template. For example, the combination of | ||
| 253 | C<$USER> and C<`date +%F`> could be used to insert the current user and | ||
| 254 | date into the documentation section of a new script. | ||
| 255 | |||
| 256 | As a result of this approach, any dollar signs (C<$>) that are to be | ||
| 257 | preserved in the template need to be escaped as do any back ticks (C<`>). | ||
| 258 | To avoid this behavior on systems with both C<cat> and C<sh>, specify | ||
| 259 | C<cp [% IN %] [% OUT %]> as the command to execute as this will simply | ||
| 260 | copy the template to a temporary file before creating the new document. | ||
| 261 | |||
| 262 | =item B<Use nothing> - | ||
| 263 | |||
| 264 | If no command is specified and the system does not have C<cat> and C<sh> | ||
| 265 | then the new document is created from the template as is. | ||
| 266 | |||
| 267 | =back | ||
| 268 | |||
| 269 | =head1 ISSUES | ||
| 270 | |||
| 271 | When adding entries to the configuration file it appears to be necessary | ||
| 272 | to have an empty line at the end of the file in order for the configuration | ||
| 273 | to properly load. No properly loaded configuration results in no menu | ||
| 274 | for the plug-in. | ||
| 275 | |||
| 276 | =head1 AUTHOR | ||
| 277 | |||
| 278 | Gregory Siems E<lt>gsiems@gmail.comE<gt> | ||
| 279 | |||
| 280 | =head1 COPYRIGHT AND LICENSE | ||
| 281 | |||
| 282 | Copyright (C) 2009 by Gregory Siems | ||
| 283 | |||
| 284 | This library is free software; you can redistribute it and/or modify | ||
| 285 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 286 | at your option, any later version of Perl 5 you may have available. | ||
| 287 | |||
| 288 | =cut | ||