Common isomorphic functionality in one kit.

templatebundle.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // The Isomorphic Go Project
  2. // Copyright (c) Wirecog, LLC. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license, which can be found in the LICENSE file.
  5. package isokit
  6. import (
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. )
  14. type TemplateBundle struct {
  15. items map[string]string
  16. }
  17. func NewTemplateBundle() *TemplateBundle {
  18. return &TemplateBundle{
  19. items: map[string]string{},
  20. }
  21. }
  22. func (t *TemplateBundle) Items() map[string]string {
  23. return t.items
  24. }
  25. func normalizeTemplateNameForWindows(path, templateDirectory, TemplateFileExtension string) string {
  26. result := strings.Replace(path, templateDirectory, "", -1)
  27. result = strings.Replace(result, string(os.PathSeparator), "/", -1)
  28. result = strings.Replace(result, TemplateFileExtension, "", -1)
  29. result = strings.TrimPrefix(result, `/`)
  30. return result
  31. }
  32. func normalizeCogTemplateNameForWindows(path, templateDirectory, TemplateFileExtension string) string {
  33. result := strings.Replace(path, templateDirectory, "", -1)
  34. result = strings.Replace(result, string(os.PathSeparator), "/", -1)
  35. result = strings.Replace(result, TemplateFileExtension, "", -1)
  36. result = strings.TrimPrefix(result, `/`)
  37. result = result + "/" + result
  38. return result
  39. }
  40. func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error {
  41. templateDirectory := filepath.Clean(templatesPath)
  42. if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
  43. if strings.HasSuffix(path, TemplateFileExtension) {
  44. name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+"/"), TemplateFileExtension)
  45. if runtime.GOOS == "windows" {
  46. name = normalizeTemplateNameForWindows(path, templateDirectory, TemplateFileExtension)
  47. }
  48. contents, err := ioutil.ReadFile(path)
  49. t.items[name] = string(contents)
  50. if err != nil {
  51. log.Println("error encountered while walking directory: ", err)
  52. return err
  53. }
  54. }
  55. return nil
  56. }); err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string, prefixName string, templateFileExtension string) error {
  62. templateDirectory := filepath.Clean(templatesPath)
  63. RegisterStaticAssetsSearchPath(strings.Replace(templateDirectory, string(os.PathSeparator)+"templates", "", -1))
  64. if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
  65. if strings.HasSuffix(path, templateFileExtension) {
  66. name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory), TemplateFileExtension)
  67. if runtime.GOOS == "windows" {
  68. name = normalizeCogTemplateNameForWindows(path, templateDirectory, TemplateFileExtension)
  69. prefixName = "cog:"
  70. }
  71. name = prefixName + name
  72. contents, err := ioutil.ReadFile(path)
  73. t.items[name] = string(contents)
  74. if err != nil {
  75. log.Println("error encountered while walking directory: ", err)
  76. return err
  77. }
  78. }
  79. return nil
  80. }); err != nil {
  81. return err
  82. }
  83. return nil
  84. }