Browse Source

Added functionality to generate static asset bundles for cogs

Wirecog 6 years ago
parent
commit
a5f1e11117
3 changed files with 113 additions and 1 deletions
  1. 4
    0
      isokit.go
  2. 107
    0
      static.go
  3. 2
    1
      templatebundle.go

+ 4
- 0
isokit.go View File

@@ -6,3 +6,7 @@
6 6
 // Package isokit provides common isomorphic functionality intended to be used
7 7
 // in an Isomorphic Go web application.
8 8
 package isokit
9
+
10
+var (
11
+	WebAppRoot = ""
12
+)

+ 107
- 0
static.go View File

@@ -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
+}

+ 2
- 1
templatebundle.go View File

@@ -58,7 +58,8 @@ func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error
58 58
 func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string, prefixName string, templateFileExtension string) error {
59 59
 
60 60
 	templateDirectory := filepath.Clean(templatesPath)
61
-	println("td: ", templateDirectory)
61
+	RegisterStaticAssetsSearchPath(strings.Replace(templateDirectory, "/templates", "", -1))
62
+	//println("td: ", templateDirectory)
62 63
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
63 64
 		if strings.HasSuffix(path, templateFileExtension) {
64 65
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)