Hi all!

As part of developing Test::Run, I maintain several CPAN modules and install 
them to a directory under my home-dir. Now, until today what I did was write 
a bash function to run the installation commands ("perl Makefile.PL 
PREFIX=$FOO", "make", "make test", make install", or the Module::Build 
equivalents), one by one for each of the modules. However, if one of the 
commands would have failed, it would still proceed.

A long time ago, I thought of a better idea in which I generate a makefile 
that will run the commands and break if one of them fails. Today, when I 
needed to update the list of installed modules, I decided to catch two birds 
with one stone, and wrote the makefile generator. I wrote it in Perl and it 
is included below. (it is licensed under the MIT X11 licence). 

Now here's how I'm using it. In my bash theme ([1]), I have:

<<<
__prepare_install_all_to_temp_makefile()
{
    (
        perl ~/bin/gen-perl-modules-inst-makefile.pl \
            --prefix="$inst_modules_dir" \
            -o "$modules_makefile" \
            --dir="$test_run" \
            --dir="$cmdline" \
            --dir="$modules_dir/plugins/backend/Test-Run-Plugin-ColorSummary" 
\
            
--dir="$modules_dir/plugins/cmdline/Test-Run-CmdLine-Plugin-ColorSummary"
    )
}
>>>

This is the function that calls the generator script to prepare the makefile.

And then I use this makefile by calling "make all" to build, test and install 
all the modules one by one, or "make $(pwd)" to build one of them.

I hope you'll find this piece of advice useful.

Regards,

        Shlomi Fish

[1] - http://www.onlamp.com/pub/a/onlamp/2006/02/02/bash_themes.html

Here's the script:

<<<<<<<<<<
#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;

my $o_fn = "-";
my $prefix = "/usr";
my @dirs;

GetOptions (
    "o=s" => \$o_fn,
    "prefix=s" => \$prefix,
    "dir=s\@" => [EMAIL PROTECTED],
);

my $text = "";

sub process_dir
{
    my $dir = shift;
    if (-e "$dir/Build.PL")
    {
        process_mb_dir($dir);
    }
    elsif (-e "$dir/Makefile.PL")
    {
        process_eumm_dir($dir);
    }
    else
    {
        die "Unknown install method for directory $dir.";
    }
}

sub process_eumm_dir
{
    my $dir = shift;
    handle_deps($dir, 
        [
            "perl Makefile.PL PREFIX=\"$prefix\"", 
            "make", 
            "make test", 
            "make install",
        ]
    );
}

sub process_mb_dir
{
    my $dir = shift;
    handle_deps($dir, 
        [
            "perl Build.PL", 
            "./Build", 
            "./Build test", 
            "./Build install prefix=\"$prefix\"",
        ]
    );
}

my $id_num = 1;

sub handle_deps
{
    my ($dir, $deps_ref) = @_;
    my @deps = reverse(@$deps_ref);
    my $id = "target" . ($id_num++);
    $text .= "${dir}: $id-step0\n\n";
    foreach my $i (0 .. $#deps)
    {
        $text .= "$id-step${i}: " . 
            (($i == $#deps) ? "" : ("$id-step" . ($i+1))) . 
            "\n";
        $text .= "\t(cd $dir && " . $deps[$i] . ")\n";
        $text .= "\n";
    }
}

foreach my $d (@dirs)
{
    process_dir($d);
}

if ($o_fn eq "-")
{
    open O, ">&STDOUT";
}
else
{
    open O, ">", $o_fn;
}
print O "all: ", join(" ", @dirs) . "\n\n";
print O $text;
close(O);
>>>>>>>>>>
-- 

---------------------------------------------------------------------
Shlomi Fish      [EMAIL PROTECTED]
Homepage:        http://www.shlomifish.org/

95% of the programmers consider 95% of the code they did not write, in the
bottom 5%.

Reply via email to