LCFG-Build-Tools

 view release on metacpan or  search on metacpan

lib/LCFG/Build/Tool/CheckMacros.pm  view on Meta::CPAN

package LCFG::Build::Tool::CheckMacros;    # -*-perl-*-
use strict;
use warnings;

# $Id: CheckMacros.pm.in 35684 2019-02-28 10:04:54Z squinney@INF.ED.AC.UK $
# $Source: /var/cvs/dice/LCFG-Build-Tools/lib/LCFG/Build/Tool/CheckMacros.pm.in,v $
# $Revision: 35684 $
# $HeadURL: https://svn.lcfg.org/svn/source/tags/LCFG-Build-Tools/LCFG_Build_Tools_0_9_30/lib/LCFG/Build/Tool/CheckMacros.pm.in $
# $Date: 2019-02-28 10:04:54 +0000 (Thu, 28 Feb 2019) $

our $VERSION = '0.9.30';

use File::Spec ();
use File::Temp ();
use IO::File ();
use LCFG::Build::Utils;

use Moose;

extends 'LCFG::Build::Tool';

# We do not want this option for these commands so use an override.

has '+resultsdir' => ( traits => ['NoGetopt'] );

has 'fix_deprecated' => (
    is            => 'rw',
    isa           => 'Bool',
    default       => 0,
    documentation => 'Replace deprecated macros with new-style names',
);

__PACKAGE__->meta->make_immutable;

sub abstract {
    return q{Check for correct macro usage};
}

my %messages = (
    unknown    => 'Use of unknown macro',
    deprecated => 'Use of deprecated macro',
    linux      => 'Use of linux-only macro',
    macosx     => 'Use of MacOSX-only macro',
    buildtime  => 'Use of build-time-only macro',
);

my %basic = map { $_ => 'basic' } qw(
    LCFG_ABSTRACT
    LCFG_NAME
    LCFG_FULLNAME
    LCFG_VERSION
    LCFG_VERSION
    LCFG_PERL_VERSION
    LCFG_RELEASE
    LCFG_SCHEMA
    LCFG_VENDOR
    LCFG_GROUP
    LCFG_AUTHOR
    LCFG_PLATFORMS
    LCFG_DATE
    LCFG_LICENSE
    LCFG_TARNAME
    LCFG_CHANGELOG
    BOOTSTAMP
    INITDIR
    LCFGBIB
    LCFGBIN
    LCFGCLIENTDEF

lib/LCFG/Build/Tool/CheckMacros.pm  view on Meta::CPAN

            for my $entry (@found) {
                if ( $entry =~ m/^(.*?):\d+$/ ) {
                    $files_using_deprecated{$1} = 1;
                }
            }
        }

        if ( 0 == scalar keys %files_using_deprecated ) {
            $self->log("No deprecated macro usage found.");
        }

        for my $file ( sort keys %files_using_deprecated ) {
            $self->log("Fixing deprecated macros in $file");

            my $path = File::Spec->catfile( $dir, $file );
            my $in = IO::File->new( $path, 'r' )
                or $self->fail("Could not open $path: $!");

            my $tmp = File::Temp->new( UNLINK => 0,
                                       DIR    => $dir );

            while ( defined( my $line = <$in> ) ) {

                # Find a unique list of macros in this line

                my @macros = ( $line =~ m/\@(\w+)\@/g );
                my %macros = map { $_ => 1 } @macros;
                @macros = keys %macros;

                for my $macro (@macros) {
                    if ( exists $deprecated{$macro} ) {
                        $line =~ s/\@\Q$macro\E\@/\@$deprecated{$macro}\@/g;
                    }
                }

                print {$tmp} $line;
            }

            my $out = $tmp->filename;
            $tmp->close or $self->fail("Could not close $out: $!");

            if ( !$self->dryrun ) {
                rename $out, $path
                    or $self->fail("Could not move $out to $path: $!");
            }
            else {
                unlink $out; # Just tidying
            }
        }
    }

    return;
}

no Moose;
1;
__END__

=head1 NAME

    LCFG::Build::Tool::CheckMacros - LCFG software packaging tool

=head1 VERSION

    This documentation refers to LCFG::Build::Tool::CheckMacros version 0.9.30

=head1 SYNOPSIS

    my $tool = LCFG::Build::Tool::CheckMacros->new( dir => '.' );

    $tool->execute;

    my $tool2 = LCFG::Build::Tool::CheckMacros->new_with_options();

    $tool2->execute;

=head1 DESCRIPTION

This module provides software release tools for the LCFG build
suite.

The LCFG build tools have support for autoconf-style (e.g. @FOO@)
macro substitution when building packages. There is a set of macros
which are built-in and the list can be extended by the user. This is a
tool for checking substitution variable usage to help spot potential
problems. It prints out a list of warnings, ordered by importance,
along with the file names and line numbers of where the macros are
used.

More information on the LCFG build tools is available from the website
http://www.lcfg.org/doc/buildtools/

=head1 ATTRIBUTES

The following attributes are modifiable via the command-line (i.e. via
@ARGV) as well as the normal way when the Tool object is
created. Unless stated the options take strings as arguments and can
be used like C<--foo=bar>. Boolean options can be expressed as either
C<--foo> or C<--no-foo> to signify true and false values.

=over 4

=item fix_deprecated

A boolean value which indicates whether any deprecated macros that are
found in the files scanned should be automatically replaced with their
modern equivalents.

=item dryrun

A boolean value which indicates whether actions which permanently
alter the contents of files should be carried out. The default value
is false (0). When running in dry-run mode various you will typically
get extra output to the screen showing what would have been done.

=item quiet

A boolean value which indicates whether the actions should attempt to
be quieter. The default value is false (0).

=item dir

The path of the project directory which contains the software for
which you want to create a release. If this is not specified then a
default value of the current directory (.) will be used. This
directory must already contain the LCFG build metadata file (lcfg.yml)
for the software.

=back

The following methods are not modifiable by the command-line, they are
however directly modifiable via the Tool object if
necessary. Typically you will only need to query these attributes,



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