File Coverage

blib/lib/Dist/Zilla/Role/Tempdir.pm
Criterion Covered Total %
statement 38 38 100.0
branch 2 4 50.0
condition n/a
subroutine 10 11 90.9
pod 1 1 100.0
total 51 54 94.4


line stmt bran cond sub pod time code
1 4     4   3102099 use 5.006; #06 -> [pragmas,our] 04 -> [ my for ]
  4         11  
2 4     4   57 use strict;
  4         6  
  4         90  
3 4     4   21 use warnings;
  4         5  
  4         273  
4              
5             package Dist::Zilla::Role::Tempdir;
6              
7             our $VERSION = '1.001003';
8              
9             # ABSTRACT: Shell Out and collect the result in a DZ plug-in.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 4     4   394 use Moose::Role qw( around with );
  4         301583  
  4         28  
14 4     4   11677 use Path::Tiny qw(path);
  4         7706  
  4         230  
15 4     4   1383 use Dist::Zilla::Tempdir::Dir;
  4         9  
  4         163  
16 4     4   27 use Scalar::Util qw( blessed );
  4         5  
  4         242  
17 4     4   16 use namespace::autoclean;
  4         5  
  4         37  
18              
19             around dump_config => sub {
20             my ( $orig, $self, @args ) = @_;
21             my $config = $self->$orig(@args);
22             my $payload = $config->{ +__PACKAGE__ } = {};
23             $payload->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION;
24             return $config;
25             };
26              
27 4     4   555 no Moose::Role;
  4         5  
  4         96  
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51             sub capture_tempdir {
52 7     7 1 1925012 my ( $self, $code, $args ) = @_;
53              
54 7 50       30 $args = {} unless defined $args;
55       0     $code = sub { }
56 7 50       27 unless defined $code;
57              
58 7         300 my $tdir = Dist::Zilla::Tempdir::Dir->new( _tempdir_owner => blessed $self, );
59              
60 7         209 my $dzil = $self->zilla;
61              
62 7         42 for my $file ( @{ $dzil->files } ) {
  7         170  
63 14         101 $tdir->add_file($file);
64             }
65              
66 7         33 $tdir->run_in($code);
67              
68 7         36030 $tdir->update_input_files;
69 7         31 $tdir->update_disk_files;
70              
71 7         253 return $tdir->files;
72             }
73              
74             1;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             Dist::Zilla::Role::Tempdir - Shell Out and collect the result in a DZ plug-in.
85              
86             =head1 VERSION
87              
88             version 1.001003
89              
90             =head1 SYNOPSIS
91              
92             package #
93             Dist::Zilla::Plugin::FooBar;
94              
95             use Moose;
96             with 'Dist::Zilla::Role::FileInjector';
97             with 'Dist::Zilla::Role::InstallTool';
98             with 'Dist::Zilla::Role::Tempdir';
99              
100             sub setup_installer {
101             my ( $self, $arg ) = @_ ;
102              
103             my ( @generated_files ) = $self->capture_tempdir(sub{
104             system( $somecommand );
105             });
106              
107             for ( @generated_files ) {
108             if( $_->is_new && $_->name =~ qr/someregex/ ){
109             $self->add_file( $_->file );
110             }
111             }
112             }
113              
114             This role is a convenience role for factoring into other plug-ins to use the power of Unix
115             in any plug-in.
116              
117             If for whatever reason you need to shell out and run your own app that is not Perl ( i.e.: Java )
118             to go through the code and make modifications, produce documentation, etc, then this role is for you.
119              
120             Important to note however, this role B<ONLY> deals with getting C<Dist::Zilla>'s state written out to disk,
121             executing your given arbitrary code, and then collecting the results. At no point does it attempt to re-inject
122             those changes back into L<< C<Dist::Zilla>|Dist::Zilla >>. That is left as an exercise to the plug-in developer.
123              
124             =head1 METHODS
125              
126             =head2 capture_tempdir
127              
128             Creates a temporary and dumps the current state of Dist::Zilla's files into it.
129              
130             Runs the specified code sub C<chdir>'ed into that C<tmpdir>, and captures the changed files.
131              
132             my ( @array ) = $self->capture_tempdir(sub{
133              
134             });
135              
136             Response is an array of L<< C<::Tempdir::Item>|Dist::Zilla::Tempdir::Item >>
137              
138             [ bless( { name => 'file/Name/Here' ,
139             status => 'O' # O = Original, N = New, M = Modified, D = Deleted
140             file => Dist::Zilla::Role::File object
141             }, 'Dist::Zilla::Tempdir::Item' ) , bless ( ... ) ..... ]
142              
143             Make sure to look at L<< C<Dist::Zilla::Tempdir::Item>|Dist::Zilla::Tempdir::Item >> for usage.
144              
145             =head1 SEE ALSO
146              
147             =over 4
148              
149             =item * L<< C<Dist::Zilla::Tempdir::Item>|Dist::Zilla::Tempdir::Item >>
150              
151             =back
152              
153             =head1 AUTHOR
154              
155             Kent Fredric <kentnl@cpan.org>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
160              
161             This is free software; you can redistribute it and/or modify it under
162             the same terms as the Perl 5 programming language system itself.
163              
164             =cut