root/misc/pmsetup

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

import to //scratch

  • 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     !system "svk import -m 'import $dist' //scratch/$dist" or die $?;
62     chdir "..";
63     rmtree $dist, 1;
64     !system "svk co //scratch/$dist" or die $?;
65 }
66
67 sub write_file {
68     my($path, $template, $vars) = @_;
69
70     if (-e $path) {
71         my $ans = prompt("$path exists. Override? [yN] ", 'n');
72         return if $ans !~ /[Yy]/;
73     }
74
75     my $dir = File::Basename::dirname($path);
76     unless (-e $dir) {
77         warn "Creating directory $dir\n";
78         File::Path::mkpath($dir, 1, 0777);
79     }
80
81     my $tt = Template->new;
82     $tt->process(\$template, $vars, \my $content);
83
84     warn "Creating $path\n";
85     open my $out, ">", $path or die "$path: $!";
86     print $out $content;
87     close $out;
88 }
89
90 __DATA__
91 ---
92 file: Makefile.PL
93 template: |
94   use inc::Module::Install;
95   name '[% dist %]';
96   all_from 'lib/[% path %]';
97
98   build_requires 'Test::More';
99   auto_include;
100   WriteAll;
101
102 ---
103 file: t/00_compile.t
104 template: |
105   use strict;
106   use Test::More tests => 1;
107
108   BEGIN { use_ok '[% module %]' }
109 ---
110 file: t/99_pod.t
111 template: |
112   use Test::More;
113   eval "use Test::Pod 1.00";
114   plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
115   all_pod_files_ok();
116 ---
117 file: Changes
118 template: |
119   Revision history for Perl extension [% module %]
120
121     0.01  [% localtime %]
122           - original version
123 ---
124 file: lib/$path
125 template: |
126   package [% module %];
127
128   use strict;
129   our $VERSION = '0.01';
130
131   1;
132   __END__
133
134   =head1 NAME
135
136   [% module %] -
137
138   =head1 SYNOPSIS
139
140     use [% module %];
141
142   =head1 DESCRIPTION
143
144   [% module %] is
145
146   =head1 AUTHOR
147
148   [% config.author %] E<lt>[% config.email %]E<gt>
149
150   =head1 LICENSE
151
152   This library is free software; you can redistribute it and/or modify
153   it under the same terms as Perl itself.
154
155   =head1 SEE ALSO
156
157   =cut
158 ---
159 file: MANIFEST.SKIP
160 template: |
161   \bRCS\b
162   \bCVS\b
163   ^MANIFEST\.
164   ^Makefile$
165   ~$
166   \.old$
167   ^blib/
168   ^pm_to_blib
169   ^MakeMaker-\d
170   \.gz$
171   \.cvsignore
172 ---
173 file: README
174 template: |
175   This is Perl module [% module %].
176
177   INSTALLATION
178
179   [% module %] installation is straightforward. If your CPAN shell is set up,
180   you should just be able to do
181
182       % cpan [% module %]
183
184   Download it, unpack it, then build it as per the usual:
185
186       % perl Makefile.PL
187       % make && make test
188
189   Then install it:
190
191       % make install
192
193   DOCUMENTATION
194
195   [% module %] documentation is available as in POD. So you can do:
196
197       % perldoc [% module %]
198
199   to read the documentation online with your favorite pager.
200
201   [% config.author %]
Note: See TracBrowser for help on using the browser.