Browse Source

Corrected template bundling for Windows

Wirecog 6 years ago
parent
commit
b945207be9
1 changed files with 33 additions and 4 deletions
  1. 33
    4
      templatebundle.go

+ 33
- 4
templatebundle.go View File

@@ -10,6 +10,7 @@ import (
10 10
 	"log"
11 11
 	"os"
12 12
 	"path/filepath"
13
+	"runtime"
13 14
 	"strings"
14 15
 )
15 16
 
@@ -21,7 +22,6 @@ func NewTemplateBundle() *TemplateBundle {
21 22
 
22 23
 	return &TemplateBundle{
23 24
 		items: map[string]string{},
24
-		// Funcs:   template.FuncMap{},
25 25
 	}
26 26
 
27 27
 }
@@ -30,6 +30,25 @@ func (t *TemplateBundle) Items() map[string]string {
30 30
 	return t.items
31 31
 }
32 32
 
33
+func normalizeTemplateNameForWindows(path, templateDirectory, TemplateFileExtension string) string {
34
+
35
+	result := strings.Replace(path, templateDirectory, "", -1)
36
+	result = strings.Replace(result, string(os.PathSeparator), "/", -1)
37
+	result = strings.Replace(result, TemplateFileExtension, "", -1)
38
+	result = strings.TrimPrefix(result, `/`)
39
+	return result
40
+}
41
+
42
+func normalizeCogTemplateNameForWindows(path, templateDirectory, TemplateFileExtension string) string {
43
+
44
+	result := strings.Replace(path, templateDirectory, "", -1)
45
+	result = strings.Replace(result, string(os.PathSeparator), "/", -1)
46
+	result = strings.Replace(result, TemplateFileExtension, "", -1)
47
+	result = strings.TrimPrefix(result, `/`)
48
+	result = result + "/" + result
49
+	return result
50
+}
51
+
33 52
 func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error {
34 53
 
35 54
 	templateDirectory := filepath.Clean(templatesPath)
@@ -37,6 +56,11 @@ func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error
37 56
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
38 57
 		if strings.HasSuffix(path, TemplateFileExtension) {
39 58
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+"/"), TemplateFileExtension)
59
+
60
+			if runtime.GOOS == "windows" {
61
+				name = normalizeTemplateNameForWindows(path, templateDirectory, TemplateFileExtension)
62
+			}
63
+
40 64
 			contents, err := ioutil.ReadFile(path)
41 65
 			t.items[name] = string(contents)
42 66
 
@@ -59,11 +83,16 @@ func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string,
59 83
 
60 84
 	templateDirectory := filepath.Clean(templatesPath)
61 85
 	RegisterStaticAssetsSearchPath(strings.Replace(templateDirectory, string(os.PathSeparator)+"templates", "", -1))
62
-	//println("td: ", templateDirectory)
63 86
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
64 87
 		if strings.HasSuffix(path, templateFileExtension) {
65
-			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+"/"), TemplateFileExtension)
66
-			name = prefixName + "/" + name
88
+			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory), TemplateFileExtension)
89
+
90
+			if runtime.GOOS == "windows" {
91
+				name = normalizeCogTemplateNameForWindows(path, templateDirectory, TemplateFileExtension)
92
+				prefixName = "cog:"
93
+			}
94
+
95
+			name = prefixName + name
67 96
 			contents, err := ioutil.ReadFile(path)
68 97
 			t.items[name] = string(contents)
69 98