matrix-dsl

A simple DSL for fast generation of Matrix Bots or Application Services
git clone git://archive.git.mtrnord.blog/Nordgedanken/matrix-dsl.git
Log | Files | Refs | LICENSE

commit 4003eb04af87ca193faeff9b5b70f6bf76568894
parent 9ca46fd15c29d46cc78bd5aee148066fa3176a00
Author: MTRNord <mtrnord1@gmail.com>
Date:   Mon, 23 Apr 2018 21:21:45 +0200

Generate the very basics of the js project including a package.json and installing the required dependency

Diffstat:
M.gitignore | 3+++
Mcmd/generate.go | 9+++++++++
Acmd/generator/js/js.go | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acmd/generator/js/templates/templates.go | 11+++++++++++
Acmd/generator/js/types.go | 9+++++++++
Mcmd/lexer/ast.go | 1+
Mexample.mx | 6++++--
7 files changed, 129 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -64,3 +64,5 @@ fabric.properties # Output of the go coverage tool, specifically when used with LiteIDE *.out + +js_project/ +\ No newline at end of file diff --git a/cmd/generate.go b/cmd/generate.go @@ -17,6 +17,7 @@ package cmd import ( "errors" "fmt" + "github.com/Nordgedanken/matrix_dsl/cmd/generator/js" "github.com/Nordgedanken/matrix_dsl/cmd/lexer" "github.com/alecthomas/participle" "github.com/spf13/cobra" @@ -57,6 +58,14 @@ to quickly create a Cobra application.`, } printMX(mx) + + for _, v := range mx.Sections { + if v.Identifier == "BOT" { + js.GenerateBot(mx.Sections[0]) + } else { + return errors.New("unknown Section type") + } + } return nil }, } diff --git a/cmd/generator/js/js.go b/cmd/generator/js/js.go @@ -0,0 +1,92 @@ +package js + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "github.com/Nordgedanken/matrix_dsl/cmd/lexer" + "io/ioutil" + "os" + "os/exec" +) + +func GenerateBot(mx *lexer.Section) error { + + err := os.MkdirAll("js_project", os.ModeDir) + if err != nil { + return err + } + + packageJson := &pkgJson{} + packageJson.Name, err = getName(mx.Properties) + if err != nil { + return err + } + + packageJson.Description, err = getDesc(mx.Properties) + if err != nil { + return err + } + + packageJson.Main = "index.js" + + packageJsonM, err := json.Marshal(packageJson) + if err != nil { + return err + } + + err = ioutil.WriteFile("./js_project/package.json", packageJsonM, 0644) + if err != nil { + return err + } + + cmd := exec.Command("npm", "install", "--save", "matrix-bot-sdk") + cmd.Dir = "./js_project" + var out bytes.Buffer + cmd.Stdout = &out + err = cmd.Run() + if err != nil { + return err + } + fmt.Printf("Output: %q\n", out.String()) + return nil +} + +func getName(p []*lexer.PropertyArrays) (string, error) { + var name string + for _, v := range p { + if v.Key == "Name" { + if v.Value != nil { + name = *v.Value.String + } else { + return "", errors.New("value of the \"Name\" Key is empty. Please fix the Value to contain a string") + } + break + } + } + if name == "" { + return "", errors.New("value of \"Name\" is empty. Please fix the Value to contain a string") + } + + return name, nil +} + +func getDesc(p []*lexer.PropertyArrays) (string, error) { + var desc string + for _, v := range p { + if v.Key == "Description" { + if v.Value != nil { + desc = *v.Value.String + } else { + return "", errors.New("value of the \"Description\" Key is empty. Please fix the Value to contain a string") + } + break + } + } + if desc == "" { + return "", errors.New("value of \"Description\" is empty. Please fix the Value to contain a string") + } + + return desc, nil +} diff --git a/cmd/generator/js/templates/templates.go b/cmd/generator/js/templates/templates.go @@ -0,0 +1,11 @@ +package templates + +var Index_autojoin = ` +const MatrixClient = require("matrix-bot-sdk").MatrixClient; +const AutojoinRoomsMixin = require("matrix-bot-sdk").AutojoinRoomsMixin; + +const client = new MatrixClient("{{homeserver}}", "{{access_token}}"); +AutojoinRoomsMixin.setupOnClient(client); + +client.start().then(() => console.log("Client started!")); +` diff --git a/cmd/generator/js/types.go b/cmd/generator/js/types.go @@ -0,0 +1,9 @@ +package js + +type pkgJson struct { + Name string `json:"name,omitempty"` + Version string `json:"version,omitempty"` + Description string `json:"description,omitempty"` + Main string `json:"main,omitempty"` + Dependencies []interface{} `json:"dependencies,omitempty"` +} diff --git a/cmd/lexer/ast.go b/cmd/lexer/ast.go @@ -30,4 +30,5 @@ type Array struct { type Value struct { String *string ` @String` + Bool *string `| @String` } diff --git a/example.mx b/example.mx @@ -1,6 +1,8 @@ [[BOT]] -Name:"Test Bot" -Prefix:"!" +Name: "Test Bot" +Description: "Some Demo Bot" +Prefix: "!" +Autojoin: "true" Commands: - Trigger: "afk" Response: "{{user}} just went afk"