-
Notifications
You must be signed in to change notification settings - Fork 0
/
midgen.pl
637 lines (478 loc) · 13.5 KB
/
midgen.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env perl
use 5.010001;
use strict;
use warnings;
our $VERSION = '0.05';
use English qw( -no_match_vars ); # Avoids regex performance penalty
local $OUTPUT_AUTOFLUSH = 1;
use Try::Tiny;
use autodie;
use Data::Printer {
caller_info => 1,
colored => 1,
};
use PPI;
use Module::CoreList;
use CPAN;
use constant {
BLANK => qq{ },
NONE => q{},
};
# use Carp::Always::Color;
#######
# menu system
use File::Spec;
use File::Find qw(find);
use File::Slurp qw(read_file write_file);
my @requires = ();
my %requires = ();
my %test_requires = ();
my @package_names;
my $package_name;
my @posiable_directories_to_search = ();
my @directories_to_search = ();
use Getopt::Long;
Getopt::Long::Configure('bundling');
use Pod::Usage;
my $help = 0;
my $base_parent = 0; # 1 true ignore perl base functions
my $core = 0; # show perl core modules as well
my $verbose = 0; # option variable with default value (false)
my @output = 'dsl';
my $mojo = 0; # 1 true ignore Mojo detection
my $noisy_children = 0; # unwanted noisy children
my $debug = 0; # lots of good stuff here :)
GetOptions(
'verbose|v' => \$verbose,
'core|c' => \$core,
'base|parent|b|p' => \$base_parent,
'help|h|?' => \$help,
'mojo|m' => \$mojo,
'output|o=s' => \@output,
'noisy_children|n' => \$noisy_children,
'debug|d' => sub { $core = 1; $verbose = 1; $base_parent = 1; $mojo = 1; $noisy_children = 1; $debug = 1; },
) or pod2usage(2);
pod2usage(1) if $help;
p @output if $debug;
sub output_format {
my $format = { dsl => 1, mi => 1, build => 1, };
try {
if ( $format->{ $output[-1] } ) {
return $output[-1];
} else {
return 'dsl';
}
};
}
my $format = output_format();
p $format if $debug;
# set up cpan bit's as well as being upto date
CPAN::HandleConfig->load;
CPAN::Shell::setup_output;
CPAN::Index->reload;
# End of Menu
#######
say 'START';
use Cwd;
my $cwd = cwd();
# Find Package name,
try {
find( \&first_package_name, File::Spec->catfile( $cwd, 'lib' ) );
};
p @package_names if $debug;
$package_name = $package_names[0];
say 'Package: ' . $package_names[0] if $verbose;
output_top($package_name);
# Find required modules
@posiable_directories_to_search = map { File::Spec->catfile( $cwd, $_ ) } qw( lib script );
@directories_to_search = ();
p @posiable_directories_to_search if $debug;
for my $directory (@posiable_directories_to_search) {
if ( defined -d $directory ) {
push @directories_to_search, $directory;
}
}
try {
find( \&requires, @directories_to_search );
};
p %requires if $debug;
remove_children( \%requires ) if ( !$verbose );
output_requires( 'requires', \%requires );
print "\n";
# Find test_required modules
@posiable_directories_to_search = File::Spec->catfile( $cwd, 't' );
@directories_to_search = ();
for my $directory (@posiable_directories_to_search) {
if ( defined -d $directory ) {
push @directories_to_search, $directory;
}
}
try {
find( \&test_requires, @directories_to_search );
};
output_requires( 'test_requires', \%test_requires );
output_bottom();
print "\n";
say 'END';
#
# Composed methods follow
##########################
sub first_package_name {
my $self = shift;
return if $_ !~ /[.]pm$/sxm;
my @items = ();
# Load a Document from a file
my $document = PPI::Document->new($_);
my $ppi_sp = $document->find('PPI::Statement::Package');
push @package_names, $ppi_sp->[0]->namespace;
return;
}
sub requires {
# return if $_ !~ /[.]p[lm]$/sxm;
# if ($verbose) {
# say 'looking for requires in: ' . $_;
# }
# my @items = ();
# Load a Document from a file
my $document = PPI::Document->new($_);
return unless ( defined $document->find('PPI::Statement::Package') || $document->find('PPI::Token::Comment') =~ /perl/ );
if ($verbose) {
say 'looking for requires in: ' . $_;
}
my @items = ();
my $includes = $document->find('PPI::Statement::Include');
# p $includes if $debug;
if ($includes) {
foreach my $include ( @{$includes} ) {
p $include if $debug;
next if $include->type eq 'no';
my @modules = $include->module;
p @modules if $debug;
if ( !$base_parent ) {
my @base_parent_modules = base_parent( $include->module, $include->content, $include->pragma );
if (@base_parent_modules) {
@modules = @base_parent_modules;
}
}
foreach my $module (@modules) {
p $module if $debug;
if ( !$core ) {
p $module if $debug;
# hash with core modules to process regardless
my $ignore_core = { 'File::Path' => 1, };
if ( !$ignore_core->{$module} ) {
next if Module::CoreList->first_release($module);
}
}
#deal with ''
next if $module eq NONE;
if ( $module =~ /^$package_name/sxm ) {
# don't include our own packages here
next;
}
if ( $module =~ /Mojo/sxm && !$mojo ) {
$module = 'Mojolicious';
}
if ( $module =~ /^Padre/sxm && $module !~ /^Padre::Plugin::/sxm ) {
# mark all Padre core as just Padre, for plugins
push @items, 'Padre';
$module = 'Padre';
} else {
push @items, $module;
}
my $mod;
my $mod_in_cpan = 0;
try {
$mod = CPAN::Shell->expand( 'Module', $module );
if ( $mod->cpan_version ne 'undef' ) {
# alociate current cpan version against module name
# $requires{$module} = $mod->cpan_version;
$mod_in_cpan = 1;
}
}
catch {
say 'caught ' . $module if $debug;
$requires{$module} = 0;
}
finally {
if ($mod_in_cpan) {
# alociate current cpan version against module name
$requires{$module} = $mod->cpan_version;
}
};
}
}
}
push @requires, @items;
p @requires if $debug;
return;
}
sub test_requires {
return if $_ !~ /[.]t|pm$/sxm;
if ($verbose) {
say 'looking for test_requires in: ' . $_;
}
my @items = ();
# Load a Document from a file
my $document = PPI::Document->new($_);
my $includes = $document->find('PPI::Statement::Include');
if ($includes) {
foreach my $include ( @{$includes} ) {
next if $include->type eq 'no';
my @modules = $include->module;
if ( !$base_parent ) {
my @base_parent_modules = base_parent( $include->module, $include->content, $include->pragma );
if (@base_parent_modules) {
@modules = @base_parent_modules;
}
}
foreach my $module (@modules) {
if ( !$core ) {
p $module if $debug;
# hash with core modules to process regardless
# don't ignore Test::More so as to get done_testing mst++
my $ignore_core = { 'Test::More' => 1, };
if ( !$ignore_core->{$module} ) {
next if Module::CoreList->first_release($module);
}
}
#deal with ''
next if $module eq NONE;
if ( $module =~ /^$package_name/sxm ) {
# don't include our own packages here
next;
}
if ( $module =~ /Mojo/sxm && !$mojo ) {
$module = 'Mojolicious';
}
if ( $module =~ /^Padre/sxm && $module !~ /^Padre::Plugin::/sxm ) {
# mark all Padre core as just Padre, for plugins
push @items, 'Padre';
$module = 'Padre';
} else {
push @items, $module;
}
try {
my $mod = CPAN::Shell->expand( 'Module', $module );
if ($mod) {
# next if not defined $mod;
if ( $mod->cpan_version && !$requires{$module} ) {
$test_requires{$module} = $mod->cpan_version;
}
} else {
$module =~ s/^(\S+)::\S+/$1/;
$mod = CPAN::Shell->expand( 'Module', $module );
p $mod if $debug;
if ( $mod->cpan_version && !$requires{$module} ) {
$test_requires{$module} = $mod->cpan_version;
}
}
};
}
}
}
return;
}
sub base_parent {
my $module = shift;
my $content = shift;
my $pragma = shift;
my @modules = ();
if ( $module =~ /base|parent/sxm ) {
if ($verbose) {
say 'Info: check ' . $pragma . ' pragma: ';
say $content;
}
$content =~ s/^use (base|parent) //;
$content =~ s/^qw[\<|\(|\{|\[]\n?\t?\s*//;
$content =~ s/\s*[\>|\)|\}|\]];\n?\t?$//;
$content =~ s/(\n\t)/, /g;
@modules = split /, /, $content;
push @modules, $module;
p @modules if $debug;
}
return @modules;
}
sub output_top {
my $package = shift || return;
# Let's get the Module::Install::DSL current version
my $mod = CPAN::Shell->expand( 'Module', 'inc::Module::Install::DSL' );
given ($format) {
# when ('mi') {
# }
when ('dsl') {
print "\n";
say 'use inc::Module::Install::DSL ' . $mod->cpan_version . ';';
print "\n";
say 'all_from lib/' . $package . '.pm';
say 'requires_from lib/' . $package . '.pm';
}
# when ('build') {
# }
}
return;
}
sub output_requires {
my $title = shift || 'title missing';
my $required_ref = shift || return;
print "\n";
my $pm_length = 0;
foreach my $module_name ( sort keys %{$required_ref} ) {
if ( length $module_name > $pm_length ) {
$pm_length = length $module_name;
}
}
say $title . ' => {' if $format eq 'build';
foreach my $module_name ( sort keys %{$required_ref} ) {
given ($format) {
when ('mi') {
if ( $module_name =~ /^Win32/sxm ) {
my $sq_key = "'$module_name'";
printf "%s %-*s => '%s' if win32;\n", $title, $pm_length + 2, $sq_key,
$required_ref->{$module_name};
} else {
my $sq_key = "'$module_name'";
printf "%s %-*s => '%s';\n", $title, $pm_length + 2, $sq_key, $required_ref->{$module_name};
}
}
when ('dsl') {
if ( $module_name =~ /^Win32/sxm ) {
printf "%s %-*s %s if win32\n", $title, $pm_length, $module_name, $required_ref->{$module_name};
} else {
printf "%s %-*s %s\n", $title, $pm_length, $module_name, $required_ref->{$module_name};
}
}
when ('build') {
my $sq_key = "'$module_name'";
printf "\t %-*s => '%s',\n", $pm_length + 2, $sq_key, $required_ref->{$module_name};
}
}
}
say '},' if $format eq 'build';
return;
}
sub output_bottom {
given ($format) {
# when ('mi') {
# }
when ('dsl') {
if ($verbose) {
print "\n";
say '#ToDo you should consider completing the following';
say "homepage\t...";
say "bugtracker\t...";
say "repository\t...";
}
print "\n";
if ( defined -d './share' ) {
say 'install_share';
print "\n";
}
say 'no_index directory qw{ t xt eg share inc privinc }';
}
# when ('build') {
# }
}
return;
}
sub remove_children {
my $required_ref = shift || return;
my @sorted_modules;
foreach my $module_name ( sort keys %{$required_ref} ) {
push @sorted_modules, $module_name;
}
p @sorted_modules if $debug;
my $n = 0;
while ( $sorted_modules[$n] ) {
my $parent_name = $sorted_modules[$n];
my @p_score = split /::/, $parent_name;
my $parent_score = @p_score;
my $child_score;
if ( ( $n + 1 ) <= $#sorted_modules ) {
$n++;
# Use of implicit split to @_ is deprecated
my $child_name = $sorted_modules[$n];
$child_score = @{ [ split /::/, $child_name ] };
}
if ( $sorted_modules[$n] =~ /^$sorted_modules[$n-1]::/ ) {
# Checking for one degree of seperation ie A::B -> A::B::C is ok but A::B::C::D is not
if ( ( $parent_score + 1 ) == $child_score ) {
# Test for same version number
if ( $required_ref->{ $sorted_modules[ $n - 1 ] } eq $required_ref->{ $sorted_modules[$n] } ) {
say 'delete miscreant noisy children '
. $sorted_modules[$n] . ' ver '
. $required_ref->{ $sorted_modules[$n] }
if $noisy_children;
try {
delete $required_ref->{ $sorted_modules[$n] };
splice( @sorted_modules, $n, 1 );
$n--;
};
p @sorted_modules if $debug;
}
}
}
$n++ if ( $n == $#sorted_modules );
}
return;
}
exit(0);
__END__
=pod
=encoding utf8
=head1 NAME
midgen.pl - generate the requires and test requires for Makefile.PL using Module::Install::DSL
=head1 SYNOPSIS
midgen.pl [options]
Options:
-help brief help message
-output change format
-core show perl core modules
-verbose take a little peek as to what is going on
-base Don't check for base includes
-mojo Don't be Mojo friendly
-debug lots of stuff
=head1 OPTIONS
=over 4
=item B<--help or -h>
Print a brief help message and exits.
=item B<--output or -o>
By default we do 'dsl' -> Module::Include::DSL
midgen.pl -o dsl # Module::Include::DSL
midgen.pl -o mi # Module::Include
midgen.pl -o build # Build.PL
=item B<-core or -c>
* Shows modules that are in Perl core
* some modules have a version number eg; constant, Carp
* some have a version of 0 eg; strict, English
=item B<--verbose or -v>
Show file that are being checked
also show contents of base|parent check
=item B<--parent or -p>
alternative --base or -b
Turn Off - try to include the contents of base|parent modules as well
=item B<--mojo or -m>
Turn Off - the /Mojo/ to Mojolicious catch
=item B<--noisy_children or -n>
* Show a required modules noisy children, as we find them
=item B<--debug or -d>
equivalent of -cv and some :)
=back
=head1 DESCRIPTION
This started out as a way of generating the core for a Module::Install::DSL Makefile.PL, why DSL because it's nice and clean, so now I can generate the contents when I want, rather than as I add new use and require statments, and because adam kicked me :)
Change to root of package and run
midgen.pl
Now with a GetOps --help or -?
midgen.pl -?
=head1 AUTHORS
Kevin Dawson E<lt>bowtie@cpan.orgE<gt>
=head2 CONTRIBUTORS
none at present
=head1 COPYRIGHT
Copyright E<copy> 2012-2013 AUTHORS and CONTRIBUTORS as listed above.
=head1 LICENSE
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl 5 itself.
=head1 SEE ALSO
L<Perl::PrereqScanner>,
=cut