Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
adds a plugcmds package and makes writing plugins even simpler (#6)
Browse files Browse the repository at this point in the history
* adds a plugcmds package and makes writing plugins even simpler

* cleaned up the way listen commands are setup

* removed accidental hardcoded short name

* shuffled the listen command around
  • Loading branch information
markbates authored Oct 17, 2018
1 parent bdad90c commit 8321a97
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 112 deletions.
27 changes: 7 additions & 20 deletions cmd/available.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
package cmd

import (
"encoding/json"
"os"

"github.com/gobuffalo/buffalo-plugins/plugins"
"github.com/gobuffalo/buffalo-plugins/plugins/plugcmds"
"github.com/spf13/cobra"
)

var Available = plugcmds.NewAvailable()

var pluginsCmd = &cobra.Command{
Use: "plugins",
Short: "tools for working with buffalo plugins",
}

// availableCmd represents the available command
var availableCmd = &cobra.Command{
Use: "available",
Short: "a list of available buffalo plugins",
RunE: func(cmd *cobra.Command, args []string) error {
plugs := plugins.Commands{
{Name: generateCmd.Use, BuffaloCommand: "generate", Description: generateCmd.Short, Aliases: generateCmd.Aliases},
{Name: pluginsCmd.Use, BuffaloCommand: "root", Description: pluginsCmd.Short, Aliases: pluginsCmd.Aliases},
{Name: "listen", UseCommand: "listen", BuffaloCommand: "events", Description: listenCmd.Short, Aliases: listenCmd.Aliases},
}
return json.NewEncoder(os.Stdout).Encode(plugs)
},
}

func init() {
rootCmd.AddCommand(pluginsCmd)
rootCmd.AddCommand(availableCmd)
Available.Add("generate", generateCmd)
Available.Add("root", pluginsCmd)
Available.Listen(Listen)
Available.Mount(rootCmd)
}
60 changes: 20 additions & 40 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,33 @@ package cmd

import (
"context"
"encoding/json"

"github.com/gobuffalo/buffalo-plugins/genny/install"
"github.com/gobuffalo/buffalo-plugins/plugins"
"github.com/gobuffalo/events"
"github.com/gobuffalo/genny"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var listenCmd = &cobra.Command{
Use: "listen",
Short: "listens to github.com/gobuffalo/events",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("must pass a payload")
}

e := events.Event{}
err := json.Unmarshal([]byte(args[0]), &e)
if err != nil {
return errors.WithStack(err)
}

if e.Kind != "buffalo:setup:started" {
return nil
}

run := genny.WetRunner(context.Background())

opts := &install.Options{}
err = run.WithNew(install.New(opts))
if err != nil {
return errors.WithStack(err)
}
payload := e.Payload
payload["plugins"] = opts.Plugins
events.EmitPayload(plugins.EvtSetupStarted, payload)
if err := run.Run(); err != nil {
events.EmitError(plugins.EvtSetupErr, err, payload)
return errors.WithStack(err)
}
events.EmitPayload(plugins.EvtSetupFinished, payload)
func Listen(e events.Event) error {
if e.Kind != "buffalo:setup:started" {
return nil
},
}

func init() {
rootCmd.AddCommand(listenCmd)
}

run := genny.WetRunner(context.Background())

opts := &install.Options{}
err := run.WithNew(install.New(opts))
if err != nil {
return errors.WithStack(err)
}
payload := e.Payload
payload["plugins"] = opts.Plugins
events.EmitPayload(plugins.EvtSetupStarted, payload)
if err := run.Run(); err != nil {
events.EmitError(plugins.EvtSetupErr, err, payload)
return errors.WithStack(err)
}
events.EmitPayload(plugins.EvtSetupFinished, payload)
return nil
}
3 changes: 2 additions & 1 deletion genny/plugin/a_plugin-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions genny/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func New(opts *Options) (*genny.Group, error) {
Version: "v0.0.1",
VersionFile: filepath.Join(opts.ShortName, "version.go"),
MainFile: "main.go",
Root: opts.Root,
})
if err != nil {
return gg, errors.WithStack(err)
Expand Down
7 changes: 4 additions & 3 deletions genny/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_Generator(t *testing.T) {
if !gomods.On() {
cmds = []string{"git init", "go get github.com/alecthomas/gometalinter", "gometalinter --install"}
} else {
cmds = []string{"git init", "go mod init", "go get github.com/alecthomas/gometalinter", "gometalinter --install", "go mod tidy"}
cmds = []string{"git init", "go mod init github.com/foo/buffalo-bar", "go get github.com/alecthomas/gometalinter", "gometalinter --install", "go mod tidy"}
}

r.Len(res.Commands, len(cmds))
Expand All @@ -49,6 +49,7 @@ func Test_Generator(t *testing.T) {
"LICENSE",
"Makefile",
"README.md",
"bar/listen.go",
"bar/version.go",
"cmd/available.go",
"cmd/bar.go",
Expand All @@ -66,12 +67,12 @@ func Test_Generator(t *testing.T) {
r.Equal("README.md", f.Name())
r.Contains(f.String(), opts.PluginPkg)

f = res.Files[11]
f = res.Files[12]
r.Equal("cmd/version.go", f.Name())
r.Contains(f.String(), opts.PluginPkg+"/"+opts.ShortName)
r.Contains(f.String(), opts.ShortName+".Version")

f = res.Files[12]
f = res.Files[13]
r.Equal("main.go", f.Name())
r.Contains(f.String(), "github.com/foo/buffalo-bar/cmd")

Expand Down
8 changes: 8 additions & 0 deletions genny/plugin/templates/-shortName-/listen.go.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package <%= opts.ShortName %>

import "github.com/gobuffalo/events"

func Listen(e events.Event) error {
// do work
return nil
}
25 changes: 10 additions & 15 deletions genny/plugin/templates/cmd/available.go.plush
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package cmd

import (
"encoding/json"
"os"

"github.com/gobuffalo/buffalo-plugins/plugins"
"github.com/gobuffalo/buffalo-plugins/plugins/plugcmds"
"github.com/spf13/cobra"
"<%= opts.PluginPkg %>"
)

// availableCmd represents the available command
var availableCmd = &cobra.Command{
Use: "available",
Short: "a list of available buffalo plugins",
RunE: func(cmd *cobra.Command, args []string) error {
plugs := plugins.Commands{
// {Name: "<%= opts.ShortName %>", UseCommand: "generate", BuffaloCommand: "generate", Description: generateCmd.Short, Aliases: generateCmd.Aliases},
}
return json.NewEncoder(os.Stdout).Encode(plugs)
},
var Available = plugcmds.NewAvailable()

var pluginsCmd = &cobra.Command{
Use: "plugins",
Short: "tools for working with buffalo plugins",
}

func init() {
rootCmd.AddCommand(availableCmd)
Available.Add("root", pluginsCmd)
Available.Listen(<%= opts.ShortName %>.Listen)
Available.Mount(rootCmd)
}
1 change: 0 additions & 1 deletion genny/plugin/with/a_with-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions genny/plugin/with/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package with

import (
"fmt"
"strings"

"github.com/gobuffalo/buffalo-plugins/genny/plugin"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/genny/genny/new"
Expand All @@ -27,6 +30,18 @@ func GenerateCmd(opts *plugin.Options) (*genny.Group, error) {

g.Transformer(genny.Replace("-shortName-", opts.ShortName))
g.Transformer(genny.Dot())

g.RunFn(func(r *genny.Runner) error {
f, err := r.FindFile("cmd/available.go")
if err != nil {
return errors.WithStack(err)
}
const g = `Available.Add("generate", generateCmd)`
const m = `Available.Mount(rootCmd)`
body := strings.Replace(f.String(), m, fmt.Sprintf("\t%s\n%s", g, m), 1)
return r.File(genny.NewFile(f.Name(), strings.NewReader(body)))
})

gg.Add(g)

g, err := new.New(&new.Options{
Expand Down
25 changes: 0 additions & 25 deletions genny/plugin/with/generate/templates/cmd/available.go.plush

This file was deleted.

30 changes: 27 additions & 3 deletions genny/plugin/with/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package with

import (
"context"
"strings"
"testing"

"github.com/gobuffalo/buffalo-plugins/genny/plugin"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/genny/gentest"
"github.com/gobuffalo/packr"
"github.com/stretchr/testify/require"
)

var gBox = packr.NewBox("../../plugin/templates")

func Test_GenerateCmd(t *testing.T) {
r := require.New(t)

Expand All @@ -19,7 +23,8 @@ func Test_GenerateCmd(t *testing.T) {
ShortName: "bar",
}

run := genny.DryRunner(context.Background())
run := gentest.NewRunner()
run.Disk.Add(genny.NewFile("cmd/available.go", strings.NewReader(availgo)))

gg, err := GenerateCmd(opts)
r.NoError(err)
Expand All @@ -33,7 +38,7 @@ func Test_GenerateCmd(t *testing.T) {

f := res.Files[0]
r.Equal("cmd/available.go", f.Name())
r.Contains(f.String(), `{Name: generateCmd.Use, BuffaloCommand: "generate", Description: generateCmd.Short, Aliases: generateCmd.Aliases}`)
r.Contains(f.String(), `Available.Add("generate", generateCmd)`)

f = res.Files[1]
r.Equal("cmd/generate.go", f.Name())
Expand All @@ -59,3 +64,22 @@ func Test_GenerateCmd(t *testing.T) {
r.Equal("genny/bar/templates/example.txt", f.Name())

}

const availgo = `package cmd
import (
"github.com/gobuffalo/buffalo-plugins/plugins/plugcmds"
"github.com/spf13/cobra"
)
var Available = plugcmds.NewAvailable()
var pluginsCmd = &cobra.Command{
Use: "plugins",
Short: "tools for working with buffalo plugins",
}
func init() {
Available.Add("root", pluginsCmd)
Available.Mount(rootCmd)
}`
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ require (
github.com/gobuffalo/buffalo v0.13.0
github.com/gobuffalo/envy v1.6.5
github.com/gobuffalo/events v1.0.7
github.com/gobuffalo/genny v0.0.0-20181012161047-33e5f43d83a6
github.com/gobuffalo/genny v0.0.0-20181017142616-0ee1b753300e
github.com/gobuffalo/licenser v0.0.0-20180924033006-eae28e638a42
github.com/gobuffalo/packr v1.13.7
github.com/gobuffalo/plush v3.7.20+incompatible
github.com/gobuffalo/release v1.0.40
github.com/gobuffalo/release v1.0.41
github.com/karrick/godirwalk v1.7.3
github.com/markbates/going v1.0.2
github.com/markbates/oncer v0.0.0-20181014194634-05fccaae8fc4
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/gobuffalo/genny v0.0.0-20181005145118-318a41a134cc/go.mod h1:WAd8HmjM
github.com/gobuffalo/genny v0.0.0-20181007153042-b8de7d566757/go.mod h1:+oG5Ljrw04czAHbPXREwaFojJbpUvcIy4DiOnbEJFTA=
github.com/gobuffalo/genny v0.0.0-20181012161047-33e5f43d83a6 h1:zwOcFHdMbna9U85wdM0NMT+r2c98EDKwjIjZVwSwdVU=
github.com/gobuffalo/genny v0.0.0-20181012161047-33e5f43d83a6/go.mod h1:+oG5Ljrw04czAHbPXREwaFojJbpUvcIy4DiOnbEJFTA=
github.com/gobuffalo/genny v0.0.0-20181017142616-0ee1b753300e h1:WjBJKSGXndtuzGTzcob3ipiEZPkk+EeJI1r5ko0Ykn4=
github.com/gobuffalo/genny v0.0.0-20181017142616-0ee1b753300e/go.mod h1:+oG5Ljrw04czAHbPXREwaFojJbpUvcIy4DiOnbEJFTA=
github.com/gobuffalo/github_flavored_markdown v1.0.4/go.mod h1:uRowCdK+q8d/RF0Kt3/DSalaIXbb0De/dmTqMQdkQ4I=
github.com/gobuffalo/github_flavored_markdown v1.0.5 h1:YvGVf7yj1akgsb+qc64Q0WX8uhpuZSibChbqOMRSAqE=
github.com/gobuffalo/github_flavored_markdown v1.0.5/go.mod h1:U0643QShPF+OF2tJvYNiYDLDGDuQmJZXsf/bHOJPsMY=
Expand Down Expand Up @@ -69,8 +71,8 @@ github.com/gobuffalo/pop v4.8.3+incompatible/go.mod h1:DwBz3SD5SsHpTZiTubcsFWcVD
github.com/gobuffalo/pop v4.8.4+incompatible/go.mod h1:DwBz3SD5SsHpTZiTubcsFWcVDpJWGsxjVjMPnkiThWg=
github.com/gobuffalo/release v1.0.35/go.mod h1:VtHFAKs61vO3wboCec5xr9JPTjYyWYcvaM3lclkc4x4=
github.com/gobuffalo/release v1.0.38/go.mod h1:VtHFAKs61vO3wboCec5xr9JPTjYyWYcvaM3lclkc4x4=
github.com/gobuffalo/release v1.0.40 h1:WdDtKwhVMDXBkwNGB/nFW0S6fUCox33gwPjKTfPRK48=
github.com/gobuffalo/release v1.0.40/go.mod h1:lJtNoEhho7WeFUFMmMG+tjWVYkud3sd/hPMychdz6YM=
github.com/gobuffalo/release v1.0.41 h1:6tPEFaV8GzBWbXR5poKt7Pptq61PviljDg6G/h/oi9k=
github.com/gobuffalo/release v1.0.41/go.mod h1:hYlf+56ZWkkoUtYRVGjNz8tKNkQey7H09LYBShqSjco=
github.com/gobuffalo/shoulders v1.0.1 h1:BqVJBUXlBWAf+WLhXijVk3SCpp75LXrVBiIkOCzZbNc=
github.com/gobuffalo/shoulders v1.0.1/go.mod h1:V33CcVmaQ4gRUmHKwq1fiTXuf8Gp/qjQBUL5tHPmvbA=
github.com/gobuffalo/tags v2.0.11+incompatible h1:zLkaontB8lWefU+DX38mzPLRKFGTJL8FKb9JnKMt0Z0=
Expand Down
Loading

0 comments on commit 8321a97

Please sign in to comment.