| 1 | package Statocles::Util; | ||||||||||||||||||||||||||||
| 2 | 103 | our $VERSION = '0.099'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Various utility functions to reduce dependencies | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 103 | ▶ | use Statocles::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 103 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 103 | ▶ | use Mojo::JSON qw( to_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 103 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 10 | trim dircopy run_editor uniq_by derp read_stdin | ||||||||||||||||||||||||||||
| 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 | 6 | ▶ | sub trim(_) { | ||||||||||||||||||||||||||
|
Subroutine
trim: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | return $_[0] if !$_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 4 | $_[0] =~ s/^\s+//; | |||||||||||||||||||||||||||
| 24 | 4 | $_[0] =~ s/\s+$//; | |||||||||||||||||||||||||||
| 25 | 4 | 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 | 23 | ▶ | sub dircopy($$) { | ||||||||||||||||||||||||||
|
Subroutine
dircopy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 23 | my ( $source, $destination ) = @_; | |||||||||||||||||||||||||||
| 38 | 23 | $source = Path::Tiny->new( $source ); | |||||||||||||||||||||||||||
| 39 | 23 | $destination = Path::Tiny->new( $destination ); | |||||||||||||||||||||||||||
| 40 | 23 | $destination->mkpath; | |||||||||||||||||||||||||||
| 41 | 23 | my $iter = $source->iterator({ recurse => 1 }); | |||||||||||||||||||||||||||
| 42 | 23 | ▶ | while ( my $thing = $iter->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 263 | my $relative = $thing->relative( $source ); | |||||||||||||||||||||||||||
| 44 | 263 | ▶ | if ( $thing->is_dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 81 | mkdir $destination->child( $relative ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 182 | $thing->copy( $destination->child( $relative ) ); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #pod =sub run_editor | ||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||
| 55 | #pod my $content = run_editor( $path ); | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod Invoke the user's text editor (from the C<EDITOR> environment variable) | ||||||||||||||||||||||||||||
| 58 | #pod to edit the given path. Returns the content if the editor was invoked, | ||||||||||||||||||||||||||||
| 59 | #pod or C<undef> C<EDITOR> was not set. If the editor was not able to be | ||||||||||||||||||||||||||||
| 60 | #pod invoked (C<EDITOR> was set but could not be run), an exception is | ||||||||||||||||||||||||||||
| 61 | #pod thrown. | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 7 | ▶ | sub run_editor { | ||||||||||||||||||||||||||
|
Subroutine
run_editor: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 7 | my ( $path ) = @_; | |||||||||||||||||||||||||||
| 67 | 7 | ▶ | return undef unless $ENV{EDITOR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 103 | ▶ | no warnings 'exec'; # We're checking everything ourselves | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | # use string "system" as env-vars need to quote to protect from spaces | ||||||||||||||||||||||||||||
| 70 | # therefore, we quote path, then append it | ||||||||||||||||||||||||||||
| 71 | 4 | system $ENV{EDITOR} . qq{ "$path"}; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | if ($? != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | die sprintf qq{Editor "%s" exited with error (non-zero) status: %d\n}, $ENV{EDITOR}, $?; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 1 | return $path->slurp_utf8; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #pod =sub uniq_by | ||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||
| 80 | #pod my @uniq_links = uniq_by { $_->href } @links; | ||||||||||||||||||||||||||||
| 81 | #pod | ||||||||||||||||||||||||||||
| 82 | #pod Filter a list into its unique items based on the result of the passed-in block. | ||||||||||||||||||||||||||||
| 83 | #pod This lets us get unique links from their C<href> attribute. | ||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||
| 85 | #pod =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 177 | ▶ | sub uniq_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
uniq_by: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 177 | my ( $sub, @list ) = @_; | |||||||||||||||||||||||||||
| 89 | 177 | my ( %found, @out ); | |||||||||||||||||||||||||||
| 90 | 177 | for my $i ( @list ) { | |||||||||||||||||||||||||||
| 91 | 104 | local $_ = $i; | |||||||||||||||||||||||||||
| 92 | 104 | ▶ | push @out, $i if !$found{ $sub->() }++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 177 | return @out; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | #pod =sub derp | ||||||||||||||||||||||||||||
| 98 | #pod | ||||||||||||||||||||||||||||
| 99 | #pod derp "This feature is deprecated in file '%s'", $file; | ||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||
| 101 | #pod Print out a deprecation message as a warning. A message will only be | ||||||||||||||||||||||||||||
| 102 | #pod printed once for each set of arguments. | ||||||||||||||||||||||||||||
| 103 | #pod | ||||||||||||||||||||||||||||
| 104 | #pod =cut | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 103 | our %DERPED; | |||||||||||||||||||||||||||
| 107 | 15 | ▶ | sub derp(@) { | ||||||||||||||||||||||||||
|
Subroutine
derp: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 15 | my @args = @_; | |||||||||||||||||||||||||||
| 109 | 15 | my $key = to_json \@args; | |||||||||||||||||||||||||||
| 110 | 15 | ▶ | return if $DERPED{ $key }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 11 | ▶ | if ( $args[0] !~ /\.$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 5 | $args[0] .= '.'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 11 | warn sprintf( $args[0], @args[1..$#args] ). " See Statocles::Help::Upgrading\n"; | |||||||||||||||||||||||||||
| 115 | 11 | $DERPED{ $key } = 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | #pod =sub read_stdin | ||||||||||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||||||||||
| 120 | #pod my $test = read_stdin(); | ||||||||||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||||||||||
| 122 | #pod Reads the standard input. Intended to provide a point to monkey-patch | ||||||||||||||||||||||||||||
| 123 | #pod for tests. | ||||||||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||||||||
| 125 | #pod =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 4 | ▶ | sub read_stdin { | ||||||||||||||||||||||||||
|
Subroutine
read_stdin: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 4 | ▶ | if ( !-t *STDIN && !-z _ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-t *STDIN || -z _
MC/DC0%0/2
-t *STDIN || -z _
-t *STDIN
-z _
Truth table33%1/3
A || B
A= -t *STDINB= -z _
| |||||||||||||||||||||||||||||
| 129 | 4 | my $content = do { local $/; <STDIN> }; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Re-open STDIN as the TTY so that the editor (vim) can use it | ||||||||||||||||||||||||||||
| 132 | # XXX Is this also a problem on Windows? | ||||||||||||||||||||||||||||
| 133 | 4 | ▶ | if ( -e '/dev/tty' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 4 | close STDIN; | |||||||||||||||||||||||||||
| 135 | 4 | open STDIN, '/dev/tty'; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4 | return $content; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 103 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||