hybridgroup.gobot/gobot/generate.go

252 lines
5.3 KiB
Go
Raw Normal View History

2013-12-03 06:50:02 +08:00
package main
import (
"fmt"
"os"
2014-06-10 10:59:59 +08:00
"strings"
2013-12-03 06:50:02 +08:00
"text/template"
2014-07-13 00:49:18 +08:00
"github.com/codegangsta/cli"
2013-12-03 06:50:02 +08:00
)
2014-06-10 10:59:59 +08:00
type generate struct {
Name string
UpperName string
FirstLetter string
2013-12-03 06:50:02 +08:00
}
2014-06-10 10:59:59 +08:00
func Generate() cli.Command {
return cli.Command{
Name: "generate",
Usage: "Generate new Gobot skeleton project",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) == 0 || len(args) > 1 {
fmt.Println("Please provide a one word package name.")
return
}
pwd, _ := os.Getwd()
dir := fmt.Sprintf("%s/%s", pwd, args[0])
fmt.Println("Creating", dir)
err := os.MkdirAll(dir, 0700)
if err != nil {
fmt.Println(err)
err = nil
}
2014-07-13 00:49:18 +08:00
upperName := fmt.Sprintf("%s%s",
strings.ToUpper(string(args[0][0])),
string(args[0][1:]))
2014-06-10 10:59:59 +08:00
2014-07-13 00:49:18 +08:00
name := generate{
UpperName: upperName,
Name: string(args[0]),
FirstLetter: string(args[0][0]),
}
2014-06-10 10:59:59 +08:00
adaptor, _ := template.New("").Parse(adaptor())
2014-06-11 06:16:11 +08:00
fileLocation := fmt.Sprintf("%s/%s_adaptor.go", dir, args[0])
fmt.Println("Creating", fileLocation)
f, err := os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
adaptor.Execute(f, name)
f.Close()
driver, _ := template.New("").Parse(driver())
2014-06-11 06:16:11 +08:00
fileLocation = fmt.Sprintf("%s/%s_driver.go", dir, args[0])
fmt.Println("Creating", fileLocation)
f, err = os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
driver.Execute(f, name)
f.Close()
readme, _ := template.New("").Parse(readme())
2014-06-11 06:16:11 +08:00
fileLocation = fmt.Sprintf("%s/README.md", dir)
fmt.Println("Creating", fileLocation)
f, err = os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
readme.Execute(f, name)
f.Close()
2014-06-11 06:16:11 +08:00
fileLocation = fmt.Sprintf("%s/LICENSE", dir)
fmt.Println("Creating", fileLocation)
f, err = os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
f.Close()
driverTest, _ := template.New("").Parse(driverTest())
2014-06-11 06:16:11 +08:00
fileLocation = fmt.Sprintf("%s/%s_driver_test.go", dir, args[0])
fmt.Println("Creating", fileLocation)
f, err = os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
driverTest.Execute(f, name)
f.Close()
adaptorTest, _ := template.New("").Parse(adaptorTest())
2014-06-11 06:16:11 +08:00
fileLocation = fmt.Sprintf("%s/%s_adaptor_test.go", dir, args[0])
fmt.Println("Creating", fileLocation)
f, err = os.Create(fileLocation)
2014-06-10 10:59:59 +08:00
if err != nil {
fmt.Println(err)
err = nil
}
adaptorTest.Execute(f, name)
f.Close()
},
2014-02-25 12:18:12 +08:00
}
2013-12-03 06:50:02 +08:00
}
func adaptor() string {
2014-06-10 10:59:59 +08:00
return `package {{ .Name }}
2013-12-03 06:50:02 +08:00
import (
2014-06-10 10:59:59 +08:00
"github.com/hybridgroup/gobot"
2013-12-03 06:50:02 +08:00
)
2014-02-25 12:18:12 +08:00
type {{ .UpperName }}Adaptor struct {
2014-06-10 10:59:59 +08:00
gobot.Adaptor
2014-02-25 12:18:12 +08:00
}
2014-06-10 10:59:59 +08:00
func New{{.UpperName}}Adaptor(name string) *{{.UpperName}}Adaptor {
return &{{.UpperName}}Adaptor{
2014-07-13 00:49:18 +08:00
Adaptor: *gobot.NewAdaptor(
name,
"{{.Name}}.{{.UpperName}}Adaptor",
),
2014-06-10 10:59:59 +08:00
}
2014-02-25 12:18:12 +08:00
}
2014-06-10 10:59:59 +08:00
func ({{.FirstLetter}} *{{ .UpperName }}Adaptor) Connect() bool {
2014-02-25 12:18:12 +08:00
return true
2013-12-03 06:50:02 +08:00
}
2014-06-10 10:59:59 +08:00
func ({{.FirstLetter}} *{{ .UpperName }}Adaptor) Finalize() bool {
2014-02-25 12:18:12 +08:00
return true
2013-12-03 06:50:02 +08:00
}
`
}
func driver() string {
2014-06-10 10:59:59 +08:00
return `package {{ .Name }}
2013-12-03 06:50:02 +08:00
import (
2014-06-10 10:59:59 +08:00
"github.com/hybridgroup/gobot"
2013-12-03 06:50:02 +08:00
)
2014-02-25 12:18:12 +08:00
type {{ .UpperName }}Driver struct {
2014-06-10 10:59:59 +08:00
gobot.Driver
2013-12-03 06:50:02 +08:00
}
2014-04-02 10:20:38 +08:00
type {{ .UpperName }}Interface interface {
2014-02-25 12:18:12 +08:00
}
2014-06-10 10:59:59 +08:00
func New{{.UpperName}}Driver(a *{{.UpperName}}Adaptor, name string) *{{.UpperName}}Driver {
return &{{.UpperName}}Driver{
2014-07-13 00:49:18 +08:00
Driver: *gobot.NewDriver(
name,
2014-07-14 23:32:33 +08:00
"{{.Name}}.{{.UpperName}}Driver",
2014-07-13 00:49:18 +08:00
a,
),
2014-06-10 10:59:59 +08:00
}
2013-12-03 06:50:02 +08:00
}
2014-06-16 08:27:51 +08:00
func ({{.FirstLetter}} *{{ .UpperName }}Driver) adaptor() *{{ .UpperName }}Adaptor {
2014-07-13 00:49:18 +08:00
return {{ .FirstLetter }}.Driver.Adaptor().(*{{ .UpperName }}Adaptor)
2014-06-16 08:27:51 +08:00
}
2014-06-10 10:59:59 +08:00
func ({{.FirstLetter}} *{{ .UpperName }}Driver) Start() bool { return true }
func ({{.FirstLetter}} *{{ .UpperName }}Driver) Halt() bool { return true }
2014-04-02 10:20:38 +08:00
`
}
func driverTest() string {
2014-06-10 10:59:59 +08:00
return `package {{ .Name }}
2014-04-02 10:20:38 +08:00
import (
2014-06-14 06:09:04 +08:00
"github.com/hybridgroup/gobot"
"testing"
2014-04-02 10:20:38 +08:00
)
2014-06-14 06:09:04 +08:00
func initTest{{ .UpperName }}Driver() *{{ .UpperName }}Driver {
return New{{ .UpperName}}Driver(New{{ .UpperName }}Adaptor("myAdaptor"), "myDriver")
2014-04-02 10:20:38 +08:00
}
2014-06-14 06:09:04 +08:00
func Test{{ .UpperName }}DriverStart(t *testing.T) {
d := initTest{{.UpperName }}Driver()
gobot.Assert(t, d.Start(), true)
2014-06-14 06:09:04 +08:00
}
2014-04-02 10:20:38 +08:00
2014-06-14 06:09:04 +08:00
func Test{{ .UpperName }}DriverHalt(t *testing.T) {
d := initTest{{.UpperName }}Driver()
gobot.Assert(t, d.Halt(), true)
2014-06-14 06:09:04 +08:00
}
2014-04-02 10:20:38 +08:00
`
2013-12-03 06:50:02 +08:00
}
2014-06-14 06:09:04 +08:00
func adaptorTest() string {
2014-06-10 10:59:59 +08:00
return `package {{ .Name }}
2014-04-02 10:20:38 +08:00
import (
2014-06-14 06:09:04 +08:00
"github.com/hybridgroup/gobot"
2014-04-02 10:20:38 +08:00
"testing"
)
2014-06-14 06:09:04 +08:00
func initTest{{ .UpperName }}Adaptor() *{{ .UpperName }}Adaptor {
return New{{ .UpperName }}Adaptor("myAdaptor")
}
func Test{{ .UpperName }}AdaptorConnect(t *testing.T) {
a := initTest{{.UpperName }}Adaptor()
gobot.Assert(t, a.Connect(), true)
2014-06-14 06:09:04 +08:00
}
func Test{{ .UpperName }}AdaptorFinalize(t *testing.T) {
a := initTest{{.UpperName }}Adaptor()
gobot.Assert(t, a.Finalize(), true)
2013-12-03 06:50:02 +08:00
}
`
}
2014-02-25 12:18:12 +08:00
func readme() string {
2014-06-10 10:59:59 +08:00
return `# {{ .Name }}
2014-02-25 12:18:12 +08:00
2014-06-10 10:59:59 +08:00
Gobot (http://gobot.io/) is a framework for robotics and physical computing using Go
2014-02-25 12:18:12 +08:00
2014-06-10 10:59:59 +08:00
This repository contains the Gobot adaptor and driver for {{ .Name }}.
2014-02-25 12:18:12 +08:00
For more information about Gobot, check out the github repo at
https://github.com/hybridgroup/gobot
## Installing
2014-06-10 10:59:59 +08:00
go get path/to/repo/{{ .Name }}
2014-02-25 12:18:12 +08:00
## 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
`
}