Skip to content

Commit

Permalink
Merge pull request #138 from loomnetwork/enum_abilities_template
Browse files Browse the repository at this point in the history
C#, Go, Solidity Enum Generator
  • Loading branch information
mattkanwisher authored Sep 24, 2018
2 parents d6fc093 + 0dddbfa commit e15e331
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ build: contracts/zombiebattleground.1.0.0

cli: bin/zb-cli

enum_gen: bin/zb-enum-gen

bin/zb-cli:
go build -o $@ $(PKG)/cli

bin/zb-enum-gen:
go build -o $@ $(PKG)/tools/cmd

contracts/zombiebattleground.1.0.0: proto
go build -o $@ $(PKG)/plugin

Expand Down Expand Up @@ -49,7 +54,8 @@ deps: $(PLUGIN_DIR) $(LOOMCHAIN_DIR)
github.com/google/uuid \
github.com/grpc-ecosystem/go-grpc-prometheus \
github.com/prometheus/client_golang/prometheus \
github.com/loomnetwork/e2e
github.com/loomnetwork/e2e \
github.com/iancoleman/strcase
go install github.com/golang/dep/cmd/dep
cd $(LOOMCHAIN_DIR) && make deps && make && cp loom $(GOPATH)/bin
cd $(GOGO_PROTOBUF_DIR) && git checkout 1ef32a8b9fc3f8ec940126907cedb5998f6318e4
Expand All @@ -65,5 +71,6 @@ clean:
types/zb/Zb.cs \
contracts/zombiebattleground.1.0.0 \
bin/zb-cli
bin/zb-enum-gen

.PHONY: all clean test deps proto cli
78 changes: 78 additions & 0 deletions tools/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"text/template"

"github.com/iancoleman/strcase"
)

type Ability struct {
Name string
Types []string
}

//FileStruct abilities dataset from json
type FileStruct struct {
Abilities []Ability
Actions []Ability
StaticConfigs []Ability
}

var fns = template.FuncMap{
"last": func(x int, a []string) bool {
fmt.Printf("x -%d len-%v\n", x, len(a))
return x == len(a)-1
},
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"ToCamel": strcase.ToCamel,
}

func outputTemple(f io.Writer, ab *FileStruct, templateBaseName, templatefile string) {
t, err := template.New(templateBaseName).Funcs(fns).ParseFiles(templatefile)
if err != nil {
panic(err)
}
err = t.Execute(f, ab)
if err != nil {
panic(err)
}
}

func outputTemplate(outputfile string, ab *FileStruct, templateBaseName, templatefile string) {
filename := outputfile

if filename == "-" {
outputTemple(os.Stdout, ab, templateBaseName, templatefile)
}

f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
fmt.Printf("\noutputfile file -%s\n", filename)
outputTemple(f, ab, templateBaseName, templatefile)
f.Sync()
}

func main() {
ab := &FileStruct{}

byteValue, err := ioutil.ReadFile("tools/cmd/templates/inputdata.json")
if err != nil {
panic(err)
}

json.Unmarshal(byteValue, &ab)

outputTemplate("Enumerators.cs", ab, "csharpabilities.parse", "tools/cmd/templates/csharpabilities.parse")
outputTemplate("enum.sol", ab, "soliditybilities.parse", "tools/cmd/templates/soliditybilities.parse")
outputTemplate("ability.go", ab, "goabilities.parse", "tools/cmd/templates/goabilities.parse")
}
26 changes: 26 additions & 0 deletions tools/cmd/templates/csharpabilities.parse
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//THIS FILE IS AUTO GENERATED IN THE BACKEND REPO
//TO MAKE CHANGES YOU NEED TO EDIT THE GAMECHAIN REPO

namespace Loom.ZombieBattleground.Common
{
public class Enumerators
{

public const string JSONVersion = '0.1' //constant for what json file generated this

{{ with .Abilities }}
{{ range . }}
public enum Ability{{.Name}}Type
{
{{ with .Types }}
{{range $i, $e := . }}
{{- if $i}},{{end}}
{{$e}}
{{- end }}
{{ end -}}
}
{{ end }}
{{ end }}

}
}
12 changes: 12 additions & 0 deletions tools/cmd/templates/goabilities.parse
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ability

{{ with .Abilities }}
{{ range . }}
const ( {{- $name := .Name}}
{{- range $i, $e := .Types }}
{{$name | ToUpper }}_{{$e }} = iota
{{- end }}
)

{{ end }}
{{ end }}
53 changes: 53 additions & 0 deletions tools/cmd/templates/inputdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"abilities": [
{
"name": "Activity",
"types": [
"PASSIVE",
"ACTIVE"
]
},
{
"name": "Call",
"types": [
"TURN",
"ENTRY",
"END",
"ATTACK",
"DEATH",
"PERMANENT",
"GOT_DAMAGE",
"AT_DEFENCE",
"IN_HAND"
]
}

],
"actions": [
{
"name": "Player",
"types": [
"StartGame",
"EndTurn",
"DrawCardPlayer",
"PlayCard",
"CardAttack",
"UseCardAbility"
]
}

],
"staticConfigs": [
{
"name": "GameConfig",
"types": [
"Health",
"CustomDeck",
"AddCard",
"SetDeck"
]
}

]

}
50 changes: 50 additions & 0 deletions tools/cmd/templates/soliditybilities.parse
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//THIS FILE IS AUTO GENERATED IN THE GAMECHAIN REPO
//TO MAKE CHANGES YOU NEED TO EDIT THE GAMECHAIN REPO

pragma solidity ^0.4.24;

contract ZUBGEnum {
{{ with .Abilities }}
{{ range . }}
enum Ability{{.Name}} {
{{- with .Types -}}
{{range $i, $e := . }}
{{- if $i}},{{end -}}
{{$e | ToLower | ToCamel }}
{{- end -}}
{{- end -}}
}
Ability{{.Name}} {{.Name | ToLower }};
{{ end }}
{{ end }}


{{- with .Actions }}
{{ range . }}
enum Action{{.Name}} {
{{- with .Types -}}
{{range $i, $e := . }}
{{- if $i}},{{end -}}
{{$e | ToLower | ToCamel }}
{{- end -}}
{{- end -}}
}
Action{{.Name}} {{.Name | ToLower }};
{{ end }}
{{ end }}

{{- with .StaticConfigs }}
{{ range . }}
enum Static{{.Name}} {
{{- with .Types -}}
{{range $i, $e := . }}
{{- if $i}},{{end -}}
{{$e | ToLower | ToCamel }}
{{- end -}}
{{- end -}}
}
Static{{.Name}} {{.Name | ToLower }};
{{ end }}
{{ end }}

}

0 comments on commit e15e331

Please sign in to comment.