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

Commit

Permalink
Generators (#40)
Browse files Browse the repository at this point in the history
* added some new generators, jquery, bootstrap, bootswatch, and railsjs

* added a basic resource generator

* fixed code climate issues.

* version bump

* added brian's "testimonial".
  • Loading branch information
markbates authored Dec 11, 2016
1 parent 3cd1c2c commit a70a4aa
Show file tree
Hide file tree
Showing 13 changed files with 517 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Buffalo is "idiomatic", for whatever that is worth. The purpose of a framework i

If you were to look through the Buffalo code base you'll find little code, just enough to assemble the amazing packages that other's have written into one coherent system.

> I :heart: web dev in go again - Brian Ketelsen
## Installation

```text
Expand Down
19 changes: 14 additions & 5 deletions buffalo/cmd/app_generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os/exec"

"github.com/markbates/buffalo/buffalo/cmd/generate"
"github.com/markbates/gentronics"
)

Expand All @@ -26,10 +27,11 @@ 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(newJQueryGenerator())
g.Add(generate.NewJQueryGenerator())
g.Add(generate.NewBootstrapGenerator())
g.Add(newSodaGenerator())
g.Add(gentronics.NewCommand(appGoGet()))
g.Add(gentronics.NewCommand(exec.Command("gofmt", "-w", ".")))
g.Add(generate.Fmt)
return g
}

Expand Down Expand Up @@ -153,15 +155,22 @@ const nIndexHTML = `<h1>Welcome to Buffalo!</h1>`
const nApplicationHTML = `<html>
<head>
<meta charset="utf-8">
<title>Buffalo - {{ .name }}</title>
<title>Buffalo - {{ .titleName }}</title>
{{if .withBootstrap -}}
<link rel="stylesheet" href="/assets/bootstrap.css" type="text/css" media="all" />
{{end -}}
<link rel="stylesheet" href="/assets/application.css" type="text/css" media="all" />
</head>
<body>
{{"{{"}} yield {{"}}"}}
{{if .withJQuery -}}
{{if .withJQuery -}}
<script src="/assets/jquery.js" type="text/javascript" charset="utf-8"></script>
{{end -}}
{{end -}}
{{if .withBootstrap -}}
<script src="/assets/bootstrap.js" type="text/javascript" charset="utf-8"></script>
{{end -}}
<script src="/assets/application.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
Expand Down
40 changes: 40 additions & 0 deletions buffalo/cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2016 Mark Bates <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"github.com/markbates/buffalo/buffalo/cmd/generate"
"github.com/spf13/cobra"
)

var generateCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"g"},
}

func init() {
generateCmd.AddCommand(generate.JQueryCmd)
generateCmd.AddCommand(generate.RailsJSCmd)
generateCmd.AddCommand(generate.BootstrapCmd)
generateCmd.AddCommand(generate.BootswatchCmd)
generateCmd.AddCommand(generate.ResourceCmd)
RootCmd.AddCommand(generateCmd)
}
75 changes: 75 additions & 0 deletions buffalo/cmd/generate/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2016 Mark Bates <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package generate

import (
"path/filepath"

"github.com/markbates/gentronics"
"github.com/spf13/cobra"
)

// BootstrapCmd will generate new Bootstrap files. Regardless of whatever
// other settings you might, this will generate jQuery files as that is a
// pre-requisite of Bootstrap.
var BootstrapCmd = &cobra.Command{
Use: "bootstrap",
Short: "Generates Bootstrap 3 files",
RunE: func(cmd *cobra.Command, args []string) error {
return NewBootstrapGenerator().Run(".", gentronics.Data{
"withBootstrap": true,
})
},
}

// NewBootstrapGenerator will generate new Bootstrap files. Regardless of whatever
// other settings you might, this will generate jQuery files as that is a
// pre-requisite of Bootstrap.
func NewBootstrapGenerator() *gentronics.Generator {
should := func(data gentronics.Data) bool {
if p, ok := data["withBootstrap"]; ok {
return p.(bool)
}
return false
}
g := gentronics.New()
jf := &gentronics.RemoteFile{
File: gentronics.NewFile(filepath.Join("assets", "bootstrap.css"), ""),
}
jf.Should = should
jf.RemotePath = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
g.Add(jf)

jf = &gentronics.RemoteFile{
File: gentronics.NewFile(filepath.Join("assets", "bootstrap.js"), ""),
}
jf.Should = should
jf.RemotePath = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
g.Add(jf)
g.Add(&gentronics.Func{
Should: should,
Runner: func(rootPath string, data gentronics.Data) error {
data["withJQuery"] = true
return NewJQueryGenerator().Run(rootPath, data)
},
})
return g
}
88 changes: 88 additions & 0 deletions buffalo/cmd/generate/bootswatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright © 2016 Mark Bates <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package generate

