-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 loc) · 1.49 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
// -*- compile-command: "go run main.go ../../make-box.bjk"; -*-
// bjk-to-obj loads a Blackjack BJK file and writes a Wavefront Obj file.
// See: https://github.com/setzer22/blackjack
//
// Usage:
//
// bjk-to-obj file.bjk [file2.bjk ...]
package main
import (
"flag"
"log"
"os"
"strings"
"github.com/alecthomas/participle/v2"
"github.com/gmlewis/go-bjk/ast"
"github.com/gmlewis/go-bjk/nodes"
)
var (
debug = flag.Bool("debug", false, "Turn on debugging info")
repoDir = flag.String("repo", "src/github.com/gmlewis/blackjack", "Path to Blackjack repo (relative to home dir or absolute path)")
outFile = flag.String("o", "", "Override output filename")
)
func main() {
flag.Parse()
c, err := nodes.New(*repoDir, *debug)
must(err)
defer c.Close()
client := &clientT{c: c}
for _, arg := range flag.Args() {
client.processFile(arg)
}
log.Printf("Done.")
}
type clientT struct {
c *nodes.Client
}
func (c *clientT) processFile(arg string) {
buf, err := os.ReadFile(arg)
must(err)
var opts []participle.ParseOption
if *debug {
opts = append(opts, participle.Trace(os.Stderr))
}
design, err := ast.Parser.ParseString("", string(buf), opts...)
if err != nil {
log.Fatalf("ERROR: ast.Parser.ParseString: %v", err)
}
outFilename := strings.Replace(arg, ".bjk", ".obj", -1)
if *outFile != "" {
outFilename = *outFile
}
log.Printf("Writing Wavefront obj file: %v", outFilename)
must(c.c.ToObj(design, outFilename))
}
func must(err error) {
if err != nil {
log.Fatal(err)
}
}