root/misc/pmsetup

Revision 2025 (checked in by miyagawa, 14 years ago)

undo svk

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use ExtUtils::MakeMaker qw(prompt);
5 use File::Basename;
6 use File::Path;
7 use File::Spec;
8 use Template;
9 use YAML;
10
11 my $path   = File::Spec->catfile($ENV{HOME}, "/.pmsetuprc");
12 my $config = eval { YAML::LoadFile($path) } || {};
13
14 my $save;
15 while (! $config->{author}) {
16     $config->{author} = prompt("Your name: ", '');
17     $save++;
18 }
19
20 while (! $config->{email}) {
21     $config->{email} = prompt("Your email: ", '');
22     $save++;
23 }
24
25 my $modname = shift @ARGV or die "Usage: $0 module\n";
26    $modname =~ s/-/::/g;
27
28 write_plugin_files($modname, $config);
29
30 END {
31     YAML::DumpFile($path, $config) if $save;
32 }
33
34 sub write_plugin_files {
35     my($module, $config) = @_;
36
37     # $module = "Foo::Bar"
38     # $dist   = "Foo-Bar"
39     # $path   = "Foo/Bar.pm"
40     my @pkg  = split /::/, $module;
41     my $dist = join "-", @pkg;
42     my $path = join("/", @pkg) . ".pm";
43
44     mkdir $dist, 0777;
45     chdir $dist;
46
47     my @template = YAML::Load(join '', <DATA>);
48     my $vars = { module => $module, dist => $dist, path => $path,
49                  config => $config, localtime => scalar localtime };
50
51     for my $tmpl (@template) {
52         my $file = $tmpl->{file};
53            $file =~ s/(\$\w+)/$1/eeg;
54         write_file($file, $tmpl->{template}, $vars);
55     }
56
57     !system "perl Makefile.PL" or die $?;
58     !system 'make test' or die $?;
59     !system 'make manifest' or die $?;
60     !system 'make distclean' or die $?;
61 }
62
63 sub write_file {
64     my($path, $template, $vars) = @_;
65
66     if (-e $path) {
67         my $ans = prompt("$path exists. Override? [yN] ", 'n');
68         return if $ans !~ /[Yy]/;
69     }
70
71     my $dir = File::Basename::dirname($path);
72     unless (-e $dir) {
73         warn "Creating directory $dir\n";
74         File::Path::mkpath($dir, 1, 0777);
75     }
76
77     my $tt = Template->new;
78     $tt->process(\$template, $vars, \my $content);
79
80     warn "Creating $path\n";
81     open my $out, ">", $path or die "$path: $!";
82     print $out $content;
83     close $out;
84 }
85
86 __DATA__
87 ---
88 file: Makefile.PL
89 template: |
90   use inc::Module::Install;
91   name '[% dist %]';
92   all_from 'lib/[% path %]';
93
94   build_requires 'Test::More';
95   auto_include;
96   WriteAll;
97
98 ---
99 file: t/00_compile.t
100 template: |
101   use strict;
102   use Test::More tests => 1;
103
104   BEGIN { use_ok '[% module %]' }
105 ---
106 file: t/99_pod.t
107 template: |
108   use Test::More;
109   eval "use Test::Pod 1.00";
110   plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
111   all_pod_files_ok();
112 ---
113 file: Changes
114 template: |
115   Revision history for Perl extension [% module %]
116
117     0.01  [% localtime %]
118           - original version
119 ---
120 file: lib/$path
121 template: |
122   package [% module %];
123
124   use strict;
125   our $VERSION = '0.01';
126
127   1;
128   __END__
129
130   =head1 NAME
131
132   [% module %] -
133
134   =head1 SYNOPSIS
135
136     use [% module %];
137
138   =head1 DESCRIPTION
139
140   [% module %] is
141
142   =head1 AUTHOR
143
144   [% config.author %] E<lt>[% config.email %]E<gt>
145
146   =head1 LICENSE
147
148   This library is free software; you can redistribute it and/or modify
149   it under the same terms as Perl itself.
150
151   =head1 SEE ALSO
152
153   =cut
154 ---
155 file: MANIFEST.SKIP
156 template: |
157   \bRCS\b
158   \bCVS\b
159   ^MANIFEST\.
160   ^Makefile$
161   ~$
162   \.old$
163   ^blib/
164   ^pm_to_blib
165   ^MakeMaker-\d
166   \.gz$
167   \.cvsignore
168 ---
169 file: README
170 template: |
171   This is Perl module [% module %].
172
173   INSTALLATION
174
175   [% module %] installation is straightforward. If your CPAN shell is set up,
176   you should just be able to do
177
178       % cpan [% module %]
179
180   Download it, unpack it, then build it as per the usual:
181
182       % perl Makefile.PL
183       % make && make test
184
185   Then install it:
186
187       % make install
188
189   DOCUMENTATION
190
191   [% module %] documentation is available as in POD. So you can do:
192
193       % perldoc [% module %]
194
195   to read the documentation online with your favorite pager.
196
197   [% config.author %]
Note: See TracBrowser for help on using the browser.