-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.go
299 lines (277 loc) · 8.21 KB
/
index.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package manifest
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/Masterminds/semver"
"github.com/satori/go.uuid"
)
// 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"`
InstallHooks []Hook `json:"-"`
UninstallHooks []Hook `json:"-"`
}
// ChocoSpec is the struct to decode the choco key of a wix.json file.
type ChocoSpec struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Authors string `json:"authors,omitempty"`
Owners string `json:"owners,omitempty"`
Description string `json:"description,omitempty"`
ProjectURL string `json:"project-url,omitempty"`
Tags string `json:"tags,omitempty"`
LicenseURL string `json:"license-url,omitempty"`
IconURL string `json:"icon-url,omitempty"`
RequireLicense bool `json:"require-license,omitempty"`
MsiFile string `json:"-"`
MsiSum string `json:"-"`
BuildDir string `json:"-"`
ChangeLog string `json:"-"`
}
const (
whenInstall = "install"
whenUninstall = "uninstall"
)
// HookPhases describes known hook phases.
var HookPhases = map[string]bool{
whenInstall: true,
whenUninstall: true,
}
// Hook describes a command to run on install / uninstall.
type Hook struct {
Command string `json:"command,omitempty"`
CookedCommand string `json:"-"`
When string `json:"when,omitempty"`
}
// WixFiles is the struct to decode files key of the wix.json file.
type WixFiles struct {
GUID string `json:"guid"`
Items []string `json:"items"`
}
// WixEnvList is the struct to decode env key of the wix.json file.
type WixEnvList struct {
GUID string `json:"guid"`
Vars []WixEnv `json:"vars"`
}
// WixEnv is the struct to decode env value of the wix.json file.
type WixEnv struct {
Name string `json:"name"`
Value string `json:"value"`
Permanent string `json:"permanent"`
System string `json:"system"`
Action string `json:"action"`
Part string `json:"part"`
}
// WixShortcuts is the struct to decode shortcuts key of the wix.json file.
type WixShortcuts struct {
GUID string `json:"guid,omitempty"`
Items []WixShortcut `json:"items,omitempty"`
}
// WixShortcut is the struct to decode shortcut value of the wix.json file.
type WixShortcut struct {
Name string `json:"name"`
Description string `json:"description"`
Target string `json:"target"`
WDir string `json:"wdir"`
Arguments string `json:"arguments"`
Icon string `json:"icon"` // a path to the ico file, no space in it.
}
// Write the manifest to the given file,
// if file is empty, writes to wix.json
func (wixFile *WixManifest) Write(p string) error {
if p == "" {
p = "wix.json"
}
byt, err := json.MarshalIndent(wixFile, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(p, byt, 0644)
if err != nil {
return err
}
return nil
}
// Load the manifest from given file path,
// if the file path is empty, reads from wix.json
func (wixFile *WixManifest) Load(p string) error {
if p == "" {
p = "wix.json"
}
if _, err := os.Stat(p); os.IsNotExist(err) {
return err
}
dat, err := ioutil.ReadFile(p)
if err != nil {
return fmt.Errorf("JSON ReadFile failed with %v", err)
}
err = json.Unmarshal(dat, &wixFile)
if err != nil {
return fmt.Errorf("JSON Unmarshal failed with %v", err)
}
return nil
}
//SetGuids generates and apply guid values appropriately
func (wixFile *WixManifest) SetGuids(force bool) (bool, error) {
updated := false
if wixFile.UpgradeCode == "" || force {
wixFile.UpgradeCode = uuid.NewV4().String()
updated = true
}
if wixFile.Files.GUID == "" || force {
wixFile.Files.GUID = uuid.NewV4().String()
updated = true
}
if (wixFile.Env.GUID == "" || force) && len(wixFile.Env.Vars) > 0 {
wixFile.Env.GUID = uuid.NewV4().String()
updated = true
}
if (wixFile.Shortcuts.GUID == "" || force) && len(wixFile.Shortcuts.Items) > 0 {
wixFile.Shortcuts.GUID = uuid.NewV4().String()
updated = true
}
return updated, nil
}
// NeedGUID tells if the manifest json file is missing guid values.
func (wixFile *WixManifest) NeedGUID() bool {
need := false
if wixFile.UpgradeCode == "" {
need = true
}
if wixFile.Files.GUID == "" {
need = true
}
if wixFile.Env.GUID == "" && len(wixFile.Env.Vars) > 0 {
need = true
}
if wixFile.Shortcuts.GUID == "" && len(wixFile.Shortcuts.Items) > 0 {
need = true
}
return need
}
// RewriteFilePaths Reads Files and Directories of the wix.json file
// and turn their values into a relative path to out
// where out is the path to the wix templates files.
func (wixFile *WixManifest) RewriteFilePaths(out string) error {
var err error
out, err = filepath.Abs(out)
if err != nil {
return err
}
for i, file := range wixFile.Files.Items {
file, err = filepath.Abs(file)
if err != nil {
return err
}
wixFile.Files.Items[i], err = filepath.Rel(out, file)
if err != nil {
return err
}
}
for _, d := range wixFile.Directories {
d, err = filepath.Abs(d)
if err != nil {
return err
}
r, err := filepath.Rel(out, d)
if err != nil {
return err
}
wixFile.RelDirs = append(wixFile.RelDirs, r)
}
for i, s := range wixFile.Shortcuts.Items {
if s.Icon != "" {
file, err := filepath.Abs(s.Icon)
if err != nil {
return err
}
wixFile.Shortcuts.Items[i].Icon, err = filepath.Rel(out, file)
if err != nil {
return err
}
}
}
return nil
}
// Normalize Appropriately fixes some values within the decoded json
// It applies defaults values on the wix/msi property to
// to generate the msi package.
// It applies defaults values on the choco property to
// generate a nuget package
func (wixFile *WixManifest) Normalize() error {
// Wix version Field of Product element
// does not support semver strings
// it supports only something like x.x.x.x
// So, if the version has metadata/prerelease values,
// lets get ride of those and save the workable version
// into VersionOk field
wixFile.VersionOk = wixFile.Version
v, err := semver.NewVersion(wixFile.Version)
if err != nil {
return fmt.Errorf("Failed to parse version '%v': %v", wixFile.Version, err)
}
okVersion := ""
okVersion += strconv.FormatInt(v.Major(), 10)
okVersion += "." + strconv.FormatInt(v.Minor(), 10)
okVersion += "." + strconv.FormatInt(v.Patch(), 10)
wixFile.VersionOk = okVersion
// choco fix
if wixFile.Choco.ID == "" {
wixFile.Choco.ID = wixFile.Product
}
if wixFile.Choco.Title == "" {
wixFile.Choco.Title = wixFile.Product
}
if wixFile.Choco.Authors == "" {
wixFile.Choco.Authors = wixFile.Company
}
if wixFile.Choco.Owners == "" {
wixFile.Choco.Owners = wixFile.Company
}
if wixFile.Choco.Description == "" {
wixFile.Choco.Description = wixFile.Product
}
wixFile.Choco.Tags += " admin" // required to pass chocolatey validation..
// Escape hook commands and ensure the command name is enclosed in quotes (needed by wix)
for i, hook := range wixFile.Hooks {
cmd := strings.Trim(hook.Command, " ")
if len(cmd) > 0 && cmd[0] != '"' {
words := strings.Split(cmd, " ")
cmd = `"` + words[0] + `"` + cmd[len(words[0]):]
}
buf := &bytes.Buffer{}
if err := xml.EscapeText(buf, []byte(cmd)); err != nil {
return err
}
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
}