-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (106 loc) · 2.94 KB
/
main.go
File metadata and controls
117 lines (106 loc) · 2.94 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/karrick/golf"
"github.com/pscn/file2go/encode"
"github.com/pscn/file2go/template"
)
//go:generate file2go -v -d -t -o template/files.go template/*.tmpl
var (
output = golf.StringP('o', "output", "", "the target file name")
test = golf.BoolP('t', "test", false, "create a test file")
pkg = golf.StringP('p', "package", "",
"package name to use, defaults to the base directory of target")
verbose = golf.BoolP('v', "verbose", false, "be more verbose")
devel = golf.BoolP('d', "devel", false, "internal → use the template files, "+
"not the ones generated by file2go\n"+
"\tonly required when compiling the templates themselves")
)
// Usage shows a short usage summary on stderr
func Usage(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, "%s\n", msg)
}
fmt.Fprintf(os.Stderr, "Usage of file2go:\n")
fmt.Fprintf(os.Stderr, "\tfile2go -o target/file.go -p mypkg file1 file2 file3\n")
golf.Usage()
}
// FlagUsage shows a short usage summary on stderr
func FlagUsage() {
Usage("")
}
func main() {
log.SetFlags(0) // log.LstdFlags | log.Lshortfile)
log.SetPrefix("file2go: ")
golf.Parse()
if len(golf.Args()) == 0 {
Usage("please specify one or more files")
os.Exit(2)
}
if *output == "" {
Usage("please specify output")
os.Exit(2)
}
if *pkg == "" {
*pkg = path.Base(path.Dir(*output))
}
files := make([]string, 0)
for _, pattern := range golf.Args() {
matches, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}
if len(matches) == 0 {
log.Fatalf("no files match pattern: %s", pattern)
}
files = append(files, matches...)
}
container := make([]template.File, len(files))
for i, filename := range files {
container[i] = template.File{Name: filepath.ToSlash(filename)}
content, err := encode.File(filename)
if err != nil {
log.Fatalf("failed to encode file: filename=%s, error=%s", filename, err)
}
container[i].Content = content
}
todo := map[string]string{*output: "template/files.tmpl"}
if *test {
tmpName := string((*output)[:len(*output)-3])
todo[tmpName+"_test.go"] = "template/files_test.tmpl"
}
for target, tmplName := range todo {
tmpl, err := template.Parse(tmplName, &container,
strings.Join(os.Args[1:], " "), *pkg, *devel)
if err != nil {
log.Fatalf("failed to generate code: %s\n", err)
}
if _, err := os.Stat(target); err == nil { // file exists
data, err := ioutil.ReadFile(target)
if err != nil {
log.Fatalf("failed to read old file: %s; error=%s", target, err)
}
if string(data) == string(*tmpl) {
if *verbose {
log.Printf("already up to date: %s", target)
continue
}
}
}
file, err := os.Create(target)
if err != nil {
log.Fatalf("failed to open file: %s\n", err)
}
defer file.Close()
_, err = file.Write(*tmpl)
if err != nil {
log.Fatalf("failed to write code to file: %s; error=%s", target, err)
}
}
}