-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgenerate.go
263 lines (217 loc) · 5.34 KB
/
generate.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"go/ast"
"go/build"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"strings"
ignore "github.com/sabhiram/go-gitignore"
"aletheia.icu/broccoli/fs"
)
// Generator collects the necessary info about the package and
// bundles the provided assets according to provided flags.
type Generator struct {
pkg *Package
inputFiles []string // list of source dirs
includeGlob string // files to be included
excludeGlob string // files to be excluded
useGitignore bool // .gitignore files will be parsed
quality int // compression level (1-11)
}
const template = `%s
package %s
import "aletheia.icu/broccoli/fs"
var %s = fs.New(%t, []byte(%q))
`
type wildcards []wildcard
func (w wildcards) test(path string, info os.FileInfo) bool {
for _, card := range w {
if !card.test(info) {
if *verbose {
log.Println("ignoring", path)
}
return false
}
}
return true
}
func (g *Generator) generate() ([]byte, error) {
var (
files []*fs.File
cards wildcards
state = map[string]bool{}
total int64
)
if g.includeGlob != "" {
cards = append(cards, wildcardFrom(true, g.includeGlob))
} else if g.excludeGlob != "" {
cards = append(cards, wildcardFrom(false, g.excludeGlob))
}
if g.useGitignore {
ignores, err := g.parseGitignores()
if err != nil {
return nil, fmt.Errorf("cannot open .gitignore: %w", err)
}
cards = append(cards, ignores...)
}
for _, input := range g.inputFiles {
info, err := os.Stat(input)
if err != nil {
return nil, fmt.Errorf("file or directory %s not found", input)
}
var f *fs.File
if !info.IsDir() {
if _, ok := state[input]; ok {
return nil, fmt.Errorf("duplicate path in the input: %s", input)
}
state[input] = true
f, err = fs.NewFile(input)
if err != nil {
return nil, fmt.Errorf("cannot open file or directory: %w", err)
}
total += f.Fsize
files = append(files, f)
continue
}
err = filepath.Walk(input, func(path string, info os.FileInfo, _ error) error {
if !cards.test(path, info) {
return nil
}
f, err := fs.NewFile(path)
if err != nil {
return err
}
if _, ok := state[path]; ok {
return fmt.Errorf("duplicate path in the input: %s", path)
}
total += f.Fsize
state[path] = true
files = append(files, f)
return nil
})
if err != nil {
return nil, fmt.Errorf("cannot open file or directory: %w", err)
}
}
if *verbose {
log.Println("total bytes read:", total)
}
bundle, err := fs.Pack(files, g.quality)
if err != nil {
return nil, fmt.Errorf("could not compress the input: %w", err)
}
if *verbose {
log.Println("total bytes compressed:", len(bundle))
}
return bundle, nil
}
type wildcard interface {
test(os.FileInfo) bool
}
type includeWildcard struct {
include bool
patterns []string
}
func (w includeWildcard) test(info os.FileInfo) bool {
if info.IsDir() {
return true
}
pass := !w.include
for _, pattern := range w.patterns {
match, err := filepath.Match(pattern, info.Name())
if err != nil {
log.Fatal("invalid wildcard:", pattern)
}
if match {
pass = w.include
break
}
}
return pass
}
func wildcardFrom(include bool, patterns string) wildcard {
w := strings.Split(patterns, ",")
for i, v := range w {
w[i] = strings.Trim(v, ` "`)
}
return includeWildcard{include, w}
}
type gitignoreWildcard struct {
ign *ignore.GitIgnore
}
func (w gitignoreWildcard) test(info os.FileInfo) bool {
return !w.ign.MatchesPath(info.Name())
}
func (g *Generator) parseGitignores() (cards []wildcard, err error) {
err = filepath.Walk(".", func(path string, info os.FileInfo, _ error) error {
if !info.IsDir() && info.Name() == ".gitignore" {
ign, err := ignore.CompileIgnoreFile(path)
if err != nil {
return err
}
cards = append(cards, gitignoreWildcard{ign: ign})
}
return nil
})
return
}
// Package holds information about a Go package
type Package struct {
dir string
name string
defs map[*ast.Ident]types.Object
typesPkg *types.Package
}
func (g *Generator) parsePackage() {
pkg, err := build.Default.ImportDir(".", 0)
if err != nil {
log.Fatalln("cannot parse package:", err)
}
var names []string
names = append(names, pkg.GoFiles...)
names = append(names, pkg.CgoFiles...)
names = append(names, pkg.SFiles...)
var astFiles []*ast.File
g.pkg = new(Package)
set := token.NewFileSet()
for _, name := range names {
if !strings.HasSuffix(name, ".go") {
continue
}
parsedFile, err := parser.ParseFile(set, name, nil, parser.ParseComments)
if err != nil {
log.Fatalf("parsing package: %s: %s\n", name, err)
}
astFiles = append(astFiles, parsedFile)
}
if len(astFiles) == 0 {
log.Fatalln("no buildable Go files")
}
g.pkg.name = astFiles[0].Name.Name
g.pkg.dir = "."
// Type check the package.
g.pkg.check(set, astFiles)
}
// check type-checks the package.
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
pkg.defs = make(map[*ast.Ident]types.Object)
config := types.Config{Importer: defaultImporter(), FakeImportC: true}
info := &types.Info{
Defs: pkg.defs,
}
typesPkg, err := config.Check(pkg.dir, fs, astFiles, info)
if err != nil {
log.Println("checking package:", err)
log.Println("proceeding anyway...")
}
pkg.typesPkg = typesPkg
}
func defaultImporter() types.Importer {
return importer.For("source", nil)
}