Skip to content

Commit

Permalink
pass config all the way down to gen'd code; use templates instead of …
Browse files Browse the repository at this point in the history
…strings
  • Loading branch information
clipperhouse committed Feb 7, 2015
1 parent a9ec737 commit 9dd2d9d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
8 changes: 4 additions & 4 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"text/template"

"github.com/clipperhouse/typewriter"
)
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -45,4 +46,4 @@ func main() {
fmt.Println(" " + tw.Name())
}
}
`
`))
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"text/template"

"github.com/clipperhouse/typewriter"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -88,4 +102,4 @@ func gen() error {
return nil
}
`
`))

0 comments on commit 9dd2d9d

Please sign in to comment.