File Coverage

blib/lib/Statocles/Util.pm
Criterion Covered Total %
statement 47 47 100.0
branch 18 18 100.0
condition n/a
subroutine 9 9 100.0
pod 5 5 100.0
total 79 79 100.0


line stmt bran cond sub pod time code
1             package Statocles::Util;
2             our $VERSION = '0.084';
3             # ABSTRACT: Various utility functions to reduce dependencies
4              
5 68     68   400 use Statocles::Base;
  68         125  
  68         369  
6 68     68   2067 use Exporter 'import';
  68         131  
  68         1788  
7 68     68   18428 use Mojo::JSON qw( to_json );
  68         6232529  
  68         19881  
8              
9             our @EXPORT_OK = qw(
10             trim dircopy run_editor uniq_by derp
11             );
12              
13             #pod =sub trim
14             #pod
15             #pod my $trimmed = trim $untrimmed;
16             #pod
17             #pod Trim the leading and trailing whitespace from the given scalar.
18             #pod
19             #pod =cut
20              
21             sub trim(_) {
22 8 100   8 1 63044 return $_[0] if !$_[0];
23 6         43 $_[0] =~ s/^\s+//;
24 6         50 $_[0] =~ s/\s+$//;
25 6         37 return $_[0];
26             }
27              
28             #pod =sub dircopy
29             #pod
30             #pod dircopy $source, $destination;
31             #pod
32             #pod Copy everything in $source to $destination, recursively.
33             #pod
34             #pod =cut
35              
36             sub dircopy($$) {
37 32     32 1 195546 my ( $source, $destination ) = @_;
38 32         170 $source = Path::Tiny->new( $source );
39 32         798 $destination = Path::Tiny->new( $destination );
40 32         570 $destination->mkpath;
41 32         6150 my $iter = $source->iterator({ recurse => 1 });
42 32         1280 while ( my $thing = $iter->() ) {
43 812         241496 my $relative = $thing->relative( $source );
44 812 100       131700 if ( $thing->is_dir ) {
45 403         5862 mkdir $destination->child( $relative );
46             }
47             else {
48 409         5976 $thing->copy( $destination->child( $relative ) );
49             }
50             }
51             }
52              
53             #pod =sub run_editor
54             #pod
55             #pod my $was_run = run_editor( $path );
56             #pod
57             #pod Invoke the user's text editor (from the C<EDITOR> environment variable) to edit
58             #pod the given path. Returns true if an editor was invoked, false otherwise. If the
59             #pod editor was not able to be invoked (C<EDITOR> was set but could not be run), an
60             #pod exception is thrown.
61             #pod
62             #pod =cut
63              
64             sub run_editor {
65 9     9 1 27196 my ( $path ) = @_;
66 9 100       58 return 0 unless $ENV{EDITOR};
67 68     68   685 no warnings 'exec'; # We're checking everything ourselves
  68         136  
  68         25210  
68 5         40590 system split( /\s+/, $ENV{EDITOR} ), $path;
69 5 100       245 if ($? == -1) {
    100          
    100          
70 1         85 die sprintf qq{Failed to invoke editor "%s": %s\n}, $ENV{EDITOR}, $!;
71             }
72             elsif ($? & 127) {
73 1         61 die sprintf qq{Editor "%s" died from signal %d\n}, $ENV{EDITOR}, ( $? & 127 );
74             }
75             elsif ( my $exit = $? >> 8 ) {
76 1         54 die sprintf qq{Editor "%s" exited with error (non-zero) status: %d\n}, $ENV{EDITOR}, $exit;
77             }
78 2         83 return 1;
79             }
80              
81             #pod =sub uniq_by
82             #pod
83             #pod my @uniq_links = uniq_by { $_->href } @links;
84             #pod
85             #pod Filter a list into its unique items based on the result of the passed-in block.
86             #pod This lets us get unique links from their C<href> attribute.
87             #pod
88             #pod =cut
89              
90             sub uniq_by(&@) {
91 2161     2161 1 28232 my ( $sub, @list ) = @_;
92 2161         3532 my ( %found, @out );
93 2161         4330 for my $i ( @list ) {
94 1769         4926 local $_ = $i;
95 1769 100       3839 push @out, $i if !$found{ $sub->() }++;
96             }
97 2161         17957 return @out;
98             }
99              
100             #pod =sub derp
101             #pod
102             #pod derp "This feature is deprecated in file '%s'", $file;
103             #pod
104             #pod Print out a deprecation message as a warning. A message will only be
105             #pod printed once for each set of arguments.
106             #pod
107             #pod =cut
108              
109             our %DERPED;
110             sub derp(@) {
111 20     20 1 24263 my @args = @_;
112 20         126 my $key = to_json \@args;
113 20 100       1064 return if $DERPED{ $key };
114 15 100       90 if ( $args[0] !~ /\.$/ ) {
115 5         16 $args[0] .= '.';
116             }
117 15         331 warn sprintf( $args[0], @args[1..$#args] ). " See Statocles::Help::Upgrading\n";
118 15         159 $DERPED{ $key } = 1;
119             }
120              
121             1;
122              
123             __END__
124              
125             =pod
126              
127             =encoding UTF-8
128              
129             =head1 NAME
130              
131             Statocles::Util - Various utility functions to reduce dependencies
132              
133             =head1 VERSION
134              
135             version 0.084
136              
137             =head1 SYNOPSIS
138              
139             use Statocles::Util qw( dircopy );
140              
141             dircopy $source, $destination;
142              
143             =head1 DESCRIPTION
144              
145             This module contains some utility functions to help reduce non-core dependencies.
146              
147             =head1 SUBROUTINES
148              
149             =head2 trim
150              
151             my $trimmed = trim $untrimmed;
152              
153             Trim the leading and trailing whitespace from the given scalar.
154              
155             =head2 dircopy
156              
157             dircopy $source, $destination;
158              
159             Copy everything in $source to $destination, recursively.
160              
161             =head2 run_editor
162              
163             my $was_run = run_editor( $path );
164              
165             Invoke the user's text editor (from the C<EDITOR> environment variable) to edit
166             the given path. Returns true if an editor was invoked, false otherwise. If the
167             editor was not able to be invoked (C<EDITOR> was set but could not be run), an
168             exception is thrown.
169              
170             =head2 uniq_by
171              
172             my @uniq_links = uniq_by { $_->href } @links;
173              
174             Filter a list into its unique items based on the result of the passed-in block.
175             This lets us get unique links from their C<href> attribute.
176              
177             =head2 derp
178              
179             derp "This feature is deprecated in file '%s'", $file;
180              
181             Print out a deprecation message as a warning. A message will only be
182             printed once for each set of arguments.
183              
184             =head1 AUTHOR
185              
186             Doug Bell <preaction@cpan.org>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2016 by Doug Bell.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut