forked from YouROK/TorrServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_web.go
65 lines (57 loc) · 1.46 KB
/
build_web.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
)
func main() {
dir, _ := os.Getwd()
os.Chdir("web")
run("npm", "run", "build-js")
os.Chdir(dir)
compileHtml := "web/dest/"
fs, _ := ioutil.ReadDir(compileHtml)
for _, f := range fs {
if strings.HasSuffix(f.Name(), ".html") {
name := filenameWithoutExtension(f.Name())
fmt.Println("Create template go:", "src/server/web/pages/template/"+name+"_html.go")
out, err := os.Create("src/server/web/pages/template/" + name + "_html.go")
if err != nil {
fmt.Println("Error create file", err)
os.Exit(1)
return
}
fmt.Println("Read html:", compileHtml+f.Name())
buf, err := ioutil.ReadFile(compileHtml + f.Name())
if err != nil {
fmt.Println("Error read file", err)
os.Exit(1)
return
}
fmt.Println("Write template...")
out.Write([]byte("package template \n\nvar " + strings.Title(name) + "Html = []byte{"))
for _, b := range buf {
out.Write([]byte(strconv.Itoa(int(b)) + ", "))
}
out.Write([]byte("}"))
out.Close()
fmt.Println("go fmt template...")
run("go", "fmt", "src/server/web/pages/template/"+name+"_html.go")
fmt.Println("Complete OK")
}
}
}
func run(name string, args ...string) {
cmd := exec.Command(name, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Run()
}
func filenameWithoutExtension(fn string) string {
return strings.TrimSuffix(filepath.Base(fn), path.Ext(fn))
}