Dist-Zilla-Role-RepoFileInjector
view release on metacpan or search on metacpan
lib/Dist/Zilla/Role/RepoFileInjector.pm view on Meta::CPAN
$config->{+__PACKAGE__} = {
version => $VERSION,
allow_overwrite => ( $self->allow_overwrite ? 1 : 0 ),
repo_root => ( $self->_has_repo_root ? path($self->repo_root)->stringify : '.' ),
};
return $config;
};
sub add_repo_file
{
my ($self, $file) = @_;
my ($pkg, undef, $line) = caller;
if ($file->can('_set_added_by'))
{
$file->_set_added_by(sprintf("%s (%s line %s)", $self->plugin_name, $pkg, $line));
}
else
{
# as done in Dist::Zilla::Role::FileInjector 4.300039
$file->meta->get_attribute('added_by')->set_value(
$file,
sprintf("%s (%s line %s)", $self->plugin_name, $pkg, $line),
);
}
$self->log_debug([ 'adding file %s', $file->name ]);
$self->__push_repo_file($file);
}
sub write_repo_files
{
my $self = shift;
foreach my $file ($self->_repo_files)
{
my $filename = path($file->name);
my $abs_filename = $filename->is_relative
? path($self->repo_root)->child($file->name)
: $file->name;
if ($abs_filename->exists and $self->allow_overwrite)
{
$self->log_debug([ 'removing pre-existing %s', $abs_filename->stringify ]);
$abs_filename->remove;
}
$self->log_fatal([ '%s already exists (allow_overwrite = 0)', $abs_filename->stringify ])
if $abs_filename->exists;
$self->log_debug([ 'writing out %s%s', $file->name,
$filename->is_relative ? ' to ' . path($self->repo_root)->stringify : '' ]);
Carp::croak("attempted to write $filename multiple times") if $abs_filename->exists;
$abs_filename->touchpath;
# handle dzil v4 files by assuming no (or latin1) encoding
my $encoded_content = $file->can('encoded_content') ? $file->encoded_content : $file->content;
$abs_filename->spew_raw($encoded_content);
chmod $file->mode, "$abs_filename" or die "couldn't chmod $abs_filename: $!";
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dist::Zilla::Role::RepoFileInjector - Create files outside the build directory
=head1 VERSION
version 0.009
=head1 SYNOPSIS
In your F<dist.ini>:
[MyPlugin]
And in your plugin:
package Dist::Zilla::Plugin::MyPlugin;
use Moose;
with 'Dist::Zilla::Role::RepoFileInjector';
sub some_method {
...
$self->add_repo_file(Dist::Zilla::File::InMemory->new(...));
}
sub some_other_method {
...
$self->write_repo_files;
}
=head1 DESCRIPTION
This role is to be consumed by any plugin that plans to create files outside
the distribution.
=head1 ATTRIBUTES
=head2 repo_root
A string indicating the base directory where the file(s) are written, when
relative paths are provided. Defaults to L<Dist::Zilla/root>.
This attribute is available as an option of your plugin in F<dist.ini>.
=head2 allow_overwrite
A boolean indicating whether it is permissible for the file to already exist
(whereupon it is overwritten). When false, a fatal exception is thrown when
the file already exists.
( run in 2.559 seconds using v1.01-cache-2.11-cpan-98e64b0badf )