-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcodegen.go
100 lines (79 loc) · 2.13 KB
/
pcodegen.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
package main
import (
"path/filepath"
"os"
"fmt"
"io/ioutil"
"strings"
"github.com/alanctgardner/gogen-avro/generator"
"github.com/alanctgardner/gogen-avro/types"
)
func main() {
targetDir := "etpsrc"
packageName := "energistics"
fileList, _ := run("Energistics")
files := fileList
var err error
pkg := generator.NewPackage(packageName)
namespace := types.NewNamespace()
for _, path := range fileList {
fmt.Println(path)
}
for _, fileName := range files {
schema, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file %q - %v\n", fileName, err)
os.Exit(2)
}
_, err = namespace.TypeForSchema(schema)
if err != nil {
fmt.Fprintf(os.Stderr, "Error decoding schema for file %q - %v\n", fileName, err)
os.Exit(3)
}
}
err = namespace.AddToPackage(pkg, codegenComment(files), false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating code for schema - %v\n", err)
os.Exit(4)
}
err = pkg.WriteFiles(targetDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing source files to directory %q - %v\n", targetDir, err)
os.Exit(4)
}
}
// codegenComment generates a comment informing readers they are looking at
// generated code and lists the source avro files used to generate the code
//
// invariant: sources > 0
func codegenComment(sources []string) string {
const fileComment = `// Code generated by github.com/alanctgardner/gogen-avro. DO NOT EDIT.
/*
* %s
*/`
var sourceBlock []string
if len(sources) == 1 {
sourceBlock = append(sourceBlock, "SOURCE:")
} else {
sourceBlock = append(sourceBlock, "SOURCES:")
}
for _, source := range sources {
_, fName := filepath.Split(source)
sourceBlock = append(sourceBlock, fmt.Sprintf(" * %s", fName))
}
return fmt.Sprintf(fileComment, strings.Join(sourceBlock, "\n"))
}
func run(searchDir string) ([]string, error) {
// searchDir := "Energistics"
fileList := make([]string, 0)
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return err
})
if e != nil {
println(e.Error())
}
return fileList, nil
}