| 1 | package Win32::Packer::InstallerMaker; | ||
| 2 | |||
| 3 | use Path::Tiny; | ||
| 4 | use Win32::Packer::Helpers qw(to_list); | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use namespace::autoclean; | ||
| 8 | |||
| 9 | extends 'Win32::Packer::Base'; | ||
| 10 | |||
| 11 | has _fs => ( is => 'ro', default => sub { {} } ); | ||
| 12 | has _lc_fs => ( is => 'ro', default => sub { {} } ); | ||
| 13 | |||
| 14 | sub add_file { | ||
| 15 | my $self = shift; | ||
| 16 | my $from = shift; | ||
| 17 | my $to = shift // $from->realpath->basename; | ||
| 18 | |||
| 19 | $self->log->debug("Adding file '$from' as '$to'"); | ||
| 20 | |||
| 21 | $self->_add_obj($to, @_, type => 'file', path => $from); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub add_tree { | ||
| 25 | my $self = shift; | ||
| 26 | my $from = shift; | ||
| 27 | my $to = shift // path($from->realpath->basename); | ||
| 28 | $self->log->debug("Adding dir '$from' as '$to'"); | ||
| 29 | my %opts = @_; | ||
| 30 | my %to_skip = map { lc(path($to)->child($_)) => 1 } to_list $opts{skip}; | ||
| 31 | $self->_add_tree($from, $to, \%to_skip); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _add_tree { | ||
| 35 | my ($self, $from, $to, $to_skip) = @_; | ||
| 36 | |||
| 37 | if ($to_skip->{lc $to}) { | ||
| 38 | $self->log->trace("Skipping '$from', to: '$to'"); | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | if ($from->is_dir) { | ||
| 42 | $self->_add_obj($to, type => 'dir'); | ||
| 43 | for my $c ($from->children) { | ||
| 44 | $self->_add_tree($c, $to->child($c->basename), $to_skip); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | elsif ($from->is_file) { | ||
| 48 | $self->_add_obj($to, type => 'file', path => $from); | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | $self->log->warn("Unsupported file system object at '$from'"); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _add_obj { | ||
| 56 | my ($self, $to, %opts) = @_; | ||
| 57 | return if $to eq '.' or $to eq '' or $to eq '/'; | ||
| 58 | |||
| 59 | my $parent = path($to)->parent; | ||
| 60 | $self->_add_obj("$parent", type => 'dir'); | ||
| 61 | |||
| 62 | $self->_add_obj_norec($to, %opts); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _normalize_to { | ||
| 66 | my ($self, $to) = @_; | ||
| 67 | $self->_lc_fs->{lc $to} //= $to; # case normalization | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _add_obj_norec { | ||
| 71 | my ($self, $to, %opts) = @_; | ||
| 72 | my $to1 = $self->_normalize_to($to); | ||
| 73 | my $obj = $self->_fs->{$to1} //= {}; | ||
| 74 | |||
| 75 | $self->_merge($to, $obj, %opts); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _merge { | ||
| 79 | my ($self, $to, $obj, %opts) = @_; | ||
| 80 | for my $k (keys %opts) { | ||
| 81 | if (defined $opts{$k}) { | ||
| 82 | if (defined $obj->{$k}) { | ||
| 83 | if (grep $k eq $_, qw(handles firewall_allow)) { | ||
| 84 | $obj->{$k} = [@{$obj->{$k}}, @{$opts{$k}}]; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | $self->_die("fs object $to reinserted with a different value for $k: $opts{$k}, was: $obj->{$k}") | ||
| 88 | unless $obj->{$k} eq $opts{$k}; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $obj->{$k} = $opts{$k} | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub merge { | ||
| 99 | my ($self, $to, %opts) = @_; | ||
| 100 | $self->log->debug("Merging object at '$to'"); | ||
| 101 | my $obj = $self->_fs->{$self->_normalize_to($to)} // $self->_die("Unable to merge nonexistent object $to"); | ||
| 102 | $self->_merge($to, $obj, %opts); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub run { | ||
| 106 | my $self = shift; | ||
| 107 | $self->_dief("class %s does not implement virtual method run", ref $self); | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; |