File: MacOSX.rakumod

package info (click to toggle)
raku-file-which 1.0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: makefile: 4; sh: 2
file content (79 lines) | stat: -rw-r--r-- 1,642 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

use v6;

unit class File::Which::MacOSX;

method which(Str $exec, Bool :$all = False) {
  return Any unless $exec;
  fail("This only works on Mac OS X") unless $*DISTRO.name.starts-with('macos');

  my @results;

  # check for aliases first
  my @aliases = %*ENV<Aliases>:exists ?? %*ENV<Aliases>.split( ',' ) !! ();
  for @aliases -> $alias {
    # This has not been tested!!
    # PPT which says MPW-Perl cannot resolve `Alias $alias`,
    # let's just hope it's fixed
    if $alias.lc eq $exec.lc {
      chomp(my $file = qx<Alias $alias>);
      last unless $file;  # if it failed, just go on the normal way
      return $file unless $all;
      @results.push( $file );
      last;
    }
  }

  my @path = flat( $*SPEC.path );

  for  @path.map({ $*SPEC.catfile($_, $exec) }) -> $file  {

    # Ignore possibly -x directories
    next if $file.IO ~~ :d;

    if
      # Executable, normal case
      $file.IO ~~ :x
      # MacOS doesn't mark as executable so we check -e
      || $file.IO ~~ :e
    {
      if $all {
        @results.push( $file );
      } else {
        return $file;
      }
    }
  }

  return @results.unique if $all;
  return Any;
}

=begin pod

=head1 NAME

File::Which::MacOSX - MacOSX which implementation

=head1 SYNOPSIS

  use File::Which::MacOSX;
  my $o = File::Which::MacOSX.new;
  say $o.which('raku');

=head1 DESCRIPTION

Implements the which method under the Mac OS X platform.

=head1 AUTHOR

Ahmad M. Zawawi <ahmad.zawawi@gmail.com>

=head1 COPYRIGHT AND LICENSE

Copyright 2016 Ahmad M. Zawawi

This library is free software; you can redistribute it and/or modify it under
the MIT License

=end pod