-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_corefn.go
72 lines (61 loc) · 1.29 KB
/
generate_corefn.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
//go:build ignore
// +build ignore
package main
import (
"bytes"
"flag"
"fmt"
"go/parser"
"go/printer"
"go/token"
"io"
"io/ioutil"
"os"
)
var pkg = flag.String("pkg", "", "package name")
func mustPrint(n int, err error) {
if err != nil {
panic(err)
}
}
func createDirtyFile(buf *bytes.Buffer, imports []string) {
mustPrint(fmt.Fprintf(buf, "package %s\n", *pkg))
mustPrint(io.WriteString(buf, "import (\n"))
for i := 0; i < len(imports); i++ {
mustPrint(fmt.Fprintf(buf, "_ \"github.com/talon-one/talang/corefn/%s\"\n", imports[i]))
}
mustPrint(io.WriteString(buf, ")\n"))
}
func main() {
flag.Parse()
if len(*pkg) == 0 {
panic("pkg not defined")
}
f, err := os.Create(fmt.Sprintf("%s_corefn.go", *pkg))
if err != nil {
panic(err)
}
defer f.Close()
fileInfo, err := ioutil.ReadDir("./corefn")
if err != nil {
panic(err)
}
var dirs []string
for i := 0; i < len(fileInfo); i++ {
if fileInfo[i].IsDir() {
dirs = append(dirs, fileInfo[i].Name())
}
}
var buf bytes.Buffer
createDirtyFile(&buf, dirs)
// pretty print
set := token.NewFileSet()
astFile, err := parser.ParseFile(set, "", buf.String(), 0)
if err != nil {
mustPrint(fmt.Println("Failed to parse package:", err))
os.Exit(1)
}
if err := printer.Fprint(f, set, astFile); err != nil {
panic(err)
}
}