-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.go
104 lines (91 loc) · 2.21 KB
/
make.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
97
98
99
100
101
102
103
104
//go:build ingore
// +build ingore
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"
)
func main() {
gop, err := getModule("github.com/goplus/gop")
check(err)
igop, _ := getModule("github.com/goplus/igop")
check(err)
tag, err := getHash()
fmt.Println(tag)
if err != nil {
panic(err)
}
// build index
data, err := ioutil.ReadFile("./index_tpl.html")
check(err)
data = bytes.Replace(data, []byte("$GopVersion"), []byte(gop.Version), 1)
data = bytes.Replace(data, []byte("$iGopVersion"), []byte(igop.Version), 1)
data = bytes.Replace(data, []byte("goplus-play.js"), []byte("igop_"+tag+".js"), 1)
err = ioutil.WriteFile("./docs/index.html", data, 0755)
err = build_js("./docs", "igop_"+tag)
check(err)
}
func check(err error) {
if err != nil {
panic(err)
}
}
func getHash() (string, error) {
h := md5.New()
for _, f := range []string{"main.go", "goplus.go", "gopherjs.go", "pkg_std.go", "pkg_runtime.go", "go.mod"} {
data, err := ioutil.ReadFile(f)
if err != nil {
return "", err
}
h.Write(data)
}
return fmt.Sprintf("%x", h.Sum(nil)[:4]), nil
// cmd := exec.Command("git", "describe", "--tag")
// return cmd.Output()
}
// GOARCH=wasm GOOS=js go build -o igop.wasm
// gopherjs build -v -m -o igop.js
func build_js(dir, tag string) error {
cmd := exec.Command("gopherjs", "build", "-a", "-v", "-m", "-o", filepath.Join(dir, tag+".js"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env := os.Environ()
cmd.Env = append(env, "GOARCH=ecmascript", "GOOS=js")
return cmd.Run()
}
func build_wasm(dir, tag string) error {
cmd := exec.Command("go", "build", "-o", filepath.Join(dir, tag+".wasm"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env := os.Environ()
cmd.Env = append(env, "GOARCH=wasm", "GOOS=js")
return cmd.Run()
}
type Module struct {
Path string
Version string
Time time.Time
Dir string
GoMod string
GoVerison string
}
func getModule(path string) (*Module, error) {
cmd := exec.Command("go", "list", "-m", "-json", path)
data, err := cmd.Output()
if err != nil {
return nil, err
}
var m Module
err = json.Unmarshal(data, &m)
if err != nil {
return nil, err
}
return &m, err
}