Skip to content

Commit

Permalink
Avoid having a big big file (#17)
Browse files Browse the repository at this point in the history
* Add `-split` option to avoid generating big file
* Modifications following first code review
  • Loading branch information
ZaniaDeveloper authored and shuLhan committed Sep 18, 2017
1 parent e134b7c commit 94068b1
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 72 deletions.
30 changes: 25 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type Config struct {

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

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

// Split the output into several files. Every embedded file is bound into
// a specific file, and a common file is also generated containing API and
// other common parts.
// If true, the output config is a directory and not a file.
Split bool

// MD5Checksum is a flag that, when set to true, indicates to calculate
// MD5 checksums for files.
MD5Checksum bool
Expand All @@ -154,7 +161,6 @@ func NewConfig() *Config {
c.NoMemCopy = false
c.NoCompress = false
c.Debug = false
c.Output = "./bindata.go"
c.Ignore = make([]*regexp.Regexp, 0)
c.Include = make([]*regexp.Regexp, 0)
return c
Expand All @@ -180,7 +186,11 @@ func (c *Config) validate() error {
return fmt.Errorf("Unable to determine current working directory")
}

c.Output = filepath.Join(cwd, "bindata.go")
if c.Split {
c.Output = cwd
} else {
c.Output = filepath.Join(cwd, "bindata.go")
}
}

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

if stat != nil && stat.IsDir() {
return fmt.Errorf("Output path is a directory")
if stat != nil {
if c.Split {
if !stat.IsDir() {
return fmt.Errorf("Output path is not a directory")

}
} else {
if stat.IsDir() {
return fmt.Errorf("Output path is a directory")

}
}
}

return nil
Expand Down
67 changes: 3 additions & 64 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package bindata

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -37,76 +36,16 @@ func Translate(c *Config) error {
}
}

// Create output file.
fd, err := os.Create(c.Output)
if err != nil {
return err
}

defer fd.Close()

// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
defer bfd.Flush()

// Write the header. This makes e.g. Github ignore diffs in generated files.
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
return err
}
if _, err = fmt.Fprint(bfd, "// sources:\n"); err != nil {
return err
}

wd, err := os.Getwd()
if err != nil {
return err
}

for _, asset := range toc {
relative, _ := filepath.Rel(wd, asset.Path)
if _, err = fmt.Fprintf(bfd, "// %s\n", filepath.ToSlash(relative)); err != nil {
return err
}
}
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
return err
}

// Write build tags, if applicable.
if len(c.Tags) > 0 {
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
return err
}
}

// Write package declaration.
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
if err != nil {
return err
}

// Write assets.
if c.Debug || c.Dev {
err = writeDebug(bfd, c, toc)
} else {
err = writeRelease(bfd, c, toc)
}

if err != nil {
return err
}

// Write table of contents
if err := writeTOC(bfd, toc); err != nil {
return err
}
// Write hierarchical tree of assets
if err := writeTOCTree(bfd, toc); err != nil {
return err
if c.Split {
return translateToDir(c, toc, wd)
}

// Write restore procedure
return writeRestore(bfd)
return translateToFile(c, toc, wd)
}

// ByName implement sort.Interface for []os.FileInfo based on Name()
Expand Down
141 changes: 141 additions & 0 deletions convert_to_dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package bindata

import (
"bufio"
"fmt"
"os"
"path/filepath"
)

// translateToDir generates splited file
func translateToDir(c *Config, toc []Asset, wd string) error {
if err := generateCommonFile(c, toc); err != nil {
return err
}

for i := range toc {
if err := generateOneAsset(c, &toc[i], wd); err != nil {
return err
}
}

return nil
}

func generateCommonFile(c *Config, toc []Asset) error {
// Create output file.
fd, err := os.Create(filepath.Join(c.Output, "bindata.go"))
if err != nil {
return err
}

defer fd.Close()

// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
defer bfd.Flush()

// Write the header. This makes e.g. Github ignore diffs in generated files.
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
return err
}

if _, err = fmt.Fprint(bfd, "// -- Common file --\n"); err != nil {
return err
}

if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
return err
}

// Write build tags, if applicable.
if len(c.Tags) > 0 {
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
return err
}
}

// Write package declaration.
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
if err != nil {
return err
}

// Write assets.
if c.Debug || c.Dev {
err = writeDebugHeader(bfd)
} else {
err = writeReleaseHeader(bfd, c)
}

if err != nil {
return err
}

// Write table of contents
if err := writeTOC(bfd, toc); err != nil {
return err
}
// Write hierarchical tree of assets
if err := writeTOCTree(bfd, toc); err != nil {
return err
}

// Write restore procedure
return writeRestore(bfd)

}

func generateOneAsset(c *Config, a *Asset, wd string) error {
// Create output file.
fd, err := os.Create(filepath.Join(c.Output, a.Func + ".go"))
if err != nil {
return err
}

defer fd.Close()

// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
defer bfd.Flush()

// Write the header. This makes e.g. Github ignore diffs in generated files.
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
return err
}

if _, err = fmt.Fprint(bfd, "// source: "); err != nil {
return err
}

relative, _ := filepath.Rel(wd, a.Path)
if _, err = fmt.Fprintln(bfd, filepath.ToSlash(relative)); err != nil {
return err
}

if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
return err
}

// Write build tags, if applicable.
if len(c.Tags) > 0 {
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
return err
}
}

// Write package declaration.
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
if err != nil {
return err
}

// Write assets.
if c.Debug || c.Dev {
err = writeOneFileDebug(bfd, c, a)
} else {
err = writeOneFileRelease(bfd, c, a)
}

return err
}
77 changes: 77 additions & 0 deletions convert_to_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package bindata

import (
"os"
"bufio"
"fmt"
"path/filepath"
)

// translateToFile generates one single file
func translateToFile(c *Config, toc []Asset, wd string) error {
// Create output file.
fd, err := os.Create(c.Output)
if err != nil {
return err
}

defer fd.Close()

// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
defer bfd.Flush()

// Write the header. This makes e.g. Github ignore diffs in generated files.
if _, err = fmt.Fprint(bfd, "// Code generated by go-bindata.\n"); err != nil {
return err
}
if _, err = fmt.Fprint(bfd, "// sources:\n"); err != nil {
return err
}

for _, asset := range toc {
relative, _ := filepath.Rel(wd, asset.Path)
if _, err = fmt.Fprintf(bfd, "// %s\n", filepath.ToSlash(relative)); err != nil {
return err
}
}
if _, err = fmt.Fprint(bfd, "// DO NOT EDIT!\n\n"); err != nil {
return err
}

// Write build tags, if applicable.
if len(c.Tags) > 0 {
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
return err
}
}

// Write package declaration.
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
if err != nil {
return err
}

// Write assets.
if c.Debug || c.Dev {
err = writeDebug(bfd, c, toc)
} else {
err = writeRelease(bfd, c, toc)
}

if err != nil {
return err
}

// Write table of contents
if err := writeTOC(bfd, toc); err != nil {
return err
}
// Write hierarchical tree of assets
if err := writeTOCTree(bfd, toc); err != nil {
return err
}

// Write restore procedure
return writeRestore(bfd)
}
Loading

0 comments on commit 94068b1

Please sign in to comment.