Skip to content

Commit

Permalink
tools: Update cli generator to latest core
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 8, 2016
1 parent ae512a3 commit a0d22e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gobot/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CLI tool for generating new Gobot projects.
0.1
COMMANDS:
generate Generate new Gobot skeleton project
generate Generate new Gobot adaptors, drivers, and projects
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
Expand Down
70 changes: 36 additions & 34 deletions gobot/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Generate() cli.Command {
fmt.Println(err)
return
}
dir := pwd + "/gobot-" + cfg.Name
dir := pwd + "/" + cfg.Name
fmt.Println("Creating", dir)
if err := os.MkdirAll(dir, 0700); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -149,10 +149,10 @@ func generateProject(c config) error {

c.dir = dir

if ex, err := ioutil.ReadFile(exampleDir + "/main.go"); err != nil {
if exp, err := ioutil.ReadFile(exampleDir + "/main.go"); err != nil {
return err
} else {
c.Example = string(ex)
c.Example = string(exp)
}

return generate(c, "README.md", readme())
Expand All @@ -161,24 +161,20 @@ func generateProject(c config) error {
func adaptor() string {
return `package {{.Package}}
import (
"github.com/hybridgroup/gobot"
)
var _ gobot.Adaptor = (*{{.UpperName}}Adaptor)(nil)
type {{.UpperName}}Adaptor struct {
name string
}
func New{{.UpperName}}Adaptor(name string) *{{.UpperName}}Adaptor {
func New{{.UpperName}}Adaptor() *{{.UpperName}}Adaptor {
return &{{.UpperName}}Adaptor{
name: name,
name: "{{.UpperName}}",
}
}
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Name() string { return {{.FirstLetter}}.name }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) SetName(name string) { {{.FirstLetter}}.name = name }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Connect() []error { return nil }
func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Finalize() []error { return nil }
Expand All @@ -196,8 +192,6 @@ import (
"github.com/hybridgroup/gobot"
)
var _ gobot.Driver = (*{{.UpperName}}Driver)(nil)
const Hello string = "hello"
type {{.UpperName}}Driver struct {
Expand All @@ -209,9 +203,9 @@ type {{.UpperName}}Driver struct {
gobot.Commander
}
func New{{.UpperName}}Driver(a *{{.UpperName}}Adaptor, name string) *{{.UpperName}}Driver {
func New{{.UpperName}}Driver(a *{{.UpperName}}Adaptor) *{{.UpperName}}Driver {
{{.FirstLetter}} := &{{.UpperName}}Driver{
name: name,
name: "{{.UpperName}}",
connection: a,
interval: 500*time.Millisecond,
halt: make(chan bool, 0),
Expand All @@ -230,6 +224,8 @@ func New{{.UpperName}}Driver(a *{{.UpperName}}Adaptor, name string) *{{.UpperNam
func ({{.FirstLetter}} *{{.UpperName}}Driver) Name() string { return {{.FirstLetter}}.name }
func ({{.FirstLetter}} *{{.UpperName}}Driver) SetName(name string) { {{.FirstLetter}}.name = name }
func ({{.FirstLetter}} *{{.UpperName}}Driver) Connection() gobot.Connection {
return {{.FirstLetter}}.connection
}
Expand All @@ -249,7 +245,7 @@ func ({{.FirstLetter}} *{{.UpperName}}Driver) Ping() string {
func ({{.FirstLetter}} *{{.UpperName}}Driver) Start() []error {
go func() {
for {
gobot.Publish({{.FirstLetter}}.Event(Hello), {{.FirstLetter}}.Hello())
{{.FirstLetter}}.Publish({{.FirstLetter}}.Event(Hello), {{.FirstLetter}}.Hello())
select {
case <- time.After({{.FirstLetter}}.interval):
Expand Down Expand Up @@ -284,11 +280,11 @@ import (
func main() {
gbot := gobot.NewGobot()
conn := {{.Package}}.New{{.UpperName}}Adaptor("conn")
dev := {{.Package}}.New{{.UpperName}}Driver(conn, "dev")
conn := {{.Package}}.New{{.UpperName}}Adaptor()
dev := {{.Package}}.New{{.UpperName}}Driver(conn)
work := func() {
gobot.On(dev.Event({{.Package}}.Hello), func(data interface{}) {
dev.On(dev.Event({{.Package}}.Hello), func(data interface{}) {
fmt.Println(data)
})
Expand Down Expand Up @@ -318,26 +314,29 @@ import (
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Driver = (*{{.UpperName}}Driver)(nil)
func Test{{.UpperName}}Driver(t *testing.T) {
d := New{{.UpperName}}Driver(New{{.UpperName}}Adaptor("conn"), "dev")
d := New{{.UpperName}}Driver(New{{.UpperName}}Adaptor())
gobot.Assert(t, d.Name(), "dev")
gobot.Assert(t, d.Connection().Name(), "conn")
gobottest.Assert(t, d.Name(), "{{.UpperName}}")
gobottest.Assert(t, d.Connection().Name(), "{{.UpperName}}")
ret := d.Command(Hello)(nil)
gobot.Assert(t, ret.(string), "hello from dev!")
gobottest.Assert(t, ret.(string), "hello from {{.UpperName}}!")
gobot.Assert(t, d.Ping(), "pong")
gobottest.Assert(t, d.Ping(), "pong")
gobot.Assert(t, len(d.Start()), 0)
gobottest.Assert(t, len(d.Start()), 0)
<-time.After(d.interval)
sem := make(chan bool, 0)
gobot.On(d.Event(Hello), func(data interface{}) {
d.On(d.Event(Hello), func(data interface{}) {
sem <- true
})
Expand All @@ -347,9 +346,9 @@ func Test{{.UpperName}}Driver(t *testing.T) {
t.Errorf("Hello Event was not published")
}
gobot.Assert(t, len(d.Halt()), 0)
gobottest.Assert(t, len(d.Halt()), 0)
gobot.On(d.Event(Hello), func(data interface{}) {
d.On(d.Event(Hello), func(data interface{}) {
sem <- true
})
Expand All @@ -370,20 +369,23 @@ import (
"testing"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Adaptor = (*{{.UpperName}}Adaptor)(nil)
func Test{{.UpperName}}Adaptor(t *testing.T) {
a := New{{.UpperName}}Adaptor("tester")
a := New{{.UpperName}}Adaptor()
gobot.Assert(t, a.Name(), "tester")
gobottest.Assert(t, a.Name(), "{{.UpperName}}")
gobot.Assert(t, len(a.Connect()), 0)
gobottest.Assert(t, len(a.Connect()), 0)
gobot.Assert(t, a.Ping(), "pong")
gobottest.Assert(t, a.Ping(), "pong")
gobot.Assert(t, len(a.Connect()), 0)
gobottest.Assert(t, len(a.Connect()), 0)
gobot.Assert(t, len(a.Finalize()), 0)
gobottest.Assert(t, len(a.Finalize()), 0)
}
`
}
Expand Down

0 comments on commit a0d22e9

Please sign in to comment.