/
raw.go
91 lines (74 loc) · 1.86 KB
/
raw.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bytes"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/ernestokarim/closurer/app"
"github.com/ernestokarim/closurer/config"
"github.com/ernestokarim/closurer/gss"
"github.com/ernestokarim/closurer/hooks"
"github.com/ernestokarim/closurer/js"
"github.com/ernestokarim/closurer/soy"
)
func RawOutput(r *app.Request) error {
if err := hooks.PreCompile(); err != nil {
return err
}
if err := gss.Compile(); err != nil {
return err
}
if err := soy.Compile(); err != nil {
return err
}
_, namespaces, err := js.GenerateDeps("input-production")
if err != nil {
return err
}
log.Println("Output RAW mode")
conf := config.Current()
content := bytes.NewBuffer(nil)
base := path.Join(conf.Library.Root, "closure", "goog", "base.js")
if err := addFile(content, base); err != nil {
return err
}
if err := addFile(content, path.Join(conf.Build, config.RENAMING_MAP_NAME)); err != nil {
return err
}
if err := addFile(content, path.Join(conf.Build, config.DEPS_NAME)); err != nil {
return err
}
if err := hooks.PostCompile(); err != nil {
return err
}
css := make([]byte, 0)
if conf.Gss != nil {
css, err = ioutil.ReadFile(filepath.Join(conf.Build, config.CSS_NAME))
if err != nil {
return app.Error(err)
}
}
data := map[string]interface{}{
"Content": template.HTML(string(content.Bytes())),
"Port": config.Port,
"LT": template.HTML("<"),
"Namespaces": template.HTML("'" + strings.Join(namespaces, "', '") + "'"),
"Css": template.HTML(template.JSEscapeString(string(css))),
}
r.W.Header().Set("Content-Type", "text/javascript")
return r.ExecuteTemplate([]string{"raw"}, data)
}
func addFile(w io.Writer, name string) error {
f, err := os.Open(name)
if err != nil {
return app.Error(err)
}
defer f.Close()
io.Copy(w, f)
return nil
}