Makefile-Update

 view release on metacpan or  search on metacpan

lib/Makefile/Update/Bakefile0.pm  view on Meta::CPAN

package Makefile::Update::Bakefile0;
# ABSTRACT: Update bakefile-0.x files list.

use Exporter qw(import);
our @EXPORT = qw(update_bakefile_0);

use strict;
use warnings;

our $VERSION = '0.4'; # VERSION



sub update_bakefile_0
{
    my ($in, $out, $vars) = @_;

    # Variable whose contents is being currently replaced.
    my $var;

    # Hash with files defined for the specified variable as keys and 0 or 1
    # depending on whether we have seen them in the input file as values.
    my %files;

    # Set to 1 if we made any changes.
    my $changed = 0;
    while (<$in>) {
        chomp;

        if (/<set var="(\w+)" hints="files">/ && exists $vars->{$1}) {
            $var = $1;
            %files = map { $_ => 0 } @{$vars->{$var}};
        } elsif (defined $var) {
            local $_ = $_;
            s/<!-- .* -->//;
            s/^\s+//;
            s/\s+$//;
            s{<if [^>]+>}{};
            s{</if>}{};
            if (m{</set>}) {
                # Check if we have any new files.
                #
                # TODO Insert them in alphabetical order.
                while (my ($file, $seen) = each(%files)) {
                    if (!$seen) {
                        # This file was wasn't present in the input, add it.
                        # TODO Use proper indentation.
                        print $out "    $file\n";

                        $changed = 1;
                    }
                }

                undef $var;
            } elsif ($_) {
                if (not exists $files{$_}) {
                    # This file was removed.
                    $changed = 1;
                    next;
                }

                if ($files{$_}) {
                    warn qq{Duplicate file "$_" in the definition of the } .
                         qq{variable "$var" at line $.\n}
                } else {
                    $files{$_} = 1;
                }
            }
        }

        print $out "$_\n";
    }

    $changed



( run in 1.254 second using v1.01-cache-2.11-cpan-39bf76dae61 )