| 1 | package Win32::Packer::InstallerMaker::dirbat; | ||
| 2 | |||
| 3 | use Path::Tiny; | ||
| 4 | use Win32; | ||
| 5 | use Win32::Packer::Helpers qw(win32_cmd_quote); | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | extends 'Win32::Packer::InstallerMaker'; | ||
| 11 | |||
| 12 | has target_dir => (is => 'lazy', coerce => \&path); | ||
| 13 | has target_bat => (is => 'lazy', coerce => \&path); | ||
| 14 | |||
| 15 | sub _build_target_dir { shift->app_name } | ||
| 16 | |||
| 17 | sub _build_target_bat { | ||
| 18 | my $self = shift; | ||
| 19 | my $basename = join '-', grep defined, $self->app_name, $self->app_version; | ||
| 20 | $self->output_dir->child("$basename.bat"); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub run { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | my $bat = $self->target_bat; | ||
| 27 | my $fh = $bat->filehandle('>'); | ||
| 28 | my $target_dir = $self->target_dir; | ||
| 29 | |||
| 30 | if ($target_dir ne '' and $target_dir ne '.') { | ||
| 31 | print {$fh} "mkdir ".win32_cmd_quote($target_dir->canonpath)."\n"; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | my $fs = $self->_fs; | ||
| 36 | |||
| 37 | # my (%group, @d); | ||
| 38 | # for my $to (sort keys %$fs) { | ||
| 39 | # my $obj = $fs->{$to}; | ||
| 40 | # my $type = $obj->{type}; | ||
| 41 | # if ($type eq 'file') { | ||
| 42 | # my $key = path($to)->parent->canonpath; | ||
| 43 | # push @{$group{$key} //= []}, $to; | ||
| 44 | # } | ||
| 45 | # else { | ||
| 46 | # push @d, $to; | ||
| 47 | # } | ||
| 48 | # } | ||
| 49 | |||
| 50 | # while (@d) { | ||
| 51 | # my $args = ''; | ||
| 52 | # while (@d and length $args < 7000) { | ||
| 53 | # my $to = shift @d; | ||
| 54 | # my $topto = $target_dir->child($to)->canonpath; | ||
| 55 | # $args .= ' ' . win32_cmd_quote($topto); | ||
| 56 | # } | ||
| 57 | # print {$fh} "mkdir$args\n"; | ||
| 58 | # } | ||
| 59 | |||
| 60 | # for my $key (sort keys %group) { | ||
| 61 | # my $topkey = $target_dir->child($key)->canonpath; | ||
| 62 | # my @f = @{$group{$key}}; | ||
| 63 | # while (@f) { | ||
| 64 | # my $args = ''; | ||
| 65 | # while (@f and length $args < 7000) { | ||
| 66 | # my $to = shift @f; | ||
| 67 | # my $path = path($fs->{$to}{path})->canonpath; | ||
| 68 | # $args .= ' ' . win32_cmd_quote($path); | ||
| 69 | # } | ||
| 70 | # print {$fh} "copy$args ".win32_cmd_quote($topkey)."\n"; | ||
| 71 | # } | ||
| 72 | # } | ||
| 73 | |||
| 74 | # The following code works but the generated BAT file is very | ||
| 75 | # slow... | ||
| 76 | |||
| 77 | for my $to (sort keys %$fs) { | ||
| 78 | my $obj = $fs->{$to}; | ||
| 79 | my $type = $obj->{type}; | ||
| 80 | my $topto = $target_dir->child($to)->canonpath; | ||
| 81 | $self->log->trace("Adding object '$topto' of type $type"); | ||
| 82 | if ($type eq 'dir') { | ||
| 83 | $self->_cmdf($fh, "mkdir %s", $topto); | ||
| 84 | } | ||
| 85 | elsif ($type eq 'file') { | ||
| 86 | my $key = path($to)->parent; | ||
| 87 | my $path = path($obj->{path})->canonpath; | ||
| 88 | $self->_cmdf($fh, "copy %s %s", $path, $topto); | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $self->log->warn("Unknown file system object type '$type' for '$topto', ignoring it"); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | $self->log->info("Bat installer generated as $bat"); | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | sub _cmdf { | ||
| 100 | my ($self, $fh, $templ, @args) = @_; | ||
| 101 | printf {$fh} "$templ\n", win32_cmd_quote @args; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 1; |