Skip to content

Commit

Permalink
Merge pull request hybridgroup#6 from hybridgroup/generator
Browse files Browse the repository at this point in the history
adaptor and driver generator
  • Loading branch information
deadprogram committed Dec 2, 2013
2 parents f2c21ed + 901574b commit 2c9247d
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
124 changes: 124 additions & 0 deletions gobot/generate.go
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions gobot/main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2c9247d

Please sign in to comment.