forked from shuLhan/go-bindata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translateToDir.go
121 lines (99 loc) · 2.17 KB
/
translateToDir.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
package bindata
import (
"bufio"
"fmt"
"os"
"path/filepath"
)
// translateToDir generates splited file
func translateToDir(c *Config, toc []Asset) error {
if err := generateCommonFile(c, toc); err != nil {
return err
}
for i := range toc {
if err := generateOneAsset(c, &toc[i]); err != nil {
return err
}
}
return nil
}
func generateCommonFile(c *Config, toc []Asset) (err error) {
// Create output file.
fd, err := os.Create(filepath.Join(c.Output, DefOutputName))
if err != nil {
return err
}
// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
err = writeHeader(bfd, c, toc)
if err != nil {
goto out
}
// Write package declaration.
_, err = fmt.Fprintf(bfd, "\npackage %s\n\n", c.Package)
if err != nil {
goto out
}
// Write assets.
if c.Debug || c.Dev {
err = writeDebugHeader(bfd)
} else {
err = writeReleaseHeader(bfd, c)
}
if err != nil {
goto out
}
// Write table of contents
err = writeTOC(bfd, toc)
if err != nil {
goto out
}
// Write hierarchical tree of assets
err = writeTOCTree(bfd, toc)
if err != nil {
goto out
}
// Write restore procedure
err = writeRestore(bfd)
out:
return flushAndClose(fd, bfd, err)
}
func generateOneAsset(c *Config, a *Asset) (err error) {
// Create output file.
fd, err := os.Create(filepath.Join(c.Output, a.Func+".go"))
if err != nil {
return err
}
// Create a buffered writer for better performance.
bfd := bufio.NewWriter(fd)
// Write the header. This makes e.g. Github ignore diffs in generated files.
_, err = fmt.Fprint(bfd, headerGeneratedBy)
if err != nil {
goto out
}
if _, err = fmt.Fprint(bfd, "// source: "); err != nil {
goto out
}
if _, err = fmt.Fprintln(bfd, a.Path); err != nil {
goto out
}
// Write build tags, if applicable.
if len(c.Tags) > 0 {
if _, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags); err != nil {
goto out
}
}
// Write package declaration.
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
if err != nil {
goto out
}
// Write assets.
if c.Debug || c.Dev {
err = writeOneFileDebug(bfd, c, a)
} else {
err = writeOneFileRelease(bfd, c, a)
}
out:
return flushAndClose(fd, bfd, err)
}