Skip to content

Commit 9dd2d9d

Browse files
committed
pass config all the way down to gen'd code; use templates instead of strings
1 parent a9ec737 commit 9dd2d9d

4 files changed

Lines changed: 35 additions & 20 deletions

File tree

execute.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// If no custom file exists, it executes the passed 'standard' func.
1717
//
1818
// If the custom file exists, new files are written to a temp directory and executed via `go run` in the shell.
19-
func execute(standard func() error, c config, imports typewriter.ImportSpecSet, body string) error {
19+
func execute(standard func(c config) error, c config, imports typewriter.ImportSpecSet, body *template.Template) error {
2020
if importsSrc, err := os.Open(c.customName); err == nil {
2121
defer importsSrc.Close()
2222

@@ -25,13 +25,13 @@ func execute(standard func() error, c config, imports typewriter.ImportSpecSet,
2525
}
2626

2727
// do it the regular way
28-
return standard()
28+
return standard(c)
2929
}
3030

3131
// executeCustom creates a temp directory, copies importsSrc into it and generates a main() using the passed imports and body.
3232
//
3333
// `go run` is then called on those files via os.Command.
34-
func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpecSet, body string) error {
34+
func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpecSet, body *template.Template) error {
3535
temp, err := getTempDir()
3636
if err != nil {
3737
return err
@@ -65,7 +65,7 @@ func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpec
6565
}
6666

6767
// write the body, usually a main()
68-
if _, err := main.WriteString(body); err != nil {
68+
if err := body.Execute(main, c); err != nil {
6969
return err
7070
}
7171

list.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"text/template"
56

67
"github.com/clipperhouse/typewriter"
78
)
@@ -13,7 +14,7 @@ func list(c config) error {
1314
typewriter.ImportSpec{Path: "github.com/clipperhouse/typewriter"},
1415
)
1516

16-
listFunc := func() error {
17+
listFunc := func(c config) error {
1718
app, err := typewriter.NewApp("+gen")
1819

1920
if err != nil {
@@ -28,10 +29,10 @@ func list(c config) error {
2829
return nil
2930
}
3031

31-
return execute(listFunc, c, imports, listBody)
32+
return execute(listFunc, c, imports, listTmpl)
3233
}
3334

34-
const listBody string = `
35+
var listTmpl = template.Must(template.New("list").Parse(`
3536
func main() {
3637
app, err := typewriter.NewApp("+gen")
3738
@@ -45,4 +46,4 @@ func main() {
4546
fmt.Println(" " + tw.Name())
4647
}
4748
}
48-
`
49+
`))

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222
err = runMain(os.Args)
2323
}
2424

25-
var exitStatusMsg = regexp.MustCompile(`^exit status \d+$`)
25+
var exitStatusMsg = regexp.MustCompile("^exit status \\d+$")
2626

2727
func runMain(args []string) error {
2828
c := defaultConfig

run.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"text/template"
67

78
"github.com/clipperhouse/typewriter"
89
)
@@ -11,14 +12,15 @@ func run(c config) error {
1112
imports := typewriter.NewImportSpecSet(
1213
typewriter.ImportSpec{Path: "fmt"},
1314
typewriter.ImportSpec{Path: "os"},
15+
typewriter.ImportSpec{Path: "regexp"},
1416
typewriter.ImportSpec{Path: "github.com/clipperhouse/typewriter"},
1517
)
1618

17-
return execute(runStandard, c, imports, runBody)
19+
return execute(runStandard, c, imports, runTmpl)
1820
}
1921

20-
func runStandard() (err error) {
21-
app, err := typewriter.NewApp("+gen")
22+
func runStandard(c config) (err error) {
23+
app, err := c.Config.NewApp("+gen")
2224

2325
if err != nil {
2426
return err
@@ -49,16 +51,28 @@ func runStandard() (err error) {
4951
return nil
5052
}
5153

52-
const runBody string = `
54+
var runTmpl = template.Must(template.New("run").Parse(`
55+
56+
var exitStatusMsg = regexp.MustCompile("^exit status \\d+$")
57+
5358
func main() {
54-
if err := gen(); err != nil {
55-
os.Stderr.WriteString(err.Error() + "\n")
56-
os.Exit(1)
57-
}
59+
var err error
60+
61+
defer func() {
62+
if err != nil {
63+
if !exitStatusMsg.MatchString(err.Error()) {
64+
os.Stderr.WriteString(err.Error() + "\n")
65+
}
66+
os.Exit(1)
67+
}
68+
}()
69+
70+
err = run()
5871
}
5972
60-
func gen() error {
61-
app, err := typewriter.NewApp("+gen")
73+
func run() error {
74+
config := {{ printf "%#v" .Config }}
75+
app, err := config.NewApp("+gen")
6276
6377
if err != nil {
6478
return err
@@ -88,4 +102,4 @@ func gen() error {
88102
89103
return nil
90104
}
91-
`
105+
`))

0 commit comments

Comments
 (0)