-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
76 lines (72 loc) · 1.58 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
func main() {
err := makeConstructor()
if err != nil {
fmt.Printf("newc: [ERROR] %v\n", err)
fmt.Printf("It seems like there is some trouble here. Try this:\n")
fmt.Printf("\t1. Check and upgrade this tool (https://github.com/Bin-Huang/newc)\n")
fmt.Printf("\t2. Submit an issue on Github (https://github.com/Bin-Huang/newc/issues)\n")
os.Exit(1)
}
}
func makeConstructor() error {
pkg, err := GetPackageInfo(".")
if err != nil {
return err
}
// skip if generated recently
genFilename, err := filepath.Abs("./constructor_gen.go")
if err != nil {
return err
}
if isGeneratedRecently(genFilename) {
return nil
}
allImports := []ImportInfo{}
allResults := []StructInfo{}
for _, filename := range pkg.GoFiles {
has, err := IncludeMakeMark(filename)
if err != nil {
return err
}
if !has {
continue
}
results, imports, err := ParseCodeFile(filename)
if err != nil {
return err
}
if len(results) == 0 {
continue
}
allImports = append(allImports, imports...)
allResults = append(allResults, results...)
}
if len(allResults) == 0 {
return nil
}
code, err := GenerateCode(pkg.Name, allImports, allResults)
if err != nil {
return err
}
err = ioutil.WriteFile(genFilename, []byte(code), 0644)
if err != nil {
return err
}
fmt.Printf("newc: [INFO] wrote %v\n", genFilename)
return nil
}
func isGeneratedRecently(genFilename string) bool {
stat, err := os.Stat(genFilename)
if err != nil {
return false
}
return time.Now().Sub(stat.ModTime()) < 5*time.Second
}