forked from getgauge/gauge-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.go
536 lines (473 loc) · 13 KB
/
make.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge-Java.
// This program is free software.
//
// It is dual-licensed under:
// 1) the GNU General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version;
// or
// 2) the Eclipse Public License v1.0.
//
// You can redistribute it and/or modify it under the terms of either license.
// We would then provide copied of each license in a separate .txt file with the name of the license as the title of the file.
package main
import (
"archive/zip"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)
const (
BUILD_DIR = "tmp"
CGO_ENABLED = "CGO_ENABLED"
)
const (
dotGauge = ".gauge"
plugins = "plugins"
GOARCH = "GOARCH"
GOOS = "GOOS"
X86 = "386"
X86_64 = "amd64"
DARWIN = "darwin"
LINUX = "linux"
WINDOWS = "windows"
bin = "bin"
newDirPermissions = 0755
gauge = "gauge"
gaugeJava = "gauge-java"
deploy = "deploy"
commonDep = "github.com/getgauge/common"
targetDir = "target"
jarExt = ".jar"
)
var BUILD_DIR_BIN = filepath.Join(BUILD_DIR, bin)
var BUILD_DIR_SRC = filepath.Join(BUILD_DIR, "src")
var BUILD_DIR_PKG = filepath.Join(BUILD_DIR, "pkg")
var BUILD_DIR_GAUGE_JAVA = filepath.Join(BUILD_DIR_SRC, gaugeJava)
var platformBinDir = filepath.Join(bin, fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
var deployDir = filepath.Join(deploy, gaugeJava)
var gaugeJavaFiles = []string{"gauge-java.go"}
func isExecMode(mode os.FileMode) bool {
return (mode & 0111) != 0
}
func mirrorFile(src, dst string) error {
sfi, err := os.Stat(src)
if err != nil {
return err
}
if sfi.Mode()&os.ModeType != 0 {
log.Fatalf("mirrorFile can't deal with non-regular file %s", src)
}
dfi, err := os.Stat(dst)
if err == nil &&
isExecMode(sfi.Mode()) == isExecMode(dfi.Mode()) &&
(dfi.Mode()&os.ModeType == 0) &&
dfi.Size() == sfi.Size() &&
dfi.ModTime().Unix() == sfi.ModTime().Unix() {
// Seems to not be modified.
return nil
}
dstDir := filepath.Dir(dst)
if err := os.MkdirAll(dstDir, newDirPermissions); err != nil {
return err
}
df, err := os.Create(dst)
if err != nil {
return err
}
sf, err := os.Open(src)
if err != nil {
return err
}
defer sf.Close()
n, err := io.Copy(df, sf)
if err == nil && n != sfi.Size() {
err = fmt.Errorf("copied wrong size for %s -> %s: copied %d; want %d", src, dst, n, sfi.Size())
}
cerr := df.Close()
if err == nil {
err = cerr
}
if err == nil {
err = os.Chmod(dst, sfi.Mode())
}
if err == nil {
err = os.Chtimes(dst, sfi.ModTime(), sfi.ModTime())
}
return err
}
func mirrorDir(src, dst string) error {
err := filepath.Walk(src, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
suffix, err := filepath.Rel(src, path)
if err != nil {
return fmt.Errorf("Failed to find Rel(%q, %q): %v", src, path, err)
}
return mirrorFile(path, filepath.Join(dst, suffix))
})
return err
}
func createGoPathForBuild() {
err := os.MkdirAll(BUILD_DIR_SRC, newDirPermissions)
if err != nil {
panic(err)
}
err = os.MkdirAll(BUILD_DIR_BIN, newDirPermissions)
if err != nil {
panic(err)
}
err = os.MkdirAll(BUILD_DIR_PKG, newDirPermissions)
if err != nil {
panic(err)
}
err = os.MkdirAll(BUILD_DIR_GAUGE_JAVA, newDirPermissions)
if err != nil {
panic(err)
}
}
//Copy gauge java files to GOPATH
func copyGaugeJavaFilesToGoPath() {
for _, f := range gaugeJavaFiles {
mirrorFile(f, path.Join(BUILD_DIR_SRC, gaugeJava, f))
}
}
func setGoEnv() {
absBuildDir, err := filepath.Abs(BUILD_DIR)
if err != nil {
panic(err)
}
set("GOPATH", absBuildDir)
set("GOBIN", filepath.Join(absBuildDir, bin))
}
func set(envName, envValue string) {
log.Printf("%s = %s\n", envName, envValue)
err := os.Setenv(envName, envValue)
if err != nil {
panic(err)
}
}
func runProcess(command string, workingDir string, arg ...string) {
cmd := exec.Command(command, arg...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = workingDir
log.Printf("Execute %v\n", cmd.Args)
err := cmd.Run()
if err != nil {
panic(err)
}
}
func executeCommand(command string, arg ...string) (string, error) {
cmd := exec.Command(command, arg...)
bytes, err := cmd.Output()
return strings.TrimSpace(fmt.Sprintf("%s", bytes)), err
}
func compileGoPackage(packageName string) {
setGoEnv()
runProcess("go", BUILD_DIR, "get", "-d", "-u", commonDep)
runProcess("go", BUILD_DIR, "install", "-v", packageName)
}
func copyBinaries() {
err := os.MkdirAll(bin, newDirPermissions)
if err != nil {
panic(err)
}
err = mirrorDir(BUILD_DIR_BIN, bin)
if err != nil {
panic(err)
}
absBin, err := filepath.Abs(bin)
if err != nil {
panic(err)
}
log.Printf("Binaries are available at: %s\n", absBin)
}
// key will be the source file and value will be the target
func copyFiles(files map[string]string, installDir string) {
for src, dst := range files {
base := filepath.Base(src)
installDst := filepath.Join(installDir, dst)
log.Printf("Copying %s -> %s\n", src, installDst)
stat, err := os.Stat(src)
if err != nil {
panic(err)
}
if stat.IsDir() {
err = mirrorDir(src, installDst)
} else {
err = mirrorFile(src, filepath.Join(installDst, base))
}
if err != nil {
panic(err)
}
}
}
func copyGaugeJavaFiles(destDir string) {
files := make(map[string]string)
if getGOOS() == "windows" {
files[filepath.Join(getBinDir(), "gauge-java.exe")] = bin
} else {
files[filepath.Join(getBinDir(), gaugeJava)] = bin
}
files[filepath.Join("java.json")] = ""
files[filepath.Join("skel", "StepImplementation.java")] = filepath.Join("skel")
files[filepath.Join("skel", "java.properties")] = filepath.Join("skel", "env")
files[filepath.Join("libs")] = filepath.Join("libs")
files[filepath.Join("notice.md")] = ""
files[filepath.Join(targetDir, gaugeJava+"-"+getGaugeJavaVersion()+jarExt)] = "libs"
copyFiles(files, destDir)
}
func copyLibsFromMaven() {
runCommand("mvn", "dependency:copy-dependencies", "-DoutputDirectory=libs", "-DexcludeTransitive=true", "-DexcludeScope=test")
}
func getFilesByExt(dir string, ext string) []string {
files := make([]string, 0)
filepath.Walk(dir, func(currentPath string, info os.FileInfo, err error) error {
if filepath.Ext(currentPath) == ext {
files = append(files, currentPath)
}
return nil
})
return files
}
func getGaugeJavaVersion() string {
javaRunnerProperties, err := getPluginProperties("java.json")
if err != nil {
panic(fmt.Sprintf("Failed to get gauge java properties file. %s", err))
}
return javaRunnerProperties["version"].(string)
}
func getBinDir() string {
if *binDir == "" {
return filepath.Join(bin, fmt.Sprintf("%s_%s", getGOOS(), getGOARCH()))
}
return path.Join(bin, *binDir)
}
func moveOSBinaryToCurrentOSArchDirectory(targetName string) {
destDir := path.Join(bin, fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
moveBinaryToDirectory(path.Base(targetName), destDir)
}
func moveBinaryToDirectory(target, destDir string) error {
if runtime.GOOS == "windows" {
target = target + ".exe"
}
srcFile := path.Join(bin, target)
destFile := path.Join(destDir, target)
if err := os.MkdirAll(destDir, newDirPermissions); err != nil {
return err
}
if err := mirrorFile(srcFile, destFile); err != nil {
return err
}
return os.Remove(srcFile)
}
func setEnv(envVariables map[string]string) {
for k, v := range envVariables {
os.Setenv(k, v)
}
}
var install = flag.Bool("install", false, "Install to the specified prefix")
var pluginInstallPrefix = flag.String("plugin-prefix", "", "Specifies the prefix where gauge plugins will be installed")
var distro = flag.Bool("distro", false, "Creates distributables for gauge java")
var test = flag.Bool("test", false, "Runs tests")
var allPlatforms = flag.Bool("all-platforms", false, "Compiles or creates distributables for all platforms windows, linux, darwin both x86 and x86_64")
var binDir = flag.String("bin-dir", "", "Specifies OS_PLATFORM specific binaries to install when cross compiling")
var (
platformEnvs = []map[string]string{
map[string]string{GOARCH: X86, GOOS: DARWIN, CGO_ENABLED: "0"},
map[string]string{GOARCH: X86_64, GOOS: DARWIN, CGO_ENABLED: "0"},
map[string]string{GOARCH: X86, GOOS: LINUX, CGO_ENABLED: "0"},
map[string]string{GOARCH: X86_64, GOOS: LINUX, CGO_ENABLED: "0"},
map[string]string{GOARCH: X86, GOOS: WINDOWS, CGO_ENABLED: "0"},
map[string]string{GOARCH: X86_64, GOOS: WINDOWS, CGO_ENABLED: "0"},
}
)
func getPluginProperties(jsonPropertiesFile string) (map[string]interface{}, error) {
pluginPropertiesJson, err := ioutil.ReadFile(jsonPropertiesFile)
if err != nil {
fmt.Printf("Could not read %s: %s\n", filepath.Base(jsonPropertiesFile), err)
return nil, err
}
var pluginJson interface{}
if err = json.Unmarshal([]byte(pluginPropertiesJson), &pluginJson); err != nil {
fmt.Printf("Could not read %s: %s\n", filepath.Base(jsonPropertiesFile), err)
return nil, err
}
return pluginJson.(map[string]interface{}), nil
}
func main() {
createGoPathForBuild()
copyGaugeJavaFilesToGoPath()
flag.Parse()
if *install {
updatePluginInstallPrefix()
installGaugeJava(*pluginInstallPrefix)
} else if *distro {
createGaugeDistro(*allPlatforms)
} else if *test {
compileGoPackage(gaugeJava)
runMavenTests()
} else {
compileGaugeJava()
}
}
func runMavenTests() {
runCommand("mvn", "test")
}
func compileGaugeJava() {
buildGaugeJavaJar()
if *allPlatforms {
compileGaugeJavaAcrossPlatforms()
} else {
compileGoPackage(gaugeJava)
}
copyBinaries()
moveOSBinaryToCurrentOSArchDirectory(gaugeJava)
}
func createGaugeDistro(forAllPlatforms bool) {
if forAllPlatforms {
for _, platformEnv := range platformEnvs {
setEnv(platformEnv)
fmt.Printf("Creating distro for platform => OS:%s ARCH:%s \n", platformEnv[GOOS], platformEnv[GOARCH])
createDistro()
}
} else {
createDistro()
}
}
func createDistro() {
packageName := fmt.Sprintf("%s-%s-%s.%s", gaugeJava, getGaugeJavaVersion(), getGOOS(), getArch())
distroDir := filepath.Join(deploy, packageName)
copyGaugeJavaFiles(distroDir)
createZipFromUtil(deploy, packageName)
os.RemoveAll(distroDir)
}
func runCommand(command string, arg ...string) {
cmd := exec.Command(command, arg...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Printf("Execute %v\n", cmd.Args)
err := cmd.Run()
if err != nil {
panic(err)
}
}
func createZipFromUtil(dir, name string) {
wd, _ := os.Getwd()
os.Chdir(filepath.Join(dir, name))
runCommand("zip", "-r", filepath.Join("..", name+".zip"), ".")
os.Chdir(wd)
}
func createZip(dir, packageName string) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
os.Chdir(dir)
zipFileName := packageName + ".zip"
newfile, err := os.Create(zipFileName)
if err != nil {
panic(err)
}
defer newfile.Close()
zipWriter := zip.NewWriter(newfile)
defer zipWriter.Close()
filepath.Walk(packageName, func(path string, info os.FileInfo, err error) error {
infoHeader, err := zip.FileInfoHeader(info)
if err != nil {
panic(err)
}
infoHeader.Name = strings.Replace(path, fmt.Sprintf("%s%c", packageName, filepath.Separator), "", 1)
if info.IsDir() {
return nil
}
writer, err := zipWriter.CreateHeader(infoHeader)
if err != nil {
panic(err)
}
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
panic(err)
}
return nil
})
os.Chdir(wd)
}
func compileGaugeJavaAcrossPlatforms() {
for _, platformEnv := range platformEnvs {
setEnv(platformEnv)
fmt.Printf("Compiling for platform => OS:%s ARCH:%s \n", platformEnv[GOOS], platformEnv[GOARCH])
compileGoPackage(gaugeJava)
}
}
func buildGaugeJavaJar() {
os.RemoveAll(targetDir)
runCommand("mvn", "package")
}
func installGaugeJava(installPrefix string) {
os.RemoveAll(deployDir)
copyGaugeJavaFiles(deployDir)
javaRunnerInstallPath := filepath.Join(installPrefix, "java", getGaugeJavaVersion())
log.Printf("Copying %s -> %s\n", deployDir, javaRunnerInstallPath)
mirrorDir(deployDir, javaRunnerInstallPath)
}
func updatePluginInstallPrefix() {
if *pluginInstallPrefix == "" {
if runtime.GOOS == "windows" {
*pluginInstallPrefix = os.Getenv("APPDATA")
if *pluginInstallPrefix == "" {
panic(fmt.Errorf("Failed to find AppData directory"))
}
*pluginInstallPrefix = filepath.Join(*pluginInstallPrefix, gauge, plugins)
} else {
userHome := getUserHome()
if userHome == "" {
panic(fmt.Errorf("Failed to find User Home directory"))
}
*pluginInstallPrefix = filepath.Join(userHome, dotGauge, plugins)
}
}
}
func getUserHome() string {
return os.Getenv("HOME")
}
func getArch() string {
arch := getGOARCH()
if arch == X86 {
return "x86"
}
return "x86_64"
}
func getGOARCH() string {
goArch := os.Getenv(GOARCH)
if goArch == "" {
return runtime.GOARCH
}
return goArch
}
func getGOOS() string {
os := os.Getenv(GOOS)
if os == "" {
return runtime.GOOS
}
return os
}