File Coverage

blib/lib/Dist/Zilla/Tempdir/Dir.pm
Criterion Covered Total %
statement 82 96 85.4
branch 9 18 50.0
condition n/a
subroutine 19 21 90.4
pod 8 8 100.0
total 118 143 82.5


line stmt bran cond sub pod time code
1 5     5   462 use 5.006; # our
  5         12  
2 5     5   19 use strict;
  5         6  
  5         85  
3 5     5   14 use warnings;
  5         7  
  5         276  
4              
5             package Dist::Zilla::Tempdir::Dir;
6              
7             our $VERSION = '1.001003';
8              
9             # ABSTRACT: A temporary directory with a collection of item states
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13              
14              
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25 5     5   515 use Moose qw( has );
  5         302567  
  5         40  
26 5     5   17783 use Carp qw( croak );
  5         6  
  5         229  
27 5     5   2174 use File::chdir;
  5         11140  
  5         469  
28 5     5   1739 use Dist::Zilla::Tempdir::Item::State;
  5         13  
  5         175  
29 5     5   2304 use Dist::Zilla::Tempdir::Item;
  5         12  
  5         173  
30 5     5   2878 use Path::Iterator::Rule;
  5         38223  
  5         137  
31 5     5   2229 use Dist::Zilla::File::InMemory;
  5         755481  
  5         256  
32 5     5   33 use Path::Tiny qw(path);
  5         6  
  5         5341  
