-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkg.go
113 lines (97 loc) · 2.28 KB
/
pkg.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
package genx
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/imports"
)
var header = []byte(`// This file was automatically generated by genx.
// Any changes will be lost if this file is regenerated.
// see https://github.com/OneOfOne/genx
`)
type ParsedFile struct {
Name string
Src []byte
}
func (f ParsedFile) WriteFile(path string) error {
return writeFile(path, f.Src)
}
type ParsedPkg []ParsedFile
func (p ParsedPkg) WritePkg(dir string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
for _, f := range p {
if err := f.WriteFile(filepath.Join(dir, f.Name)); err != nil {
return err
}
}
return nil
}
func (p ParsedPkg) MergeAll(tests bool) (ParsedFile, error) {
// TODO: look into doing this with ast
// var cleanSrc = regexp.MustCompile(`// nolint$`)
var totalLen int
for _, f := range p {
isTest := strings.HasSuffix(f.Name, "_test.go")
if (isTest && !tests) || (!isTest && tests) {
continue
}
totalLen += len(f.Src)
}
pf := ParsedFile{
Name: "all_gen.go",
Src: make([]byte, 0, totalLen),
}
for i, f := range p {
isTest := strings.HasSuffix(f.Name, "_test.go")
if (isTest && !tests) || (!isTest && tests) {
continue
}
// f.Src = cleanSrc.ReplaceAll(f.Src, []byte("$1"))
if i > 0 {
f.Src = removePkgAndImports.ReplaceAll(f.Src, nil)
}
pf.Src = append(pf.Src, f.Src...)
}
// log.Printf("%s", out)
out, err := imports.Process(pf.Name, pf.Src, &imports.Options{
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 4,
})
if err == nil {
pf.Src = out
}
return pf, err
}
func (p ParsedPkg) WriteAllMerged(fname string, tests bool) error {
pf, err := p.MergeAll(tests)
if err != nil {
log.Printf("partial output:\n%s", pf.Src)
return err
}
return writeFile(fname, pf.Src)
}
func writeFile(fp string, data []byte) error {
dir := filepath.Dir(fp)
if dir != "" && dir != "./" && dir != "/dev" {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
f, err := os.OpenFile(fp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
f.Write(header)
if args := os.Args; len(args) > 0 && args[0] == "genx" {
fmt.Fprintf(f, "// cmd: %s\n", strings.Join(args, " "))
}
fmt.Fprintf(f, "// +build !genx\n\n")
f.Write(data)
return f.Close()
}