-
Notifications
You must be signed in to change notification settings - Fork 14
/
modelgen.go
141 lines (113 loc) · 3.12 KB
/
modelgen.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
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/contiv/modelgen/generators"
)
var app *cli.App
func main() {
app = cli.NewApp()
app.Usage = "Generate REST scaffolding from a json-described object model"
app.ArgsUsage = "[source directory] [output directory]"
app.HideHelp = true
app.Version = ""
app.Action = run
app.Run(os.Args)
}
func run(ctx *cli.Context) {
if len(ctx.Args()) != 2 {
cli.ShowAppHelp(ctx)
os.Exit(1)
}
sourceDir := ctx.Args()[0]
if sourceDir == "" {
sourceDir = "."
}
outputDir := ctx.Args()[1]
if outputDir == "" {
outputDir = "output"
}
if err := generators.ParseTemplates(); err != nil {
panic(err)
}
var schema *Schema
// Parse all files in input directory
err := filepath.Walk(sourceDir, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore non-json files and ignore files in subdirs
if filepath.Ext(path) != ".json" || filepath.Dir(path) != filepath.Dir(sourceDir) {
return nil
}
fmt.Printf("Parsing file: %q\n", path)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Parse the schema
sch, err := ParseSchema(b)
if err != nil {
return err
}
// Append to global schema
schema = MergeSchema(schema, sch)
return nil
})
if err != nil {
log.Fatal(err)
}
if err = VerifyObjects(propList, validObjects); err != nil {
log.Fatal(err)
}
if schema == nil {
log.Fatal("Could not find schema, aborting.")
}
if err := os.MkdirAll(outputDir, 0755); err != nil {
log.Fatalf("Error creating output directory: %v", err)
}
// create client dir
clientDir := path.Join(outputDir, "client")
if err := os.MkdirAll(clientDir, 0755); err != nil {
log.Fatalf("Error creating output directory: %v", err)
}
outputs := map[string][]byte{}
funcs := map[string]func() ([]byte, error){
"%s.go": schema.GenerateGo,
"client/%s.js": schema.GenerateJs,
"client/%sClient.go": schema.GenerateClient,
"client/%sClient.py": schema.GeneratePythonClient,
}
for frmt, fun := range funcs {
str, err := fun()
if err != nil {
log.Fatalf("Error generating output for target %q: %v", frmt, err)
}
outFilename := fmt.Sprintf(frmt, schema.Name)
outputs[outFilename] = []byte(str)
}
for outFilename, content := range outputs {
target := path.Join(outputDir, outFilename)
fmt.Printf("Generating file: %q\n", target)
if err := ioutil.WriteFile(target, content, 0666); err != nil {
log.Fatal(err)
}
}
}