refactoring
Этот коммит содержится в:
родитель
2f32a193cb
коммит
c8eb4e021b
7 изменённых файлов: 71 добавлений и 161 удалений
23
Makefile
23
Makefile
|
@ -1,27 +1,6 @@
|
|||
APPNAME=$(shell basename `pwd`)
|
||||
LDFLAGS="-s"
|
||||
TS=$(shell date -u '+%Y/%m/%d %H:%M:%S')
|
||||
|
||||
all: clean test build install
|
||||
|
||||
build/$(APPNAME):
|
||||
@echo $(TS) Building $(APPAME) ...
|
||||
@go build -ldflags $(LDFLAGS) -o build/$(APPNAME) main.go
|
||||
@echo $(TS) Done.
|
||||
|
||||
build: build/$(APPNAME)
|
||||
|
||||
clean:
|
||||
@echo $(TS) Cleaning up previous build ...
|
||||
@rm -f build/*
|
||||
@echo $(TS) Done.
|
||||
|
||||
install:
|
||||
@echo $(TS) Installing $(APPNAME) ...
|
||||
@cp build/$(APPNAME) $(GOPATH)/bin/
|
||||
@mkdir -p $(HOME)/esp32/
|
||||
@cp mapping.json $(HOME)/esp32/mapping.json
|
||||
@echo $(TS) Done.
|
||||
all: test
|
||||
|
||||
packages:
|
||||
@echo $(TS) Installing Go packages ...
|
||||
|
|
|
@ -3,5 +3,4 @@ package worker
|
|||
// Mapping specifies the api logic to apply transformation to a specific identifier.
|
||||
type Mapping interface {
|
||||
Apply(ident string) string
|
||||
Read() error
|
||||
}
|
||||
|
|
|
@ -1,41 +1,73 @@
|
|||
package worker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/andygeiss/esp32-transpiler/api/worker"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var (
|
||||
rules = map[string]string{
|
||||
"digital.Low": "LOW",
|
||||
"digital.High": "HIGH",
|
||||
"digital.ModeInput": "INPUT",
|
||||
"digital.ModeOutput": "OUTPUT",
|
||||
"digital.PinMode": "pinMode",
|
||||
"digital.Write": "digitalWrite",
|
||||
"random.Num": "random",
|
||||
"random.NumBetween": "random",
|
||||
"random.Seed": "randomSeed",
|
||||
"serial.Available": "Serial.available",
|
||||
"serial.BaudRate300": "300",
|
||||
"serial.BaudRate600": "600",
|
||||
"serial.BaudRate1200": "1200",
|
||||
"serial.BaudRate2400": "2400",
|
||||
"serial.BaudRate4800": "4800",
|
||||
"serial.BaudRate9600": "9600",
|
||||
"serial.BaudRate14400": "14400",
|
||||
"serial.BaudRate28800": "28800",
|
||||
"serial.BaudRate38400": "38400",
|
||||
"serial.BaudRate57600": "57600",
|
||||
"serial.BaudRate115200": "115200",
|
||||
"serial.Begin": "Serial.begin",
|
||||
"serial.Print": "Serial.print",
|
||||
"serial.Println": "Serial.println",
|
||||
"timer.Delay": "delay",
|
||||
"wifi": "WiFi",
|
||||
"wifi.Begin": "WiFi.begin",
|
||||
"wifi.BeginEncrypted": "WiFi.begin",
|
||||
"wifi.BSSID": "WiFi.BSSID",
|
||||
"wifi.Disconnect": "WiFi.disconnect",
|
||||
"wifi.EncryptionType": "WiFi.encryptionType",
|
||||
"wifi.EncryptionTypeAuto": "8",
|
||||
"wifi.EncryptionTypeCCMP": "4",
|
||||
"wifi.EncryptionTypeNone": "7",
|
||||
"wifi.EncryptionTypeTKIP": "2",
|
||||
"wifi.EncryptionTypeWEP": "5",
|
||||
"wifi.LocalIP": "WiFi.localIP",
|
||||
"wifi.RSSI": "WiFi.RSSI",
|
||||
"wifi.ScanNetworks": "WiFi.scanNetworks",
|
||||
"wifi.SetDNS": "WiFi.setDNS",
|
||||
"wifi.SSID": "WiFi.SSID",
|
||||
"wifi.StatusConnected": "WL_CONNECTED",
|
||||
"wifi.StatusIdle": "WL_IDLE",
|
||||
"Loop": "void loop",
|
||||
"Setup": "void setup",
|
||||
}
|
||||
)
|
||||
|
||||
// Mapping specifies the api logic to apply transformation to a specific Golang identifier by reading simple JSON map.
|
||||
type Mapping struct {
|
||||
Filename string `json:"filename"`
|
||||
Rules map[string]string `json:"rules"`
|
||||
}
|
||||
type Mapping struct {}
|
||||
|
||||
// NewMapping creates a new mapping and returns its address.
|
||||
func NewMapping(filename string) worker.Mapping {
|
||||
rules := make(map[string]string, 0)
|
||||
return &Mapping{filename, rules}
|
||||
func NewMapping() worker.Mapping {
|
||||
return &Mapping{}
|
||||
}
|
||||
|
||||
// Apply checks the Golang identifier and transforms it to a specific representation.
|
||||
func (m *Mapping) Apply(ident string) string {
|
||||
for wanted := range m.Rules {
|
||||
func (*Mapping) Apply(ident string) string {
|
||||
for wanted := range rules {
|
||||
if ident == wanted {
|
||||
ident = m.Rules[ident]
|
||||
ident = rules[ident]
|
||||
}
|
||||
}
|
||||
return ident
|
||||
}
|
||||
|
||||
// Read gets the mapping rules from the local filesystem.
|
||||
func (m *Mapping) Read() error {
|
||||
bytes, err := ioutil.ReadFile(m.Filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var rules map[string]string
|
||||
json.Unmarshal(bytes, &rules)
|
||||
m.Rules = rules
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
{
|
||||
"digital.Low": "LOW",
|
||||
"digital.High": "HIGH",
|
||||
"digital.ModeInput": "INPUT",
|
||||
"digital.ModeOutput": "OUTPUT",
|
||||
"digital.PinMode": "pinMode",
|
||||
"digital.Write": "digitalWrite",
|
||||
"random.Num": "random",
|
||||
"random.NumBetween": "random",
|
||||
"random.Seed": "randomSeed",
|
||||
"serial.Available": "Serial.available",
|
||||
"serial.BaudRate300": "300",
|
||||
"serial.BaudRate600": "600",
|
||||
"serial.BaudRate1200": "1200",
|
||||
"serial.BaudRate2400": "2400",
|
||||
"serial.BaudRate4800": "4800",
|
||||
"serial.BaudRate9600": "9600",
|
||||
"serial.BaudRate14400": "14400",
|
||||
"serial.BaudRate28800": "28800",
|
||||
"serial.BaudRate38400": "38400",
|
||||
"serial.BaudRate57600": "57600",
|
||||
"serial.BaudRate115200": "115200",
|
||||
"serial.Begin": "Serial.begin",
|
||||
"serial.Print": "Serial.print",
|
||||
"serial.Println": "Serial.println",
|
||||
"timer.Delay": "delay",
|
||||
"wifi": "WiFi",
|
||||
"wifi.Begin": "WiFi.begin",
|
||||
"wifi.BeginEncrypted": "WiFi.begin",
|
||||
"wifi.BSSID": "WiFi.BSSID",
|
||||
"wifi.Disconnect": "WiFi.disconnect",
|
||||
"wifi.EncryptionType": "WiFi.encryptionType",
|
||||
"wifi.EncryptionTypeAuto": "8",
|
||||
"wifi.EncryptionTypeCCMP": "4",
|
||||
"wifi.EncryptionTypeNone": "7",
|
||||
"wifi.EncryptionTypeTKIP": "2",
|
||||
"wifi.EncryptionTypeWEP": "5",
|
||||
"wifi.LocalIP": "WiFi.localIP",
|
||||
"wifi.RSSI": "WiFi.RSSI",
|
||||
"wifi.ScanNetworks": "WiFi.scanNetworks",
|
||||
"wifi.SetDNS": "WiFi.setDNS",
|
||||
"wifi.SSID": "WiFi.SSID",
|
||||
"wifi.StatusConnected": "WL_CONNECTED",
|
||||
"wifi.StatusIdle": "WL_IDLE",
|
||||
"Loop": "void loop",
|
||||
"Setup": "void setup"
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
package worker
|
||||
package worker_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
. "github.com/andygeiss/assert"
|
||||
"github.com/andygeiss/esp32-transpiler/impl/worker"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -20,11 +21,9 @@ func Trim(s string) string {
|
|||
// The Worker will be started and used to transform the source into an Arduino sketch format.
|
||||
func Validate(source, expected string, t *testing.T) {
|
||||
var in, out bytes.Buffer
|
||||
mapping := NewMapping("mapping.json")
|
||||
Assert(t, mapping.Read(), IsNil())
|
||||
in.WriteString(source)
|
||||
worker := NewWorker(&in, &out, mapping)
|
||||
Assert(t, worker.Start(), IsNil())
|
||||
wrk := worker.NewWorker(&in, &out, worker.NewMapping())
|
||||
Assert(t, wrk.Start(), IsNil())
|
||||
code := out.String()
|
||||
tcode, texpected := Trim(code), Trim(expected)
|
||||
Assert(t, tcode, IsEqual(texpected))
|
||||
|
|
25
main.go
25
main.go
|
@ -10,24 +10,23 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
mapping, source, target := getFlags()
|
||||
checkFlagsAreValid(mapping, source, target)
|
||||
safeTranspile(mapping, source, target)
|
||||
source, target := getFlags()
|
||||
checkFlagsAreValid(source, target)
|
||||
safeTranspile(source, target)
|
||||
}
|
||||
|
||||
func checkFlagsAreValid(mapping, source, target string) {
|
||||
if mapping == "" || source == "" || target == "" {
|
||||
func checkFlagsAreValid(source, target string) {
|
||||
if source == "" || target == "" {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func getFlags() (string, string, string) {
|
||||
mapping := flag.String("mapping", fmt.Sprintf("%s/esp32/mapping.json", os.Getenv("HOME")), "API Mapping file")
|
||||
func getFlags() (string, string) {
|
||||
source := flag.String("source", "", "Golang source file")
|
||||
target := flag.String("target", "", "Arduino sketch file")
|
||||
flag.Parse()
|
||||
return *mapping, *source, *target
|
||||
return *source, *target
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
|
@ -39,7 +38,7 @@ func printUsage() {
|
|||
fmt.Printf("\tesp32 -source impl/blink/controller.go -target impl/blink/controller.worker\n\n")
|
||||
}
|
||||
|
||||
func safeTranspile(mapping, source, target string) {
|
||||
func safeTranspile(source, target string) {
|
||||
// Read the Golang source file.
|
||||
in, err := os.Open(source)
|
||||
if err != nil {
|
||||
|
@ -53,12 +52,8 @@ func safeTranspile(mapping, source, target string) {
|
|||
log.Fatal("Arduino sketch file [%s] could not be opened! %v", target, err)
|
||||
}
|
||||
// Transpiles the Golang source into Arduino sketch.
|
||||
m := worker.NewMapping(mapping)
|
||||
if err := m.Read(); err != nil {
|
||||
log.Fatal("%v", err)
|
||||
}
|
||||
worker := worker.NewWorker(in, out, m)
|
||||
trans := transpile.NewTranspiler(worker)
|
||||
wrk := worker.NewWorker(in, out, worker.NewMapping())
|
||||
trans := transpile.NewTranspiler(wrk)
|
||||
if err := trans.Transpile(); err != nil {
|
||||
log.Fatal("%v", err)
|
||||
}
|
||||
|
|
47
mapping.json
47
mapping.json
|
@ -1,47 +0,0 @@
|
|||
{
|
||||
"digital.Low": "LOW",
|
||||
"digital.High": "HIGH",
|
||||
"digital.ModeInput": "INPUT",
|
||||
"digital.ModeOutput": "OUTPUT",
|
||||
"digital.PinMode": "pinMode",
|
||||
"digital.Write": "digitalWrite",
|
||||
"random.Num": "random",
|
||||
"random.NumBetween": "random",
|
||||
"random.Seed": "randomSeed",
|
||||
"serial.Available": "Serial.available",
|
||||
"serial.BaudRate300": "300",
|
||||
"serial.BaudRate600": "600",
|
||||
"serial.BaudRate1200": "1200",
|
||||
"serial.BaudRate2400": "2400",
|
||||
"serial.BaudRate4800": "4800",
|
||||
"serial.BaudRate9600": "9600",
|
||||
"serial.BaudRate14400": "14400",
|
||||
"serial.BaudRate28800": "28800",
|
||||
"serial.BaudRate38400": "38400",
|
||||
"serial.BaudRate57600": "57600",
|
||||
"serial.BaudRate115200": "115200",
|
||||
"serial.Begin": "Serial.begin",
|
||||
"serial.Print": "Serial.print",
|
||||
"serial.Println": "Serial.println",
|
||||
"timer.Delay": "delay",
|
||||
"wifi": "WiFi",
|
||||
"wifi.Begin": "WiFi.begin",
|
||||
"wifi.BeginEncrypted": "WiFi.begin",
|
||||
"wifi.BSSID": "WiFi.BSSID",
|
||||
"wifi.Disconnect": "WiFi.disconnect",
|
||||
"wifi.EncryptionType": "WiFi.encryptionType",
|
||||
"wifi.EncryptionTypeAuto": "8",
|
||||
"wifi.EncryptionTypeCCMP": "4",
|
||||
"wifi.EncryptionTypeNone": "7",
|
||||
"wifi.EncryptionTypeTKIP": "2",
|
||||
"wifi.EncryptionTypeWEP": "5",
|
||||
"wifi.LocalIP": "WiFi.localIP",
|
||||
"wifi.RSSI": "WiFi.RSSI",
|
||||
"wifi.ScanNetworks": "WiFi.scanNetworks",
|
||||
"wifi.SetDNS": "WiFi.setDNS",
|
||||
"wifi.SSID": "WiFi.SSID",
|
||||
"wifi.StatusConnected": "WL_CONNECTED",
|
||||
"wifi.StatusIdle": "WL_IDLE",
|
||||
"Loop": "void loop",
|
||||
"Setup": "void setup"
|
||||
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче