-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_cpanfile.pl
executable file
·97 lines (67 loc) · 2.33 KB
/
generate_cpanfile.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
#!/usr/bin/env perl
use strict;
use warnings;
use Module::ExtractUse;
use Data::Dump;
use Pod::Usage;
my $p = Module::ExtractUse->new;
# Specify the directories to scan for dependencies
my @directories = ('.', 'lib');
# get a list of all .pl and .pm files located in the directories
my @files = map { glob "$_/*.pm $_/*.pl" } @directories;
print "Analyzing files: @files\n";
# get the module names for files in lib dir
my @local_modules = map { glob "$_/*.pm" } 'lib';
# strip prefix lib/ and suffix .pm
for (@local_modules) {
s/^lib\///;
s/\.pm$//;
}
print "Ignoring local modules: @local_modules\n\n";
my %dependencies;
foreach my $file (@files) {
# Extract the modules used in the file
$p->extract_use($file);
print "File = $file\n";
my $used = $p->used;
# remove from used if modules is in local_modules
for my $module (@local_modules) {
delete $used->{$module};
}
# ignore internal perl modules like lib, strict, warnings, etc.
delete $used->{$_} for qw(lib strict warnings utf8 vars subs feature);
# append used modules to the dependencies hash
$dependencies{$_} = 1 for keys %$used;
}
#dd \%dependencies;
# Generate the cpanfile content
my $cpanfile_content = '';
foreach my $module (sort keys %dependencies) {
$cpanfile_content .= "requires '$module';\n";
}
# Write the cpanfile
open my $cpanfile, '>', 'cpanfile' or die "Cannot open cpanfile: $!";
print "\nGenerated cpanfile:\n$cpanfile_content\n";
print $cpanfile $cpanfile_content;
close $cpanfile;
print "Modules can be installed using 'cpanm --installdeps .'\n";
print "Or using carton: 'carton install'\n";
print "Note: to run with carton: 'carton exec perl your_script.pl' to run the script\n";
# Display the perldocs if the user runs the script with the --help or -h option
pod2usage(1) if @ARGV == 1 and $ARGV[0] =~ /^--?h(?:elp)?$/;
__END__
=head1 NAME
generate_cpanfile.pl - Generate a cpanfile from the modules used in a Perl script or module
=head1 SYNOPSIS
perl generate_cpanfile.pl
=head1 DESCRIPTION
This script scans the current directory and the 'lib' directory for .pl and .pm files, extracts the modules used in these files, and generates a cpanfile listing these modules.
=head1 VERSION
1.0.0
=head1 RELEASE DATE
2024-05-17
=head1 AUTHOR
Eric Blue <ericblue76@gmail.com>
Website: https://eric-blue.com
=cut
```