/
ie_ext.go
227 lines (197 loc) · 5.3 KB
/
ie_ext.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
// Copyright (c) 2013 The AUTHORS
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package main
import (
// Stdlib
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"text/template"
// Others
"github.com/dmotylev/nutrition"
"github.com/tchap/gocli"
"gopkg.in/yaml.v1"
)
// Subcommand initialisation and registration.
func init() {
ieExt := &gocli.Command{
UsageLine: `
ie_ext SUBCMD`,
Short: "IE extensions manipulation",
}
genBhoversionRc := &gocli.Command{
UsageLine: `
gen_bhoversion_rc [-manifest MANIFEST_FILE] [FILE]`,
Short: "generate bhoversion.rc",
Long: `
gen_brhoversion_rc generates bhoversion.rc in the current working directory
unless FILE is specified. It can optionally read a Chrome extension
manifest.json to get the extension version.
This subcommand uses environmental variables to fill the bhoversion.rc template.
The required environmental variables are:
BHRC_COMPANYNAME
BHRC_FILEDESCRIPTION
BHRC_VERSION - must be a.b.c.d, $BUILD_NUMBER is used for $d if present;
does not have to be set if manifest is being used
BHRC_LEGALCOPYRIGHT
BHRC_PRODUCTNAME
`,
Action: runGenBhoversionRc,
}
genBhoversionRc.Flags.StringVar(&manifestJson, "manifest", manifestJson,
"read version from manifest.json")
ieExt.MustRegisterSubcommand(genBhoversionRc)
getApp().MustRegisterSubcommand(ieExt)
}
var manifestJson string
// Subcommand handler.
func runGenBhoversionRc(cmd *gocli.Command, args []string) {
if len(args) > 1 {
cmd.Usage()
os.Exit(2)
}
var filename string
if len(args) == 1 {
filename = args[0]
} else {
filename = "bhoversion.rc"
}
var version string
if manifestJson != "" {
var manifest struct {
Version string
}
content, err := ioutil.ReadFile(manifestJson)
if err != nil {
log.Fatalf("Error: %v\n", err)
}
if err := yaml.Unmarshal(content, &manifest); err != nil {
log.Fatalf("Error: %v\n", err)
}
version = manifest.Version
}
var ctx struct {
CompanyName string
FileDescription string
Version string
VersionCommas string
LegalCopyright string
ProductName string
}
if err := nutrition.Env("BHRC_").Feed(&ctx); err != nil {
log.Fatalf("Error: %v\n", err)
}
if version != "" {
ctx.Version = version
}
switch {
case ctx.CompanyName == "":
log.Fatalln("Error: BHRC_COMPANYNAME not set")
case ctx.FileDescription == "":
log.Fatalln("Error: BHRC_FILEDESCRIPTION not set")
case ctx.Version == "":
log.Fatalln("Error: BHRC_VERSION not set")
case ctx.LegalCopyright == "":
log.Fatalln("Error: BHRC_LEGALCOPYRIGHT not set")
case ctx.ProductName == "":
log.Fatalln("Error: BHRC_PRODUCTNAME not set")
}
// In case this is a.b.c-d
ctx.Version = strings.Replace(ctx.Version, "-", ".", 1)
buildNum := os.Getenv("BUILD_NUMBER")
if buildNum == "" {
buildNum = "0"
}
// In case we already have a.b.c.d, we make a.b.c.db out of it where
// b is the build number. That is the only way that can be used inside
// of that IE crap I guess...
if strings.Count(ctx.Version, ".") == 3 {
ctx.Version += buildNum
} else {
ctx.Version = fmt.Sprintf("%v.%v", ctx.Version, buildNum)
}
matched, err := regexp.Match("[0-9]+([.][0-9]+){3}", []byte(ctx.Version))
if err != nil {
panic(err)
}
if !matched {
log.Fatalf("Error: invalid version string: %v", ctx.Version)
}
ctx.VersionCommas = strings.Replace(ctx.Version, ".", ",", -1)
t := template.Must(template.New("bhoversion.rc").Parse(bhoversionRcTemplate))
file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error: %v\n", err)
}
if err := t.Execute(file, ctx); err != nil {
file.Close()
log.Fatalf("Error: %v\n", err)
}
if err := file.Close(); err != nil {
log.Fatalf("Error: %v\n", err)
}
}
const bhoversionRcTemplate = `
1 VERSIONINFO
FILEVERSION {{.VersionCommas}}
PRODUCTVERSION {{.VersionCommas}}
FILEFLAGSMASK 0x3fL
FILEFLAGS 0x0L
FILEOS 0x4L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904e4"
BEGIN
VALUE "CompanyName", "{{.CompanyName}}"
VALUE "FileDescription", "{{.FileDescription}}"
VALUE "FileVersion", "{{.Version}}"
VALUE "InternalName", "ancho.dll"
VALUE "LegalCopyright", "{{.LegalCopyright}}"
VALUE "OriginalFilename", "ancho.dll"
VALUE "ProductName", "{{.ProductName}}"
VALUE "ProductVersion", "{{.Version}}"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END
`
/*
const bhoversionRcTemplate = `
1 VERSIONINFO
FILEVERSION {{.VersionCommas}}
PRODUCTVERSION {{.VersionCommas}}
FILEOS 0x4
FILETYPE 0x2
{
BLOCK "StringFileInfo"
{
BLOCK "040904b0"
{
VALUE "CompanyName", "{{.CompanyName}}"
VALUE "FileDescription", "{{.FileDescription}}"
VALUE "FileVersion", "{{.Version}}"
VALUE "InternalName", "ancho.dll"
VALUE "LegalCopyright", "{{.LegalCopyright}}"
VALUE "OriginalFilename", "ancho.dll"
VALUE "ProductName", "{{.ProductName}}"
VALUE "ProductVersion", "{{.Version}}"
}
}
BLOCK "VarFileInfo"
{
VALUE "Translation", 0x0409 0x04E4
}
}
`
*/