-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
163 lines (135 loc) · 4.53 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
func use() {
fmt.Fprintf(os.Stderr, "\nuse: bambam -o outdir -p package myGoSourceFile.go myGoSourceFile2.go ...\n")
fmt.Fprintf(os.Stderr, " # Bambam makes it easy to use Capnproto serialization[1] from Go.\n")
fmt.Fprintf(os.Stderr, " # Bambam reads .go files and writes a .capnp schema and Go bindings.\n")
fmt.Fprintf(os.Stderr, " # options:\n")
fmt.Fprintf(os.Stderr, " # -o=\"odir\" specifies the directory to write to (created if need be).\n")
fmt.Fprintf(os.Stderr, " # -p=\"main\" specifies the package header to write (e.g. main, mypkg).\n")
fmt.Fprintf(os.Stderr, " # -X exports private fields of Go structs. Default only maps public fields.\n")
fmt.Fprintf(os.Stderr, " # -version shows build version with git commit hash.\n")
fmt.Fprintf(os.Stderr, " # -debug print lots of debug info as we process.\n")
fmt.Fprintf(os.Stderr, " # -OVERWRITE modify .go files in-place, adding capid tags (write to -o dir by default).\n")
fmt.Fprintf(os.Stderr, " # required: at least one .go source file for struct definitions. Must be last, after options.\n")
fmt.Fprintf(os.Stderr, " #\n")
fmt.Fprintf(os.Stderr, " # [1] https://github.com/glycerine/go-capnproto \n")
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func main() {
MainArgs(os.Args)
}
// allow invocation from test
func MainArgs(args []string) {
//fmt.Println(os.Args)
os.Args = args
flag.Usage = use
if len(os.Args) < 2 {
use()
}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
debug := flag.Bool("debug", false, "print lots of debug info as we process.")
verrequest := flag.Bool("version", false, "request git commit hash used to build this bambam")
outdir := flag.String("o", "odir", "specify output directory")
pkg := flag.String("p", "main", "specify package for generated code")
privs := flag.Bool("X", false, "export private as well as public struct fields")
overwrite := flag.Bool("OVERWRITE", false, "replace named .go files with capid tagged versions.")
flag.Parse()
if debug != nil {
Verbose = *debug
}
if verrequest != nil && *verrequest {
fmt.Printf("%s\n", LASTGITCOMMITHASH)
os.Exit(0)
}
if outdir == nil || *outdir == "" {
fmt.Fprintf(os.Stderr, "required -o option missing. Use bambam -o <dirname> myfile.go # to specify the output directory.\n")
use()
}
if !DirExists(*outdir) {
err := os.MkdirAll(*outdir, 0755)
if err != nil {
panic(err)
}
}
if pkg == nil || *pkg == "" {
fmt.Fprintf(os.Stderr, "required -p option missing. Specify a package name for the generated go code with -p <pkgname>\n")
use()
}
// all the rest are input .go files
inputFiles := flag.Args()
if len(inputFiles) == 0 {
fmt.Fprintf(os.Stderr, "bambam needs at least one .go golang source file to process specified on the command line.\n")
os.Exit(1)
}
for _, fn := range inputFiles {
if !strings.HasSuffix(fn, ".go") && !strings.HasSuffix(fn, ".go.txt") {
fmt.Fprintf(os.Stderr, "error: bambam input file '%s' did not end in '.go' or '.go.txt'.\n", fn)
os.Exit(1)
}
}
x := NewExtractor()
x.fieldPrefix = " "
x.fieldSuffix = "\n"
x.outDir = *outdir
if privs != nil {
x.extractPrivate = *privs
}
if overwrite != nil {
x.overwrite = *overwrite
}
for _, inFile := range inputFiles {
_, err := x.ExtractStructsFromOneFile(nil, inFile)
if err != nil {
panic(err)
}
}
// get rid of default tmp dir
x.compileDir.Cleanup()
x.compileDir.DirPath = *outdir
x.pkgName = *pkg
schemaFN := x.compileDir.DirPath + "/schema.capnp"
schemaFile, err := os.Create(schemaFN)
if err != nil {
panic(err)
}
defer schemaFile.Close()
by := x.GenCapnpHeader()
schemaFile.Write(by.Bytes())
_, err = x.WriteToSchema(schemaFile)
if err != nil {
panic(err)
}
fmt.Fprintf(schemaFile, "\n")
fmt.Fprintf(schemaFile, "##compile with:\n\n##\n##\n## capnp compile -ogo %s\n\n", schemaFN)
// translator library of go functions is separate from the schema
translateFn := x.compileDir.DirPath + "/translateCapn.go"
translatorFile, err := os.Create(translateFn)
if err != nil {
panic(err)
}
defer translatorFile.Close()
fmt.Fprintf(translatorFile, `package %s
import (
capn "github.com/glycerine/go-capnproto"
"io"
)
`, x.pkgName)
_, err = x.WriteToTranslators(translatorFile)
if err != nil {
panic(err)
}
err = x.CopySourceFilesAddCapidTag()
if err != nil {
panic(err)
}
exec.Command("cp", "-p", "go.capnp", x.compileDir.DirPath).Run()
fmt.Printf("generated files in '%s'\n", x.compileDir.DirPath)
}