Skip to content

Commit

Permalink
Make sure uninstall hooks run before files are removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Feb 28, 2017
1 parent e71bba8 commit 2948d0c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
47 changes: 32 additions & 15 deletions manifest/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ import (

// WixManifest is the struct to decode a wix.json file.
type WixManifest struct {
Product string `json:"product"`
Company string `json:"company"`
Version string `json:"version,omitempty"`
VersionOk string `json:"-"`
License string `json:"license,omitempty"`
UpgradeCode string `json:"upgrade-code"`
Files WixFiles `json:"files,omitempty"`
Directories []string `json:"directories,omitempty"`
RelDirs []string `json:"-"`
Env WixEnvList `json:"env,omitempty"`
Shortcuts WixShortcuts `json:"shortcuts,omitempty"`
Choco ChocoSpec `json:"choco,omitempty"`
Hooks []Hook `json:"hooks,omitempty"`
Product string `json:"product"`
Company string `json:"company"`
Version string `json:"version,omitempty"`
VersionOk string `json:"-"`
License string `json:"license,omitempty"`
UpgradeCode string `json:"upgrade-code"`
Files WixFiles `json:"files,omitempty"`
Directories []string `json:"directories,omitempty"`
RelDirs []string `json:"-"`
Env WixEnvList `json:"env,omitempty"`
Shortcuts WixShortcuts `json:"shortcuts,omitempty"`
Choco ChocoSpec `json:"choco,omitempty"`
Hooks []Hook `json:"hooks,omitempty"`
InstallHooks []Hook `json:"-"`
UninstallHooks []Hook `json:"-"`
}

// ChocoSpec is the struct to decode the choco key of a wix.json file.
Expand All @@ -50,9 +52,14 @@ type ChocoSpec struct {
ChangeLog string `json:"-"`
}

const (
whenInstall = "install"
whenUninstall = "uninstall"
)

var PossibleWhenValues = map[string]struct{}{
"install": struct{}{},
"uninstall": struct{}{},
whenInstall: struct{}{},
whenUninstall: struct{}{},
}

type Hook struct {
Expand Down Expand Up @@ -272,5 +279,15 @@ func (wixFile *WixManifest) Normalize() error {
wixFile.Hooks[i].CookedCommand = buf.String()
}

// Separate install and uninstall hooks to simplify templating
for _, hook := range wixFile.Hooks {
switch hook.When {
case whenInstall:
wixFile.InstallHooks = append(wixFile.InstallHooks, hook)
case whenUninstall:
wixFile.UninstallHooks = append(wixFile.UninstallHooks, hook)
}
}

return nil
}
17 changes: 12 additions & 5 deletions templates/product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,21 @@

</Directory>

{{range $i, $e := .Hooks}}
<SetProperty Id="CustomExec{{$i}}" Value="{{$e.CookedCommand}}" Before="CustomExec{{$i}}" Sequence="execute"/>
<CustomAction Id="CustomExec{{$i}}" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check" Impersonate="no"/>
{{range $i, $e := .InstallHooks}}
<SetProperty Id="CustomInstallExec{{$i}}" Value="{{$e.CookedCommand}}" Before="CustomInstallExec{{$i}}" Sequence="execute"/>
<CustomAction Id="CustomInstallExec{{$i}}" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check" Impersonate="no"/>
{{end}}
{{range $i, $e := .UninstallHooks}}
<SetProperty Id="CustomUninstallExec{{$i}}" Value="{{$e.CookedCommand}}" Before="CustomUninstallExec{{$i}}" Sequence="execute"/>
<CustomAction Id="CustomUninstallExec{{$i}}" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check" Impersonate="no"/>
{{end}}
<InstallExecuteSequence>
<RemoveExistingProducts After="InstallValidate"/>
{{range $i, $e := .Hooks}}
<Custom Action="CustomExec{{$i}}" After="{{if eq $i 0}}InstallFiles{{else}}CustomExec{{dec $i}}{{end}}">{{if eq $e.When "install"}}NOT Installed AND NOT REMOVE{{else}}REMOVE ~= "ALL"{{end}}</Custom>
{{range $i, $e := .InstallHooks}}
<Custom Action="CustomInstallExec{{$i}}" After="{{if eq $i 0}}InstallFiles{{else}}CustomInstallExec{{dec $i}}{{end}}">NOT Installed AND NOT REMOVE</Custom>
{{end}}
{{range $i, $e := .InstallHooks}}
<Custom Action="CustomUninstallExec{{$i}}" After="{{if eq $i 0}}InstallInitialize{{else}}CustomUninstallExec{{dec $i}}{{end}}">REMOVE ~= "ALL"</Custom>
{{end}}
</InstallExecuteSequence>

Expand Down

0 comments on commit 2948d0c

Please sign in to comment.