/
angular-templatecache.go
71 lines (59 loc) · 1.75 KB
/
angular-templatecache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ngcache
import (
"bytes"
"fmt"
"io"
"path/filepath"
"text/template"
"github.com/omeid/slurp"
)
type Config struct {
Name string //Name of the generated file.
Module string //Name of AngularJS module.
Standalone bool //Create a new AngularJS module, instead of using an existing.
}
func readall(r io.Reader) (string, error) {
var buff bytes.Buffer
_, err := buff.ReadFrom(r)
fmt.Println(buff.String())
return buff.String(), err
}
var cacheTemplate = template.Must(template.New("").Funcs(template.FuncMap{"readall": readall}).Parse(`angular.module("{{ .Module }}"{{ if .Standalone }} , [] {{ end }}).run(["$templateCache", function($templateCache) { {{ range $path, $file := .Files }}
$templateCache.put("{{ $path }}", "{{ $file | js }}"); {{ end }}
}])`))
type cache struct {
Config
Files map[string]*bytes.Buffer
}
// A build stage creates a new build and adds all the files coming through the channel to
// the Build and returns the result of Build as a File on the output channel.
func Build(c *slurp.C, config Config) slurp.Stage {
return func(in <-chan slurp.File, out chan<- slurp.File) {
b := cache{config, make(map[string]*bytes.Buffer)}
for file := range in {
path, _ := filepath.Rel(file.Dir, file.Path)
path = filepath.ToSlash(path)
c.Infof("Adding %s", path)
buff := new(bytes.Buffer)
_, err := buff.ReadFrom(file)
if err != nil {
c.Error(err)
}
b.Files[path] = buff
file.Close() //Close files AFTER we have build our package.
}
buff := new(bytes.Buffer)
err := cacheTemplate.Execute(buff, b)
if err != nil {
c.Error(err)
return
}
sf := slurp.File{
Reader: buff,
Path: b.Name,
}
sf.FileInfo.SetName(b.Name)
sf.FileInfo.SetSize(int64(buff.Len()))
out <- sf
}
}