This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
staticfiles.go
186 lines (173 loc) · 4.26 KB
/
staticfiles.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
package main // import "bou.ke/staticfiles"
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"flag"
"go/format"
"io"
"io/ioutil"
"log"
"mime"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
)
type file struct {
name string
data string
mime string
mtime time.Time
// size is the result size before compression. If 0, it means the data is uncompressed
size int64
hash []byte
}
func skipFile(file string, excludeSlice []string) bool {
for _, pattern := range excludeSlice {
matched, err := path.Match(pattern, file)
if err != nil {
log.Fatal(err)
}
if matched {
return true
}
}
return false
}
func processDir(c chan [2]string, dir string, parents []string, excludeSlice []string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if strings.HasPrefix(file.Name(), ".") {
continue
}
n := filepath.Join(dir, file.Name())
id := append(parents, file.Name())
if skipFile(path.Join(id...), excludeSlice) {
continue
}
if file.IsDir() {
processDir(c, n, id, excludeSlice)
} else {
c <- [2]string{n, path.Join(id...)}
}
}
}
type fileSlice []*file
func (a fileSlice) Len() int { return len(a) }
func (a fileSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a fileSlice) Less(i, j int) bool { return a[i].name < a[j].name }
func main() {
var outputFile, packageName, buildTags, excludeList string
flag.StringVar(&outputFile, "o", "staticfiles.go", "File to write results to.")
flag.StringVar(&packageName, "package", "", "Package name of the resulting file. Defaults to name of the resulting file directory")
flag.StringVar(&buildTags, "build-tags", "", "Build tags to write to the file")
flag.StringVar(&excludeList, "exclude", "", "Comma-separated patterns to exclude. (e.g. '*.scss,templates/')")
flag.Parse()
if flag.NArg() != 1 {
log.Println("Please pass in a directory to process")
flag.PrintDefaults()
os.Exit(1)
return
}
if packageName == "" {
f, err := filepath.Abs(outputFile)
if err != nil {
log.Fatal(err)
}
packageName = filepath.Base(filepath.Dir(f))
}
excludeSlice := strings.Split(excludeList, ",")
c := make(chan [2]string, 128)
go func() {
for _, arg := range flag.Args() {
processDir(c, arg, []string{}, excludeSlice)
}
close(c)
}()
// Used to communicate back all the files that were read and compressed.
results := make(chan fileSlice)
process := func() {
var b bytes.Buffer
var b2 bytes.Buffer
hash := sha256.New()
files := make(fileSlice, 0, 32)
for asset := range c {
f, err := os.Open(asset[0])
if err != nil {
log.Fatal(err)
}
stat, err := f.Stat()
if err != nil {
log.Fatal(err)
}
if _, err := b.ReadFrom(f); err != nil {
log.Fatal(err)
}
f.Close()
compressedWriter, _ := gzip.NewWriterLevel(&b2, gzip.BestCompression)
writer := io.MultiWriter(compressedWriter, hash)
if _, err := writer.Write(b.Bytes()); err != nil {
log.Fatal(err)
}
compressedWriter.Close()
if b2.Len() < b.Len() {
files = append(files, &file{
name: asset[1],
data: b2.String(),
mime: mime.TypeByExtension(filepath.Ext(asset[0])),
mtime: stat.ModTime(),
size: stat.Size(),
hash: hash.Sum(nil),
})
} else {
files = append(files, &file{
name: asset[1],
data: b.String(),
mime: mime.TypeByExtension(filepath.Ext(asset[0])),
mtime: stat.ModTime(),
hash: hash.Sum(nil),
})
}
b.Reset()
b2.Reset()
hash.Reset()
}
results <- files
}
// Concurrency! Read and compress in parallel, and combine all the slices at the end.
concurrency := runtime.NumCPU()
for i := 0; i < concurrency; i++ {
go process()
}
var files fileSlice
for i := 0; i < concurrency; i++ {
files = append(files, (<-results)...)
}
// Should sort to make sure the output is stable
sort.Sort(files)
var b bytes.Buffer
if err := GenerateTemplate(&b, packageName, files, buildTags); err != nil {
log.Fatal(err)
}
res, err := format.Source(b.Bytes())
if err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
log.Fatal(err)
}
f, err := os.Create(outputFile)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(res); err != nil {
log.Fatal(err)
}
}