import (
"fmt"
"path/filepath"
"strings"

"github.com/markbates/gentronics"
"github.com/spf13/cobra"
)

// BootswatchCmd will generate new Bootswatch themes. Regardless of whatever
// other settings you have, this will generate jQuery and Bootstrap files as
// they are pre-requisites for Bootswatch
var BootswatchCmd = &cobra.Command{
Use: "bootswatch [theme]",
Short: "Generates Bootswatch 3 files",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("You must choose a theme! [%s]", strings.Join(bootswatchThemes, ", "))
}
g, err := NewBootswatchGenerator(args[0])
if err != nil {
return err
}
return g.Run(".", gentronics.Data{
"withBootswatch": true,
})
},
}

// NewBootswatchGenerator will generate new Bootswatch themes. Regardless of whatever
// other settings you have, this will generate jQuery and Bootstrap files as
// they are pre-requisites for Bootswatch
func NewBootswatchGenerator(theme string) (*gentronics.Generator, error) {
themeFound := false
for _, t := range bootswatchThemes {
if t == theme {
themeFound = true
break
}
}
if !themeFound {
return nil, fmt.Errorf("Could not find a Bootswatch theme for %s!", theme)
}
g := gentronics.New()
g.Add(&gentronics.Func{
Should: func(data gentronics.Data) bool { return true },
Runner: func(rootPath string, data gentronics.Data) error {
data["withJQuery"] = true
data["withBootstrap"] = true
err := NewJQueryGenerator().Run(rootPath, data)
if err != nil {
return err
}
return NewBootstrapGenerator().Run(rootPath, data)
},
})
jf := &gentronics.RemoteFile{
File: gentronics.NewFile(filepath.Join("assets", "bootstrap.css"), ""),
}
jf.RemotePath = fmt.Sprintf("https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/%s/bootstrap.min.css", theme)
g.Add(jf)

return g, nil
}

var bootswatchThemes = []string{"cerulean", "cosmo", "cyborg", "darkly", "flatly", "journal", "lumen", "paper", "readable", "sandstone", "simplex", "slate", "spacelab", "superhero", "united", "yeti"}
20 changes: 20 additions & 0 deletions buffalo/cmd/generate/gofmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package generate

import (
"os/exec"

"github.com/markbates/gentronics"
)

// Fmt is command that will use `goimports` if available,
// or fail back to `gofmt` otherwise.
var Fmt *gentronics.Command

func init() {
c := "gofmt"
_, err := exec.LookPath("goimports")
if err == nil {
c = "goimports"
}
Fmt = gentronics.NewCommand(exec.Command(c, "-w", "."))
}
66 changes: 66 additions & 0 deletions buffalo/cmd/generate/jquery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2016 Mark Bates <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package generate

import (
"path/filepath"

"github.com/markbates/gentronics"
"github.com/spf13/cobra"
)

// JQueryCmd will generate jQuery files.
var JQueryCmd = &cobra.Command{
Use: "jquery",
Short: "Generates an assets/jquery.js file",
RunE: func(cmd *cobra.Command, args []string) error {
data := gentronics.Data{
"withJQuery": true,
}
return NewJQueryGenerator().Run(".", data)
},
}

// NewJQueryGenerator will generate jQuery files.
func NewJQueryGenerator() *gentronics.Generator {
should := func(data gentronics.Data) bool {
if p, ok := data["withJQuery"]; ok {
return p.(bool)
}
return false
}

g := gentronics.New()
jf := &gentronics.RemoteFile{
File: gentronics.NewFile(filepath.Join("assets", "jquery.js"), ""),
}
jf.Should = should
jf.RemotePath = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"
g.Add(jf)

jm := &gentronics.RemoteFile{
File: gentronics.NewFile(filepath.Join("assets", "jquery.map"), ""),
}
jm.Should = should
jm.RemotePath = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.map"
g.Add(jm)
return g
}
Loading

0 comments on commit a70a4aa

Please sign in to comment.