Skip to content

Commit 2b70da0

Browse files
committed
implement simple list() command; refactor to share temp + execute code as run; fewer if's :)
1 parent 40c3696 commit 2b70da0

File tree

6 files changed

+103
-41
lines changed

6 files changed

+103
-41
lines changed

custom.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func custom(filename string) error {
1414
p := pkg{
1515
Name: "main",
1616
Imports: stdImports,
17-
Main: false,
1817
}
1918

2019
if err := tmpl.Execute(w, p); err != nil {

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func main() {
3333
},
3434
Usage: "Creates a custom _gen.go file in which to specify your own typewriter imports",
3535
},
36-
3736
{
3837
Name: "get",
3938
Action: func(c *cli.Context) {
@@ -46,6 +45,13 @@ func main() {
4645
cli.BoolFlag{"u", "use the network to update the typewriter packages and their dependencies"},
4746
},
4847
},
48+
{
49+
Name: "list",
50+
Action: func(c *cli.Context) {
51+
list(customFilename)
52+
},
53+
Usage: "Lists current typewriters",
54+
},
4955
},
5056
}
5157

pkg.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ var stdImports = []string{
88
type pkg struct {
99
Name string
1010
Imports []string
11-
Main bool
1211
}

run.go

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,46 @@ import (
1313
"github.com/clipperhouse/gen/typewriter"
1414
)
1515

16-
// filename is the optional file containing custom typewriters
17-
func run(filename string) error {
18-
if src, err := os.Open(filename); err == nil {
16+
// execute runs a gen command by first determining whether a custom imports file (typically _gen.go) exists
17+
//
18+
// If no custom file exists, it executes the passed 'standard' func.
19+
//
20+
// If the custom file exists, new files are written to a temp directory and executed via `go run` in the shell.
21+
func execute(standard func() error, customFilename string, imports []string, body string) error {
22+
if src, err := os.Open(customFilename); err == nil {
1923
// custom imports file exists, use it
2024
defer src.Close()
2125

22-
if err := runCustom(src); err != nil {
26+
if err := executeCustom(src, imports, body); err != nil {
2327
return err
2428
}
2529
} else {
2630
// do it the regular way
27-
if err := runStandard(); err != nil {
31+
if err := standard(); err != nil {
2832
return err
2933
}
3034
}
3135
return nil
3236
}
3337

34-
func runStandard() error {
35-
app, err := typewriter.NewApp("+gen")
36-
37-
if err != nil {
38-
return err
39-
}
40-
41-
if err := app.WriteAll(); err != nil {
42-
return err
43-
}
44-
45-
return nil
46-
}
47-
48-
func runCustom(src io.Reader) error {
49-
38+
// executeCustom creates a temp directory, copies src into it and generates a main() using the passed imports and body.
39+
//
40+
// `go run` is then called on those files via os.Command.
41+
func executeCustom(src io.Reader, imports []string, body string) error {
5042
temp, err := getTempDir()
5143
if err != nil {
5244
return err
5345
}
5446
defer os.RemoveAll(temp)
5547

5648
// set up imports file containing the custom typewriters (from _gen.go)
57-
imports, err := os.Create(filepath.Join(temp, "imports.go"))
49+
imps, err := os.Create(filepath.Join(temp, "imports.go"))
5850
if err != nil {
5951
return err
6052
}
61-
defer imports.Close()
53+
defer imps.Close()
6254

63-
io.Copy(imports, src)
55+
io.Copy(imps, src)
6456

6557
// set up main to be run
6658
main, err := os.Create(filepath.Join(temp, "main.go"))
@@ -70,20 +62,24 @@ func runCustom(src io.Reader) error {
7062
defer main.Close()
7163

7264
p := pkg{
73-
Name: "main",
74-
Imports: []string{
75-
`"github.com/clipperhouse/gen/typewriter"`,
76-
},
77-
Main: true,
65+
Name: "main",
66+
Imports: imports,
7867
}
7968

69+
// execute the package declaration and imports
8070
if err := tmpl.Execute(main, p); err != nil {
8171
return err
8272
}
8373

74+
// write the body, usually a main()
75+
if _, err := main.WriteString(body); err != nil {
76+
return err
77+
}
78+
79+
// call `go run` on these files & send back output/err
8480
var out, outerr bytes.Buffer
8581

86-
cmd := exec.Command("go", "run", main.Name(), imports.Name())
82+
cmd := exec.Command("go", "run", main.Name(), imps.Name())
8783
cmd.Stdout = &out
8884
cmd.Stderr = &outerr
8985

@@ -104,3 +100,51 @@ func runCustom(src io.Reader) error {
104100

105101
return nil
106102
}
103+
104+
func run(customFilename string) error {
105+
imports := []string{
106+
`"log"`,
107+
`"github.com/clipperhouse/gen/typewriter"`,
108+
}
109+
110+
return execute(runStandard, customFilename, imports, runBody)
111+
}
112+
113+
func runStandard() error {
114+
app, err := typewriter.NewApp("+gen")
115+
116+
if err != nil {
117+
return err
118+
}
119+
120+
if err := app.WriteAll(); err != nil {
121+
return err
122+
}
123+
124+
return nil
125+
}
126+
127+
func list(customFilename string) error {
128+
imports := []string{
129+
`"fmt"`,
130+
`"log"`,
131+
`"github.com/clipperhouse/gen/typewriter"`,
132+
}
133+
134+
return execute(listStandard, customFilename, imports, listBody)
135+
}
136+
137+
func listStandard() error {
138+
app, err := typewriter.NewApp("+gen")
139+
140+
if err != nil {
141+
return err
142+
}
143+
144+
fmt.Println("Installed typewriters:")
145+
for _, tw := range app.TypeWriters {
146+
fmt.Println(" " + tw.Name())
147+
}
148+
149+
return nil
150+
}

run_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// +gen methods:"Where"
99
type dummy int
1010

11-
func TestRunCustom(t *testing.T) {
11+
func TestRun(t *testing.T) {
1212
customName := "_gen_test.go"
1313
genName := "dummy_gen.go"
1414
fooName := "dummy_foo.go"
@@ -47,7 +47,6 @@ func TestRunCustom(t *testing.T) {
4747
// non-standard typewriter
4848
`_ "github.com/clipperhouse/gen/typewriters/foowriter"`,
4949
},
50-
Main: false,
5150
}
5251

5352
if err := tmpl.Execute(w, p); err != nil {

write.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,33 @@ var tmpl = template.Must(template.New("package").Parse(`package {{.Name}}
2020
import ({{range .Imports}}
2121
{{.}}{{end}}
2222
)
23-
{{end}}
24-
{{if .Main}}
23+
{{end}}`))
24+
25+
const runBody string = `
2526
func main() {
2627
app, err := typewriter.NewApp("+gen")
2728
2829
if err != nil {
29-
panic(err)
30+
log.Fatal(err)
3031
}
3132
3233
if err := app.WriteAll(); err != nil {
33-
panic(err)
34+
log.Fatal(err)
35+
}
36+
}
37+
`
38+
39+
const listBody string = `
40+
func main() {
41+
app, err := typewriter.NewApp("+gen")
42+
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
47+
fmt.Println("Installed typewriters (custom):")
48+
for _, tw := range app.TypeWriters {
49+
fmt.Println(" " + tw.Name())
3450
}
3551
}
36-
{{end}}
37-
`))
52+
`

0 commit comments

Comments
 (0)