This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
magefile.go
224 lines (194 loc) · 4.14 KB
/
magefile.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
// +build mage
package main
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default mage target
var Default = Build
var (
binPath = path.Join("bin")
buildPath = path.Join(binPath, "nodejs-portable.exe")
buildEnv = map[string]string{
"GO111MODULE": "on",
"GOOS": "windows",
"GOARCH": "386",
"CGO_ENABLED": "0",
}
)
// Build Run go build
func Build() error {
mg.Deps(Clean)
mg.Deps(Generate)
var args []string
args = append(args, "build", "-o", buildPath, "-v")
args = append(args, "-ldflags", flags())
fmt.Println("⚙️ Go build...")
if err := sh.RunWith(buildEnv, mg.GoCmd(), args...); err != nil {
return err
}
return nil
}
// Clean Remove files generated at build-time
func Clean() error {
if err := createDir(binPath); err != nil {
return err
}
if err := cleanDir(binPath); err != nil {
return err
}
return nil
}
// Download Run go mod download
func Download() error {
fmt.Println("⚙️ Go mod download...")
if err := sh.RunWith(buildEnv, mg.GoCmd(), "mod", "download"); err != nil {
return err
}
return nil
}
// Generate Run go generate
func Generate() error {
mg.Deps(Download)
mg.Deps(appConf)
mg.Deps(versionInfo)
fmt.Println("⚙️ Go generate...")
if err := sh.RunV(mg.GoCmd(), "generate", "-v"); err != nil {
return err
}
return nil
}
// flags returns ldflags
func flags() string {
//hash := hash()
tag := tag()
mod := mod()
return fmt.Sprintf(`-s -w -X "main.version=%s" -X "main.module=%s"`, tag, mod)
}
// mod returns module name
func mod() string {
f, err := os.Open("go.mod")
if err == nil {
reader := bufio.NewReader(f)
line, _, _ := reader.ReadLine()
return strings.Replace(string(line), "module ", "", 1)
}
return ""
}
// tag returns the git tag for the current branch or "" if none.
func tag() string {
s, _ := sh.Output("bash", "-c", "git describe --abbrev=0 --tags 2> /dev/null")
if s == "" {
return "0.0.0"
}
return s
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
// appConf generates app.conf file
func appConf() error {
fmt.Println("🔨 Generating nodejs-portable.conf...")
var tpl = template.Must(template.New("").Parse(`{
"version": "{{ .Version }}",
"immediateMode": false,
"shell": "cmd",
"workPath": "./work",
"customPaths": [
"C:/Program Files (x86)/Git/cmd",
"D:/another_path",
"../a_relative_path"
]
}`))
f, err := os.Create("nodejs-portable.conf")
if err != nil {
return err
}
defer f.Close()
return tpl.Execute(f, struct {
Version string
}{
Version: tag(),
})
}
// versionInfo generates versioninfo.json
func versionInfo() error {
fmt.Println("🔨 Generating versioninfo.json...")
var tpl = template.Must(template.New("").Parse(`{
"FixedFileInfo":
{
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo":
{
"Comments": "",
"CompanyName": "",
"FileDescription": "Make Node.js portable on Windows",
"FileVersion": "{{ .Version }}.0",
"InternalName": "",
"LegalCopyright": "https://{{ .Package }}",
"LegalTrademarks": "",
"OriginalFilename": "nodejs-portable.exe",
"PrivateBuild": "",
"ProductName": "Node.js Portable",
"ProductVersion": "{{ .Version }}.0",
"SpecialBuild": ""
},
"VarFileInfo":
{
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
}
}`))
f, err := os.Create("versioninfo.json")
if err != nil {
return err
}
defer f.Close()
return tpl.Execute(f, struct {
Package string
Version string
}{
Package: mod(),
Version: tag(),
})
}
func createDir(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, 777)
}
return nil
}
func cleanDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}