create.pl 2.11 KB
Newer Older
1
#!/usr/bin/perl
2 3 4
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8

9
use 5.10.1;
10
use strict;
11 12
use warnings;

13
use lib qw(. lib);
14

15 16 17 18 19 20 21 22 23 24
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Util qw(get_text);

use File::Path qw(mkpath);

my $base_dir = bz_locations()->{'extensionsdir'};

my $name = $ARGV[0] or ThrowUserError('extension_create_no_name');
25
$name = ucfirst($name);
26 27 28 29 30 31 32 33 34
if ($name !~ /^[A-Z]/) {
    ThrowUserError('extension_first_letter_caps', { name => $name });
}

my $extension_dir = "$base_dir/$name"; 
mkpath($extension_dir) 
  || die "$extension_dir already exists or cannot be created.\n";

my $lcname = lc($name);
35
foreach my $path (qw(lib doc web template/en/default/hook), 
36 37 38 39 40 41
                  "template/en/default/$lcname")
{
    mkpath("$extension_dir/$path") || die "$extension_dir/$path: $!";
}

my $template = Bugzilla->template;
42
my $vars = { name => $name, path => $extension_dir };
43 44 45 46
my %create_files = (
    'config.pm.tmpl'       => 'Config.pm',
    'extension.pm.tmpl'    => 'Extension.pm',
    'util.pm.tmpl'         => 'lib/Util.pm',
47
    'web-readme.txt.tmpl'  => 'web/README',
48 49
    'hook-readme.txt.tmpl' => 'template/en/default/hook/README',
    'name-readme.txt.tmpl' => "template/en/default/$lcname/README",
50
    'name.rst.tmpl'        => "doc/$lcname.rst",
51 52 53 54 55 56 57 58 59 60 61 62
);

foreach my $template_file (keys %create_files) {
    my $target = $create_files{$template_file};
    my $output;
    $template->process("extensions/$template_file", $vars, \$output)
      or ThrowTemplateError($template->error());
   open(my $fh, '>', "$extension_dir/$target");
   print $fh $output;
   close($fh);
}

63
say get_text('extension_created', $vars);
64 65 66 67 68 69 70 71 72 73 74 75 76

__END__

=head1 NAME

extensions/create.pl - Create a framework for a new Bugzilla Extension.

=head1 SYNOPSIS

 extensions/create.pl NAME

 Creates a framework for an extension called NAME in the F<extensions/>
 directory.