-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
133 lines (106 loc) · 3.76 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
package main
import (
"fmt"
. "github.com/ThoughtWorksStudios/bobcat/common"
. "github.com/ThoughtWorksStudios/bobcat/emitter"
"github.com/ThoughtWorksStudios/bobcat/interpreter"
"github.com/docopt/docopt-go"
"log"
"os"
"path/filepath"
)
const (
VERSION = "0.6.0"
USAGE = `
Usage: %s [-o DESTFILE] [-d DICTPATH] [--stdout] [-cfms] [--] INPUTFILE
%s -v
%s -h
Arguments:
INPUTFILE The file describing entities and generation statements
DESTFILE The output file (defaults to "entities.json"); accepts "-" to output to STDOUT
DICTPATH The path to your user-defined dictionaries
Options:
-h --help
-v --version
-c --check Check syntax of INPUTFILE
-m --no-metadata Omit metadata in generated entities (e.g. $type, $extends, etc.)
-o DESTFILE --output=DESTFILE Specify output file [default: entities.json] (use "-" for DESTFILE
to specify STDOUT)
-d DICTPATH --dictionaries=DICTPATH Specify DICTPATH
-f --flatten Flattens entity hierarchies into a flat array; entities are
outputted in reverse order of dependency, and linked by "$id"
-s --split-output Aggregates entities by type into separate files; DESTFILE
serves as the filename template, meaning each file has the
entity type appended to its basename (i.e. before the ".json"
extension, as in "entities-myType.json"). Implies --flatten.
--stdout Alias for '-o -'; forcefully redirects output to STDOUT and
supercedes setting DESTFILE elsewhere. Not compatible
with --split-output.
`
)
func init() {
log.SetFlags(0)
}
func createEmitter(filename string, config map[string]interface{}) Emitter {
var err error
var emitter Emitter
splitOutput, _ := config["--split-output"].(bool)
flatten, _ := config["--flatten"].(bool)
forceStdout, _ := config["--stdout"].(bool)
if forceStdout {
filename = "-"
}
if "-" == filename && splitOutput {
log.Fatalln("Cannot use --split-output with STDOUT")
}
switch true {
case splitOutput:
emitter, err = SplitEmitterForFile(filename)
case flatten:
emitter, err = FlatEmitterForFile(filename)
default:
emitter, err = NestedEmitterForFile(filename)
}
if err != nil {
log.Fatalln(err)
}
return emitter
}
func main() {
progname := filepath.Base(os.Args[0])
usage := fmt.Sprintf(USAGE, progname, progname, progname)
autoExit := true // set to `true` to let docopt automatically exit; `false` to handle exit ourselves
args, _ := docopt.Parse(usage, nil, true, VERSION, false, autoExit)
outputFile, _ := args["--output"].(string)
disableMetadata, _ := args["--no-metadata"].(bool)
syntaxCheck, _ := args["--check"].(bool)
filename, _ := args["INPUTFILE"].(string)
var emitter Emitter
if !syntaxCheck {
emitter = createEmitter(outputFile, args)
}
i := interpreter.New(emitter, disableMetadata)
if customDicts, ok := args["--dictionaries"].(string); !ok {
a, _ := filepath.Abs(filename)
i.SetCustomDictonaryPath(filepath.Dir(a))
} else {
i.SetCustomDictonaryPath(customDicts)
}
if syntaxCheck {
i.ConfigureDryRun()
if _, errors := i.LoadFile(filename, NewRootScope(), false); errors != nil {
log.Fatalf("Syntax check failed: %v\n", errors)
}
log.Println("Syntax OK")
os.Exit(0)
}
if errors := emitter.Init(); errors != nil {
log.Fatalln(errors)
}
if _, errors := i.LoadFile(filename, NewRootScope(), false); errors != nil {
log.Fatalln(errors)
}
if errors := emitter.Finalize(); errors != nil {
log.Fatalln(errors)
}
}