-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
69 lines (58 loc) · 1.21 KB
/
writer.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
)
type GoWriter struct {
pkg string
imports, code []string
}
func MakeWriter() GoWriter {
pkg := ""
imports := make([]string, 0, 20)
code := make([]string, 0, 4000)
return GoWriter{pkg, imports, code}
}
func (w *GoWriter) AddGoFile(gf GoFile) {
if len(gf.pkg) > 0 {
w.pkg = gf.pkg
}
for _, fimp := range gf.imports {
found := false
for _, wimp := range w.imports {
if fimp == wimp {
found = true
break
}
}
if !found && len(fimp) != 0 {
w.imports = append(w.imports, fimp)
}
}
w.code = append(w.code, "")
w.code = append(w.code, "// **** "+gf.fname+" ****")
w.code = append(w.code, "")
w.code = append(w.code, gf.code...)
}
func (w *GoWriter) Write(path, outFname string) error {
f, err := os.Create(path + string(filepath.Separator) + outFname)
if err != nil {
return err
}
defer f.Close()
fmt.Fprintln(f, "// "+time.Now().Format("2006-01-02 15:04"))
fmt.Fprintln(f, w.pkg)
fmt.Fprintln(f, "")
fmt.Fprintln(f, "import (")
for _, imp := range w.imports {
fmt.Fprintln(f, "\t"+imp)
}
fmt.Fprintln(f, ")")
fmt.Fprintln(f, "")
for _, line := range w.code {
fmt.Fprintln(f, line)
}
return nil
}