/
embed.go
96 lines (88 loc) · 1.88 KB
/
embed.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
92
93
94
95
96
package web
import (
"bufio"
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/mjibson/esc/embed"
)
// Functions to run tsc to compile typescript and esc to embed static assests.
// Assume that working path is set to `cmd/bosun`
// Run esc to embed entire static directory into static.go
func RunEsc() {
cfg := &embed.Config{
ModTime: "0",
OutputFile: "web/static.go",
Package: "web",
Prefix: "web/static",
Ignore: `.*\.ts`,
Files: []string{"web/static"},
}
embed.Run(cfg)
}
// Run tsc to compile all ts files into bosun.js
func RunTsc() {
base := filepath.Join("web", "static", "js")
tmp := filepath.Join(base, "bosun-new.js")
dst := filepath.Join(base, "bosun.js")
args := []string{
"--out", tmp,
}
matches, _ := filepath.Glob(filepath.Join(base, "*.ts"))
sort.Strings(matches)
args = append(args, matches...)
run("tsc", args...)
if _, err := os.Stat(dst); os.IsNotExist(err) {
overwriteFile(tmp, dst)
} else {
if deepCompareDifferent(tmp, dst) {
overwriteFile(tmp, dst)
} else {
err := os.Remove(tmp)
if err != nil {
log.Println(err)
return
}
}
}
}
func run(name string, arg ...string) {
log.Println("running", name, arg)
c := exec.Command(name, arg...)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
if err := c.Run(); err != nil {
log.Printf("run error: %v: %v", name, err)
}
}
func deepCompareDifferent(file1, file2 string) bool {
sf, err := os.Open(file1)
if err != nil {
log.Fatal(err)
}
df, err := os.Open(file2)
if err != nil {
log.Fatal(err)
}
defer sf.Close()
defer df.Close()
sscan := bufio.NewScanner(sf)
dscan := bufio.NewScanner(df)
for sscan.Scan() {
dscan.Scan()
if !bytes.Equal(sscan.Bytes(), dscan.Bytes()) {
return true
}
}
return false
}
func overwriteFile(filesrc, filedst string) {
err := os.Rename(filesrc, filedst)
if err != nil {
log.Println(err)
return
}
}