| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | FunctionalPerl::Dependencies::ChjBin | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Chj::TEST use => 'FunctionalPerl::Dependencies::ChjBin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | # or | ||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Test::Requires +{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | 'FunctionalPerl::Dependencies::ChjBin' => '"trigger-listen"' }; | |||||||||||||||||||||||||||
| 20 | # or | ||||||||||||||||||||||||||||
| 21 | # list in FunctionalPerl::Dependencies's %dependencies | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | A way to specify dependencies on tools from | ||||||||||||||||||||||||||||
| 26 | L<chj-bin|https://github.com/pflanze/chj-bin> to the test system. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | L<FP::Repl::Dependencies>, L<FunctionalPerl::Dependencies> | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 NOTE | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 35 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | package FunctionalPerl::Dependencies::ChjBin; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # XX use ~FP::Memoize instead, but that one should be split into | ||||||||||||||||||||||||||||
| 48 | # in-memory and disk versions. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | sub simple_memoize { | ||||||||||||||||||||||||||
|
Subroutine
simple_memoize: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | my ($cacheref, $fn) = @_; # cacheref must be a hash ref | |||||||||||||||||||||||||||
| 53 | 11 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 54 | 11 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # only one argument supported, and argument must be a string, | ||||||||||||||||||||||||||||
| 57 | # or identical with regards of that string. And only does | ||||||||||||||||||||||||||||
| 58 | # scalar context for the result. | ||||||||||||||||||||||||||||
| 59 | 11 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 60 | 11 | ▶ | exists $cacheref->{$arg} ? $cacheref->{$arg} : do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 11 | my $v = $fn->($_[0]); | |||||||||||||||||||||||||||
| 62 | 11 | $cacheref->{$arg} = $v; | |||||||||||||||||||||||||||
| 63 | 11 | $v | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 4 | } | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Calling out to the `which` utility apparently spams the terminal | ||||||||||||||||||||||||||||
| 69 | # with messages even on some "GNU/Linux" distros. D'uh. So go full | ||||||||||||||||||||||||||||
| 70 | # Perl. Don't want to depend on https://metacpan.org/pod/File::Which, | ||||||||||||||||||||||||||||
| 71 | # and we don't need Windows support as chj-bin is Unix only (really | ||||||||||||||||||||||||||||
| 72 | # Linux only?) anyway. (Should we return undef on non-Linux $^O? No, | ||||||||||||||||||||||||||||
| 73 | # nobody should be installing chj-bin if it doesn't work? Not sure.) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 11 | ▶ | sub _maybe_which { | ||||||||||||||||||||||||||
|
Subroutine
_maybe_which: called CC=6 SCAR=19.3
| |||||||||||||||||||||||||||||
| 76 | 11 | my ($str) = @_; | |||||||||||||||||||||||||||
| 77 | 11 | ▶ | my ($prog) = $str =~ /^([\w_.-]+)\z/s or die "invalid progname '$str'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 11 | ▶ | my $paths = [grep { length $_ } split m{:}, $ENV{PATH} // ""]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"PATH"} // ""
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
| |||||||||||||||||||||||||||||
| 79 | 11 | for my $path (@$paths) { | |||||||||||||||||||||||||||
| 80 | 121 | my $progpath = "$path/$prog"; | |||||||||||||||||||||||||||
| 81 | 121 | ▶ | return $progpath if (-f $progpath and -x _); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $progpath and -x _
MC/DC0%0/2
-f $progpath and -x _
-f $progpath
-x _
Truth table33%1/3
A and B
A= -f $progpathB= -x _
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | undef | ||||||||||||||||||||||||||||
| 84 | 11 | } | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | my %which; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub maybe_which; | ||||||||||||||||||||||||||||
| 89 | 2 | *maybe_which = simple_memoize \%which, \&_maybe_which; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | use Chj::IO::Command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | use Chj::xperlfunc ":all"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | my %dir_is_chjbin; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub dir_is_chjbin; | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | *dir_is_chjbin = simple_memoize \%dir_is_chjbin, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=20.6
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($dirpath) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # using combinedsender just to silence stderr, ok? | ||||||||||||||||||||||||||||
| 101 | my $in = Chj::IO::Command->new_combinedsender( | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 103 | 0 | xchdir $dirpath; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 0 | warn "really checking " . getcwd; | |||||||||||||||||||||||||||
| 106 | 0 | xexec "git", "remote", "-v" | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | ); | |||||||||||||||||||||||||||
| 109 | 0 | my $cnt = $in->xcontent; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (0 == $in->xfinish) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # OH, must accept _bin, too, my special habit. So ugly. | ||||||||||||||||||||||||||||
| 113 | ( | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $cnt =~ m{/github\.com/[^/]+/chj-bin(?:\.git)?/? } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | or $cnt =~ m{/_bin/\.git} | ||||||||||||||||||||||||||||
| 116 | ) | ||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | undef | ||||||||||||||||||||||||||||
| 119 | 0 | } | |||||||||||||||||||||||||||
| 120 | 2 | }; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub path_is_chjbin { | ||||||||||||||||||||||||||
|
Subroutine
path_is_chjbin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($path) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | dir_is_chjbin dirname $path | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 128 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # my ($package, $filename, $line) = caller; | ||||||||||||||||||||||||||||
| 131 | 4 | my (@programs) = @_; | |||||||||||||||||||||||||||
| 132 | 4 | my @which = map { [$_, maybe_which $_] } @programs; | |||||||||||||||||||||||||||
| 133 | 4 | my @_found = grep { defined $_->[1] } @which; | |||||||||||||||||||||||||||
| 134 | 4 | my @_notfound = map { $_->[0] } grep { not defined $_->[1] } @which; | |||||||||||||||||||||||||||
| 135 | 0 | my @found_really = grep { path_is_chjbin $_->[1] } @_found; | |||||||||||||||||||||||||||
| 136 | my @found_notfound | ||||||||||||||||||||||||||||
| 137 | 0 | = map { $_->[0] } grep { !path_is_chjbin $_->[1] } @_found; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # "found" means, the binary from chj-bin is in PATH, thus fine. | ||||||||||||||||||||||||||||
| 140 | 4 | my @notfound = (@_notfound, @found_notfound); | |||||||||||||||||||||||||||
| 141 | 4 | ▶ | if (@notfound) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 3 | ▶ | my @msgs = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | ( | ||||||||||||||||||||||||||||
| 144 | @_notfound | ||||||||||||||||||||||||||||
| 145 | ? "these programs are not available: " . join(", ", @_notfound) | ||||||||||||||||||||||||||||
| 146 | : () | ||||||||||||||||||||||||||||
| 147 | ), | ||||||||||||||||||||||||||||
| 148 | ( | ||||||||||||||||||||||||||||
| 149 | @found_notfound | ||||||||||||||||||||||||||||
| 150 | ? "these programs are not from chj-bin: " | ||||||||||||||||||||||||||||
| 151 | . join(", ", @found_notfound) | ||||||||||||||||||||||||||||
| 152 | : () | ||||||||||||||||||||||||||||
| 153 | ) | ||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||
| 155 | 3 | my $msg = join "; ", @msgs; | |||||||||||||||||||||||||||
| 156 | 3 | die __PACKAGE__ . " import: $msg"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # XX what to do if this module is loaded without triggering the | ||||||||||||||||||||||||||||
| 161 | # import? Dangerous play. Alarm handler? Another kind of hook? | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | 1 | |||||||||||||||||||||||||||