2016-08-26 01:36:22 +05:30
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ignoreFile uses "git check-ignore" to determine if we should ignore a file.
|
|
|
|
func ignoreFile(p string) (ok bool, err error) {
|
|
|
|
err = exec.Command("git", "check-ignore", p).Run()
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
exitErr, ok := err.(*exec.ExitError)
|
|
|
|
if ok {
|
|
|
|
if sys := exitErr.Sys(); sys != nil {
|
|
|
|
e, ok := sys.(interface {
|
|
|
|
// Is the returned value something that returns an exit status?
|
|
|
|
ExitStatus() int
|
|
|
|
})
|
|
|
|
if ok && e.ExitStatus() == 1 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-09-06 06:36:17 +05:30
|
|
|
// Maps aren't deterministic, use a struct instead.
|
|
|
|
|
2016-08-26 01:36:22 +05:30
|
|
|
type fileData struct {
|
|
|
|
name string
|
|
|
|
data string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-09-06 06:36:17 +05:30
|
|
|
// ReadDir guarentees result in sorted order.
|
2016-08-26 01:36:22 +05:30
|
|
|
dir, err := ioutil.ReadDir("web/templates")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
files := []fileData{}
|
|
|
|
for _, file := range dir {
|
|
|
|
p := filepath.Join("web/templates", file.Name())
|
|
|
|
ignore, err := ignoreFile(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if ignore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-09-06 06:36:17 +05:30
|
|
|
if bytes.Contains(data, []byte{'`'}) {
|
|
|
|
log.Fatalf("file %s contains escape character '`' and cannot be compiled into go source", p)
|
|
|
|
}
|
2016-08-26 01:36:22 +05:30
|
|
|
files = append(files, fileData{file.Name(), string(data)})
|
|
|
|
}
|
|
|
|
|
|
|
|
f := new(bytes.Buffer)
|
|
|
|
|
|
|
|
fmt.Fprintln(f, "// This file was generated by the makefile. Do not edit.")
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
fmt.Fprintln(f, "package server")
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.")
|
|
|
|
fmt.Fprintln(f, "var defaultTemplates = map[string]string{")
|
|
|
|
for _, file := range files {
|
2016-09-06 06:36:17 +05:30
|
|
|
fmt.Fprintf(f, "\t%q: `%s`,\n", file.name, file.data)
|
2016-08-26 01:36:22 +05:30
|
|
|
}
|
|
|
|
fmt.Fprintln(f, "}")
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("server/templates_default.go", f.Bytes(), 0644); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|