1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-09 19:29:27 +08:00

Merge pull request #6 from hybridgroup/generator

adaptor and driver generator
This commit is contained in:
Ron Evans 2013-12-02 14:59:15 -08:00
commit 2c9247dcc7
2 changed files with 161 additions and 0 deletions

124
gobot/generate.go Normal file
View File

@ -0,0 +1,124 @@
package main
import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"os"
"text/template"
"unicode"
)
func generate() *commander.Command {
cmd := &commander.Command{
Run: doGenerate,
UsageLine: "generate [options]",
Short: "Generates a gobot library skeleton",
Long: `
Generates a gobot library skeleton.
ex:
$ gobot generate myProject
`,
Flag: *flag.NewFlagSet("gobot-generate", flag.ExitOnError),
}
return cmd
}
type Generate struct {
Name string
}
func doGenerate(cmd *commander.Command, args []string) {
if len(args) == 0 {
fmt.Println(cmd.Long)
return
}
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)
name := Generate{Name: s}
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()
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, _ := template.New("").Parse(driver())
driver.Execute(f, name)
f.Close()
}
func adaptor() string {
return `package gobot{{ .Name }}
import (
"github.com/hybridgroup/gobot"
)
type {{ .Name }}Adaptor struct {
gobot.Adaptor
}
func (me *{{ .Name }}Adaptor) Connect() {
}
func (me *{{ .Name }}Adaptor) Disconnect() {
}
`
}
func driver() string {
return `package gobot{{ .Name }}
import (
"fmt"
"github.com/hybridgroup/gobot"
)
type {{ .Name }}Driver struct {
gobot.Driver
{{ .Name }}Adaptor *{{ .Name }}Adaptor
}
func New{{ .Name }}(adaptor *{{ .Name }}Adaptor) *{{ .Name }}Driver {
d := new({{ .Name }}Driver)
d.Events = make(map[string]chan interface{})
d.{{ .Name }}Adaptor = adaptor
d.Commands = []string{}
return d
}
func (me *{{ .Name }}Driver) StartDriver() {
gobot.Every(sd.Interval, func() {
me.handleMessageEvents()
})
}
func (me *{{ .Name }}Driver) handleMessageEvents() {
}
`
}

37
gobot/main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"os"
)
var g_cmd *commander.Commander
func init() {
g_cmd = &commander.Commander{
Name: os.Args[0],
Commands: []*commander.Command{
generate(),
},
Flag: flag.NewFlagSet("gobot", flag.ExitOnError),
}
}
func main() {
err := g_cmd.Flag.Parse(os.Args[1:])
if err != nil {
fmt.Printf("**err**: %v\n", err)
os.Exit(1)
}
args := g_cmd.Flag.Args()
err = g_cmd.Run(args)
if err != nil {
fmt.Printf("**err**: %v\n", err)
os.Exit(1)
}
return
}