Common isomorphic functionality in one kit.

templatebundle.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. type TemplateBundle struct {
  14. items map[string]string
  15. }
  16. func NewTemplateBundle() *TemplateBundle {
  17. return &TemplateBundle{
  18. items: map[string]string{},
  19. // Funcs: template.FuncMap{},
  20. }
  21. }
  22. func (t *TemplateBundle) Items() map[string]string {
  23. return t.items
  24. }
  25. func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error {
  26. templateDirectory := filepath.Clean(templatesPath)
  27. if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
  28. if strings.HasSuffix(path, TemplateFileExtension) {
  29. name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
  30. contents, err := ioutil.ReadFile(path)
  31. t.items[name] = string(contents)
  32. if err != nil {
  33. fmt.Println("error encountered while walking directory: ", err)
  34. return err
  35. }
  36. }
  37. return nil
  38. }); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string, prefixName string, templateFileExtension string) error {
  44. templateDirectory := filepath.Clean(templatesPath)
  45. println("td: ", templateDirectory)
  46. if err := filepath.Walk(templateDirectory, func(path string, info os.FileInfo, err error) error {
  47. if strings.HasSuffix(path, templateFileExtension) {
  48. name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
  49. name = prefixName + "/" + name
  50. contents, err := ioutil.ReadFile(path)
  51. t.items[name] = string(contents)
  52. if err != nil {
  53. fmt.Println("error encountered while walking directory: ", err)
  54. return err
  55. }
  56. }
  57. return nil
  58. }); err != nil {
  59. return err
  60. }
  61. return nil
  62. }