| 1 | package App::cpm::Util; | ||
| 2 | use v5.24; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw(lexical_subs signatures); | ||
| 5 | |||
| 6 | use Config; | ||
| 7 | use Cwd (); | ||
| 8 | use Digest::MD5 (); | ||
| 9 | use File::Spec; | ||
| 10 | use IO::Uncompress::Bunzip2 (); | ||
| 11 | use IO::Uncompress::Gunzip (); | ||
| 12 | |||
| 13 | use Exporter 'import'; | ||
| 14 | |||
| 15 | our @EXPORT_OK = qw(DEBUG perl_identity maybe_abs WIN32 determine_home gunzip bunzip2 uniq terminal_width); | ||
| 16 | |||
| 17 | use constant DEBUG => $ENV{PERL_CPM_DEBUG} ? 1 : 0; | ||
| 18 | use constant WIN32 => $^O eq 'MSWin32'; | ||
| 19 | |||
| 20 | sub perl_identity () { | ||
| 21 | my $digest = Digest::MD5::md5_hex($Config{perlpath} . Config->myconfig); | ||
| 22 | $digest = substr $digest, 0, 8; | ||
| 23 | join '-', $Config{version}, $Config{archname}, $digest | ||
| 24 | } | ||
| 25 | |||
| 26 | sub maybe_abs ($path, $cwd = undef) { | ||
| 27 | if (File::Spec->file_name_is_absolute($path)) { | ||
| 28 | return $path; | ||
| 29 | } | ||
| 30 | $cwd ||= Cwd::cwd(); | ||
| 31 | File::Spec->canonpath(File::Spec->catdir($cwd, $path)); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub determine_home () { # taken from Menlo | ||
| 35 | my $homedir = $ENV{HOME} | ||
| 36 | || eval { require File::HomeDir; File::HomeDir->my_home } | ||
| 37 | || join('', @ENV{qw(HOMEDRIVE HOMEPATH)}); # Win32 | ||
| 38 | |||
| 39 | if (WIN32) { | ||
| 40 | require Win32; # no fatpack | ||
| 41 | $homedir = Win32::GetShortPathName($homedir); | ||
| 42 | } | ||
| 43 | |||
| 44 | File::Spec->catdir($homedir, ".perl-cpm"); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub terminal_width () { | ||
| 48 | my $request = $^O eq 'linux' ? 0x5413 : 0x40087468; | ||
| 49 | my $winsize = pack("S4", 0, 0, 0, 0); | ||
| 50 | return 0 if !ioctl(\*STDERR, $request, $winsize); | ||
| 51 | my (undef, $cols, undef, undef) = unpack("S4", $winsize); | ||
| 52 | $cols || 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub gunzip ($from, $to) { | ||
| 56 | my $ok = IO::Uncompress::Gunzip::gunzip($from, $to, BinModeOut => 1); | ||
| 57 | if ($ok) { | ||
| 58 | return (1, undef); | ||
| 59 | } | ||
| 60 | my $err = "gunzip($from, $to) failed: $IO::Uncompress::Gunzip::GunzipError"; | ||
| 61 | return (undef, $err); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub bunzip2 ($from, $to) { | ||
| 65 | my $ok = IO::Uncompress::Bunzip2::bunzip2($from, $to, BinModeOut => 1); | ||
| 66 | if ($ok) { | ||
| 67 | return (1, undef); | ||
| 68 | } | ||
| 69 | my $err = "bunzip2($from, $to) failed: $IO::Uncompress::Bunzip2::Bunzip2Error"; | ||
| 70 | return (undef, $err); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub uniq (@argv) { | ||
| 74 | my %u; | ||
| 75 | grep !$u{$_}++, @argv; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |