-
Notifications
You must be signed in to change notification settings - Fork 1
/
installer_windows.go
276 lines (242 loc) · 7.56 KB
/
installer_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package installer
import (
"fmt"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"log"
"os/exec"
"path/filepath"
"syscall"
)
const (
createShortcutShellCmdFmt = `
$SourceFileLocation = "%s"
$ShortcutLocation = "%s"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $SourceFileLocation
$Shortcut.Save()
`
)
func (i *installer) AddStepCreateShortcut(src, dst string) {
process := func() error { return createShortcut(src, dst) }
description := i.getShortcutCreatingText(src, dst)
i.AddStep(process, description)
}
//AddStepRmvFolderAfterInstall sets an onClose function to delete
//a path and its subpaths.
//Process is empty because it's not an actual immediatly processed function
//but rather a delayed one.
func (i *installer) AddStepRmvFolderAfterInstall(path string) {
process := func() error { return nil }
description := i.getRemoveFolderAfterInstallText(path)
i.onClose = func() {
err := rmvFolderAfterDelay(path)
if err != nil {
log.Fatal(err)
}
}
i.AddStep(process, description)
}
func rmvFolderAfterDelay(path string) error {
cmd := fmt.Sprintf("Start-Sleep -s 5; rm -r %s", path)
return startHiddenPowerShellCmd(cmd)
}
//AddStepCreateScheme adds a step that creates registry keys to handle a custom scheme.
//shellCmd will be executed when scheme is called.
//Please note that registry keys are user related and not global.
//FriendlyTypeName is the name displayed to the user when he attempts to open that scheme.
func (i *installer) AddStepCreateScheme(scheme string, friendlyTypeName string, shellCmd string) {
process := func() error {
return createScheme(scheme, friendlyTypeName, shellCmd)
}
description := i.getRegisterSchemeText(scheme)
i.AddStep(process, description)
}
//UninstallOptions is used to create a registry key with optional options provided
type UninstallOptions struct {
DisplayName string
DisplayVersion string
Publisher string
//UninstallString is the path to the uninstaller
UninstallString string
URLInfoAbout string
//Path to the display icon. Usually main binary exe who has already an icon set
DisplayIcon string
//KeyName to be found in SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\KeyName
KeyName string
}
func (i *installer) AddStepCreateUninstallOpt(opts UninstallOptions) {
process := func() error {
return createUninstallOpt(opts)
}
description := i.getUninstallOptText()
i.AddStep(process, description)
}
//AddStepDeleteScheme adds a step that deletes scheme association registry keys
func (i *installer) AddStepDeleteScheme(scheme string) {
process := func() error { return deleteSchemeKey(scheme) }
description := i.getUnregisterSchemeText(scheme)
i.AddStep(process, description)
}
//AddStepDeleteUninstallOpt deletes uninstall registry keys associated
//with a given program.
func (i *installer) AddStepDeleteUninstallOpt(prog string) {
process := func() error { return deleteUninstallKey(prog) }
description := i.getRemoveUninstallOptText()
i.AddStep(process, description)
}
func createShortcut(src, dst string) error {
shellCmd := fmt.Sprintf(createShortcutShellCmdFmt, src, dst)
if err := startHiddenPowerShellCmd(shellCmd); err != nil {
return err
}
return nil
}
func startHiddenPowerShellCmd(shellCmd string) error {
p, err := getPowershellPath()
if err != nil {
return err
}
cmd := exec.Command(p, shellCmd)
hidePowershellCmd(cmd)
return cmd.Start()
}
func getPowershellPath() (string, error) {
w, err := windows.GetSystemWindowsDirectory()
if err != nil {
return "", err
}
return filepath.Join(w, "System32", "WindowsPowershell", "v1.0", "powershell.exe"), nil
}
func hidePowershellCmd(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
}
func windowsSoftwareKeyPath() string {
return filepath.Join("SOFTWARE", "Microsoft",
"Windows", "CurrentVersion")
}
func createUninstallOpt(opts UninstallOptions) error {
k, err := createUninstallProgKey(opts.KeyName)
if err != nil {
return err
}
defer k.Close()
return setUninstKeyValues(k, opts)
}
func setUninstKeyValues(k registry.Key, opts UninstallOptions) error {
return setRegistryKeyValues(k, map[string]string{
"NoModify": "1",
"NoRepair": "1",
"UninstallString": opts.UninstallString,
"DisplayName": opts.DisplayName,
"DisplayIcon": opts.DisplayIcon,
"Publisher": opts.Publisher,
"URLInfoAbout": opts.URLInfoAbout,
"DisplayVersion": opts.DisplayVersion,
})
}
func setRegistryKeyValues(k registry.Key, values map[string]string) error {
for name, val := range values {
err := k.SetStringValue(name, val)
if err != nil {
return err
}
}
return nil
}
func createScheme(scheme, friendlyTypeName, shellCmd string) error {
schemeK, err := createURLSchemeKey(scheme, friendlyTypeName)
if err != nil {
return err
}
defer schemeK.Close()
return createShellCmdKey(schemeK, friendlyTypeName, shellCmd)
}
func deleteSchemeKey(scheme string) error {
classK, err := openSwClassKey()
if err != nil {
return err
}
defer classK.Close()
keys := append([]string{scheme}, "shell", "open", "command")
return deleteKeys(classK, keys)
}
func deleteUninstallKey(prog string) error {
uninstallK, err := openUninstallKey()
if err != nil {
return err
}
defer uninstallK.Close()
return registry.DeleteKey(uninstallK, prog)
}
func openSwClassKey() (registry.Key, error) {
return openKey(registry.CURRENT_USER, classKeyPath())
}
func openUninstallKey() (registry.Key, error) {
return openKey(registry.CURRENT_USER, uninstallKeyPath())
}
func createUninstallProgKey(prog string) (registry.Key, error) {
return addKey(registry.CURRENT_USER, uninstallProgKeyPath(prog))
}
func createURLSchemeKey(scheme, friendlyTypeName string) (registry.Key, error) {
k, err := addKey(registry.CURRENT_USER, schemeKeyPath(scheme))
if err != nil {
return k, err
}
return k, setRegistryKeyValues(k, map[string]string{
"URL Protocol": "",
"FriendlyTypeName": friendlyTypeName + " " + "Protocol",
})
return k, k.SetStringValue("URL Protocol", "")
}
func createShellCmdKey(sourceK registry.Key, friendlyTypeName, shellCmd string) error {
k, err := createShellOpenKey(sourceK, friendlyTypeName)
if err != nil {
return err
}
cmdK, err := addKey(k, "command")
if err != nil {
return err
}
defer cmdK.Close()
return cmdK.SetStringValue("", shellCmd)
}
func createShellOpenKey(sourceK registry.Key, friendlyTypeName string) (registry.Key, error) {
p := filepath.Join("shell", "open")
openK, err := addKey(sourceK, p)
if err != nil {
return openK, err
}
return openK, openK.SetStringValue("FriendlyAppName", friendlyTypeName)
}
func classKeyPath() string {
return filepath.Join("SOFTWARE", "Classes")
}
func schemeKeyPath(scheme string) string {
return filepath.Join(classKeyPath(), scheme)
}
func uninstallKeyPath() string {
return filepath.Join(windowsSoftwareKeyPath(), "Uninstall")
}
func uninstallProgKeyPath(prog string) string {
return filepath.Join(uninstallKeyPath(), prog)
}
func addKey(sourceK registry.Key, newKPath string) (registry.Key, error) {
newK, _, err := registry.CreateKey(sourceK, newKPath, registry.ALL_ACCESS)
return newK, err
}
func openKey(sourceK registry.Key, path string) (registry.Key, error) {
k, _, err := registry.CreateKey(sourceK, path, registry.ALL_ACCESS)
return k, err
}
func deleteKeys(sourceK registry.Key, subKeyNames []string) error {
if len(subKeyNames) == 0 {
return nil
}
err := registry.DeleteKey(sourceK, filepath.Join(subKeyNames...))
if err != nil {
return err
}
return deleteKeys(sourceK, subKeyNames[:len(subKeyNames)-1])
}