File Coverage

lib/Dist/Zilla/Role/Tempdir.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


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