/
main.go
270 lines (229 loc) · 7.37 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"path/filepath"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/Varjelus/kopsa"
"github.com/Varjelus/archivist"
)
// Versions
const (
VERSION = "0.1.0"
NW_VERSION = "0.12.3"
AR_VERSION = "0.9.0"
)
// Defaults
const (
DEFAULT_SRC = "."
DEFAULT_REL = "nw-release"
DEFAULT_ICO = "icon.ico"
)
var (
DEFAULT_NWF = func() string {
if _, err := os.Stat("nw"); os.IsNotExist(err) {
return "nw.zip"
}
return "nw" // Use unzipped version if possible
}()
DEFAULT_TMP = os.TempDir()
DEFAULT_TOL = func() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fatalError(err.Error())
}
return filepath.Join(dir, "buildTools")
}()
)
// Colors
var (
fatalRed = color.New(color.BgRed)
red = color.New(color.FgRed)
yellow = color.New(color.FgYellow)
cyan = color.New(color.FgCyan)
green = color.New(color.FgGreen)
)
// Platforms
var platforms = []string{"win64"}
// Flags
var (
helpFlag = flag.Bool("help", false, "Show possible arguments and their default values and explanations.")
platformsFlag = flag.Bool("targets", false, "Show available target platforms.")
versionFlag = flag.Bool("version", false, "Show the versions and required versions of external tools.")
nwDir = flag.String("nw", fmt.Sprintf("%s%c%s", DEFAULT_TOL, os.PathSeparator, DEFAULT_NWF), "Set NW.js path.")
deflate = flag.Bool("cmp", false, "Should the ZIP be compressed.")
srcDir = flag.String("src", DEFAULT_SRC, "Source directory containing all your project files, including package.json.")
outDir = flag.String("out", DEFAULT_REL, "Output directory where you want the packaged application.")
icon = flag.String("icon", DEFAULT_ICO, "Desired application icon path.")
target = flag.String("target", "win64", "Target platform.")
projectName = flag.String("name", "app", "Application name.")
tmp = flag.String("tmp", DEFAULT_TMP, "Temporary files directory.")
usePdf = flag.Bool("pdf", false, "Does your app need PDF capabilities via pdf.dll? (Large DLL)")
)
var exePath string
var NW_OMIT = []string{"nwjc.exe", "nw.exe", "credits.html"}
func init() {
flag.Parse()
if *helpFlag {
flag.PrintDefaults()
os.Exit(0)
}
if *platformsFlag {
fmt.Println("Available target platforms:")
for i := range platforms {
fmt.Printf("\t* %s", platforms[i])
}
os.Exit(0)
}
if *versionFlag {
fmt.Printf("nodebob-go version %s\n", VERSION)
fmt.Printf("nw.js version %s\n", NW_VERSION)
fmt.Printf("Anolis Resourcer version %s\n", AR_VERSION)
os.Exit(0)
}
exePath = filepath.Join(*outDir, fmt.Sprintf("%s.exe", *projectName))
}
func fatalError(err string) {
cleanUp()
fatalRed.Println(err)
os.Exit(2)
}
// Copy NW.js files
func copyWalk(path string, info os.FileInfo, err error) error {
if err != nil { return err }
fileName := strings.TrimPrefix(path, *nwDir)
if len(fileName) < 1 {
return nil
}
dstPath := filepath.Join(*outDir, fileName)
// Check if the destination file exists
dstInfo, err := os.Stat(dstPath)
// If it does exist and it errors, return
if err != nil && os.IsExist(err) {
return err
}
// If it does exist and it's the same file, return nil
if os.IsExist(err) && os.SameFile(info, dstInfo) {
return nil
}
if info.IsDir() {
if err := os.Mkdir(dstPath, info.Mode()); err != nil {
return err
}
return nil
}
// Keep only regular, nonempty files
if !info.Mode().IsRegular() || info.Size() == 0 {
return nil
}
// Omit pdf.dll unless otherwise specified
if info.Name() == "pdf.dll" && !*usePdf {
return nil
}
// Omit specified files
for _, e := range NW_OMIT {
if e == info.Name() {
return nil
}
}
if _, err = kopsa.Copy(dstPath, path); err != nil {
return err
}
return nil
}
func createZip() {
// Make sure *srcDir/package.json exists
if _, err := os.Stat(filepath.Join(*srcDir, "package.json")); err != nil {
if os.IsNotExist(err) {
fatalError(fmt.Sprintf("%s/package.json not found", *srcDir))
}
}
if err := archivist.Zip(*srcDir, filepath.Join(*tmp, "bob.nw")); err != nil {
fatalError(err.Error())
}
}
func createExe() {
if err := os.RemoveAll(*outDir); err != nil && os.IsExist(err) {
fatalError(err.Error())
}
if err := os.MkdirAll(*outDir, os.ModeDir); err != nil {
fatalError(err.Error())
}
if _, err := kopsa.Copy(exePath, filepath.Join(*nwDir, "nw.exe")); err != nil {
fatalError(err.Error())
}
if err := createIcon(); err != nil {
yellow.Println(err.Error())
}
if _, err := kopsa.Copy(exePath, exePath, filepath.Join(*tmp, "bob.nw")); err != nil {
fatalError("Can't copy app.nw+nw.exe: " + err.Error())
}
}
func createIcon() error {
if _, err := os.Stat(*icon); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("Can't find '%s'", *icon)
}
return fmt.Errorf("Can't read '%s': %s", *icon, err.Error())
}
resourcer, err := exec.LookPath(filepath.Join(DEFAULT_TOL, "ar", "Resourcer.exe"))
if err != nil {
return fmt.Errorf("Can't find the Anolis Resourcer executable: %s", err.Error())
}
srcParam := fmt.Sprintf("-src:%s", exePath)
icoParam := fmt.Sprintf("-file:%s", *icon)
cmd := exec.Command(resourcer, "-op:upd", srcParam, "-type:14", "-name:IDR_MAINFRAME", icoParam)
if err := cmd.Run(); err != nil {
return fmt.Errorf("Can't embed icon: %s", err.Error())
}
return nil
}
func cleanUp() {
temps := []string{"bobunzip", "bob.nw"}
for _, temp := range temps {
if err := os.RemoveAll(filepath.Join(*tmp, temp)); err != nil {
yellow.Println(err.Error())
}
}
}
func main() {
cyan.Println("nodebob-go\n---\n\n")
fmt.Printf("NW.js files directory: %s\n", *nwDir)
fmt.Printf("Source files directory: %s\n", *srcDir)
fmt.Printf("Output directory: %s\n", *outDir)
fmt.Printf("Icon path: %s\n", *icon)
fmt.Printf("Output executable path: %s \n", exePath)
fmt.Printf("Temporary files directory: %s\n\n", *tmp)
// Make a ZIP
fmt.Print("Packaging sources... ")
createZip()
green.Println("OK")
// Unzip NW.js files if necessary
if filepath.Ext(*nwDir) == ".zip" {
fmt.Print("Unpacking NW.js files... ")
if err := archivist.Unzip(*nwDir, filepath.Join(*tmp, "bobunzip")); err != nil {
fatalError(err.Error())
}
*nwDir = filepath.Join(*tmp, "bobunzip")
green.Println("OK")
}
// Create the exe
fmt.Print("Creating the executable... ")
createExe()
green.Println("OK")
// Copy files
fmt.Print("Copying NW.js files... ")
if err := filepath.Walk(*nwDir, copyWalk); err != nil {
fatalError("Error copying NW.js files: " + err.Error())
}
green.Println("OK")
// Delete temporary files
fmt.Print("Deleting temporary files... ")
cleanUp()
green.Println("OK")
cyan.Println("Done!")
}