| 1 |
|
|---|
| 2 |
use strict; |
|---|
| 3 |
use warnings; |
|---|
| 4 |
|
|---|
| 5 |
use Config; |
|---|
| 6 |
use FindBin; |
|---|
| 7 |
use ExtUtils::MakeMaker; |
|---|
| 8 |
use File::Basename; |
|---|
| 9 |
use File::Path; |
|---|
| 10 |
use YAML; |
|---|
| 11 |
use Template; |
|---|
| 12 |
|
|---|
| 13 |
chdir "$FindBin::Bin/.."; |
|---|
| 14 |
|
|---|
| 15 |
my $module = shift @ARGV or die "Usage: plugin-start.pl Plugin::Name\n"; |
|---|
| 16 |
$module =~ s/-/::/g; |
|---|
| 17 |
|
|---|
| 18 |
my $file = "$ENV{HOME}/.plagger-module.yml"; |
|---|
| 19 |
my $config = eval { YAML::LoadFile($file) } || {}; |
|---|
| 20 |
|
|---|
| 21 |
my $save; |
|---|
| 22 |
$config->{author} ||= do { |
|---|
| 23 |
$save++; |
|---|
| 24 |
prompt("Your name: "); |
|---|
| 25 |
}; |
|---|
| 26 |
|
|---|
| 27 |
write_plugin_files($module, $config->{author}); |
|---|
| 28 |
|
|---|
| 29 |
YAML::DumpFile($file, $config) if $save; |
|---|
| 30 |
|
|---|
| 31 |
sub write_plugin_files { |
|---|
| 32 |
my($module, $author) = @_; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
(my $plugin = $module) =~ s!::!-!g; |
|---|
| 38 |
(my $path = $module) =~ s!::!/!g; |
|---|
| 39 |
|
|---|
| 40 |
my $template = YAML::Load(join '', <DATA>); |
|---|
| 41 |
my $vars = { module => $module, plugin => $plugin, path => $path, author => $author }; |
|---|
| 42 |
|
|---|
| 43 |
my @files; |
|---|
| 44 |
push @files, write_file("lib/Plagger/Plugin/$path.pm", $template->{plugin}, $vars); |
|---|
| 45 |
push @files, write_file("deps/$plugin.yaml", $template->{deps}, $vars); |
|---|
| 46 |
push @files, write_file("t/plugins/$plugin/base.t", $template->{test}, $vars); |
|---|
| 47 |
|
|---|
| 48 |
if (my $vcs = version_control()) { |
|---|
| 49 |
my $ans = prompt("$vcs add newly created files? [Yn]", 'y'); |
|---|
| 50 |
if ($ans =~ /[Yy]/) { |
|---|
| 51 |
system($vcs, 'add', @files); |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
sub write_file { |
|---|
| 57 |
my($path, $template, $vars) = @_; |
|---|
| 58 |
|
|---|
| 59 |
if (-e $path) { |
|---|
| 60 |
my $ans = prompt("$path exists. Override? [yN] ", 'n'); |
|---|
| 61 |
return if $ans !~ /[Yy]/; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
my $dir = File::Basename::dirname($path); |
|---|
| 65 |
unless (-e $dir) { |
|---|
| 66 |
warn "Creating directory $dir\n"; |
|---|
| 67 |
File::Path::mkpath($dir, 1, 0777); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
my $tt = Template->new; |
|---|
| 71 |
$tt->process(\$template, $vars, \my $content); |
|---|
| 72 |
|
|---|
| 73 |
warn "Creating $path\n"; |
|---|
| 74 |
open my $out, ">", $path or die "$path: $!"; |
|---|
| 75 |
print $out $content; |
|---|
| 76 |
close $out; |
|---|
| 77 |
|
|---|
| 78 |
return $path; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
sub version_control { |
|---|
| 82 |
return 'svk' if check_command('svk', 'svk info', qr/Checkout Path/); |
|---|
| 83 |
return 'svn' if -e ".svn/entries"; |
|---|
| 84 |
return; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
sub check_command { |
|---|
| 88 |
my($bin, $command, $re) = @_; |
|---|
| 89 |
return unless grep { -e File::Spec->catfile($_, $bin) } split /$Config::Config{path_sep}/, $ENV{PATH}; |
|---|
| 90 |
|
|---|
| 91 |
my $res = qx($command); |
|---|
| 92 |
defined $res && $res =~ $re; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
__DATA__ |
|---|
| 96 |
plugin: | |
|---|
| 97 |
package Plagger::Plugin::[% module %]; |
|---|
| 98 |
use strict; |
|---|
| 99 |
use base qw( Plagger::Plugin ); |
|---|
| 100 |
|
|---|
| 101 |
sub register { |
|---|
| 102 |
my($self, $context) = @_; |
|---|
| 103 |
$context->register_hook( |
|---|
| 104 |
$self, |
|---|
| 105 |
|
|---|
| 106 |
); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
1; |
|---|
| 110 |
__END__ |
|---|
| 111 |
|
|---|
| 112 |
=head1 NAME |
|---|
| 113 |
|
|---|
| 114 |
Plagger::Plugin::[% module %] - |
|---|
| 115 |
|
|---|
| 116 |
=head1 SYNOPSIS |
|---|
| 117 |
|
|---|
| 118 |
- module: [% module %] |
|---|
| 119 |
|
|---|
| 120 |
=head1 DESCRIPTION |
|---|
| 121 |
|
|---|
| 122 |
XXX Write the description for [% module %] |
|---|
| 123 |
|
|---|
| 124 |
=head1 CONFIG |
|---|
| 125 |
|
|---|
| 126 |
XXX Document configuration variables if any. |
|---|
| 127 |
|
|---|
| 128 |
=head1 AUTHOR |
|---|
| 129 |
|
|---|
| 130 |
[% author %] |
|---|
| 131 |
|
|---|
| 132 |
=head1 SEE ALSO |
|---|
| 133 |
|
|---|
| 134 |
L<Plagger> |
|---|
| 135 |
|
|---|
| 136 |
=cut |
|---|
| 137 |
|
|---|
| 138 |
deps: | |
|---|
| 139 |
name: [% module %] |
|---|
| 140 |
author: [% author %] |
|---|
| 141 |
depends: |
|---|
| 142 |
|
|---|
| 143 |
test: | |
|---|
| 144 |
use strict; |
|---|
| 145 |
use t::TestPlagger; |
|---|
| 146 |
|
|---|
| 147 |
test_plugin_deps; |
|---|
| 148 |
plan 'no_plan'; |
|---|
| 149 |
run_eval_expected; |
|---|
| 150 |
|
|---|
| 151 |
__END__ |
|---|
| 152 |
|
|---|
| 153 |
=== Loading [% module %] |
|---|
| 154 |
--- input config |
|---|
| 155 |
plugins: |
|---|
| 156 |
- module: [% module %] |
|---|
| 157 |
--- expected |
|---|
| 158 |
ok 1, $block->name; |
|---|