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

switched over to using gentronics for generating templates #25

Merged
merged 1 commit into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 29 additions & 47 deletions buffalo/cmd/new_templates.go → buffalo/cmd/app_generators.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package cmd

var newTemplates = map[string]string{
"main.go": nMain,
".buffalo.dev.yml": nRefresh,
"actions/app.go": nApp,
"actions/home.go": nHomeHandler,
"actions/home_test.go": nHomeHandlerTest,
"actions/render.go": nRender,
"grifts/routes.go": nGriftRoutes,
"templates/index.html": nIndexHTML,
"templates/application.html": nApplicationHTML,
"assets/application.js": "",
"assets/application.css": nApplicationCSS,
".gitignore": nGitignore,
import "github.com/markbates/gentronics"

func newAppGenerator() *gentronics.Generator {
g := gentronics.New()
g.Add(gentronics.NewFile("main.go", nMain))
g.Add(gentronics.NewFile(".buffalo.dev.yml", nRefresh))
g.Add(gentronics.NewFile("actions/app.go", nApp))
g.Add(gentronics.NewFile("actions/home.go", nHomeHandler))
g.Add(gentronics.NewFile("actions/home_test.go", nHomeHandlerTest))
g.Add(gentronics.NewFile("actions/render.go", nRender))
g.Add(gentronics.NewFile("grifts/routes.go", nGriftRoutes))
g.Add(gentronics.NewFile("templates/index.html", nIndexHTML))
g.Add(gentronics.NewFile("templates/application.html", nApplicationHTML))
g.Add(gentronics.NewFile("assets/application.js", ""))
g.Add(gentronics.NewFile("assets/application.css", nApplicationCSS))
g.Add(gentronics.NewFile(".gitignore", nGitignore))
g.Add(newSodaGenerator())
return g
}

var nMain = `package main
const nMain = `package main

import (
"log"
Expand All @@ -29,7 +34,7 @@ func main() {
}

`
var nApp = `package actions
const nApp = `package actions

import (
"net/http"
Expand Down Expand Up @@ -57,7 +62,7 @@ func App() http.Handler {
}
`

var nRender = `package actions
const nRender = `package actions

import (
"net/http"
Expand Down Expand Up @@ -86,7 +91,7 @@ func fromHere(p string) string {
}
`

var nHomeHandler = `package actions
const nHomeHandler = `package actions

import "github.com/markbates/buffalo"

Expand All @@ -95,7 +100,7 @@ func HomeHandler(c buffalo.Context) error {
}
`

var nHomeHandlerTest = `package actions_test
const nHomeHandlerTest = `package actions_test

import (
"testing"
Expand All @@ -116,9 +121,9 @@ func Test_HomeHandler(t *testing.T) {
}
`

var nIndexHTML = `<h1>Welcome to Buffalo!</h1>`
const nIndexHTML = `<h1>Welcome to Buffalo!</h1>`

var nApplicationHTML = `<html>
const nApplicationHTML = `<html>
<head>
<meta charset="utf-8">
<title>Buffalo - {{ .name }}</title>
Expand All @@ -132,20 +137,20 @@ var nApplicationHTML = `<html>
</html>
`

var nApplicationCSS = `body {
const nApplicationCSS = `body {
font-family: helvetica;
}
`

var nGitignore = `vendor/
const nGitignore = `vendor/
**/*.log
**/*.sqlite
bin/
node_modules/
{{ .name }}
`

var nGriftRoutes = `package grifts
const nGriftRoutes = `package grifts

import (
"os"
Expand All @@ -171,7 +176,7 @@ var _ = Add("routes", func(c *Context) error {
return nil
})`

var nRefresh = `app_root: .
const nRefresh = `app_root: .
ignored_folders:
- vendor
- log
Expand All @@ -186,26 +191,3 @@ command_flags: []
enable_colors: true
log_name: buffalo
`

var nModels = `package models

import (
"log"
"os"

"github.com/markbates/going/defaults"
"github.com/markbates/pop"
)

var DB *pop.Connection

func init() {
var err error
env := defaults.String(os.Getenv("GO_ENV"), "development")
DB, err = pop.Connect(env)
if err != nil {
log.Fatal(err)
}
pop.Debug = env == "development"
}
`
28 changes: 3 additions & 25 deletions buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmd
import (
"errors"
"fmt"
"html/template"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -161,33 +160,12 @@ func genNewFiles(name, rootPath string) error {
"actionsPath": filepath.Join(packagePath, "actions"),
"modelsPath": filepath.Join(packagePath, "models"),
"withPop": !skipPop,
"dbType": dbType,
}

if !skipPop {
newTemplates["models/models.go"] = nModels
}
g := newAppGenerator()
return g.Run(rootPath, data)

for fn, tv := range newTemplates {
dir := filepath.Dir(fn)
err := os.MkdirAll(filepath.Join(rootPath, dir), 0755)
if err != nil {
return err
}
t, err := template.New(fn).Parse(tv)
if err != nil {
return err
}
fmt.Printf("--> ./%s/%s\n", name, fn)
f, err := os.Create(filepath.Join(rootPath, fn))
if err != nil {
return err
}
err = t.Execute(f, data)
if err != nil {
return err
}
}
return nil
}

func appGoGet() *exec.Cmd {
Expand Down
40 changes: 40 additions & 0 deletions buffalo/cmd/soda_generators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import "github.com/markbates/gentronics"

func newSodaGenerator() *gentronics.Generator {
g := gentronics.New()

f := gentronics.NewFile("models/models.go", nModels)
f.Should = func(data gentronics.Data) bool {
if p, ok := data["withPop"]; ok {
return p.(bool)
}
return false
}
g.Add(f)
return g
}

const nModels = `package models

import (
"log"
"os"

"github.com/markbates/going/defaults"
"github.com/markbates/pop"
)

var DB *pop.Connection

func init() {
var err error
env := defaults.String(os.Getenv("GO_ENV"), "development")
DB, err = pop.Connect(env)
if err != nil {
log.Fatal(err)
}
pop.Debug = env == "development"
}
`
2 changes: 1 addition & 1 deletion buffalo/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd

var Version = "0.3.6"
var Version = "0.3.7"