1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/gobot/generate.go

295 lines
6.0 KiB
Go
Raw Normal View History

2013-12-02 14:50:02 -08:00
package main
import (
"fmt"
2014-02-24 20:18:12 -08:00
"github.com/gonuts/commander"
"github.com/gonuts/flag"
2013-12-02 14:50:02 -08:00
"os"
"text/template"
"unicode"
)
func generate() *commander.Command {
cmd := &commander.Command{
Run: doGenerate,
UsageLine: "generate [options]",
2014-02-24 20:18:12 -08:00
Short: "Generates a Gobot library skeleton",
2013-12-02 14:50:02 -08:00
Long: `
2014-02-24 20:18:12 -08:00
Generates a Gobot library skeleton.
2013-12-02 14:50:02 -08:00
ex:
$ gobot generate myProject
`,
Flag: *flag.NewFlagSet("gobot-generate", flag.ExitOnError),
}
return cmd
}
type Generate struct {
2014-02-24 20:18:12 -08:00
Name string
UpperName string
2013-12-02 14:50:02 -08:00
}
func doGenerate(cmd *commander.Command, args []string) error {
2013-12-02 14:50:02 -08:00
if len(args) == 0 {
fmt.Println(cmd.Long)
return nil
2013-12-02 14:50:02 -08:00
}
pwd, _ := os.Getwd()
dir := fmt.Sprintf("%s/gobot-%s", pwd, args[0])
fmt.Println("Creating", dir)
err := os.MkdirAll(dir, 0700)
if err != nil {
fmt.Println(err)
err = nil
}
a := []rune(args[0])
a[0] = unicode.ToUpper(a[0])
s := string(a)
2014-02-24 20:18:12 -08:00
name := Generate{UpperName: s, Name: string(args[0])}
2013-12-02 14:50:02 -08:00
adaptor, _ := template.New("").Parse(adaptor())
file_location := fmt.Sprintf("%s/%s_adaptor.go", dir, args[0])
fmt.Println("Creating", file_location)
f, err := os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
adaptor.Execute(f, name)
f.Close()
2014-02-24 20:18:12 -08:00
driver, _ := template.New("").Parse(driver())
2013-12-02 14:50:02 -08:00
file_location = fmt.Sprintf("%s/%s_driver.go", dir, args[0])
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
driver.Execute(f, name)
2014-02-24 20:18:12 -08:00
f.Close()
readme, _ := template.New("").Parse(readme())
file_location = fmt.Sprintf("%s/README.md", dir)
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
readme.Execute(f, name)
f.Close()
file_location = fmt.Sprintf("%s/LICENSE", dir)
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
f.Close()
2014-04-01 19:20:38 -07:00
driverTest, _ := template.New("").Parse(driverTest())
file_location = fmt.Sprintf("%s/%s_driver_test.go", dir, args[0])
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
driverTest.Execute(f, name)
f.Close()
adaptorTest, _ := template.New("").Parse(adaptorTest())
file_location = fmt.Sprintf("%s/%s_adaptor_test.go", dir, args[0])
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
adaptorTest.Execute(f, name)
f.Close()
testSuite, _ := template.New("").Parse(testSuite())
file_location = fmt.Sprintf("%s/gobot-%s_suite_test.go", dir, args[0])
fmt.Println("Creating", file_location)
f, err = os.Create(file_location)
if err != nil {
fmt.Println(err)
err = nil
}
testSuite.Execute(f, name)
f.Close()
2014-02-24 20:18:12 -08:00
return nil
2013-12-02 14:50:02 -08:00
}
func adaptor() string {
2014-02-24 20:18:12 -08:00
return `package gobot{{ .UpperName }}
2013-12-02 14:50:02 -08:00
import (
"github.com/hybridgroup/gobot"
)
2014-02-24 20:18:12 -08:00
type {{ .UpperName }}Adaptor struct {
2013-12-02 14:50:02 -08:00
gobot.Adaptor
}
2014-02-24 20:18:12 -08:00
func (me *{{ .UpperName }}Adaptor) Connect() bool {
return true
}
func (me *{{ .UpperName }}Adaptor) Reconnect() bool {
return true
}
2014-03-06 22:45:18 -08:00
func (me *{{ .UpperName }}Adaptor) Disconnect() bool {
2014-02-24 20:18:12 -08:00
return true
2013-12-02 14:50:02 -08:00
}
2014-03-06 22:45:18 -08:00
func (me *{{ .UpperName }}Adaptor) Finalize() bool {
2014-02-24 20:18:12 -08:00
return true
2013-12-02 14:50:02 -08:00
}
`
}
func driver() string {
2014-02-24 20:18:12 -08:00
return `package gobot{{ .UpperName }}
2013-12-02 14:50:02 -08:00
import (
"github.com/hybridgroup/gobot"
)
2014-02-24 20:18:12 -08:00
type {{ .UpperName }}Driver struct {
2013-12-02 14:50:02 -08:00
gobot.Driver
2014-02-24 20:18:12 -08:00
{{ .UpperName }}Adaptor *{{ .UpperName }}Adaptor
2013-12-02 14:50:02 -08:00
}
2014-04-01 19:20:38 -07:00
type {{ .UpperName }}Interface interface {
2014-02-24 20:18:12 -08:00
}
func New{{ .UpperName }}(adaptor *{{ .UpperName }}Adaptor) *{{ .UpperName }}Driver {
d := new({{ .UpperName }}Driver)
2013-12-02 14:50:02 -08:00
d.Events = make(map[string]chan interface{})
2014-02-24 20:18:12 -08:00
d.{{ .UpperName }}Adaptor = adaptor
2013-12-02 14:50:02 -08:00
d.Commands = []string{}
return d
}
2014-04-01 19:20:38 -07:00
func (me *{{ .UpperName }}Driver) Init() bool { return true }
func (me *{{ .UpperName }}Driver) Start() bool { return true }
func (me *{{ .UpperName }}Driver) Halt() bool { return true }
`
}
func driverTest() string {
return `package gobot{{ .UpperName }}
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("{{ .UpperName }}Driver", func() {
var (
driver *{{ .UpperName }}Driver
)
BeforeEach(func() {
driver = New{{ .UpperName }}(new({{ .UpperName }}Adaptor))
})
It("Must be able to Start", func() {
Expect(driver.Start()).To(Equal(true))
})
It("Must be able to Init", func() {
Expect(driver.Init()).To(Equal(true))
})
It("Must be able to Halt", func() {
Expect(driver.Halt()).To(Equal(true))
})
})
`
}
func adaptorTest() string {
return `package gobot{{ .UpperName }}
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("{{ .UpperName }}Adaptor", func() {
var (
adaptor *{{ .UpperName }}Adaptor
)
BeforeEach(func() {
adaptor = new({{ .UpperName }}Adaptor)
})
It("Must be able to Finalize", func() {
Expect(adaptor.Finalize()).To(Equal(true))
})
It("Must be able to Connect", func() {
Expect(adaptor.Connect()).To(Equal(true))
})
It("Must be able to Disconnect", func() {
Expect(adaptor.Disconnect()).To(Equal(true))
})
It("Must be able to Reconnect", func() {
Expect(adaptor.Reconnect()).To(Equal(true))
})
})
`
2013-12-02 14:50:02 -08:00
}
2014-04-01 19:20:38 -07:00
func testSuite() string {
return `package gobot{{ .UpperName }}
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestGobot{{ .UpperName }}(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gobot-{{ .UpperName }} Suite")
2013-12-02 14:50:02 -08:00
}
`
}
2014-02-24 20:18:12 -08:00
func readme() string {
return `# Gobot for {{ .Name }}
Gobot (http://gobot.io/) is a library for robotics and physical computing using Go
This repository contains the Gobot adaptor for {{ .Name }}.
For more information about Gobot, check out the github repo at
https://github.com/hybridgroup/gobot
## Installing
go get path/to/repo/gobot-{{ .Name }}
## Using
your example code here...
## Connecting
Explain how to connect from the computer to the device here...
## License
Copyright (c) 2014 Your Name Here. See LICENSE for more details
`
}