| 1 | package Module::MakeMaker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # TODO Use ScanDeps to scan deps. | ||
| 7 | use Getopt::Long; | ||
| 8 | use Template::Toolkit::Simple; | ||
| 9 | use YAML::XS; | ||
| 10 | use Cwd; | ||
| 11 | use File::Find::Rule; | ||
| 12 | use File::ShareDir; | ||
| 13 | |||
| 14 | our $VERSION = '0.03'; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my $self = bless {}, $class; | ||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _run_command { | ||
| 23 | my $class = shift; | ||
| 24 | my $self = $class->new; | ||
| 25 | |||
| 26 | $self->check_sanity; | ||
| 27 | $self->init; | ||
| 28 | |||
| 29 | my $called = 0; | ||
| 30 | |||
| 31 | my $call_method = sub { | ||
| 32 | my ($name, $value) = @_; | ||
| 33 | my $method = lc($name); | ||
| 34 | $method =~ s/-/_/g; | ||
| 35 | $self->$method($value); | ||
| 36 | $called = 1; | ||
| 37 | }; | ||
| 38 | |||
| 39 | GetOptions( | ||
| 40 | map { | ||
| 41 | my $option = $_; | ||
| 42 | my $option2 = $option; | ||
| 43 | $option .= "|$option2" | ||
| 44 | if $option2 =~ s/_/-/g; | ||
| 45 | ($option, $call_method); | ||
| 46 | } qw( | ||
| 47 | tt_yaml | ||
| 48 | make_makefile | ||
| 49 | create_config | ||
| 50 | cpan_makefile | ||
| 51 | commit | ||
| 52 | ) | ||
| 53 | ); | ||
| 54 | |||
| 55 | return if $called; | ||
| 56 | |||
| 57 | $self->ask_create_config and return; | ||
| 58 | |||
| 59 | $self->make_makefile(); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub check_sanity { | ||
| 63 | my $cwd = Cwd::cwd(); | ||
| 64 | my $home = $ENV{HOME}; | ||
| 65 | $cwd =~ s/\/$//; | ||
| 66 | $home =~ s/\/$//; | ||
| 67 | die "Don't run 'mmm' from your home directory, silly.\n" | ||
| 68 | if $cwd eq $home; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get_path { | ||
| 72 | my $self = shift; | ||
| 73 | my $type = shift; | ||
| 74 | my @path = (); | ||
| 75 | my $path = Cwd::cwd(); | ||
| 76 | die "Don't support path '$path'" | ||
| 77 | unless $path =~ m!^/!; | ||
| 78 | while ($path) { | ||
| 79 | if ($type eq 'config.yaml') { | ||
| 80 | my $template = "$path/MMM.yaml"; | ||
| 81 | push @path, $template if -e $template; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $template = "$path/MMM/$type"; | ||
| 85 | push @path, $template if -e $template; | ||
| 86 | |||
| 87 | $template = "$path/.mmm/$type"; | ||
| 88 | push @path, $template if -e $template; | ||
| 89 | |||
| 90 | $path =~ s!(.*)/.*!$1! | ||
| 91 | or die "Reduction failure for '$path'"; | ||
| 92 | } | ||
| 93 | |||
| 94 | # $INC{'Module/MakeMaker.pm'} = '/usr/local/lib/perl5/site_perl/5.10.0/Module/MakeMaker.pm'; | ||
| 95 | my $template = File::ShareDir::module_dir('Module::MakeMaker') . "/$type"; | ||
| 96 | push @path, $template if -e $template; | ||
| 97 | |||
| 98 | return @path; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub init { | ||
| 102 | my $self = shift; | ||
| 103 | $self->get_config; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub get_config { | ||
| 107 | my $self = shift; | ||
| 108 | |||
| 109 | my $config = {}; | ||
| 110 | for (reverse $self->get_path('config.yaml')) { | ||
| 111 | my $config_next = eval { YAML::XS::LoadFile($_) }; | ||
| 112 | if ($@ or ref($config_next) ne 'HASH') { | ||
| 113 | warn "Failed to load '$_' as a hash: $@\n"; | ||
| 114 | $config_next = {}; | ||
| 115 | } | ||
| 116 | $config = { %$config, %$config_next }; | ||
| 117 | } | ||
| 118 | |||
| 119 | $config->{mmm_path} ||= [ $self->get_path('template') ]; | ||
| 120 | my $cwd = Cwd::cwd(); | ||
| 121 | $cwd =~ s/.*[\/\\](.+)[\/\\]?$/$1/; | ||
| 122 | $config->{name} ||= $cwd; | ||
| 123 | $config->{module_name} ||= $config->{name}; | ||
| 124 | $config->{module_libpath} ||= 'lib/' . $config->{name} . '.pm'; | ||
| 125 | $config->{dist_name} ||= $config->{name}; | ||
| 126 | $config->{module_name} =~ s/[\-\/\\]/::/g; | ||
| 127 | $config->{module_libpath} =~ s/(-|::)/\//g; | ||
| 128 | $config->{dist_name} =~ s/([\/\\]|::)/-/g; | ||
| 129 | $config->{copy_files} ||= []; | ||
| 130 | $config->{copy_files} = | ||
| 131 | [File::Find::Rule->file->in(@{$config->{copy_files}})]; | ||
| 132 | $config->{template_files} ||= [ grep { -e } qw( Changes lib t bin) ]; | ||
| 133 | $config->{template_files} = | ||
| 134 | [File::Find::Rule->file->in(@{$config->{template_files}})]; | ||
| 135 | $config->{include_path} ||= []; | ||
| 136 | if (defined $config->{requires} and ref($config->{requires}) ne 'ARRAY') { | ||
| 137 | $config->{requires} = [$config->{requires}]; | ||
| 138 | } | ||
| 139 | $config->{requires} ||= []; | ||
| 140 | for (my $i = 0; $i < @{$config->{requires}}; $i++) { | ||
| 141 | if (not ref($config->{requires}[$i])) { | ||
| 142 | $config->{requires}[$i] = {$config->{requires}[$i] => 0}; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | push @{$config->{include_path}}, | ||
| 146 | $self->get_path('template'), | ||
| 147 | '..'; | ||
| 148 | $config->{config_yaml} = | ||
| 149 | -e 'MMM.yaml' ? Cwd::abs_path('MMM.yaml') : | ||
| 150 | -e 'MMM/config.yaml' ? Cwd::abs_path('MMM/config.yaml') : | ||
| 151 | ''; | ||
| 152 | |||
| 153 | $config->{gmtime} = gmtime (). ' GMT'; | ||
| 154 | |||
| 155 | $self->{config} = $config; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub make_makefile { | ||
| 159 | my $self = shift; | ||
| 160 | $self->render_template('Makefile', $self->{config}, 'Makefile'); | ||
| 161 | print "Makefile created.\n"; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub cpan_makefile { | ||
| 165 | my $self = shift; | ||
| 166 | $self->render_template('mmm.mk', $self->{config}, 'cpan/mmm.mk'); | ||
| 167 | print "cpan/mmm.mk created.\n"; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub tt_yaml { | ||
| 171 | my $self = shift; | ||
| 172 | YAML::XS::DumpFile('tt.yaml', $self->{config}); | ||
| 173 | print "tt.yaml created.\n"; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub render_template { | ||
| 177 | my $self = shift; | ||
| 178 | my ($template, $data, $output, %options) = @_; | ||
| 179 | my $tt = tt | ||
| 180 | ->output($output) | ||
| 181 | ->path($self->{config}{mmm_path}) | ||
| 182 | ->data($data); | ||
| 183 | for my $option (keys %options) { | ||
| 184 | $tt->$option($options{$option}); | ||
| 185 | } | ||
| 186 | $tt->render($template); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub ask_create_config { | ||
| 190 | my $self = shift; | ||
| 191 | return if -f 'MMM.yaml' || -f 'MMM/config.yaml'; | ||
| 192 | print "Module::MakeMaker has determined that you have no MMM.yaml\n"; | ||
| 193 | my $answer = ''; | ||
| 194 | while ($answer !~ /^[yn]$/) { | ||
| 195 | print "Would you like to create that now? [Yn] "; | ||
| 196 | $answer = <>; | ||
| 197 | chomp $answer; | ||
| 198 | $answer ||= 'y'; | ||
| 199 | $answer =~ s/y(es)?/y/i; | ||
| 200 | $answer =~ s/n(o)?/n/i; | ||
| 201 | } | ||
| 202 | return 1 if $answer eq 'n'; | ||
| 203 | $self->create_config($self->default_data);; | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub default_data { | ||
| 208 | my $self = shift; | ||
| 209 | my $name = Cwd::cwd(); | ||
| 210 | $name =~ s/[\/\\]*$//; | ||
| 211 | $name =~ s/.*[\/\\]//; | ||
| 212 | return { | ||
| 213 | name => $name, | ||
| 214 | dist_name => $name, | ||
| 215 | version => '0.01', | ||
| 216 | }; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub create_config { | ||
| 220 | my $self = shift; | ||
| 221 | my $data = shift; | ||
| 222 | $self->render_template('MMM.yaml', $data, 'MMM.yaml'); | ||
| 223 | print <<'...'; | ||
| 224 | MMM.yaml has been created. | ||
| 225 | Please edit it now. | ||
| 226 | Then run `mmm` to generate a Makefile. | ||
| 227 | Then run `make test`, etc. | ||
| 228 | ... | ||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub commit { | ||
| 233 | my $self = shift; | ||
| 234 | my $vcs = $self->{config}{vcs} || 'svn'; | ||
| 235 | die "'$vcs' is unsupported VCS" unless $vcs =~ /^(svn|svk)$/; | ||
| 236 | print "make clean\n"; | ||
| 237 | system("make clean") == 0 or die; | ||
| 238 | print "$vcs st\n"; | ||
| 239 | my @lines = `$vcs status`; | ||
| 240 | print @lines; | ||
| 241 | if (not @lines) { | ||
| 242 | print "Nothing to commit.\n\n"; | ||
| 243 | system("mmm; make init"); | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | if (grep /^\?/, @lines) { | ||
| 247 | print "You have unaccounted files.\n"; | ||
| 248 | if ($self->_prompt("Would you like to add them?", 'y')) { | ||
| 249 | for (grep /^\?/, @lines) { | ||
| 250 | chomp; | ||
| 251 | s/\?\s*//; | ||
| 252 | print "$vcs add $_\n"; | ||
| 253 | system("$vcs add $_") == 0 or die; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $self->_next() or return; | ||
| 259 | } | ||
| 260 | system("$vcs diff | vim -"); | ||
| 261 | $self->_next() or return; | ||
| 262 | print "$vcs commit\n"; | ||
| 263 | system("$vcs commit"); | ||
| 264 | print "$vcs commit complete!\n\n"; | ||
| 265 | system("mmm; make init"); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _next { | ||
| 269 | print "Press ENTER to continue"; | ||
| 270 | return <>; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _prompt { | ||
| 274 | my $self = shift; | ||
| 275 | my $prompt = shift; | ||
| 276 | my $default = shift; | ||
| 277 | $prompt .= ($default eq 'y') ? ' [Yn] ' : ' [yN] '; | ||
| 278 | my $answer = ''; | ||
| 279 | while (not $answer) { | ||
| 280 | print $prompt; | ||
| 281 | my $answer = <>; | ||
| 282 | chomp $answer; | ||
| 283 | $answer ||= $default; | ||
| 284 | $answer = lc($answer); | ||
| 285 | $answer =~ s/^yes$/y/; | ||
| 286 | $answer =~ s/^no$/y/; | ||
| 287 | $answer = '' unless $answer =~ /^[yn]$/; | ||
| 288 | } | ||
| 289 | return ($answer eq 'y'); | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | 1; | ||
| 294 | |||
| 295 | =head1 NAME | ||
| 296 | |||
| 297 | Module::MakeMaker - A New Way to Make Modules | ||
| 298 | |||
| 299 | =head1 SYNOPSIS | ||
| 300 | |||
| 301 | > mmm | ||
| 302 | > vim Mmm.yaml | ||
| 303 | > make | ||
| 304 | > make test | ||
| 305 | > make install | ||
| 306 | > make dist | ||
| 307 | |||
| 308 | =head1 DESCRIPTION | ||
| 309 | |||
| 310 | Module::MakeMaker (MMM) is a new way to make modules. It builds on the | ||
| 311 | the old, tried and true method, but automates the repetitive, | ||
| 312 | cumbersome and error prone steps involved. It leverages YAML, | ||
| 313 | Makefiles and templating, to do the right things you want to do, when | ||
| 314 | they need to be done. This means you can apply a simple patch to your | ||
| 315 | module, and then run: | ||
| 316 | |||
| 317 | make upload | ||
| 318 | |||
| 319 | and know that all the appropriate actions need will be performed, and if | ||
| 320 | they are all successful, your module will be on its way to CPAN, and you | ||
| 321 | can move on to your next task. | ||
| 322 | |||
| 323 | MMM bootstraps a module environment from meta information that you | ||
| 324 | specify in C<MMM.yaml> or C<MMM/config.yaml> files. You can spread this | ||
| 325 | data over local and general yaml files to eliminate duplication. | ||
| 326 | |||
| 327 | The main difference between MMM and the traditional style is that none | ||
| 328 | of the files that you create in your module directory are used in the | ||
| 329 | actual distribution. These files are all copied (or templated or | ||
| 330 | generated) into a C<cpan/> subdirectory and that is the final place for | ||
| 331 | testing and building the CPAN distribution. | ||
| 332 | |||
| 333 | You never need to edit the files in the C<cpan/> directory. Also you can | ||
| 334 | run all the common make commands: | ||
| 335 | |||
| 336 | > make | ||
| 337 | > make test | ||
| 338 | > make install | ||
| 339 | > make dist | ||
| 340 | |||
| 341 | without needing to cd into the C<cpan/> directory. The MMM Makefile has | ||
| 342 | targets to run these commands from the top level for you. | ||
| 343 | |||
| 344 | To get started, run the C<mmm> command in your new or existing module | ||
| 345 | directory. It will set up things for you and tell you what you need | ||
| 346 | to do next. | ||
| 347 | |||
| 348 | =head1 AUTHOR | ||
| 349 | |||
| 350 | Ingy döt Net <ingy@cpan.org> | ||
| 351 | |||
| 352 | =head1 COPYRIGHT | ||
| 353 | |||
| 354 | Copyright (c) 2008. Ingy döt Net. | ||
| 355 | |||
| 356 | This program is free software; you can redistribute it and/or modify it | ||
| 357 | under the same terms as Perl itself. | ||
| 358 | |||
| 359 | See http://www.perl.com/perl/misc/Artistic.html | ||
| 360 | |||
| 361 | =cut | ||
| 362 |