forked from josephspurrier/goversioninfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goversioninfo.go
327 lines (271 loc) · 7.21 KB
/
goversioninfo.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Package goversioninfo creates a syso file which contains Microsoft Version Information and an optional icon.
package goversioninfo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"reflect"
"strconv"
"strings"
"github.com/akavel/rsrc/binutil"
"github.com/akavel/rsrc/coff"
)
// *****************************************************************************
// JSON and Config
// *****************************************************************************
// ParseJSON parses the given bytes as a VersionInfo JSON.
func (vi *VersionInfo) ParseJSON(jsonBytes []byte) error {
return json.Unmarshal([]byte(jsonBytes), &vi)
}
// VersionInfo data container
type VersionInfo struct {
FixedFileInfo `json:"FixedFileInfo"`
StringFileInfo `json:"StringFileInfo"`
VarFileInfo `json:"VarFileInfo"`
Timestamp bool
Buffer bytes.Buffer
Structure VSVersionInfo
IconPath string `json:"IconPath"`
ManifestPath string `json:"ManifestPath"`
}
// Translation with langid and charsetid.
type Translation struct {
LangID `json:"LangID"`
CharsetID `json:"CharsetID"`
}
// FileVersion with 3 parts.
type FileVersion struct {
Major int
Minor int
Patch int
Build int
}
// FixedFileInfo contains file characteristics - leave most of them at the defaults.
type FixedFileInfo struct {
FileVersion `json:"FileVersion"`
ProductVersion FileVersion
FileFlagsMask string
FileFlags string
FileOS string
FileType string
FileSubType string
}
// VarFileInfo is the translation container.
type VarFileInfo struct {
Translation `json:"Translation"`
}
// StringFileInfo is what you want to change.
type StringFileInfo struct {
Comments string
CompanyName string
FileDescription string
FileVersion string
InternalName string
LegalCopyright string
LegalTrademarks string
OriginalFilename string
PrivateBuild string
ProductName string
ProductVersion string
SpecialBuild string
}
// *****************************************************************************
// Helpers
// *****************************************************************************
// SizedReader is a *bytes.Buffer.
type SizedReader struct {
*bytes.Buffer
}
// Size returns the length of the buffer.
func (s SizedReader) Size() int64 {
return int64(s.Buffer.Len())
}
func str2Uint32(s string) uint32 {
if s == "" {
return 0
}
u, err := strconv.ParseUint(s, 16, 32)
if err != nil {
log.Printf("Error parsing %q as uint32: %v", s, err)
return 0
}
return uint32(u)
}
func padString(s string, zeros int) []byte {
b := make([]byte, 0, len([]rune(s))*2)
for _, x := range s {
tt := int32(x)
b = append(b, byte(tt))
if tt > 255 {
tt = tt >> 8
b = append(b, byte(tt))
} else {
b = append(b, byte(0))
}
}
for i := 0; i < zeros; i++ {
b = append(b, 0x00)
}
return b
}
func padBytes(i int) []byte {
return make([]byte, i)
}
func (f FileVersion) getVersionHighString() string {
return fmt.Sprintf("%04x%04x", f.Major, f.Minor)
}
func (f FileVersion) getVersionLowString() string {
return fmt.Sprintf("%04x%04x", f.Patch, f.Build)
}
// GetVersionString returns a string representation of the version
func (f FileVersion) GetVersionString() string {
return fmt.Sprintf("%d.%d.%d.%d", f.Major, f.Minor, f.Patch, f.Build)
}
func (t Translation) getTranslationString() string {
return fmt.Sprintf("%04X%04X", t.LangID, t.CharsetID)
}
func (t Translation) getTranslation() string {
return fmt.Sprintf("%04x%04x", t.CharsetID, t.LangID)
}
// *****************************************************************************
// IO Methods
// *****************************************************************************
// Walk writes the data buffer with hexadecimal data from the structs
func (vi *VersionInfo) Walk() {
// Create a buffer
var b bytes.Buffer
w := binutil.Writer{W: &b}
// Write to the buffer
binutil.Walk(vi.Structure, func(v reflect.Value, path string) error {
if binutil.Plain(v.Kind()) {
w.WriteLE(v.Interface())
}
return nil
})
vi.Buffer = b
}
// WriteSyso creates a resource file from the version info and optionally an icon.
// arch must be an architecture string accepted by coff.Arch, like "386" or "amd64"
func (vi *VersionInfo) WriteSyso(filename string, arch string) error {
var i uint16
newID := func() uint16 {
i++
return i
}
// Create a new RSRC section
rsrc := coff.NewRSRC()
// Set the architecture
err := rsrc.Arch(arch)
if err != nil {
return err
}
// ID 16 is for Version Information
rsrc.AddResource(16, 1, SizedReader{bytes.NewBuffer(vi.Buffer.Bytes())})
// If manifest is enabled
if vi.ManifestPath != "" {
manifest, err := binutil.SizedOpen(vi.ManifestPath)
if err != nil {
return err
}
defer manifest.Close()
id := newID()
rsrc.AddResource(rtManifest, id, manifest)
}
// If icon is enabled
if vi.IconPath != "" {
if err := addIcon(rsrc, vi.IconPath, newID); err != nil {
return err
}
}
rsrc.Freeze()
// Write to file
return writeCoff(rsrc, filename)
}
// WriteHex creates a hex file for debugging version info
func (vi *VersionInfo) WriteHex(filename string) error {
return ioutil.WriteFile(filename, vi.Buffer.Bytes(), 0655)
}
// WriteGo creates a Go file that contains the version info so you can access
// it in the application
func (vi *VersionInfo) WriteGo(filename, packageName string) error {
if len(packageName) == 0 {
packageName = "main"
}
out, err := os.Create(filename)
if err != nil {
return err
}
ffib, err := json.MarshalIndent(vi.FixedFileInfo, "\t", "\t")
if err != nil {
return err
}
sfib, err := json.MarshalIndent(vi.StringFileInfo, "\t", "\t")
if err != nil {
return err
}
vfib, err := json.MarshalIndent(vi.VarFileInfo, "\t", "\t")
if err != nil {
return err
}
replace := "`\" + \"`\" + \"`"
str := "`{\n\t"
str += `"FixedFileInfo":`
str += strings.Replace(string(ffib), "`", replace, -1)
str += ",\n\t"
str += `"StringFileInfo":`
str += strings.Replace(string(sfib), "`", replace, -1)
str += ",\n\t"
str += `"VarFileInfo":`
str += strings.Replace(string(vfib), "`", replace, -1)
str += "\n"
str += "}`"
fmt.Fprintf(out, `// Auto-generated file by goversioninfo. Do not edit.
package %v
import (
"encoding/json"
"github.com/josephspurrier/goversioninfo"
)
func unmarshalGoVersionInfo(b []byte) goversioninfo.VersionInfo {
vi := goversioninfo.VersionInfo{}
json.Unmarshal(b, &vi)
return vi
}
var versionInfo = unmarshalGoVersionInfo([]byte(%v))
`, packageName, string(str))
return nil
}
func writeCoff(coff *coff.Coff, fnameout string) error {
out, err := os.Create(fnameout)
if err != nil {
return err
}
if err = writeCoffTo(out, coff); err != nil {
return fmt.Errorf("error writing %q: %v", fnameout, err)
}
return nil
}
func writeCoffTo(w io.WriteCloser, coff *coff.Coff) error {
bw := binutil.Writer{W: w}
// write the resulting file to disk
binutil.Walk(coff, func(v reflect.Value, path string) error {
if binutil.Plain(v.Kind()) {
bw.WriteLE(v.Interface())
return nil
}
vv, ok := v.Interface().(binutil.SizedReader)
if ok {
bw.WriteFromSized(vv)
return binutil.WALK_SKIP
}
return nil
})
err := bw.Err
if closeErr := w.Close(); closeErr != nil && err == nil {
err = closeErr
}
return err
}