From 9dd2d9d4a7d7e5c010920700d3bf16432b4d20c9 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Sat, 7 Feb 2015 14:36:47 -0500 Subject: [PATCH] pass config all the way down to gen'd code; use templates instead of strings --- execute.go | 8 ++++---- list.go | 9 +++++---- main.go | 2 +- run.go | 36 +++++++++++++++++++++++++----------- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/execute.go b/execute.go index a26e2d5..371e800 100644 --- a/execute.go +++ b/execute.go @@ -16,7 +16,7 @@ import ( // If no custom file exists, it executes the passed 'standard' func. // // If the custom file exists, new files are written to a temp directory and executed via `go run` in the shell. -func execute(standard func() error, c config, imports typewriter.ImportSpecSet, body string) error { +func execute(standard func(c config) error, c config, imports typewriter.ImportSpecSet, body *template.Template) error { if importsSrc, err := os.Open(c.customName); err == nil { defer importsSrc.Close() @@ -25,13 +25,13 @@ func execute(standard func() error, c config, imports typewriter.ImportSpecSet, } // do it the regular way - return standard() + return standard(c) } // executeCustom creates a temp directory, copies importsSrc into it and generates a main() using the passed imports and body. // // `go run` is then called on those files via os.Command. -func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpecSet, body string) error { +func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpecSet, body *template.Template) error { temp, err := getTempDir() if err != nil { return err @@ -65,7 +65,7 @@ func executeCustom(importsSrc io.Reader, c config, imports typewriter.ImportSpec } // write the body, usually a main() - if _, err := main.WriteString(body); err != nil { + if err := body.Execute(main, c); err != nil { return err } diff --git a/list.go b/list.go index 4c70c54..5b51e74 100644 --- a/list.go +++ b/list.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "text/template" "github.com/clipperhouse/typewriter" ) @@ -13,7 +14,7 @@ func list(c config) error { typewriter.ImportSpec{Path: "github.com/clipperhouse/typewriter"}, ) - listFunc := func() error { + listFunc := func(c config) error { app, err := typewriter.NewApp("+gen") if err != nil { @@ -28,10 +29,10 @@ func list(c config) error { return nil } - return execute(listFunc, c, imports, listBody) + return execute(listFunc, c, imports, listTmpl) } -const listBody string = ` +var listTmpl = template.Must(template.New("list").Parse(` func main() { app, err := typewriter.NewApp("+gen") @@ -45,4 +46,4 @@ func main() { fmt.Println(" " + tw.Name()) } } -` +`)) diff --git a/main.go b/main.go index 7533b19..a33c8a1 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { err = runMain(os.Args) } -var exitStatusMsg = regexp.MustCompile(`^exit status \d+$`) +var exitStatusMsg = regexp.MustCompile("^exit status \\d+$") func runMain(args []string) error { c := defaultConfig diff --git a/run.go b/run.go index 5420488..adcc285 100644 --- a/run.go +++ b/run.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "text/template" "github.com/clipperhouse/typewriter" ) @@ -11,14 +12,15 @@ func run(c config) error { imports := typewriter.NewImportSpecSet( typewriter.ImportSpec{Path: "fmt"}, typewriter.ImportSpec{Path: "os"}, + typewriter.ImportSpec{Path: "regexp"}, typewriter.ImportSpec{Path: "github.com/clipperhouse/typewriter"}, ) - return execute(runStandard, c, imports, runBody) + return execute(runStandard, c, imports, runTmpl) } -func runStandard() (err error) { - app, err := typewriter.NewApp("+gen") +func runStandard(c config) (err error) { + app, err := c.Config.NewApp("+gen") if err != nil { return err @@ -49,16 +51,28 @@ func runStandard() (err error) { return nil } -const runBody string = ` +var runTmpl = template.Must(template.New("run").Parse(` + +var exitStatusMsg = regexp.MustCompile("^exit status \\d+$") + func main() { - if err := gen(); err != nil { - os.Stderr.WriteString(err.Error() + "\n") - os.Exit(1) - } + var err error + + defer func() { + if err != nil { + if !exitStatusMsg.MatchString(err.Error()) { + os.Stderr.WriteString(err.Error() + "\n") + } + os.Exit(1) + } + }() + + err = run() } -func gen() error { - app, err := typewriter.NewApp("+gen") +func run() error { + config := {{ printf "%#v" .Config }} + app, err := config.NewApp("+gen") if err != nil { return err @@ -88,4 +102,4 @@ func gen() error { return nil } -` +`))