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
 	"log"
10
 	"log"
11
 	"os"
11
 	"os"
12
 	"path/filepath"
12
 	"path/filepath"
13
+	"runtime"
13
 	"strings"
14
 	"strings"
14
 )
15
 )
15
 
16
 
21
 
22
 
22
 	return &TemplateBundle{
23
 	return &TemplateBundle{
23
 		items: map[string]string{},
24
 		items: map[string]string{},
24
-		// Funcs:   template.FuncMap{},
25
 	}
25
 	}
26
 
26
 
27
 }
27
 }
30
 	return t.items
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
 func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error {
52
 func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error {
34
 
53
 
35
 	templateDirectory := filepath.Clean(templatesPath)
54
 	templateDirectory := filepath.Clean(templatesPath)
37
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
56
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
38
 		if strings.HasSuffix(path, TemplateFileExtension) {
57
 		if strings.HasSuffix(path, TemplateFileExtension) {
39
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+"/"), TemplateFileExtension)
58
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+"/"), TemplateFileExtension)
59
+
60
+			if runtime.GOOS == "windows" {
61
+				name = normalizeTemplateNameForWindows(path, templateDirectory, TemplateFileExtension)
62
+			}
63
+
40
 			contents, err := ioutil.ReadFile(path)
64
 			contents, err := ioutil.ReadFile(path)
41
 			t.items[name] = string(contents)
65
 			t.items[name] = string(contents)
42
 
66
 
59
 
83
 
60
 	templateDirectory := filepath.Clean(templatesPath)
84
 	templateDirectory := filepath.Clean(templatesPath)
61
 	RegisterStaticAssetsSearchPath(strings.Replace(templateDirectory, string(os.PathSeparator)+"templates", "", -1))
85
 	RegisterStaticAssetsSearchPath(strings.Replace(templateDirectory, string(os.PathSeparator)+"templates", "", -1))
62
-	//println("td: ", templateDirectory)
63
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
86
 	if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
64
 		if strings.HasSuffix(path, templateFileExtension) {
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
 			contents, err := ioutil.ReadFile(path)
96
 			contents, err := ioutil.ReadFile(path)
68
 			t.items[name] = string(contents)
97
 			t.items[name] = string(contents)
69
 
98