root/misc/pmsetup

Revision 2974 (checked in by miyagawa, 7 years ago)

use Milla

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env 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 sub ask {
12     my($prompt, $field, $config, $save_ref) = @_;
13     while (! $config->{$field}) {
14         $config->{$field} = prompt("$prompt: ", '');
15         $$save_ref++;
16     }
17 }
18
19 my $path   = File::Spec->catfile($ENV{HOME}, "/.pmsetuprc");
20 my $config = eval { YAML::LoadFile($path) } || {};
21
22 my $save;
23 ask 'Your name', 'author', $config, \$save;
24 ask 'Your email', 'email', $config, \$save;
25 ask 'Github account', 'github_user', $config, \$save;
26
27 my $modname = shift @ARGV or die "Usage: $0 module\n";
28    $modname =~ s/-/::/g;
29
30 write_plugin_files($modname, $config);
31
32 END {
33     YAML::DumpFile($path, $config) if $save;
34 }
35
36 sub write_plugin_files {
37     my($module, $config) = @_;
38
39     # $module = "Foo::Bar"
40     # $dist   = "Foo-Bar"
41     # $path   = "Foo/Bar.pm"
42     my @pkg  = split /::/, $module;
43     my $dist = join "-", @pkg;
44     my $path = join("/", @pkg) . ".pm";
45
46     mkdir $dist, 0777;
47     chdir $dist;
48
49     my @template = YAML::Load(join '', <DATA>);
50     my $vars = { module => $module, dist => $dist, path => $path,
51                  config => $config, localtime => scalar localtime, year => (localtime)[5] + 1900 };
52
53     for my $tmpl (@template) {
54         my $file = $tmpl->{file};
55            $file =~ s/(\$\w+)/$1/eeg;
56         write_file($file, $tmpl->{template}, $vars);
57     }
58
59     # init first to make Milla Git integration happy
60     system "git", "init";
61     system "git", "add", ".";
62
63     !system "dzil", "build" or die $?;
64     !system "dzil", "clean" or die $?;
65
66     system "git", "add", ".";
67 }
68
69 sub write_file {
70     my($path, $template, $vars) = @_;
71
72     if (-e $path) {
73         my $ans = prompt("$path exists. Override? [yN] ", 'n');
74         return if $ans !~ /[Yy]/;
75     }
76
77     my $dir = File::Basename::dirname($path);
78     unless (-e $dir) {
79         warn "Creating directory $dir\n";
80         File::Path::mkpath($dir, 1, 0777);
81     }
82
83     my $tt = Template->new;
84     $tt->process(\$template, $vars, \my $content);
85
86     warn "Creating $path\n";
87     open my $out, ">", $path or die "$path: $!";
88     print $out $content;
89     close $out;
90 }
91
92 __DATA__
93 ---
94 file: dist.ini
95 template: |
96   [@Milla]
97
98 ---
99 file: cpanfile
100 template: |
101   requires 'perl', '5.008001';
102  
103   on test => sub {
104       requires 'Test::More', '0.88';
105   };
106 ---
107 file: Changes
108 template: |
109   Revision history for [% module %]
110
111   {{$NEXT}}
112           - Initial release
113 ---
114 file: lib/$path
115 template: |
116   package [% module %];
117
118   use strict;
119   use 5.008_001;
120   our $VERSION = '0.01';
121
122   1;
123   __END__
124
125   =encoding utf-8
126
127   =for stopwords
128
129   =head1 NAME
130
131   [% module %] - Blah blah blah
132
133   =head1 SYNOPSIS
134
135     use [% module %];
136
137   =head1 DESCRIPTION
138
139   [% module %] is
140
141   =head1 AUTHOR
142
143   [% config.author %] E<lt>[% config.email %]E<gt>
144
145   =head1 COPYRIGHT
146
147   Copyright [% year %]- [% config.author %]
148
149   =head1 LICENSE
150
151   This library is free software; you can redistribute it and/or modify
152   it under the same terms as Perl itself.
153
154   =head1 SEE ALSO
155
156   =cut
157 ---
158 file: .gitignore
159 template: |
160   MYMETA.*
161   META.yml
162   /[% dist %]-*
163   /.build
164   !META.json
Note: See TracBrowser for help on using the browser.