root/lang/perl/MENTA/trunk/bin/install-lib.pl @ 24116

Revision 24116, 2.4 kB (checked in by tokuhirom, 5 years ago)

強制上書きオプションつけた

Line 
1use strict;
2use warnings;
3use CPAN;
4use File::Temp qw/tempdir tempfile/;
5use File::Spec::Functions;
6use FindBin;
7use Cwd;
8use Data::Dumper;
9use Module::CoreList;
10use Getopt::Long;
11
12my %installed;
13my %optional_args = (
14    'version'          => '--perl_only',
15    'List::MoreUtils'  => '-pm',
16    'Params::Validate' => '--pm',
17    'DateTime'         => '--pm',
18);
19my %skip_packages = map { $_ => 1 } qw/Module::Build/;
20my $target_version = '5.008001';
21my $outdir;
22my $dstdir;
23my $pkg;
24my $overwrite = 0;
25my $cwd = getcwd();
26
27&main; exit;
28
29# utils
30sub Path::Class::Dir::basename { shift->dir_list(-1, 1) }
31
32sub main {
33    # process args
34    GetOptions(
35        "version=f" => \$target_version,
36        "overwrite" => \$overwrite,
37    );
38    unless (@ARGV == 2) {
39        die "Usage: $0 Acme::Hello extlib/";
40    }
41    ($pkg, $dstdir) = @ARGV;
42
43    # init
44    my $tmpdir = tempdir(CLENAUP => 1);
45    $outdir = catfile($tmpdir, "outputdir");
46    mkdir -d $outdir;
47    CPAN::HandleConfig->load;
48    CPAN::Shell::setup_output;
49
50    # install
51    install_pkg($pkg);
52
53    unless (%installed) {
54        warn "no modules for install";
55        return;
56    }
57
58    chdir $cwd;
59
60    # copy to dst dir
61    my $outlibdir = catfile($outdir, 'lib', 'perl5') . '/';
62    print "sync $outlibdir => $dstdir\n";
63    system qw/rsync --verbose --recursive/,  $outlibdir, $dstdir;
64}
65
66sub install_pkg {
67    my $pkg = shift;
68    return if $installed{$pkg};
69    if ($Module::CoreList::version{$target_version}{$pkg}) {
70        print "skip $pkg(standard lib)\n";
71        return;
72    }
73    if ($skip_packages{$pkg}) {
74        print "skip $pkg(build util?)\n";
75        return;
76    }
77    unless ($overwrite) {
78        my $path = $pkg;
79        $path =~ s{::}{/}g;
80        $path = catfile($cwd, $dstdir, "${path}.pm");
81        warn $path;
82        if (-f $path) {
83            print "skip $pkg(already installed)\n";
84            return;
85        }
86    }
87
88    $installed{$pkg}++;
89    local $CPAN::Config->{histfile}   = tempfile(CLEANUP => 1);
90    local $CPAN::Config->{makepl_arg} = "INSTALL_BASE=$outdir " . ($optional_args{$pkg} ? $optional_args{$pkg} : '');
91    local $CPAN::Config->{mbuildpl_arg} = "--install_base=$outdir";
92
93    my $mod = CPAN::Shell->expand("Module", $pkg) or die "cannot find $pkg";
94    my $dist = $mod->distribution;
95    $dist->make;
96    if (my $requires = $dist->prereq_pm) {
97        for my $req (keys %{$requires->{requires}}) {
98            install_pkg($req);
99        }
100    }
101    $dist->install();
102}
103
Note: See TracBrowser for help on using the browser.