mirror of https://github.com/perkeep/perkeep.git
180 lines
3.6 KiB
Go
180 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
const (
|
|
templateHeader = "Template generated by " + reactGenCmd
|
|
|
|
templateMinimal = "minimal"
|
|
templateBootstrap = "bootstrap"
|
|
)
|
|
|
|
// TODO this needs a significant overhaul to support multiple templates
|
|
// extensibility etc but works for now
|
|
func doinit(wd string, tmplName string) {
|
|
tmpl := struct {
|
|
DirName string
|
|
Bootstrap bool
|
|
}{
|
|
DirName: filepath.Base(wd),
|
|
}
|
|
|
|
switch tmplName {
|
|
case templateMinimal:
|
|
case templateBootstrap:
|
|
tmpl.Bootstrap = true
|
|
default:
|
|
panic(fmt.Errorf("unknown template %q", tmplName))
|
|
}
|
|
|
|
for fn, c := range minimal {
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
t := template.New(fn)
|
|
_, err := t.Parse(c)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to parse template %v: %v", fn, err))
|
|
}
|
|
|
|
err = t.Execute(b, tmpl)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to execute template %v: %v", fn, err))
|
|
}
|
|
|
|
toWrite := b.Bytes()
|
|
|
|
fp := filepath.Join(wd, fn)
|
|
|
|
if strings.HasSuffix(fn, ".go") {
|
|
|
|
out, err := goFmtBuf(b)
|
|
if err == nil {
|
|
toWrite = out.Bytes()
|
|
}
|
|
}
|
|
|
|
err = ioutil.WriteFile(fp, toWrite, 0644)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to write file %v: %v", fp, err))
|
|
}
|
|
}
|
|
|
|
gg := exec.Command("go", "generate")
|
|
gg.Dir = wd
|
|
gg.Stderr = os.Stderr
|
|
gg.Stdout = os.Stdout
|
|
|
|
err := gg.Run()
|
|
if err != nil {
|
|
fatalf("failed to run go generate: %v", err)
|
|
}
|
|
}
|
|
|
|
var minimal = map[string]string{
|
|
// app.go
|
|
"app.go": `// ` + templateHeader + `
|
|
|
|
package main
|
|
|
|
import (
|
|
"myitcv.io/react"
|
|
)
|
|
|
|
type AppDef struct {
|
|
react.ComponentDef
|
|
}
|
|
|
|
func App() *AppElem {
|
|
return buildAppElem()
|
|
}
|
|
|
|
func (a AppDef) Render() react.Element {
|
|
return react.Div(nil,
|
|
react.H1(nil,
|
|
react.S("Hello World"),
|
|
),
|
|
react.P(nil,
|
|
react.S("This is my first GopherJS React App."),
|
|
),
|
|
)
|
|
}
|
|
`,
|
|
|
|
// main.go
|
|
"main.go": `// ` + templateHeader + `
|
|
|
|
package main
|
|
|
|
import (
|
|
"myitcv.io/react"
|
|
|
|
"honnef.co/go/js/dom"
|
|
)
|
|
|
|
//go:generate reactGen
|
|
|
|
var document = dom.GetWindow().Document()
|
|
|
|
func main() {
|
|
domTarget := document.GetElementByID("app")
|
|
|
|
react.Render(App(), domTarget)
|
|
}
|
|
`,
|
|
|
|
// index.html
|
|
"index.html": `<!--` + templateHeader + `-->
|
|
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
{{if .Bootstrap}}
|
|
<title>Hello World Bootstrap</title>
|
|
|
|
<!-- START BOOTSTRAP -->
|
|
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
|
|
|
|
|
|
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
|
<!--[if lt IE 9]>
|
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
|
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
|
<![endif]-->
|
|
|
|
<!-- END BOOTSTRAP -->
|
|
{{else}}
|
|
<title>Hello World</title>
|
|
{{end}}
|
|
|
|
</head>
|
|
<body>
|
|
{{if .Bootstrap}}
|
|
<div class="container-fluid">
|
|
{{end}}
|
|
<div id="app"></div>
|
|
{{if .Bootstrap}}
|
|
</div>
|
|
{{end}}
|
|
<script src="{{.DirName}}.js"></script>
|
|
</body>
|
|
</html>
|
|
`,
|
|
}
|