| 1 | package FindApp::Object::State::Group::Behavior; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use mro "c3"; | ||
| 7 | |||
| 8 | use FindApp::Vars qw(:all); | ||
| 9 | use FindApp::Utils qw(:all); | ||
| 10 | #use Env qw(@PATH); | ||
| 11 | |||
| 12 | sub bin2man() { | ||
| 13 | my(@manpath, %seen); | ||
| 14 | for my $bindir (@PATH) { | ||
| 15 | next if $bindir =~ /^[.]{0,2}\z/; | ||
| 16 | my $parent = dirname($bindir); | ||
| 17 | for my $mandir (map "$parent/$_", qw(man share/man)) { | ||
| 18 | next if $seen{$mandir}++; | ||
| 19 | next unless -d $mandir; | ||
| 20 | my($dev, $ino) = stat _; | ||
| 21 | next if $seen{$dev, $ino}++; | ||
| 22 | push @manpath, $mandir; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | return @manpath; | ||
| 26 | } | ||
| 27 | |||
| 28 | use namespace::clean; | ||
| 29 | |||
| 30 | sub base_has_wanteds { &ENTER_TRACE_2; | ||
| 31 | good_args(@_ == 2); | ||
| 32 | my($self, $base) = @_; | ||
| 33 | # alloweds are always dirs, so tack on a trailing slash | ||
| 34 | my @have = $self->allowed->count | ||
| 35 | ? grep { -e } map { "$base/$_" } $self->allowed | ||
| 36 | : $base; | ||
| 37 | # Can we find everthing we want in what we have? | ||
| 38 | for my $want (map { /^\w+(::\w+)+$/ ? module2path : $_ } $self->wanted) { | ||
| 39 | my $found; | ||
| 40 | for my $have (@have) { | ||
| 41 | if ($found = -e "$have/$want") { | ||
| 42 | debug "found $have/$want"; | ||
| 43 | last; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return unless $found; | ||
| 47 | } | ||
| 48 | $self->found(map { abs_path } @have); | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub expected_name { | ||
| 53 | good_args(@_ == 2); | ||
| 54 | my($self, $want_name) = @_; | ||
| 55 | my $have_name = $self->name; | ||
| 56 | unless ($have_name eq $want_name) { | ||
| 57 | panic "group $have_name is not a $want_name group"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | sub export_to_env { &ENTER_TRACE_2; | ||
| 62 | my $self = shift; | ||
| 63 | my $exporter = "export_" . $self->name . "_to_env"; | ||
| 64 | $self->$exporter; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub export_root_to_env { &ENTER_TRACE_2; | ||
| 68 | my $self = shift; | ||
| 69 | $self->expected_name("root"); | ||
| 70 | if ($self->have_exported) { debug "already exported root to env" } | ||
| 71 | ($APP_ROOT) = $self->found; | ||
| 72 | debug("\$APP_ROOT = '$APP_ROOT';"); | ||
| 73 | $self->bump_exports; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub export_lib_to_env { &ENTER_TRACE_2; | ||
| 77 | my $self = shift; | ||
| 78 | $self->expected_name("lib"); | ||
| 79 | return unless my @libdirs = $self->found; | ||
| 80 | if ($self->have_exported) { debug "already exported lib to env" } | ||
| 81 | debug qq{use lib (@libdirs);}; | ||
| 82 | eval q{use lib (@libdirs); 1} || die; | ||
| 83 | $self->bump_exports; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub export_bin_to_env { &ENTER_TRACE_2; | ||
| 87 | my $self = shift; | ||
| 88 | $self->expected_name("bin"); | ||
| 89 | return unless my @bindirs = $self->found; | ||
| 90 | my @old_path = @PATH; | ||
| 91 | my @new_path = uniq @bindirs, @PATH; | ||
| 92 | return if @old_path == @new_path && | ||
| 93 | "@old_path" eq "@new_path"; | ||
| 94 | if ($self->have_exported) { debug "already exported bin to env" } | ||
| 95 | debug("\@PATH = (@new_path);"); | ||
| 96 | @PATH = @new_path; | ||
| 97 | $self->bump_exports; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub export_man_to_env { &ENTER_TRACE_2; | ||
| 101 | my $self = shift; | ||
| 102 | $self->expected_name("man"); | ||
| 103 | return unless my @mandirs = $self->found; | ||
| 104 | if ($self->have_exported) { debug "already exported man to env" } | ||
| 105 | unless ($MANPATH && @MANPATH) { | ||
| 106 | local $/ = "\n"; | ||
| 107 | # temp variable so we don't clobber real one on error | ||
| 108 | my $manpath = `manpath 2>&1`; | ||
| 109 | chomp $manpath if defined $manpath; | ||
| 110 | if ($?) { | ||
| 111 | debug("`manpath` command failed with wstat=$?: $manpath"); | ||
| 112 | $manpath = join ":", bin2man(); | ||
| 113 | } | ||
| 114 | return unless $manpath; | ||
| 115 | $MANPATH = $manpath; | ||
| 116 | debug("\$MANPATH = $MANPATH;"); | ||
| 117 | $self->bump_exports; | ||
| 118 | } | ||
| 119 | my @old_path = @MANPATH; | ||
| 120 | my @new_path = uniq @mandirs, @MANPATH; | ||
| 121 | return if @old_path == @new_path && | ||
| 122 | "@old_path" eq "@new_path"; | ||
| 123 | debug("\@MANPATH = (@new_path);"); | ||
| 124 | @MANPATH = @new_path; | ||
| 125 | $self->bump_exports; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | __END__ |