-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (189 loc) · 4.01 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2018 Paolo Machiavelli. All rights reserved.
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/printer"
"go/scanner"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func usage() {
fmt.Fprint(os.Stderr, `usage: gooey [flags] [path ...]
Gooey processes its file arguments, and any *.goo files contained in its
directory arguments. If no path is specified, the current directory is
assumed. If -fmt is true, input files are reformatted in place. If -gen is
true, they are translated and written to corresponding .go files.
-fmt reformat input
-gen generate Go code (default true)
-std read stdin and write to stdout
`)
}
var (
_fmt = flag.Bool("fmt", false, "")
_gen = flag.Bool("gen", true, "")
_std = flag.Bool("std", false, "")
)
const (
cprefTag = "GOOEY_COLON_"
tempTag = "GOOEY_TEMP_"
)
func main() {
flag.Usage = usage
flag.Parse()
if *_std {
processStdin()
return
}
var args = flag.Args()
if len(args) == 0 {
args = []string{"."}
}
for _, arg := range args {
var info, err = os.Stat(arg)
if err != nil {
fatal(err)
}
if !info.IsDir() {
info, err = os.Lstat(arg)
if err != nil {
fatal(err)
}
var mode = info.Mode()
if !mode.IsRegular() {
fatalf("%s is not a regular file", arg)
}
processFile(arg, mode)
continue
}
GOOEY_TEMP_0, GOOEY_TEMP_1 := os.Open(arg)
var dir = GOOEY_TEMP_0
err = GOOEY_TEMP_1
if err != nil {
fatal(err)
}
GOOEY_TEMP_2, GOOEY_TEMP_3 := dir.Readdirnames(-1)
var names = GOOEY_TEMP_2
err = GOOEY_TEMP_3
if err != nil {
fatal(err)
}
dir.Close()
for _, n := range names {
if !strings.HasSuffix(n, ".goo") {
continue
}
var path = filepath.Join(arg, n)
info, err = os.Lstat(path)
if err != nil {
fatal(err)
}
var mode = info.Mode()
if !mode.IsRegular() {
logf("%s is not a regular file: skipping", path)
continue
}
processFile(path, mode)
}
}
}
func processStdin() {
var src, err = ioutil.ReadAll(os.Stdin)
if err != nil {
fatal(err)
}
var fmt, gen = processCode("stdin", src)
if *_fmt {
_, err = os.Stdout.Write(fmt)
} else if *_gen {
_, err = os.Stdout.Write(gen)
}
if err != nil {
fatal(err)
}
}
func processFile(path string, mode os.FileMode) {
var src, err = ioutil.ReadFile(path)
if err != nil {
fatal(err)
}
var fmt, gen = processCode(path, src)
if *_fmt {
writeFile(path, mode, fmt)
}
if *_gen {
writeFile(strings.TrimSuffix(path, ".goo")+".go", mode, gen)
}
}
// writeFile writes data in a temp file and moves it over path.
func writeFile(path string, mode os.FileMode, data []byte) {
var file, err = ioutil.TempFile(filepath.Dir(path), "tmp")
if err != nil {
fatal(err)
}
defer file.Close()
err = file.Chmod(mode.Perm())
if err != nil {
logf("%v\n", err)
}
_, err = file.Write(data)
if err != nil {
fatal(err)
}
err = os.Rename(file.Name(), path)
if err != nil {
fatal(err)
}
}
// processCode parses and translates src, and returns formatted
// and/or translated code according to the respective flags.
func processCode(name string, src []byte) (fmt, gen []byte) {
var fset = token.NewFileSet()
var file, err = parseFile(fset, name, src)
if err != nil {
fatal(err)
}
ast.SortImports(fset, file)
if *_fmt {
fmt = print2buf(fset, file)
}
err = xlateFile(fset, file)
if err != nil {
fatal(err)
}
if *_gen {
gen = print2buf(fset, file)
}
return
}
// same config used by go/format
var format = printer.Config{
Mode: printer.UseSpaces | printer.TabIndent,
Tabwidth: 8,
}
func print2buf(fset *token.FileSet, file *ast.File) []byte {
var buf bytes.Buffer
var err = format.Fprint(&buf, fset, file)
if err != nil {
fatal(err)
}
return buf.Bytes()
}
func logf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
}
func fatalf(format string, a ...interface{}) {
logf(format, a...)
os.Exit(1)
}
func fatal(err error) {
scanner.PrintError(os.Stderr, err)
os.Exit(1)
}