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

generate.go (4043B)


      1 // Copyright © 2018 NAME HERE <EMAIL ADDRESS>
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package cmd
     16 
     17 import (
     18 	"errors"
     19 	"fmt"
     20 	"github.com/Nordgedanken/matrix_dsl/cmd/generator/js"
     21 	"github.com/Nordgedanken/matrix_dsl/cmd/lexer"
     22 	"github.com/alecthomas/participle"
     23 	"github.com/spf13/cobra"
     24 	"os"
     25 )
     26 
     27 // generateCmd represents the generate command
     28 var generateCmd = &cobra.Command{
     29 	Use:   "generate",
     30 	Short: "A brief description of your command",
     31 	Long: `A longer description that spans multiple lines and likely contains examples
     32 and usage of using your command. For example:
     33 
     34 Cobra is a CLI library for Go that empowers applications.
     35 This application is a tool to generate the needed files
     36 to quickly create a Cobra application.`,
     37 	Args: func(cmd *cobra.Command, args []string) error {
     38 		if len(args) < 1 {
     39 			return errors.New("requires at least one arg")
     40 		}
     41 		return nil
     42 	},
     43 	RunE: func(cmd *cobra.Command, args []string) error {
     44 		parser, err := participle.Build(&lexer.Matrix{}, lexer.MatrixDefinition)
     45 		if err != nil {
     46 			return err
     47 		}
     48 
     49 		mx := &lexer.Matrix{}
     50 		file, err := os.Open(args[0])
     51 		if err != nil {
     52 			return err
     53 		}
     54 
     55 		err = parser.Parse(file, mx)
     56 		if err != nil {
     57 			return err
     58 		}
     59 
     60 		printMX(mx)
     61 
     62 		for _, v := range mx.Sections {
     63 			if v.Identifier == "BOT" {
     64 				js.GenerateBot(mx.Sections[0])
     65 			} else {
     66 				return errors.New("unknown Section type")
     67 			}
     68 		}
     69 		return nil
     70 	},
     71 }
     72 
     73 //printMX is a temporary pretty print of the generated AST
     74 func printMX(mx *lexer.Matrix) error {
     75 	if mx.Properties != nil {
     76 	} else {
     77 		fmt.Println("No Properties found")
     78 	}
     79 	if mx.Sections != nil {
     80 		for _, s := range mx.Sections {
     81 			fmt.Println("Section Name: ", s.Identifier)
     82 			if s.Properties != nil {
     83 				for _, p := range s.Properties {
     84 					fmt.Printf("[%s] Prop Key: %s\n", s.Identifier, p.Key)
     85 					if p.Event != nil {
     86 						fmt.Printf("[%s] Event: %s\n", s.Identifier, *p.Event)
     87 					}
     88 					if p.Value != nil && p.Value.String != nil {
     89 						fmt.Printf("[%s] Value: %s\n", s.Identifier, *p.Value.String)
     90 					}
     91 					if p.Arrays != nil {
     92 						for i, a := range p.Arrays {
     93 							fmt.Printf("[%s][%s][%d] Array Index: %d\n", s.Identifier, p.Key, i, i)
     94 							if a.Key != "" {
     95 								return errors.New("array key should always be empty")
     96 							}
     97 							if a.Properties != nil {
     98 								for _, ap := range a.Properties {
     99 									fmt.Printf("[%s][%s][%d] Prop Key: %s\n", s.Identifier, p.Key, i, ap.Key)
    100 									if ap.Event != nil {
    101 										fmt.Printf("[%s][%s][%d] Event: %s\n", s.Identifier, p.Key, i, *ap.Event)
    102 									}
    103 									if ap.Value != nil && ap.Value.String != nil {
    104 										fmt.Printf("[%s][%s][%d] Value: %s\n", s.Identifier, p.Key, i, *ap.Value.String)
    105 									}
    106 								}
    107 							} else {
    108 								fmt.Printf("[%s][%s] No Arrays found\n", s.Identifier, p.Key)
    109 							}
    110 						}
    111 					} else {
    112 						fmt.Printf("[%s][%s] No Arrays found\n", s.Identifier, p.Key)
    113 					}
    114 				}
    115 			} else {
    116 				fmt.Printf("[%s] No Properties found\n", s.Identifier)
    117 			}
    118 		}
    119 	} else {
    120 		fmt.Println("No Sections found")
    121 	}
    122 	return nil
    123 }
    124 
    125 func init() {
    126 	rootCmd.AddCommand(generateCmd)
    127 
    128 	// Here you will define your flags and configuration settings.
    129 
    130 	// Cobra supports Persistent Flags which will work for this command
    131 	// and all subcommands, e.g.:
    132 	// generateCmd.PersistentFlags().String("foo", "", "A help for foo")
    133 
    134 	// Cobra supports local flags which will only run when this command
    135 	// is called directly, e.g.:
    136 	// generateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    137 }