File Coverage

blib/lib/Stepford/Role/Step/FileGenerator.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             package Stepford::Role::Step::FileGenerator;
2              
3 32     32   387756 use strict;
  32         103  
  32         1066  
4 32     32   165 use warnings;
  32         190  
  32         805  
5 32     32   203 use namespace::autoclean;
  32         52  
  32         236  
6              
7             our $VERSION = '0.006001';
8              
9 32     32   2706 use Carp qw( croak );
  32         103  
  32         1961  
10 32     32   2150 use List::AllUtils qw( any max );
  32         33626  
  32         2555  
11              
12             # Sadly, there's no (sane) way to make Path::Class::File use this
13 32     32   3902 use Time::HiRes 1.9726 qw( stat );
  32         10142  
  32         473  
14              
15 32     32   5092 use Moose::Role;
  32         76  
  32         545  
16              
17             with 'Stepford::Role::Step';
18              
19       470 1   sub BUILD { }
20             before BUILD => sub {
21             my $self = shift;
22              
23             my @not_files = sort map { $_->name } grep {
24             !( $_->has_type_constraint && _is_a_file_type( $_->type_constraint ) )
25             } $self->productions;
26              
27             croak 'The '
28             . ( ref $self )
29             . ' class consumed the Stepford::Role::Step::FileGenerator role but contains'
30             . " the following productions which are not a supported file type: @not_files"
31             if @not_files;
32              
33             return;
34             };
35              
36             sub _is_a_file_type {
37 593     593   30232 my $type = shift;
38              
39 593     609   5214 return any { $type->is_a_type_of($_) } qw(
  609         24036  
40             MooseX::Types::Path::Class::File
41             MooseX::Types::Path::Tiny::File
42             MooseX::Types::Path::Tiny::AbsFile
43             MooseX::Types::Path::Tiny::Path
44             MooseX::Types::Path::Tiny::AbsPath
45             );
46             }
47              
48             sub last_run_time {
49 485     485 1 1115 my $self = shift;
50              
51             my @production_files
52 485         2096 = map { $self->${ \( $_->get_read_method ) } } $self->productions;
  604         130834  
  604         5208  
53              
54 485 100   556   119880 return undef if any { !-f } @production_files;
  556         7232  
55              
56 290         16567 my @times = map { ( stat $_ )[9] } @production_files;
  361         5213  
57              
58 290         23540 return max @times;
59             }
60              
61             1;
62              
63             # ABSTRACT: A role for steps that generate files
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Stepford::Role::Step::FileGenerator - A role for steps that generate files
74              
75             =head1 VERSION
76              
77             version 0.006001
78              
79             =head1 DESCRIPTION
80              
81             This role consumes the L<Stepford::Role::Step> role and adds some additional
82             functionality specific to generating files.
83              
84             =head1 METHODS
85              
86             This role provides the following methods:
87              
88             =head2 $step->BUILD
89              
90             This method adds a wrapper to the BUILD method which checks that all of the
91             class's productions are of the C<File> type provided by
92             L<MooseX::Types::Path::Class> or one of the L<MooseX::Types::Path::Tiny> file
93             types. The attributes can also be subtypes of these types.
94              
95             This check may be changed so that it is done as part of the class definition,
96             if I can think of a way to do this sanely.
97              
98             =head2 $step->last_run_time
99              
100             This returns the most recent file modification time from all of the step's
101             productions, or C<undef> (requesting an unconditional run) if any productions
102             are missing.
103              
104             =head1 SUPPORT
105              
106             Bugs may be submitted through L<https://github.com/maxmind/Stepford/issues>.
107              
108             =head1 AUTHOR
109              
110             Dave Rolsky <drolsky@maxmind.com>
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             This software is copyright (c) 2014 - 2023 by MaxMind, Inc.
115              
116             This is free software; you can redistribute it and/or modify it under
117             the same terms as the Perl 5 programming language system itself.
118              
119             =cut