Skip to content

Commit 94068b1

Browse files
ZaniaDevelopershuLhan
authored andcommitted
Avoid having a big big file (#17)
* Add `-split` option to avoid generating big file * Modifications following first code review
1 parent e134b7c commit 94068b1

File tree

8 files changed

+318
-72
lines changed

8 files changed

+318
-72
lines changed

config.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type Config struct {
3939

4040
// Output defines the output file for the generated code.
4141
// If left empty, this defaults to 'bindata.go' in the current
42-
// working directory.
42+
// working directory and the current directory in case of having true
43+
// to `Split` config.
4344
Output string
4445

4546
// Prefix defines a regular expression which should used to strip
@@ -142,6 +143,12 @@ type Config struct {
142143
Ignore []*regexp.Regexp
143144
Include []*regexp.Regexp
144145

146+
// Split the output into several files. Every embedded file is bound into
147+
// a specific file, and a common file is also generated containing API and
148+
// other common parts.
149+
// If true, the output config is a directory and not a file.
150+
Split bool
151+
145152
// MD5Checksum is a flag that, when set to true, indicates to calculate
146153
// MD5 checksums for files.
147154
MD5Checksum bool
@@ -154,7 +161,6 @@ func NewConfig() *Config {
154161
c.NoMemCopy = false
155162
c.NoCompress = false
156163
c.Debug = false
157-
c.Output = "./bindata.go"
158164
c.Ignore = make([]*regexp.Regexp, 0)
159165
c.Include = make([]*regexp.Regexp, 0)
160166
return c
@@ -180,7 +186,11 @@ func (c *Config) validate() error {
180186
return fmt.Errorf("Unable to determine current working directory")
181187
}
182188

183-
c.Output = filepath.Join(cwd, "bindata.go")
189+
if c.Split {
190+
c.Output = cwd
191+
} else {
192+
c.Output = filepath.Join(cwd, "bindata.go")
193+
}
184194
}
185195

186196
stat, err := os.Lstat(c.Output)
@@ -201,8 +211,18 @@ func (c *Config) validate() error {
201211
}
202212
}
203213

204-
if stat != nil && stat.IsDir() {
205-
return fmt.Errorf("Output path is a directory")
214+
if stat != nil {
215+
if c.Split {
216+
if !stat.IsDir() {
217+
return fmt.Errorf("Output path is not a directory")
218+
219+
}
220+
} else {
221+
if stat.IsDir() {
222+
return fmt.Errorf("Output path is a directory")
223+
224+
}
225+
}
206226
}
207227

208228
return nil

convert.go

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package bindata
66

77
import (
8-
"bufio"
98
"fmt"
109
"os"
1110
"path/filepath"
@@ -37,76 +36,16 @@ func Translate(c *Config) error {
3736
}
3837
}
3938

40-
// Create output file.
41-
fd, err := os.Create(c.Output)
42-
if err != nil {
43-
return err
44-
}
45-
46-
defer fd.Close()
47-
48-
// Create a buffered writer for better performance.
49-
bfd := bufio.NewWriter(fd)
50-
defer bfd.Flush()
51-
52-
// Write the header. This makes e.g. Github ignore diffs in generated files.
53-
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
54-
return err
55-
}
56-
if _, err = fmt.Fprint(bfd, "// sources:\n"); err != nil {
57-
return err
58-
}
59-
6039
wd, err := os.Getwd()
6140
if err != nil {
6241
return err
6342
}
6443

65-
for _, asset := range toc {
66-
relative, _ := filepath.Rel(wd, asset.Path)
67-
if _, err = fmt.Fprintf(bfd, "// %s\n", filepath.ToSlash(relative)); err != nil {
68-
return err
69-
}
70-
}
71-
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
72-
return err
73-
}
74-
75-
// Write build tags, if applicable.
76-
if len(c.Tags) > 0 {
77-
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
78-
return err
79-
}
80-
}
81-
82-
// Write package declaration.
83-
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
84-
if err != nil {
85-
return err
86-
}
87-
88-
// Write assets.
89-
if c.Debug || c.Dev {
90-
err = writeDebug(bfd, c, toc)
91-
} else {
92-
err = writeRelease(bfd, c, toc)
93-
}
94-
95-
if err != nil {
96-
return err
97-
}
98-
99-
// Write table of contents
100-
if err := writeTOC(bfd, toc); err != nil {
101-
return err
102-
}
103-
// Write hierarchical tree of assets
104-
if err := writeTOCTree(bfd, toc); err != nil {
105-
return err
44+
if c.Split {
45+
return translateToDir(c, toc, wd)
10646
}
10747

108-
// Write restore procedure
109-
return writeRestore(bfd)
48+
return translateToFile(c, toc, wd)
11049
}
11150

11251
// ByName implement sort.Interface for []os.FileInfo based on Name()

