forked from tbaud0n/dojoBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdojoBuilder.go
87 lines (69 loc) · 1.82 KB
/
dojoBuilder.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
package dojoBuilder
import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
)
type Config struct {
BuildMode bool // Use dojo build if true
SrcDir string // Absolute path of the src js dir
DestDir string // Absolute path where the output files will be placed
Bin string // Name of the bin used to build dojo (optional) [node, node-debug, java]
DojoConfigRelPath string // Path (relative to SrcDir) of the file containing the dojoConfig JSON
BuildConfigs map[string]BuildConfig
}
type HookFunc func() error
func SetBeforeHookFunc(f HookFunc) { beforeHook = f }
func SetAfterHookFunc(f HookFunc) { afterHook = f }
// ExcludeFunc is called
// It allows ignore some folder when linking source files to DestDir
type ExcludeFunc func(path string, f os.FileInfo) (bool, error)
var beforeHook, afterHook HookFunc
func Run(c *Config, names []string, reset bool) (err error) {
if c.DestDir == "" {
return errors.New("No DestDir defined in config")
}
if _, err = os.Stat(c.DestDir); os.IsNotExist(err) {
if err = os.MkdirAll(c.DestDir, 0754); err != nil {
return
}
}
if reset {
filepath.Walk(c.DestDir, func(path string, f os.FileInfo, err error) (_err error) {
if path != c.DestDir {
_err = os.RemoveAll(path)
}
return
})
}
if beforeHook != nil {
if err = beforeHook(); err != nil {
return
}
}
if c.BuildMode {
err = c.build(names)
} else {
err = c.installFiles()
}
if err != nil {
return
}
if afterHook != nil {
if err = afterHook(); err != nil {
return
}
}
return
}
func GetDojoConfig(c *Config) (template.JS, error) {
dojoConfigFilePath := fmt.Sprintf("%s/%s", c.DestDir, c.DojoConfigRelPath)
b, err := ioutil.ReadFile(dojoConfigFilePath)
if err != nil {
return "", err
}
return template.JS(b), err
}