-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
204 lines (158 loc) · 4.69 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
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
package dojoBuilder
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"text/template"
)
const profileTemplate = `var profile = {{.}};`
type BuildConfig struct {
RemoveUncompressed bool `json:"removeUncompressed,omitempty"` // Remove uncompressed js files after build
RemoveConsoleStripped bool `json:"removeConsoleStripped,omitempty"`
BasePath string `json:"basePath"`
ReleaseDir string `json:"releaseDir"`
ReleaseName string `json:"releaseName,omitempty"`
Action string `json:"action"`
Packages []Package `json:"packages"`
Layers map[string]Layer `json:"layers"`
LayerOptimize string `json:"layerOptimize,omitempty"`
Optimize string `json:"optimize,omitempty"`
CssOptimize string `json:"cssOptimize,omitempty"`
Mini bool `json:"mini,omitempty"`
StripConsole string `json:"stripConsole,omitempty"`
SelectorEngine string `json:"selectorEngine,omitempty"`
StaticHasFeatures map[string]Feature `json:"staticHasFeatures,omitempty"`
UseSourceMaps bool `json:"useSourceMaps"` // Build generate source maps
}
type Package struct {
Name string `json:"name"`
Location string `json:"location"`
}
type Layer struct {
Boot bool `json:"boot"`
CustomBase bool `json:"customBase"`
Include []string `json:"include,omitempty"`
Exclude []string `json:"exclude,omitempty"`
}
type Feature bool
func (f Feature) MarshalJSON() ([]byte, error) {
var v uint8 = 0
if bool(f) {
v = 1
}
return json.Marshal(v)
}
var (
buildExcludeFunc ExcludeFunc = func(path string, f os.FileInfo) (bool, error) {
return false, nil
}
// DefaultBuildExcludeFunc skips uncompressed and consoleStripped js files
DefaultBuildExcludeFunc = func(path string, f os.FileInfo) (bool, error) {
var skippedFilesPatterns []string = []string{`.*\.js\.(uncompressed|consoleStripped)\.js`}
var skippedDirsPatterns []string = []string{}
if f.IsDir() {
return IsMatchSliceMember(skippedDirsPatterns, path)
}
return IsMatchSliceMember(skippedFilesPatterns, path)
}
)
func SetBuildExcludeFunc(exFunc ExcludeFunc) { buildExcludeFunc = exFunc }
func (c *Config) generateBuildProfile(name string) (profileFullPath string, err error) {
bc, ok := c.BuildConfigs[name]
if !ok {
return "", errors.New("No build config found with name '" + name + "'")
}
if bc.Action == "" {
bc.Action = "release"
}
profilePath := c.SrcDir + "/profiles/"
os.MkdirAll(profilePath, 0754)
profileFullPath = profilePath + name + ".profile.js"
bc.BasePath = ".."
bc.ReleaseDir = c.DestDir + "/dojoBuilderTMP"
j, err := json.Marshal(bc)
if err != nil {
return "", err
}
f, err := os.OpenFile(profileFullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return "", err
}
t := template.Must(template.New("profileTemplate").Parse(profileTemplate))
err = t.Execute(f, string(j))
return profileFullPath, err
}
func (c *Config) build(names []string) (err error) {
var profilePath string
if len(names) == 0 {
for n, _ := range c.BuildConfigs {
names = append(names, n)
}
}
for _, n := range names {
fmt.Printf("Generating %s build\n", n)
profilePath, err = c.generateBuildProfile(n)
if err != nil {
return
}
if err = c.executeBuildProfile(profilePath); err != nil {
return
}
bc, _ := c.BuildConfigs[n]
bc.ReleaseDir = c.DestDir + "/dojoBuilderTMP"
err = filepath.Walk(bc.ReleaseDir, func(path string, f os.FileInfo, err error) (_err error) {
if path == bc.ReleaseDir {
return
}
isDir := f.IsDir()
dest := c.DestDir + path[len(bc.ReleaseDir):]
if skip, err := buildExcludeFunc(path, f); err != nil {
return err
} else if skip {
if isDir {
return filepath.SkipDir
}
return
} else if isDir {
if _err = os.Mkdir(dest, 0754); _err != nil {
return
}
} else if _err = CopyFile(path, dest); _err != nil {
return
}
st := f.Sys().(*syscall.Stat_t)
os.Chown(dest, int(st.Uid), int(st.Gid))
return
})
os.RemoveAll(bc.ReleaseDir)
}
return
}
func (c *Config) executeBuildProfile(profilePath string) (err error) {
buildScriptPath := c.SrcDir + "/util/buildscripts/build.sh"
args := []string{"--profile", profilePath}
if c.Bin != "" {
args = append(args, "--bin", c.Bin)
}
cmd := exec.Command(buildScriptPath, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
err = cmd.Run()
if err != nil {
return errors.New("Build command failed")
}
return
}