| 1 | package Win32::Packer::InstallerMaker::msi; | ||
| 2 | |||
| 3 | use Win32::Packer::Helpers qw(guid mkpath assert_file assert_file_name assert_dir); | ||
| 4 | |||
| 5 | use XML::FromPerl qw(xml_from_perl); | ||
| 6 | use Path::Tiny; | ||
| 7 | use Win32 (); | ||
| 8 | use Moo; | ||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | extends 'Win32::Packer::InstallerMaker'; | ||
| 12 | |||
| 13 | has versioned_app_name => (is => 'lazy', isa => \&assert_file_name); | ||
| 14 | |||
| 15 | has wix_dir => ( is => 'lazy', coerce => \&mkpath, isa => \&assert_dir ); | ||
| 16 | has wxs_fn => ( is => 'lazy', coerce => \&path); | ||
| 17 | has wixobj_fn => ( is => 'lazy', cperce => \&path); | ||
| 18 | has msi_fn => ( is => 'lazy', coerce => \&path); | ||
| 19 | |||
| 20 | has _wxs_perl => ( is => 'lazy' ); | ||
| 21 | has _wxs => ( is => 'lazy', coerce => \&path, isa => \&assert_file ); | ||
| 22 | has _wixobj => ( is => 'lazy', coerce => \&path, isa => \&assert_file ); | ||
| 23 | has _msi => ( is => 'lazy', coerce => \&path, isa => \&assert_file ); | ||
| 24 | |||
| 25 | has wix_toolset => ( is => 'lazy', coerce => \&path, isa => \&assert_dir ); | ||
| 26 | has candle_exe => ( is => 'lazy', coerce => \&path, isa => \&assert_file ); | ||
| 27 | has light_exe => ( is => 'lazy', coerce => \&path, isa => \&assert_file ); | ||
| 28 | |||
| 29 | sub _mkid { | ||
| 30 | my $self = shift; | ||
| 31 | my $prefix = shift; | ||
| 32 | my $ix = ++($self->{_last_ix}); | ||
| 33 | my $id = join('_', grep defined, $prefix, $ix, @_); | ||
| 34 | $id =~ s/\W/_/g; | ||
| 35 | $id | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _build_msi_fn { | ||
| 39 | my $self = shift; | ||
| 40 | my $basename = join '-', grep defined, $self->app_name, $self->app_version; | ||
| 41 | $self->output_dir->child("$basename.msi"); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _build_wix_toolset { | ||
| 45 | my $self = shift; | ||
| 46 | my $pfiles = Win32::GetFolderPath(Win32::CSIDL_PROGRAM_FILES()) | ||
| 47 | // $self->system_drive->child('Program Files'); | ||
| 48 | |||
| 49 | my @c = path($pfiles)->children(qr/^Wix\s+Toolset\b/i); | ||
| 50 | $c[0] // $self->_die("Wix Toolset not found in '$pfiles'"); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _build_candle_exe { shift->wix_toolset->child('bin/candle.exe') } | ||
| 54 | |||
| 55 | sub _build_light_exe { shift->wix_toolset->child('bin/light.exe') } | ||
| 56 | |||
| 57 | sub _build_versioned_app_name { | ||
| 58 | my $self = shift; | ||
| 59 | join ' ', grep defined, $self->app_name, $self->app_version; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _build_wix_dir { shift->work_dir->child('wix') } | ||
| 63 | |||
| 64 | sub _build_wixobj_fn { | ||
| 65 | my $self = shift; | ||
| 66 | $self->wix_dir->child($self->app_name . ".wixobj"); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _build_wxs_fn { | ||
| 70 | my $self = shift; | ||
| 71 | $self->wix_dir->child($self->app_name . ".wxs"); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _build__wxs_perl { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | my $data = [ Wix => { xmlns => 'http://schemas.microsoft.com/wix/2006/wi', | ||
| 78 | 'xmlns:fw' => 'http://schemas.microsoft.com/wix/FirewallExtension' }, | ||
| 79 | my $product = | ||
| 80 | [ Product => { Name => $self->versioned_app_name, | ||
| 81 | Id => guid, | ||
| 82 | Manufacturer => $self->app_vendor, | ||
| 83 | Version => $self->app_version, | ||
| 84 | Language => '1033', Codepage => '1252' }, | ||
| 85 | [ Package => { Description => $self->app_description, | ||
| 86 | Keywords => $self->app_keywords, | ||
| 87 | Comments => $self->app_comments, | ||
| 88 | Manufacturer => $self->app_vendor, | ||
| 89 | InstallerVersion => '405', | ||
| 90 | Languages => '1033', | ||
| 91 | Compressed => 'yes', | ||
| 92 | SummaryCodepage => '1252' } ], | ||
| 93 | [ MediaTemplate => { EmbedCab => 'yes' } ], | ||
| 94 | my $target_dir = | ||
| 95 | [ Directory => { Id => 'TARGETDIR', Name => 'SourceDir' }, | ||
| 96 | [ Directory => { Id => 'ProgramFilesFolder', Name => 'PFiles' }, | ||
| 97 | my $install_dir = | ||
| 98 | [ Directory => { Id => 'INSTALLDIR', Name => $self->app_name } ]]], | ||
| 99 | my $feature = | ||
| 100 | [ Feature => { Id => 'MainProduct', | ||
| 101 | Title => 'Main Product', | ||
| 102 | Level => '1' } ], | ||
| 103 | [ UIRef => { Id => 'WixUI_InstallDir' } ], | ||
| 104 | [ Property => { Id => 'WIXUI_INSTALLDIR', Value => 'INSTALLDIR' } ]]]; | ||
| 105 | |||
| 106 | if (defined (my $app_id = $self->app_id)) { | ||
| 107 | $self->log->info("MSI UpgradeCode: $app_id"); | ||
| 108 | $product->[1]{UpgradeCode} = $self->app_id; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | $self->log->warn("An application Id has not been provided, consider adding one otherwise " | ||
| 112 | . "the generated installer would not be upgreadable!"); | ||
| 113 | } | ||
| 114 | |||
| 115 | if (defined (my $icon = $self->icon)) { | ||
| 116 | push @$product, | ||
| 117 | [ Icon => { Id => 'Icon.ico', SourceFile => $icon } ], | ||
| 118 | [ Property => { Id => "ARPPRODUCTICON", Value => "Icon.ico" } ]; | ||
| 119 | } | ||
| 120 | |||
| 121 | my %dir = ('.' => $install_dir); | ||
| 122 | my %dir_id = ('.' => 'INSTALLDIR'); | ||
| 123 | my @shortcuts; | ||
| 124 | my $license; | ||
| 125 | my $fs = $self->_fs; | ||
| 126 | for my $to (sort keys %$fs) { | ||
| 127 | my $obj = $fs->{$to}; | ||
| 128 | my $parent = path($to)->parent; | ||
| 129 | my $basename = path($to)->basename; | ||
| 130 | my $type = $obj->{type}; | ||
| 131 | my $e; | ||
| 132 | if ($type eq 'dir') { | ||
| 133 | my $id = $dir_id{$to} = $self->_mkid(dir => $basename); | ||
| 134 | $dir{$to} = $e = [ Directory => { Id => $id, | ||
| 135 | Name => $basename }]; | ||
| 136 | } | ||
| 137 | elsif ($type eq 'file') { | ||
| 138 | my $id = $self->_mkid(component => $basename); | ||
| 139 | my $file_id = $self->_mkid(component => $basename); | ||
| 140 | $e = [ Component => { Id => $id, Guid => guid }, | ||
| 141 | my $file = | ||
| 142 | [ File => { Name => $basename, | ||
| 143 | Source => path($obj->{path})->canonpath, | ||
| 144 | Id => $file_id } ] ]; | ||
| 145 | |||
| 146 | $license = $to if $obj->{_is_license}; | ||
| 147 | |||
| 148 | if (defined (my $hs = $obj->{handles})) { | ||
| 149 | $self->log->info("Adding handlers for $to") if @$hs; | ||
| 150 | for my $hs (@$hs) { | ||
| 151 | if (defined (my $ext = $hs->{extension})) { | ||
| 152 | $ext =~ s/^\.//; | ||
| 153 | $self->log->debug("file $to handles extension $ext"); | ||
| 154 | my $id = $self->_mkid(component => $basename); | ||
| 155 | push @$target_dir, | ||
| 156 | [ Component => { Id => $id, Guid => guid, KeyPath => 'yes' }, | ||
| 157 | [ ProgId => { Id => $self->_mkid(progid => $basename), | ||
| 158 | Icon => $file_id, IconIndex => 0 }, | ||
| 159 | [ Extension => { Id => ".$ext", | ||
| 160 | ContentType => $hs->{content_type} // "application/x-$ext" }, | ||
| 161 | [ Verb => { Id => 'open', Command => '&Open', | ||
| 162 | TargetFile => $file_id, Argument => '"%1"' } ] ] ] ]; | ||
| 163 | |||
| 164 | push @$feature, [ ComponentRef => { Id => $id } ]; | ||
| 165 | } | ||
| 166 | |||
| 167 | if (defined (my $scheme = $hs->{scheme})) { | ||
| 168 | $self->log->debug("file $to handles scheme $scheme"); | ||
| 169 | push @$e, | ||
| 170 | [ RegistryKey => { Root => 'HKCR', | ||
| 171 | Key => $scheme }, | ||
| 172 | [ RegistryValue => { Type=> 'string', Name => 'URL Protocol', Value => ''} ], | ||
| 173 | [ RegistryValue => { Type => 'string', Value => "URL:$scheme" } ], | ||
| 174 | [ RegistryKey => { Key => "DefaultIcon" }, | ||
| 175 | [ RegistryValue => { Type => 'string', Value => "[$dir_id{$parent}]$basename" } ] ], | ||
| 176 | [ RegistryKey => { Key => 'shell\\open\\command' }, | ||
| 177 | [ RegistryValue => { Type => "string", | ||
| 178 | Value => qq("[$dir_id{$parent}]$basename" "%1") } ]]]; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | push @$feature, [ ComponentRef => { Id => $id }]; | ||
| 184 | |||
| 185 | if (defined(my $shortcut = $obj->{shortcut})) { | ||
| 186 | my $id = $self->_mkid(component => $basename); | ||
| 187 | |||
| 188 | push @shortcuts, | ||
| 189 | [ Component => { Id => $id, Guid => guid }, | ||
| 190 | [ Shortcut => { Id => $self->_mkid(shortcut => $basename), | ||
| 191 | Name => $shortcut, | ||
| 192 | Description => $obj->{shortcut_description} // $shortcut, | ||
| 193 | Target => "[$dir_id{$parent}]$basename" } ], | ||
| 194 | [ RemoveFolder => { Id => $self->_mkid(remove => $basename), | ||
| 195 | On => 'uninstall' } ], | ||
| 196 | [ RegistryValue => { Root => 'HKCU', | ||
| 197 | Key => join('\\', 'Software', $self->app_vendor, $self->app_name), | ||
| 198 | Name => 'installed', | ||
| 199 | Type => 'integer', | ||
| 200 | Value => '1', | ||
| 201 | KeyPath => 'yes' } ] ]; | ||
| 202 | push @$feature, | ||
| 203 | [ ComponentRef => { Id => $id } ]; | ||
| 204 | } | ||
| 205 | |||
| 206 | if (defined (my $rules = $obj->{firewall_allow})) { | ||
| 207 | if (@$rules) { | ||
| 208 | for my $rule (@$rules) { | ||
| 209 | my ($src, $proto) = split /:/, lc $rule; | ||
| 210 | my $id = $self->_mkid(component => $basename, 'fw_rule'); | ||
| 211 | my $rule_id = $self->_mkid(firewall => $basename); | ||
| 212 | push @$target_dir, | ||
| 213 | [ Component => { Id => $id, Guid => guid, KeyPath => 'yes' }, | ||
| 214 | my $r = | ||
| 215 | [ 'fw:FirewallException' => { Id => $rule_id, | ||
| 216 | Name => join(" ", $self->versioned_app_name, rule => $rule_id), | ||
| 217 | Program => "[$dir_id{$parent}]$basename", | ||
| 218 | IgnoreFailure => 'yes' } ] ]; | ||
| 219 | push @$feature, | ||
| 220 | [ ComponentRef => { Id => $id } ]; | ||
| 221 | |||
| 222 | if ($src eq 'localhost') { | ||
| 223 | push @$r, [ 'fw:RemoteAddress' => '127.0.0.1/24' ] | ||
| 224 | } | ||
| 225 | elsif ($src eq 'localnet' or $src eq 'localsubnet') { | ||
| 226 | $r->[1]{Scope} = 'localSubnet'; | ||
| 227 | } | ||
| 228 | elsif ($src eq 'any' or $src eq '*') { | ||
| 229 | $r->[1]{Scope} = 'any'; | ||
| 230 | } | ||
| 231 | if (defined $proto) { | ||
| 232 | $r->[1]{Protocol} = $proto; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | $self->log->warn("Unknown object type '$type' for '$to', ignoring..."); | ||
| 240 | next; | ||
| 241 | } | ||
| 242 | my $parent_dir = $dir{$parent} // $self->_die("Parent directory '$parent' for '$to' not found"); | ||
| 243 | push @{$parent_dir}, $e; | ||
| 244 | } | ||
| 245 | |||
| 246 | if (@shortcuts) { | ||
| 247 | if (@shortcuts > 1) { | ||
| 248 | push @$target_dir, | ||
| 249 | [ Directory => { Id => 'ProgramMenuFolder' }, | ||
| 250 | [ Directory => { Id => 'MyShortcutsDir', | ||
| 251 | Name => $self->app_name }, @shortcuts ] ]; | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | push @$target_dir, | ||
| 255 | [ Directory => { Id => 'ProgramMenuFolder' }, @shortcuts ]; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | if (defined $license) { | ||
| 260 | push @$product, | ||
| 261 | [ WixVariable => { Id => 'WixUILicenseRtf', | ||
| 262 | Value => $license } ]; | ||
| 263 | } | ||
| 264 | |||
| 265 | $data | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _build__wxs { | ||
| 269 | my $self = shift; | ||
| 270 | $self->log->info("Generating Wxs file"); | ||
| 271 | my $data = $self->_wxs_perl; | ||
| 272 | my $doc = xml_from_perl $data; | ||
| 273 | my $wxs_fn = $self->wxs_fn; | ||
| 274 | $doc->toFile($wxs_fn, 2); | ||
| 275 | $self->log->debug("Wxs file created at '$wxs_fn'"); | ||
| 276 | $wxs_fn | ||
| 277 | } | ||
| 278 | |||
| 279 | sub _build__wixobj { | ||
| 280 | my $self = shift; | ||
| 281 | my $out = $self->wixobj_fn; | ||
| 282 | my $in = $self->_wxs; | ||
| 283 | $self->log->info("Generating Wixobj file"); | ||
| 284 | my $rc = $self->_run_cmd($self->candle_exe, $in, -out => $out, -ext => 'WixFirewallExtension') | ||
| 285 | or $self->_die("unable to compile wxs file '$in'"); | ||
| 286 | $out; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub _build__msi { | ||
| 290 | my $self = shift; | ||
| 291 | my $out = $self->msi_fn; | ||
| 292 | my $in = $self->_wixobj; | ||
| 293 | |||
| 294 | $self->log->info("Generating MSI file"); | ||
| 295 | my $wix_toolset = $self->wix_toolset; | ||
| 296 | my @ext = map { -ext => $wix_toolset->child(bin => "Wix${_}Extension.dll")->canonpath } qw(UI Firewall); | ||
| 297 | my $rc = $self->_run_cmd($self->light_exe, $in, @ext, -out => $out) | ||
| 298 | or $self->_die("unable to link wixobj file '$in'"); | ||
| 299 | $out | ||
| 300 | } | ||
| 301 | |||
| 302 | sub run { | ||
| 303 | my $self = shift; | ||
| 304 | my $wxs = $self->_msi; | ||
| 305 | } | ||
| 306 | |||
| 307 | 1; |