|
@@ -0,0 +1,107 @@
|
|
1
|
+package isokit
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "io/ioutil"
|
|
5
|
+ "log"
|
|
6
|
+ "os"
|
|
7
|
+ "path/filepath"
|
|
8
|
+ "regexp"
|
|
9
|
+)
|
|
10
|
+
|
|
11
|
+var StaticAssetsPath string
|
|
12
|
+var CogStaticAssetsSearchPaths []string
|
|
13
|
+
|
|
14
|
+func RegisterStaticAssetsSearchPath(path string) {
|
|
15
|
+ //fmt.Println("cog search path: ", path)
|
|
16
|
+ CogStaticAssetsSearchPaths = append(CogStaticAssetsSearchPaths, path)
|
|
17
|
+}
|
|
18
|
+
|
|
19
|
+func findStaticAssets(ext string, paths []string) []string {
|
|
20
|
+
|
|
21
|
+ var files []string
|
|
22
|
+
|
|
23
|
+ for i := 0; i < len(paths); i++ {
|
|
24
|
+ //fmt.Println("file search path: ", paths[i])
|
|
25
|
+ filepath.Walk(paths[i], func(path string, f os.FileInfo, _ error) error {
|
|
26
|
+ if !f.IsDir() {
|
|
27
|
+ r, err := regexp.MatchString(ext, f.Name())
|
|
28
|
+ if err == nil && r {
|
|
29
|
+ files = append(files, path)
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+ return nil
|
|
33
|
+ })
|
|
34
|
+
|
|
35
|
+ }
|
|
36
|
+ return files
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+func bundleJavaScript(jsfiles []string) {
|
|
40
|
+
|
|
41
|
+ var result []byte = make([]byte, 0)
|
|
42
|
+
|
|
43
|
+ if StaticAssetsPath == "" {
|
|
44
|
+ return
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ if _, err := os.Stat(StaticAssetsPath + "/js"); os.IsNotExist(err) {
|
|
48
|
+ os.Mkdir(StaticAssetsPath+"/js", 0711)
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ destinationFile := StaticAssetsPath + "/js/cogimports.js"
|
|
52
|
+
|
|
53
|
+ for i := 0; i < len(jsfiles); i++ {
|
|
54
|
+ b, err := ioutil.ReadFile(jsfiles[i])
|
|
55
|
+ if err != nil {
|
|
56
|
+ log.Println(err)
|
|
57
|
+ }
|
|
58
|
+ result = append(result, b...)
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ err := ioutil.WriteFile(destinationFile, result, 0644)
|
|
62
|
+ if err != nil {
|
|
63
|
+ log.Println(err)
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+func bundleCSS(cssfiles []string) {
|
|
69
|
+
|
|
70
|
+ var result []byte = make([]byte, 0)
|
|
71
|
+
|
|
72
|
+ if StaticAssetsPath == "" {
|
|
73
|
+ return
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ if _, err := os.Stat(StaticAssetsPath + "/css"); os.IsNotExist(err) {
|
|
77
|
+ os.Mkdir(StaticAssetsPath+"/css", 0711)
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ destinationFile := StaticAssetsPath + "/css/cogimports.css"
|
|
81
|
+
|
|
82
|
+ for i := 0; i < len(cssfiles); i++ {
|
|
83
|
+ b, err := ioutil.ReadFile(cssfiles[i])
|
|
84
|
+ if err != nil {
|
|
85
|
+ log.Println(err)
|
|
86
|
+ }
|
|
87
|
+ result = append(result, b...)
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ err := ioutil.WriteFile(destinationFile, result, 0644)
|
|
91
|
+ if err != nil {
|
|
92
|
+ log.Println(err)
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+func BundleStaticAssets() {
|
|
98
|
+
|
|
99
|
+ jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
|
|
100
|
+ bundleJavaScript(jsfiles)
|
|
101
|
+ cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
|
|
102
|
+ bundleCSS(cssfiles)
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+func init() {
|
|
106
|
+ CogStaticAssetsSearchPaths = make([]string, 0)
|
|
107
|
+}
|