File Coverage

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   3000930 use 5.006; #06 -> [pragmas,our] 04 -> [ my for ]
  4         9  
2 4     4   13 use strict;
  4         4  
  4         69  
3 4     4   11 use warnings;
  4         10  
  4         214  
4              
5             package Dist::Zilla::Role::Tempdir;
6              
7             our $VERSION = '1.001002'; # TRIAL
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   422 use Moose::Role qw( around with );
  4         317353  
  4         24  
14 4     4   11317 use Path::Tiny qw(path);
  4         6891  
  4         215  
15 4     4   1424 use Dist::Zilla::Tempdir::Dir;
  4         9  
  4         140  
16 4     4   25 use Scalar::Util qw( blessed );
  4         5  
  4         231  
17 4     4   17 use namespace::autoclean;
  4         5  
  4         29  
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   499 no Moose::Role;
  4         4  
  4         88  
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 1528078 my ( $self, $code, $args ) = @_;
53              
54 7 50       26 $args = {} unless defined $args;
55       0     $code = sub { }
56 7 50       20 unless defined $code;
57              
58 7         266 my $tdir = Dist::Zilla::Tempdir::Dir->new( _tempdir_owner => blessed $self, );
59              
60 7         185 my $dzil = $self->zilla;
61              
62 7         33 for my $file ( @{ $dzil->files } ) {
  7         147  
63 14         84 $tdir->add_file($file);
64             }
65              
66 7         23 $tdir->run_in($code);
67              
68 7         23151 $tdir->update_input_files;
69 7         25 $tdir->update_disk_files;
70              
71 7         274 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.001002
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) 2016 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