-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist.go
65 lines (49 loc) · 1.82 KB
/
dist.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
//go:build ignore
// +build ignore
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/vugu/vugu/distutil"
"github.com/vugu/vugu/simplehttp"
)
func main() {
clean := flag.Bool("clean", false, "Remove dist dir before starting")
dist := flag.String("dist", "dist", "Directory to put distribution files in")
flag.Parse()
start := time.Now()
if *clean {
os.RemoveAll(*dist)
}
os.MkdirAll(*dist, 0755) // create dist dir if not there
// copy static files
distutil.MustCopyDirFiltered(".", *dist, nil)
// find and copy wasm_exec.js
distutil.MustCopyFile(distutil.MustWasmExecJsPath(), filepath.Join(*dist, "wasm_exec.js"))
// check for vugugen and go get if not there
if _, err := exec.LookPath("vugugen"); err != nil {
fmt.Print(distutil.MustExec("go", "get", "github.com/vugu/vugu/cmd/vugugen"))
}
// run go generate
fmt.Print(distutil.MustExec("go", "generate", "."))
// run go build for wasm binary
fmt.Print(distutil.MustEnvExec([]string{"GOOS=js", "GOARCH=wasm"}, "go", "build", "-o", filepath.Join(*dist, "main.wasm"), "."))
// STATIC INDEX FILE:
// if you are hosting with a static file server or CDN, you can write out the default index.html from simplehttp
req, _ := http.NewRequest("GET", "/index.html", nil)
outf, err := os.OpenFile(filepath.Join(*dist, "index.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
distutil.Must(err)
defer outf.Close()
template.Must(template.New("_page_").Parse(simplehttp.DefaultPageTemplateSource)).Execute(outf, map[string]interface{}{"Request": req})
// BUILD GO SERVER:
// or if you are deploying a Go server (yay!) you can build that binary here
// fmt.Print(distutil.MustExec("go", "build", "-o", filepath.Join(*dist, "server"), "."))
log.Printf("dist.go complete in %v", time.Since(start))
}