33              
34             has '_tempdir' => (
35             is => ro =>,
36             lazy_build => 1,
37             );
38              
39             has '_tempdir_owner' => (
40             is => ro =>,
41             predicate => '_has_tempdir_owner',
42             );
43              
44             sub _build__tempdir {
45 7     7   9 my ($self) = @_;
46              
47 7         18 my $template = 'DZ_R_Tempdir_';
48 7 50       258 if ( $self->_has_tempdir_owner ) {
49 7         209 my $owner = $self->_tempdir_owner;
50 7         57 $owner =~ s/[^[:alpha:]\d]+/_/xmsg;
51 7         19 $template .= $owner . '_';
52             }
53 7         17 $template .= 'XXXXXX';
54 7         60 return Path::Tiny->tempdir( TEMPLATE => $template );
55             }
56              
57             has '_input_files' => (
58             isa => 'HashRef',
59             traits => [qw( Hash )],
60             is => ro =>,
61             lazy => 1,
62             default => sub { {} },
63             handles => {
64             '_set_input_file' => 'set',
65             '_all_input_files' => 'values',
66             '_has_input_file' => 'exists',
67             },
68             );
69              
70              
71              
72              
73              
74              
75              
76             has '_output_files' => (
77             isa => 'HashRef',
78             traits => [qw( Hash )],
79             is => ro =>,
80             lazy => 1,
81             default => sub { {} },
82             handles => {
83             '_set_output_file' => 'set',
84             'files' => 'values',
85             },
86             );
87              
88              
89              
90              
91              
92              
93              
94              
95              
96              
97             sub add_file {
98 14     14 1 20 my ( $self, $file ) = @_;
99 14         441 my $state = Dist::Zilla::Tempdir::Item::State->new(
100             file => $file,
101             storage_prefix => $self->_tempdir,
102             );
103 14         45 $state->write_out;
104 14         56 $self->_set_input_file( $file->name, $state );
105 14         40 return;
106             }
107              
108              
109              
110              
111              
112              
113              
114              
115              
116              
117             sub update_input_file {
118 14     14 1 16 my ( $self, $file ) = @_;
119              
120 14         83 my $update_item = Dist::Zilla::Tempdir::Item->new( name => $file->name, file => $file->file, );
121 14         71 $update_item->set_original;
122              
123 14 100       53 if ( not $file->on_disk ) {
    100          
124 2         69 $update_item->set_deleted;
125             }
126             elsif ( $file->on_disk_changed ) {
127 2         24 $update_item->set_modified;
128 2         10 my %params = ( name => $file->name, content => $file->new_content );
129 2 50       30 if ( Dist::Zilla::File::InMemory->can('encoded_content') ) {
130 2         7 $params{encoded_content} = delete $params{content};
131             }
132 2         59 $update_item->file( Dist::Zilla::File::InMemory->new(%params) );
133             }
134 14         51 $self->_set_output_file( $file->name, $update_item );
135 14         27 return;
136             }
137              
138              
139              
140              
141              
142              
143              
144              
145              
146             sub update_disk_file {
147 4     4 1 6 my ( $self, $fullname ) = @_;
148 4         8 my $fullpath = path($fullname);
149 4         149 my $shortname = $fullpath->relative( $self->_tempdir );
150              
151 4         472 my %params = ( name => "$shortname", content => $fullpath->slurp_raw );
152 4 50       429 if ( Dist::Zilla::File::InMemory->can('encoded_content') ) {
153 4         11 $params{encoded_content} = delete $params{content};
154             }
155 4         10 my $item = Dist::Zilla::Tempdir::Item->new(
156             name => "$shortname",
157             file => Dist::Zilla::File::InMemory->new(%params),
158             );
159 4         19 $item->set_new;
160 4         11 $self->_set_output_file( "$shortname", $item );
161 4         14 return;
162             }
163              
164              
165              
166              
167              
168              
169              
170              
171              
172             sub update_input_files {
173 7     7 1 20 my ($self) = @_;
174 7         502 for my $file ( $self->_all_input_files ) {
175 14         61 $self->update_input_file($file);
176             }
177 7         14 return;
178             }
179              
180              
181              
182              
183              
184              
185              
186              
187              
188              
189             sub update_disk_files {
190 7     7 1 10 my ($self) = @_;
191 7         88 for my $filename ( Path::Iterator::Rule->new->file->all( $self->_tempdir->stringify ) ) {
192 16 100       4441 next if $self->_has_input_file( path($filename)->relative( $self->_tempdir ) );
193 4         28 $self->update_disk_file($filename);
194             }
195 7         207 return;
196             }
197              
198              
199              
200              
201              
202              
203              
204              
205              
206              
207              
208             sub run_in {
209 7     7 1 12 my ( $self, $code ) = @_;
210             ## no critic ( ProhibitLocalVars )
211 7         224 local $CWD = $self->_tempdir->stringify;
212 7         329 return $code->($self);
213             }
214              
215              
216              
217              
218              
219              
220              
221              
222              
223              
224              
225              
226              
227              
228              
229              
230              
231             sub keepalive {
232 0     0 1   my $nargs = my ( $self, $keep ) = @_;
233              
234 0           my $path = $self->_tempdir;
235              
236 0 0         if ( $nargs < 2 ) {
237 0           return $path;
238             }
239              
240 0 0         if ($keep) {
241 0           $path->[Path::Tiny::TEMP]->unlink_on_destroy(0);
242             }
243             else {
244 0           $path->[Path::Tiny::TEMP]->unlink_on_destroy(1);
245             }
246 0           return $path;
247             }
248              
249              
250              
251              
252              
253              
254              
255              
256              
257              
258              
259             sub keepalive_fail {
260 0     0 1   my ( $self, $message ) = @_;
261              
262 0 0         if ( not $message ) {
263 0           $message = q[];
264             }
265             else {
266 0           $message .= qq[\n];
267             }
268 0           $message .= q[Role::Tempdir's scratch directory preserved at ] . $self->keepalive(1);
269 0           croak $message;
270             }
271              
272 5     5   25 no Moose;
  5         8  
  5         29  
273             __PACKAGE__->meta->make_immutable;
274              
275             1;
276              
277             __END__
278              
279             =pod
280              
281             =encoding UTF-8
282              
283             =head1 NAME
284              
285             Dist::Zilla::Tempdir::Dir - A temporary directory with a collection of item states
286              
287             =head1 VERSION
288              
289             version 1.001003
290              
291             =head1 SYNOPSIS
292              
293             my $dir = Dist::Zilla::Tempdir::Dir->new();
294             $dir->add_file( $zilla_file );
295             $dir->run_in(sub { });
296             $dir->update_input_files;
297             $dir->update_disk_files;
298              
299             my @file_states = $dir->files();
300              
301             =head1 METHODS
302              
303             =head2 C<files>
304              
305             Returns a list of L<< C<Dist::Zilla::Tempdir::Item>|Dist::Zilla::Tempdir::Item >>
306              
307             =head2 C<add_file>
308              
309             $dir->add_file( $dzil_file );
310              
311             Adds C<$dzil_file> to the named temporary directory, written out to disk, and records
312             it internally as an "original" file.
313              
314             =head2 C<update_input_file>
315              
316             $dir->update_input_file( $dzil_file );
317              
318             Refreshes the C<$dzil_file> from its written out context, determining if that file has been changed since
319             addition or not, recording the relevant data for C<< ->files >>
320              
321             =head2 C<update_disk_file>
322              
323             $dir->update_disk_file( $disk_path );
324              
325             Assume C<$disk_path> is a path of a B<NEW> file and record it in C<< ->files >>
326              
327             =head2 C<update_input_files>
328              
329             $dir->update_input_files
330              
331             Refresh the state of all written out files and record them ready for C<< ->files >>
332              
333             =head2 C<update_disk_files>
334              
335             $dir->update_disk_files
336              
337             Scan the temporary directory for files that weren't added as an C<input> file, and record their status
338             and information ready for C<< ->files >>
339              
340             =head2 C<run_in>
341              
342             my $rval = $dir->run_in(sub {
343             return 1;
344             });
345              
346             Enter the temporary directory and run the passed code block, which is assumed to be creating/modifying/deleting files.
347              
348             =head2 C<keepalive>
349              
350             Utility method: Marks the temporary directory for preservation.
351              
352             $dir->keepalive() # simply returns the path to the tempdir
353             $dir->keepalive(1) # mark for retention
354             $dir->keepalive(0) # mark for erasure
355              
356             This is mostly an insane glue layer for
357              
358             $dir->_tempdir->[Path::Tiny::TEMP]->unlink_on_destroy($x)
359              
360             Except the insanity of poking too many internal guts is well encapsulated.
361              
362             =head2 C<keepalive_fail>
363              
364             A utility method to invoke a croak (heh) that preserves the scratch directory, and tells
365             the croak recipient where to find it.
366              
367             $dir->keepalive_fail();
368             $dir->keepalive_fail("Some Diagnostic Reason");
369              
370             =head1 AUTHOR
371              
372             Kent Fredric <kentnl@cpan.org>
373              
374             =head1 COPYRIGHT AND LICENSE
375              
376             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
377              
378             This is free software; you can redistribute it and/or modify it under
379             the same terms as the Perl 5 programming language system itself.
380              
381             =cut