Skip to content

Commit

Permalink
Update generator
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Feb 25, 2014
1 parent 375d4ab commit acd4aa1
Showing 1 changed file with 84 additions and 21 deletions.
105 changes: 84 additions & 21 deletions gobot/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package main

import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"os"
"text/template"
"unicode"

"github.com/gonuts/commander"
"github.com/gonuts/flag"
)

func generate() *commander.Command {
cmd := &commander.Command{
Run: doGenerate,
UsageLine: "generate [options]",
Short: "Generates a gobot library skeleton",
Short: "Generates a Gobot library skeleton",
Long: `
Generates a gobot library skeleton.
Generates a Gobot library skeleton.
ex:
$ gobot generate myProject
Expand All @@ -27,7 +26,8 @@ ex:
}

type Generate struct {
Name string
Name string
UpperName string
}

func doGenerate(cmd *commander.Command, args []string) error {
Expand All @@ -48,7 +48,7 @@ func doGenerate(cmd *commander.Command, args []string) error {
a[0] = unicode.ToUpper(a[0])
s := string(a)

name := Generate{Name: s}
name := Generate{UpperName: s, Name: string(args[0])}

adaptor, _ := template.New("").Parse(adaptor())
file_location := fmt.Sprintf("%s/%s_adaptor.go", dir, args[0])
Expand All @@ -61,64 +61,127 @@ func doGenerate(cmd *commander.Command, args []string) error {
adaptor.Execute(f, name)
f.Close()

driver, _ := template.New("").Parse(driver())
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)
return f.Close()
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()
return nil
}

func adaptor() string {
return `package gobot{{ .Name }}
return `package gobot{{ .UpperName }}
import (
"github.com/hybridgroup/gobot"
)
type {{ .Name }}Adaptor struct {
type {{ .UpperName }}Adaptor struct {
gobot.Adaptor
}
func (me *{{ .Name }}Adaptor) Connect() {
func (me *{{ .UpperName }}Adaptor) Connect() bool {
return true
}
func (me *{{ .UpperName }}Adaptor) Reconnect() bool {
return true
}
func (me *{{ .Name }}Adaptor) Disconnect() bool {
return true
}
func (me *{{ .Name }}Adaptor) Disconnect() {
func (me *{{ .Name }}Adaptor) Finalize() bool {
return true
}
`
}

func driver() string {
return `package gobot{{ .Name }}
return `package gobot{{ .UpperName }}
import (
"github.com/hybridgroup/gobot"
)
type {{ .Name }}Driver struct {
type {{ .UpperName }}Driver struct {
gobot.Driver
{{ .Name }}Adaptor *{{ .Name }}Adaptor
{{ .UpperName }}Adaptor *{{ .UpperName }}Adaptor
}
func New{{ .Name }}(adaptor *{{ .Name }}Adaptor) *{{ .Name }}Driver {
d := new({{ .Name }}Driver)
type {{ .UpperName}}Interface interface {
}
func New{{ .UpperName }}(adaptor *{{ .UpperName }}Adaptor) *{{ .UpperName }}Driver {
d := new({{ .UpperName }}Driver)
d.Events = make(map[string]chan interface{})
d.{{ .Name }}Adaptor = adaptor
d.{{ .UpperName }}Adaptor = adaptor
d.Commands = []string{}
return d
}
func (me *{{ .Name }}Driver) StartDriver() {
func (me *{{ .UpperName }}Driver) Start() bool {
gobot.Every(sd.Interval, func() {
me.handleMessageEvents()
})
return true
}
func (me *{{ .Name }}Driver) handleMessageEvents() {
func (me *{{ .UpperName }}Driver) handleMessageEvents() {
}
`
}

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
`
}

0 comments on commit acd4aa1

Please sign in to comment.