| 1 | package Win32::Packer::Helpers; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Path::Tiny; | ||
| 8 | |||
| 9 | use Exporter qw(import); | ||
| 10 | our @EXPORT_OK = grep { no strict 'refs'; /^[a-z]/i and defined *{$_}{CODE} } keys %{Win32::Packer::Helpers::}; | ||
| 11 | |||
| 12 | #use Data::Dumper; warn Dumper(\@EXPORT_OK); | ||
| 13 | |||
| 14 | sub assert_file { $_[0]->is_file or croak "$_[0] is not a file" } | ||
| 15 | sub assert_file_name { $_[0] =~ tr{<>:"/\\|?*}{} and croak "$_[0] is not a valid Windows file name" } | ||
| 16 | sub assert_dir { $_[0]->is_dir or croak "$_[0] is not a directory" } | ||
| 17 | |||
| 18 | sub assert_aoh_path { defined $_->{path} or croak "$_ is not a path" for @{$_[0]} } | ||
| 19 | sub assert_aoh_path_file { $_->{path}->is_file or croak "$_->{path} is not a file" for @{$_[0]} } | ||
| 20 | sub assert_aoh_path_dir { $_->{path}->is_dir or croak "$_->{path} is not a directory" for @{$_[0]} } | ||
| 21 | |||
| 22 | sub assert_subsystem { | ||
| 23 | $_[0] =~ /^(?:windows|console)$/ | ||
| 24 | or croak "app_subsystem must be 'windows' or 'console'"; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub assert_guid { $_[0] =~ /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/ } | ||
| 28 | |||
| 29 | sub mkpath { | ||
| 30 | my $p = path(shift); | ||
| 31 | $p->mkpath; | ||
| 32 | $p | ||
| 33 | } | ||
| 34 | |||
| 35 | sub to_bool { $_[0] ? 1 : 0 } | ||
| 36 | sub to_list { | ||
| 37 | return @{$_[0]} if ref $_[0] eq 'ARRAY'; | ||
| 38 | return $_[0] if defined $_[0]; | ||
| 39 | () | ||
| 40 | } | ||
| 41 | |||
| 42 | sub to_uc { uc $_[0] } | ||
| 43 | |||
| 44 | sub to_array { [to_list(shift)] } | ||
| 45 | |||
| 46 | sub to_array_path { [map path($_), to_list(shift)] } | ||
| 47 | |||
| 48 | sub to_loh_path { | ||
| 49 | map { | ||
| 50 | my %h = (ref eq 'HASH' ? %$_ : (path => $_)); | ||
| 51 | defined and $_ = path($_) for @h{qw(path subdir icon)}; | ||
| 52 | $_ = to_array($_) for @h{qw(handles firewall_allow)}; | ||
| 53 | $_ = to_array_path($_) for @h{qw(search_path)}; | ||
| 54 | $h{basename} //= $h{path}->basename(qr/\.\w*/); | ||
| 55 | assert_subsystem($h{subsystem}) if defined $h{subsystem}; | ||
| 56 | \%h | ||
| 57 | } to_list(shift) | ||
| 58 | } | ||
| 59 | |||
| 60 | sub to_aoh_path { [ to_loh_path(shift) ] } | ||
| 61 | |||
| 62 | sub windows_directory { | ||
| 63 | require Win32::API; | ||
| 64 | state $fn = Win32::API->new("KERNEL32","GetWindowsDirectoryA","PN","N"); | ||
| 65 | my $buffer = "\0" x 255; | ||
| 66 | $fn->Call($buffer, length $buffer); | ||
| 67 | $buffer =~ tr/\0//d; | ||
| 68 | path($buffer)->realpath; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub c_string_quote { | ||
| 72 | my $str = shift; | ||
| 73 | $str =~ s/(["\\])/\\$1/g; | ||
| 74 | qq("$str") | ||
| 75 | } | ||
| 76 | |||
| 77 | sub fn_maybe_add_extension { | ||
| 78 | my ($fn, $ext) = @_; | ||
| 79 | $ext =~ s/^\.*/./; | ||
| 80 | $fn =~ s/\Q$ext\E$//; | ||
| 81 | "$fn$ext" | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _w32q { | ||
| 85 | my $arg = shift; | ||
| 86 | for ($arg) { | ||
| 87 | $_ eq '' and return '""'; | ||
| 88 | if (/[ \t\n\x0b"]/) { | ||
| 89 | s{(\\+)(?="|\z)}{$1$1}g; | ||
| 90 | s{"}{\\"}g; | ||
| 91 | return qq("$_") | ||
| 92 | } | ||
| 93 | return $_ | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub win32_cmd_quote { | ||
| 98 | my @r = map _w32q($_), @_; | ||
| 99 | wantarray ? @r : $r[0]; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub guid { | ||
| 103 | my $self = shift; | ||
| 104 | require Data::GUID; | ||
| 105 | Data::GUID->new->as_string; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |