Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
use raymond for generator templates
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Dec 19, 2016
1 parent c66eecc commit bf19ae6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 54 deletions.
40 changes: 21 additions & 19 deletions buffalo/cmd/app_generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func newAppGenerator() *gentronics.Generator {
g.Add(gentronics.NewCommand(goInstall("github.com/markbates/refresh")))
g.Add(gentronics.NewCommand(goGet("github.com/markbates/grift/...")))
g.Add(gentronics.NewCommand(goInstall("github.com/markbates/grift")))
g.Add(gentronics.NewCommand(goGet("github.com/motemen/gore")))
g.Add(gentronics.NewCommand(goInstall("github.com/motemen/gore")))
g.Add(generate.NewJQueryGenerator())
g.Add(generate.NewBootstrapGenerator())
g.Add(newSodaGenerator())
Expand All @@ -52,7 +54,7 @@ import (
"net/http"
"os"
"{{.actionsPath}}"
"{{actionsPath}}"
"github.com/markbates/going/defaults"
)
Expand All @@ -68,10 +70,10 @@ import (
"os"
"github.com/markbates/buffalo"
{{if .withPop -}}
{{#if withPop }}
"github.com/markbates/buffalo/middleware"
"{{.modelsPath}}"
{{end -}}
"{{modelsPath}}"
{{/if}}
"github.com/markbates/going/defaults"
)
Expand All @@ -89,9 +91,9 @@ func App() *buffalo.App {
Env: ENV,
})
{{if .withPop -}}
{{#if withPop }}
app.Use(middleware.PopTransaction(models.DB))
{{end -}}
{{/if}}
app.ServeFiles("/assets", assetsPath())
app.GET("/", HomeHandler)
Expand Down Expand Up @@ -146,7 +148,7 @@ const nHomeHandlerTest = `package actions_test
import (
"testing"
"{{.actionsPath}}"
"{{actionsPath}}"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)
Expand All @@ -167,22 +169,22 @@ const nIndexHTML = `<h1>Welcome to Buffalo!</h1>`
const nApplicationHTML = `<html>
<head>
<meta charset="utf-8">
<title>Buffalo - {{ .titleName }}</title>
{{if .withBootstrap -}}
<title>Buffalo - {{ titleName }}</title>
{{#if withBootstrap }}
<link rel="stylesheet" href="/assets/css/bootstrap.css" type="text/css" media="all" />
{{end -}}
{{/if}}
<link rel="stylesheet" href="/assets/css/application.css" type="text/css" media="all" />
</head>
<body>
{{"{{"}} yield {{"}}"}}
\{{ yield }}
{{if .withJQuery -}}
{{#if withJQuery }}
<script src="/assets/js/jquery.js" type="text/javascript" charset="utf-8"></script>
{{end -}}
{{if .withBootstrap -}}
{{/if}}
{{#if withBootstrap }}
<script src="/assets/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
{{end -}}
{{/if}}
<script src="/assets/js/application.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
Expand All @@ -198,7 +200,7 @@ const nGitignore = `vendor/
**/*.sqlite
bin/
node_modules/
{{ .name }}
{{ name }}
`

const nGriftRoutes = `package grifts
Expand All @@ -207,7 +209,7 @@ import (
"os"
. "github.com/markbates/grift/grift"
"{{.actionsPath}}"
"{{actionsPath}}"
"github.com/olekukonko/tablewriter"
)
Expand Down Expand Up @@ -240,11 +242,11 @@ included_extensions:
- .md
build_path: /tmp
build_delay: 200ns
binary_name: {{.name}}-build
binary_name: {{name}}-build
command_flags: []
enable_colors: true
log_name: buffalo
`

const nProcfile = `web: {{.name}}`
const nProcfile = `web: {{name}}`
const nProcfileDev = `web: buffalo dev`
4 changes: 2 additions & 2 deletions buffalo/cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func init() {
var cMain = `
package main
{{range .packages}}
{{#each packages}}
import _ "{{.}}"
{{end}}
{{/each}}
`
15 changes: 7 additions & 8 deletions buffalo/cmd/generate/goth.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ var GothCmd = &cobra.Command{
// NewGothGenerator a actions/goth.go file configured to the specified providers.
func NewGothGenerator() *gentronics.Generator {
g := gentronics.New()
f := gentronics.NewFile(filepath.Join("actions", "goth.go"), gGoth)
g.Add(f)
g.Add(gentronics.NewFile(filepath.Join("actions", "goth.go"), gGoth))
g.Add(Fmt)
return g
}
Expand All @@ -60,18 +59,18 @@ import (
"github.com/markbates/buffalo"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
{{ range .providers -}}
"github.com/markbates/goth/providers/{{. | downcase}}"
{{ end -}}
{{#each providers}}
"github.com/markbates/goth/providers/{{ downcase . }}"
{{/each}}
)
func init() {
gothic.Store = App().SessionStore
goth.UseProviders(
{{ range .providers -}}
{{.|downcase}}.New(os.Getenv("{{.|upcase}}_KEY"), os.Getenv("{{.|upcase}}_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/{{.|downcase}}/callback")),
{{ end -}}
{{#each providers}}
{{downcase .}}.New(os.Getenv("{{upcase .}}_KEY"), os.Getenv("{{upcase .}}_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/{{downcase .}}/callback")),
{{/each}}
)
app := App().Group("/auth")
Expand Down
46 changes: 23 additions & 23 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var ResourceCmd = &cobra.Command{
}
name := args[0]
data := gentronics.Data{
"name": name,
"singular": inflect.Singularize(name),
"plural": inflect.Pluralize(name),
"camel": inflect.Camelize(name),
"underscore": inflect.Underscore(name),
"name": name,
"singular": inflect.Singularize(name),
"plural": inflect.Pluralize(name),
"camel": inflect.Camelize(name),
"under": inflect.Underscore(name),
}
return NewResourceGenerator(data).Run(".", data)
},
Expand All @@ -54,8 +54,8 @@ var ResourceCmd = &cobra.Command{
// NewResourceGenerator generates a new actions/resource file and a stub test.
func NewResourceGenerator(data gentronics.Data) *gentronics.Generator {
g := gentronics.New()
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["underscore"])), rAction))
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["underscore"])), rActionTest))
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["under"])), rAction))
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["under"])), rActionTest))
g.Add(Fmt)
return g
}
Expand All @@ -64,46 +64,46 @@ var rAction = `package actions
import "github.com/markbates/buffalo"
type {{.camel}}Resource struct{
type {{camel}}Resource struct{
buffalo.Resource
}
func init() {
App().Resource("/{{.underscore}}", &{{.camel}}Resource{&buffalo.BaseResource{}})
App().Resource("/{{under}}", &{{camel}}Resource{&buffalo.BaseResource{}})
}
// List default implementation. Returns a 404
func (v *{{.camel}}Resource) List(c buffalo.Context) error {
func (v *{{camel}}Resource) List(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Show default implementation. Returns a 404
func (v *{{.camel}}Resource) Show(c buffalo.Context) error {
func (v *{{camel}}Resource) Show(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// New default implementation. Returns a 404
func (v *{{.camel}}Resource) New(c buffalo.Context) error {
func (v *{{camel}}Resource) New(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Create default implementation. Returns a 404
func (v *{{.camel}}Resource) Create(c buffalo.Context) error {
func (v *{{camel}}Resource) Create(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Edit default implementation. Returns a 404
func (v *{{.camel}}Resource) Edit(c buffalo.Context) error {
func (v *{{camel}}Resource) Edit(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Update default implementation. Returns a 404
func (v *{{.camel}}Resource) Update(c buffalo.Context) error {
func (v *{{camel}}Resource) Update(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Destroy default implementation. Returns a 404
func (v *{{.camel}}Resource) Destroy(c buffalo.Context) error {
func (v *{{camel}}Resource) Destroy(c buffalo.Context) error {
return c.Error(404, errors.New("resource not implemented"))
}`

Expand All @@ -115,37 +115,37 @@ import (
"github.com/stretchr/testify/require"
)
func Test_{{.camel}}Resource_List(t *testing.T) {
func Test_{{camel}}Resource_List(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_Show(t *testing.T) {
func Test_{{camel}}Resource_Show(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_New(t *testing.T) {
func Test_{{camel}}Resource_New(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_Create(t *testing.T) {
func Test_{{camel}}Resource_Create(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_Edit(t *testing.T) {
func Test_{{camel}}Resource_Edit(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_Update(t *testing.T) {
func Test_{{camel}}Resource_Update(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
func Test_{{.camel}}Resource_Destroy(t *testing.T) {
func Test_{{camel}}Resource_Destroy(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
Expand Down
7 changes: 7 additions & 0 deletions render/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/aymerick/raymond"
"github.com/markbates/inflect"
)

// Helpers that are automatically injected into templates.
Expand All @@ -19,6 +20,12 @@ var Helpers = map[string]interface{}{
"downcase": strings.ToLower,
}

func init() {
for k, v := range inflect.Helpers {
Helpers[k] = v
}
}

// ToJSON converts an interface into a string.
func ToJSON(v interface{}) string {
b, err := json.Marshal(v)
Expand Down
2 changes: 0 additions & 2 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/aymerick/raymond"
"github.com/markbates/buffalo/render/helpers"
"github.com/markbates/buffalo/render/resolvers"
"github.com/markbates/inflect"
)

// Engine used to power all defined renderers.
Expand Down Expand Up @@ -39,7 +38,6 @@ func New(opts Options) *Engine {
moot: &sync.Mutex{},
}
e.RegisterHelpers(helpers.Helpers)
e.RegisterHelpers(inflect.Helpers)
e.RegisterHelpers(h)
return e
}
Expand Down

0 comments on commit bf19ae6

Please sign in to comment.