-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.go
125 lines (98 loc) · 2.63 KB
/
build.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
package cmd
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
)
var buildCmdConfig buildConfig
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "Builds your abcweb binary and executes the gulp build task",
Long: "Builds your abcweb binary and executes the gulp build task",
Example: "abcweb build",
RunE: buildCmdRun,
}
func init() {
buildCmd.Flags().BoolP("go-only", "g", false, "Only build the go binary")
buildCmd.Flags().BoolP("assets-only", "a", false, "Only build the assets")
buildCmd.Flags().BoolP("verbose", "v", false, "Verbose output")
buildCmd.Flags().BoolP("alpine", "", false, "Create a statically linked binary for compatibility with Alpine systems")
RootCmd.AddCommand(buildCmd)
}
func buildCmdRun(cmd *cobra.Command, args []string) error {
cnf.ModeViper.BindPFlags(cmd.Flags())
// Bare minimum requires git and go dependencies
checkDep("git", "go")
if !cnf.ModeViper.GetBool("go-only") {
fmt.Println("Building assets...")
if err := buildAssets(); err != nil {
return err
}
}
if !cnf.ModeViper.GetBool("assets-only") {
fmt.Println("Building Go app...")
if err := buildApp(); err != nil {
return err
}
}
return nil
}
func buildApp() error {
args := []string{"build"}
if cnf.ModeViper.GetBool("verbose") {
args = append(args, "-v")
}
if cnf.ModeViper.GetBool("alpine") {
args = append(args, "-tags netgo")
}
var version string
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
out := &bytes.Buffer{}
cmd.Stdout = out
_ = cmd.Run()
version = strings.TrimSpace(out.String())
if len(version) == 0 {
version = "0"
}
buildTime := time.Now().Format(time.RFC3339)
args = append(args, "-trimpath", "-ldflags", fmt.Sprintf("-X main.version=%s -X main.buildTime=%s", version, buildTime))
cmd = exec.Command("go", args...)
cmd.Dir = cnf.AppPath
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func buildAssets() error {
_, err := os.Stat(filepath.Join(cnf.AppPath, "gulpfile.js"))
if os.IsNotExist(err) {
fmt.Println("No gulpfile.js present, skipping gulp build.")
return nil
} else if err != nil {
return err
}
_, err = exec.LookPath("gulp")
if err != nil {
fmt.Println("Cannot find gulp in PATH. Is it installed? Skipping gulp build.")
return nil
}
cmd := exec.Command("gulp", "build")
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err = cmd.Run(); err != nil {
return err
}
fmt.Printf("SUCCESS.\n\n")
return nil
}