This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
const.go
199 lines (154 loc) · 3.29 KB
/
const.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package fileconst
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
log "github.com/sirupsen/logrus"
)
type fileConstError string
func (e fileConstError) Error() string {
return string(e)
}
const (
NotPackageFolder fileConstError = "This should be executed in a Go package folder"
)
const fileConstTemplate = `// Code generated by fileconst DO NOT EDIT
//
// If the associated files have changed, run the following command in
// the project's root directory:
//
// go generate ./...
//
// For more information, see the project's home page at:
//
// https://github.com/PennState/fileconst
//
package {{ .Package }}
{{ range .Files }}
{{ if .Comment -}}
{{ .Comment }}
{{ end -}}
const {{ .Name }} = {{ .Content }}
{{ end -}}
`
type Spec struct {
Package string
Files []FileSpec
}
type FileSpec struct {
Name string
Comment string
Content string
}
func Run(dirs []string, exts map[string]bool) error {
log.Info("Started fileconst")
pkg, ok := os.LookupEnv("GOPACKAGE")
if !ok {
return NotPackageFolder
}
log.Infof("Go package name: %s", pkg)
dir, err := directory()
if err != nil {
return err
}
if len(dirs) == 0 {
dirs = append(dirs, dir)
}
log.Infof("Go package directory: %s", dir)
paths, err := files(dirs)
if err != nil {
return err
}
spec, err := process(pkg, paths, exts)
if err != nil {
return err
}
log.Info("Constant files found: ", len(spec.Files))
log.Debug("Spec: ", spec)
out, err := os.Create(filepath.Join(dir, "fileconst_gen.go"))
if err != nil {
return err
}
log.Info("Generating \"fileconst_gen.go\" file")
err = generate(spec, out)
if err != nil {
return err
}
log.Info("Finished fileconst")
return nil
}
func directory() (string, error) {
file, ok := os.LookupEnv("GOFILE")
if !ok {
return "", NotPackageFolder
}
path, err := filepath.Abs(file)
if err != nil {
return "", err
}
return filepath.Dir(path), nil
}
func files(dirs []string) ([]string, error) {
paths := []string{}
for _, dir := range dirs {
fis, err := ioutil.ReadDir(dir)
if err != nil {
return paths, err
}
for _, fi := range fis {
if fi.IsDir() {
continue
}
paths = append(paths, filepath.Join(dir, fi.Name()))
}
}
return paths, nil
}
func generate(spec *Spec, out io.WriteCloser) error {
defer out.Close()
tmplt, err := template.New("fileconst").Parse(fileConstTemplate)
if err != nil {
return err
}
err = tmplt.Execute(out, spec)
if err != nil {
return err
}
return nil
}
func process(pkg string, paths []string, exts map[string]bool) (*Spec, error) {
spec := &Spec{
Package: pkg,
}
for _, path := range paths {
ext := path[strings.LastIndex(path, ".")+1:]
if !exts[ext] {
continue
}
data, err := ioutil.ReadFile(path)
if err != nil {
return spec, err
}
cmnt, ctnt := []string{}, []string{}
for _, l := range strings.Split(string(data), "\n") {
if strings.HasPrefix(l, "//") {
cmnt = append(cmnt, l)
continue
}
ctnt = append(ctnt, l)
}
_, name := filepath.Split(path)
name = name[:strings.LastIndex(name, ".")]
fspec := FileSpec{
Name: name,
Comment: strings.Join(cmnt, "\n"),
Content: "`" + strings.Join(ctnt, "\n") + "`",
}
spec.Files = append(spec.Files, fspec)
log.Info("Added file to spec: ", path)
}
return spec, nil
}