convert_to_dir.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package bindata
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
// translateToDir generates splited file
11+
func translateToDir(c *Config, toc []Asset, wd string) error {
12+
if err := generateCommonFile(c, toc); err != nil {
13+
return err
14+
}
15+
16+
for i := range toc {
17+
if err := generateOneAsset(c, &toc[i], wd); err != nil {
18+
return err
19+
}
20+
}
21+
22+
return nil
23+
}
24+
25+
func generateCommonFile(c *Config, toc []Asset) error {
26+
// Create output file.
27+
fd, err := os.Create(filepath.Join(c.Output, "bindata.go"))
28+
if err != nil {
29+
return err
30+
}
31+
32+
defer fd.Close()
33+
34+
// Create a buffered writer for better performance.
35+
bfd := bufio.NewWriter(fd)
36+
defer bfd.Flush()
37+
38+
// Write the header. This makes e.g. Github ignore diffs in generated files.
39+
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
40+
return err
41+
}
42+
43+
if _, err = fmt.Fprint(bfd, "// -- Common file --\n"); err != nil {
44+
return err
45+
}
46+
47+
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
48+
return err
49+
}
50+
51+
// Write build tags, if applicable.
52+
if len(c.Tags) > 0 {
53+
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
54+
return err
55+
}
56+
}
57+
58+
// Write package declaration.
59+
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
60+
if err != nil {
61+
return err
62+
}
63+
64+
// Write assets.
65+
if c.Debug || c.Dev {
66+
err = writeDebugHeader(bfd)
67+
} else {
68+
err = writeReleaseHeader(bfd, c)
69+
}
70+
71+
if err != nil {
72+
return err
73+
}
74+
75+
// Write table of contents
76+
if err := writeTOC(bfd, toc); err != nil {
77+
return err
78+
}
79+
// Write hierarchical tree of assets
80+
if err := writeTOCTree(bfd, toc); err != nil {
81+
return err
82+
}
83+
84+
// Write restore procedure
85+
return writeRestore(bfd)
86+
87+
}
88+
89+
func generateOneAsset(c *Config, a *Asset, wd string) error {
90+
// Create output file.
91+
fd, err := os.Create(filepath.Join(c.Output, a.Func + ".go"))
92+
if err != nil {
93+
return err
94+
}
95+
96+
defer fd.Close()
97+
98+
// Create a buffered writer for better performance.
99+
bfd := bufio.NewWriter(fd)
100+
defer bfd.Flush()
101+
102+
// Write the header. This makes e.g. Github ignore diffs in generated files.
103+
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
104+
return err
105+
}
106+
107+
if _, err = fmt.Fprint(bfd, "// source: "); err != nil {
108+
return err
109+
}
110+
111+
relative, _ := filepath.Rel(wd, a.Path)
112+
if _, err = fmt.Fprintln(bfd, filepath.ToSlash(relative)); err != nil {
113+
return err
114+
}
115+
116+
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
117+
return err
118+
}
119+
120+
// Write build tags, if applicable.
121+
if len(c.Tags) > 0 {
122+
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
123+
return err
124+
}
125+
}
126+
127+
// Write package declaration.
128+
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
129+
if err != nil {
130+
return err
131+
}
132+
133+
// Write assets.
134+
if c.Debug || c.Dev {
135+
err = writeOneFileDebug(bfd, c, a)
136+
} else {
137+
err = writeOneFileRelease(bfd, c, a)
138+
}
139+
140+
return err
141+
}

convert_to_file.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package bindata
2+
3+
import (
4+
"os"
5+
"bufio"
6+
"fmt"
7+
"path/filepath"
8+
)
9+
10+
// translateToFile generates one single file
11+
func translateToFile(c *Config, toc []Asset, wd string) error {
12+
// Create output file.
13+
fd, err := os.Create(c.Output)
14+
if err != nil {
15+
return err
16+
}
17+
18+
defer fd.Close()
19+
20+
// Create a buffered writer for better performance.
21+
bfd := bufio.NewWriter(fd)
22+
defer bfd.Flush()
23+
24+
// Write the header. This makes e.g. Github ignore diffs in generated files.
25+
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
26+
return err
27+
}
28+
if _, err = fmt.Fprint(bfd, "// sources:\n"); err != nil {
29+
return err
30+
}
31+
32+
for _, asset := range toc {
33+
relative, _ := filepath.Rel(wd, asset.Path)
34+
if _, err = fmt.Fprintf(bfd, "// %s\n", filepath.ToSlash(relative)); err != nil {
35+
return err
36+
}
37+
}
38+
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
39+
return err
40+
}
41+
42+
// Write build tags, if applicable.
43+
if len(c.Tags) > 0 {
44+
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
45+
return err
46+
}
47+
}
48+
49+
// Write package declaration.
50+
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
51+
if err != nil {
52+
return err
53+
}
54+
55+
// Write assets.
56+
if c.Debug || c.Dev {
57+
err = writeDebug(bfd, c, toc)
58+
} else {
59+
err = writeRelease(bfd, c, toc)
60+
}
61+
62+
if err != nil {
63+
return err
64+
}
65+
66+
// Write table of contents
67+
if err := writeTOC(bfd, toc); err != nil {
68+
return err
69+
}
70+
// Write hierarchical tree of assets
71+
if err := writeTOCTree(bfd, toc); err != nil {
72+
return err
73+
}
74+
75+
// Write restore procedure
76+
return writeRestore(bfd)
77+
}

0 commit comments

Comments
 (0)