Dist-Zilla-Role-PluginBundle-Zilla

 view release on metacpan or  search on metacpan

lib/Dist/Zilla/Role/PluginBundle/Zilla.pm  view on Meta::CPAN

package Dist::Zilla::Role::PluginBundle::Zilla;

use 5.006;
use strict;
use warnings;

our $VERSION = '0.001';

use Moose::Role;
with 'Dist::Zilla::Role::PluginBundle';

use List::Util qw(first);
use Moose::Util::TypeConstraints qw(class_type);
use Scalar::Util qw(weaken);

use namespace::autoclean;

has name => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

has zilla => (
    is       => 'ro',
    isa      => class_type('Dist::Zilla'),
    init_arg => undef,
    weak_ref => 1,
    lazy     => 1,
    builder  => '_build_zilla',
);

has logger => (
    is      => 'ro',
    lazy    => 1,
    handles => [qw(log log_debug log_fatal)],
    default => sub {
        $_[0]->zilla->chrome->logger->proxy(
            {
                proxy_prefix => '[' . $_[0]->name . '] ',
            },
        );
    },
);

{
    my %zilla;

    around 'register_component' => sub {
        my ( $orig, $class, $name, $arg, $section ) = @_;

        my @sections       = $section->sequence->sections;
        my ($root_section) = first { $_->name eq q{_} } @sections;
        my $zilla          = $root_section->zilla;

        $zilla{$name} = $zilla;
        weaken( $zilla{$name} );

        return $class->$orig( $name, $arg, $section );
    };

    sub _build_zilla {
        my ($self) = @_;

        my $name = $self->name;

        my $zilla = delete $zilla{$name};
        return $zilla;
    }
}

sub BUILD {
    my ($self) = @_;

    # move the zilla object from our hash to the object
    $self->zilla;

    return;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Dist::Zilla::Role::PluginBundle::Zilla - adds the zilla object and the logger to your bundles

=head1 VERSION

Version 0.001

=head1 SYNOPSIS

=head2 Dist::Zilla::Role::PluginBundle

    package Dist::Zilla::PluginBundle::MyBundle;

    use Moose;
    with 'Dist::Zilla::Role::PluginBundle::Zilla';

    sub bundle_config {
        my ( $class, $section ) = @_;

        my $self = $class->new($section);
        $self->log('Hello from your friendly bundle! We are running in ' . $self->zilla->root);
        $self->log_fatal('Something went wrong...');

        return;
    }

=head2 Dist::Zilla::Role::PluginBundle::Easy



( run in 2.189 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )