| 1 | package App::mkdist; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | $App::mkdist::AUTHORITY = 'cpan:TOBYINK'; | ||
| 7 | $App::mkdist::VERSION = '0.014'; | ||
| 8 | } | ||
| 9 | |||
| 10 | use Carp; | ||
| 11 | use DateTime; | ||
| 12 | use IO::All; | ||
| 13 | use Software::License; | ||
| 14 | use Text::Template; | ||
| 15 | use URI::Escape qw[]; | ||
| 16 | |||
| 17 | { | ||
| 18 | my %templates; | ||
| 19 | sub import | ||
| 20 | { | ||
| 21 | my ($class) = @_; | ||
| 22 | |||
| 23 | my $key = undef; | ||
| 24 | while (my $line = <DATA>) | ||
| 25 | { | ||
| 26 | if ($line =~ /^COMMENCE\s+(.+)\s*$/) | ||
| 27 | { | ||
| 28 | $key = $1; | ||
| 29 | } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | $templates{$key} .= $line; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | sub _get_template | ||
| 37 | { | ||
| 38 | my ($class, $key) = @_; | ||
| 39 | return Text::Template->new(-type=>'string', -source=>$templates{$key}); | ||
| 40 | } | ||
| 41 | sub _get_template_names | ||
| 42 | { | ||
| 43 | my ($class) = @_; | ||
| 44 | return keys %templates; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _fill_in_template | ||
| 49 | { | ||
| 50 | require Module::Package::RDF; | ||
| 51 | |||
| 52 | my ($self, $template) = @_; | ||
| 53 | $template = $self->_get_template($template) unless ref $template; | ||
| 54 | |||
| 55 | my %hash = ( mpr_version => $Module::Package::RDF::VERSION ); | ||
| 56 | while (my ($k, $v) = each %$self) | ||
| 57 | { | ||
| 58 | $hash{$k} = ref $v ? \$v : $v; | ||
| 59 | } | ||
| 60 | |||
| 61 | return $template->fill_in( | ||
| 62 | -hash => \%hash, | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _iofile | ||
| 67 | { | ||
| 68 | my ($self, $file) = @_; | ||
| 69 | my $basedir = sprintf($self->{destination}, $self->{dist_name}); | ||
| 70 | return io($basedir.$file)->assert; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub create | ||
| 74 | { | ||
| 75 | my ($class, $name, %options) = @_; | ||
| 76 | |||
| 77 | $options{module_name} = $name; | ||
| 78 | $options{dist_name} = $name; | ||
| 79 | |||
| 80 | if ($name =~ /::/) | ||
| 81 | { | ||
| 82 | $options{dist_name} =~ s/::/-/g; | ||
| 83 | } | ||
| 84 | elsif ($name =~ /\-/) | ||
| 85 | { | ||
| 86 | $options{module_name} =~ s/\-/::/g; | ||
| 87 | } | ||
| 88 | |||
| 89 | my %use; | ||
| 90 | if (ref $options{use} eq 'ARRAY') | ||
| 91 | { | ||
| 92 | %use = map {$_=>1} @{$options{use}}; | ||
| 93 | } | ||
| 94 | $options{use} = \%use; | ||
| 95 | |||
| 96 | my $self = bless \%options, $class; | ||
| 97 | |||
| 98 | $self->set_defaults; | ||
| 99 | $self->create_module; | ||
| 100 | $self->create_makefile_pl; | ||
| 101 | $self->create_metadata; | ||
| 102 | $self->create_tests; | ||
| 103 | $self->create_author_tests; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub set_defaults | ||
| 107 | { | ||
| 108 | my ($self) = @_; | ||
| 109 | |||
| 110 | croak "Need an author name." unless defined $self->{author}{name}; | ||
| 111 | croak "Need an author cpanid." unless defined $self->{author}{cpanid}; | ||
| 112 | |||
| 113 | $self->{author}{cpanid} = lc $self->{author}{cpanid}; | ||
| 114 | $self->{author}{mbox} ||= sprintf('%s@cpan.org', $self->{author}{cpanid}); | ||
| 115 | |||
| 116 | $self->{backpan} ||= sprintf('http://backpan.cpan.org/authors/id/%s/%s/%s/', | ||
| 117 | substr(uc $self->{author}{cpanid}, 0, 1), | ||
| 118 | substr(uc $self->{author}{cpanid}, 0, 2), | ||
| 119 | uc $self->{author}{cpanid}, | ||
| 120 | ); | ||
| 121 | |||
| 122 | $self->{abstract} ||= 'a module that does something-or-other'; | ||
| 123 | $self->{version} ||= '0.001'; | ||
| 124 | |||
| 125 | $self->{package_flavour} ||= 'standard'; | ||
| 126 | |||
| 127 | $self->{version_ident} = 'v_'.$self->{version}; | ||
| 128 | $self->{version_ident} =~ s/\./-/g; | ||
| 129 | |||
| 130 | $self->{destination} ||= './%s/'; | ||
| 131 | |||
| 132 | unless ($self->{module_filename}) | ||
| 133 | { | ||
| 134 | $self->{module_filename} = 'lib::'.$self->{module_name}; | ||
| 135 | $self->{module_filename} =~ s/::/\//g; | ||
| 136 | $self->{module_filename} .= '.pm'; | ||
| 137 | } | ||
| 138 | |||
| 139 | $self->{copyright}{holder} ||= $self->{author}{name}; | ||
| 140 | $self->{copyright}{year} ||= DateTime->now->year; | ||
| 141 | |||
| 142 | $self->{licence_class} ||= 'Software::License::Perl_5'; | ||
| 143 | eval sprintf('use %s;', $self->{licence_class}); | ||
| 144 | $self->{licence} = $self->{licence_class}->new({ | ||
| 145 | year => $self->{copyright}{year}, | ||
| 146 | holder => $self->{copyright}{holder}, | ||
| 147 | }); | ||
| 148 | |||
| 149 | # 'includes' is 'use' minus some modules we handle specially | ||
| 150 | $self->{includes} = [grep {!/^(autodie|boolean|common_sense|strict|warnings|moose|5\.[0-9_]+|namespace_clean)$/} keys %{$self->{use}}]; | ||
| 151 | |||
| 152 | { | ||
| 153 | my @mr = @{ $self->{includes} }; | ||
| 154 | push @mr, 'Moose' if $self->{use}{moose}; | ||
| 155 | push @mr, 'autodie' if $self->{use}{autodie}; | ||
| 156 | push @mr, 'boolean' if $self->{use}{boolean}; | ||
| 157 | push @mr, 'common::sense' if $self->{use}{common_sense}; | ||
| 158 | push @mr, 'namespace::clean' if $self->{use}{namespace_clean} || $self->{use}{moose}; | ||
| 159 | |||
| 160 | if (@mr) | ||
| 161 | { | ||
| 162 | $self->{requires} = sprintf( | ||
| 163 | ";\n\t:runtime-requirement %s", | ||
| 164 | (join ' , ', (map { my ($pkg, $ver) = split /\s+/, $_; ($ver =~ /^v?[0-9\._]+/) ? "[ :on \"$pkg $ver\"^^:CpanId ]" : "[ :on \"$pkg\"^^:CpanId ]" } @mr)) | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | else | ||
| 168 | { | ||
| 169 | $self->{requires} = ''; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | $self->{pragmas} ||= join "\n", do { | ||
| 174 | my @pragmas = grep { /^5\.[0-9_]+$/ } keys %{$self->{use}}; | ||
| 175 | push @pragmas, '5.010' unless @pragmas; | ||
| 176 | push @pragmas, 'autodie' if $self->{use}{autodie}; | ||
| 177 | push @pragmas, ($self->{use}{boolean}) ? 'boolean' : 'constant { false => 0, true => 1 }'; | ||
| 178 | push @pragmas, ($self->{use}{common_sense} ? ('common::sense') : ('strict','warnings')); | ||
| 179 | push @pragmas, 'utf8'; | ||
| 180 | push @pragmas, 'Moose' if $self->{use}{moose}; | ||
| 181 | map { sprintf('use %s;', $_) } @pragmas; | ||
| 182 | }; | ||
| 183 | |||
| 184 | $self->{final_pragmas} ||= join "\n", do { | ||
| 185 | my @pragmas; | ||
| 186 | push @pragmas, 'namespace::clean' if $self->{use}{namespace_clean} || $self->{use}{moose}; | ||
| 187 | map { sprintf('use %s;', $_) } @pragmas; | ||
| 188 | }; | ||
| 189 | |||
| 190 | $self->{final_code} ||= join "\n", do { | ||
| 191 | my @lines; | ||
| 192 | push @lines, '__PACKAGE__->meta->make_immutable;' if $self->{use}{moose}; | ||
| 193 | push @lines, 'true;'; | ||
| 194 | @lines; | ||
| 195 | }; | ||
| 196 | |||
| 197 | foreach (qw(pragmas final_pragmas includes)) | ||
| 198 | { | ||
| 199 | if (ref $self->{$_} eq 'ARRAY') | ||
| 200 | { | ||
| 201 | $self->{$_} = join "\n", map { "use $_;" } @{ $self->{$_} }; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | sub create_module | ||
| 207 | { | ||
| 208 | my ($self) = @_; | ||
| 209 | |||
| 210 | $self->_iofile( $self->{module_filename} )->print( $self->_fill_in_template('module') ); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub create_makefile_pl | ||
| 215 | { | ||
| 216 | my ($self) = @_; | ||
| 217 | |||
| 218 | $self->_iofile('Makefile.PL')->print($self->_fill_in_template('Makefile.PL')); | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub create_metadata | ||
| 223 | { | ||
| 224 | my ($self) = @_; | ||
| 225 | |||
| 226 | $self->_iofile($_)->print($self->_fill_in_template($_)) | ||
| 227 | foreach grep { m#^meta/# } $self->_get_template_names; | ||
| 228 | return; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub create_tests | ||
| 232 | { | ||
| 233 | my ($self) = @_; | ||
| 234 | |||
| 235 | $self->_iofile($_)->print($self->_fill_in_template($_)) | ||
| 236 | foreach grep { m#^t/# } $self->_get_template_names; | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub create_author_tests | ||
| 241 | { | ||
| 242 | my ($self) = @_; | ||
| 243 | |||
| 244 | $self->_iofile($_)->print($self->_fill_in_template($_)) | ||
| 245 | foreach grep { m#^xt/# } $self->_get_template_names; | ||
| 246 | |||
| 247 | my $xtdir = io->catdir($ENV{HOME}, qw(perl5 xt)); | ||
| 248 | $self->_iofile("xt/".$_->filename)->print(scalar $_->slurp) | ||
| 249 | foreach grep { $_->filename =~ /\.t$/ } $xtdir->all; | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | 1; | ||
| 254 | |||
| 255 | =head1 NAME | ||
| 256 | |||
| 257 | App::mkdist - create distributions that will use Module::Package::RDF. | ||
| 258 | |||
| 259 | =head1 SYNOPSIS | ||
| 260 | |||
| 261 | mkdist Local::Example::Useful | ||
| 262 | |||
| 263 | =head1 DESCRIPTION | ||
| 264 | |||
| 265 | This package provides just one (class) method: | ||
| 266 | |||
| 267 | =over | ||
| 268 | |||
| 269 | =item C<< App::mkdist->create($distname, %options) >> | ||
| 270 | |||
| 271 | Create a distribution directory including all needed files. | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | There are various methods that may be useful for people subclassing this | ||
| 276 | class to look at (and possibly override). | ||
| 277 | |||
| 278 | =over | ||
| 279 | |||
| 280 | =item C<< set_defaults >> | ||
| 281 | |||
| 282 | =item C<< create_module >> | ||
| 283 | |||
| 284 | =item C<< create_makefile_pl >> | ||
| 285 | |||
| 286 | =item C<< create_metadata >> | ||
| 287 | |||
| 288 | =item C<< create_tests >> | ||
| 289 | |||
| 290 | =item C<< create_author_tests >> | ||
| 291 | |||
| 292 | =back | ||
| 293 | |||
| 294 | =head1 SEE ALSO | ||
| 295 | |||
| 296 | L<Module::Package::RDF>. | ||
| 297 | |||
| 298 | =head1 AUTHOR | ||
| 299 | |||
| 300 | Toby Inkster E<lt>tobyink@cpan.orgE<gt>. | ||
| 301 | |||
| 302 | =head1 COPYRIGHT AND LICENCE | ||
| 303 | |||
| 304 | This software is copyright (c) 2011-2013 by Toby Inkster. | ||
| 305 | |||
| 306 | This is free software; you can redistribute it and/or modify it under | ||
| 307 | the same terms as the Perl 5 programming language system itself. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | __DATA__ |