diff --git a/analyze/analyze.go b/analyze/analyze.go index 8a72e39..2184790 100644 --- a/analyze/analyze.go +++ b/analyze/analyze.go @@ -10,6 +10,7 @@ import ( func main() { a := flag.String("a", "", "analyze a file") g := flag.Bool("g", false, "generate model") + e := flag.Bool("e", false, "regenerate events") flag.Parse() if len(*a) > 0 { @@ -17,9 +18,26 @@ func main() { } if *g { - err := df.Generate() + a, err := df.LoadAnalyzeData() if err != nil { log.Fatal(err) } + + df.ListEnumCandidates(a) + + m, err := df.CreateMetadata(a) + if err != nil { + log.Fatal(err) + } + if err := df.GenerateBackendCode(m); err != nil { + log.Fatal(err) + } + + if *e { + if err := df.GenerateEventsCode(m); err != nil { + log.Fatal(err) + } + } + } } diff --git a/analyze/df/analyze.go b/analyze/df/analyze.go index 02ceed1..a33c80d 100644 --- a/analyze/df/analyze.go +++ b/analyze/df/analyze.go @@ -9,34 +9,6 @@ import ( "github.com/robertjanetzko/LegendsBrowser2/backend/util" ) -func Generate() error { - a, err := LoadAnalyzeData() - if err != nil { - return err - } - - listEnumCandidates(a) - - m, err := createMetadata(a) - if err != nil { - return err - } - - if err := generateBackendCode(m); err != nil { - return err - } - - if err := generateEventsCode(m); err != nil { - return err - } - - if err := generateFrontendCode(m); err != nil { - return err - } - - return nil -} - var allowedTyped = map[string]bool{ "df_world|historical_events|historical_event": true, "df_world|historical_event_collections|historical_event_collection": true, @@ -51,7 +23,7 @@ func typeName(k string) string { type Metadata map[string]Object -func createMetadata(a *AnalyzeData) (*Metadata, error) { +func CreateMetadata(a *AnalyzeData) (*Metadata, error) { fs := filterSubtypes(&a.Fields) // unique type names @@ -257,7 +229,7 @@ func isObject(typ string, types []string) (bool, string) { return fc > 0, typ } -func listEnumCandidates(a *AnalyzeData) { +func ListEnumCandidates(a *AnalyzeData) { keys := util.Keys(a.Fields) sort.Strings(keys) diff --git a/analyze/df/generate_backend.go b/analyze/df/generate_backend.go index 079062e..ea87930 100644 --- a/analyze/df/generate_backend.go +++ b/analyze/df/generate_backend.go @@ -251,7 +251,7 @@ func LoadSameFields() error { return nil } -func generateBackendCode(objects *Metadata) error { +func GenerateBackendCode(objects *Metadata) error { LoadSameFields() file, _ := json.MarshalIndent(objects, "", " ") diff --git a/analyze/df/generate_events.go b/analyze/df/generate_events.go index 3f72b6a..eb2ee77 100644 --- a/analyze/df/generate_events.go +++ b/analyze/df/generate_events.go @@ -20,7 +20,7 @@ func (x *{{ $obj.Name }}) Html() string { return "UNKNWON {{ $obj.Name }}" } {{- end }} `)) -func generateEventsCode(objects *Metadata) error { +func GenerateEventsCode(objects *Metadata) error { file, _ := json.MarshalIndent(objects, "", " ") _ = ioutil.WriteFile("model.json", file, 0644) diff --git a/analyze/df/generate_frontend.go b/analyze/df/generate_frontend.go deleted file mode 100644 index f18696c..0000000 --- a/analyze/df/generate_frontend.go +++ /dev/null @@ -1,77 +0,0 @@ -package df - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "os" - "text/template" - - "github.com/iancoleman/strcase" -) - -var frontendTemplate = template.Must(template.New("").Parse(`// Code generated by legendsbrowser; DO NOT EDIT. -{{- range $name, $obj := $.Objects }} -export interface {{ $obj.Name }} { - {{- range $fname, $field := $obj.Fields }} - {{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }} - {{ $field.JsonTypeLine }}; - {{- end }} - {{- end }} - {{- if not (not $obj.SubTypes) }} - Details: any; - {{- end }} -} -{{- end }} -`)) - -func generateFrontendCode(objects *Metadata) error { - file, _ := json.MarshalIndent(objects, "", " ") - _ = ioutil.WriteFile("model.json", file, 0644) - - f, err := os.Create("../frontend/src/app/types.ts") - if err != nil { - return err - } - defer f.Close() - - var buf bytes.Buffer - err = frontendTemplate.Execute(&buf, struct { - Objects *Metadata - Modes []bool - }{ - Objects: objects, - Modes: []bool{false, true}, - }) - if err != nil { - return err - } - - _, err = f.Write(buf.Bytes()) - return err -} - -func (f Field) JsonTypeLine() string { - m := "" - if f.Multiple { - m = "[]" - } - t := f.Type - if f.Type == "int" { - t = "number" - } - if f.Type == "bool" { - t = "boolean" - } - if f.Type == "array" { - t = *f.ElementType + "[]" - } - if f.Type == "map" { - t = "{ [key:number]:" + *f.ElementType + "; }" - } - if f.Type == "object" { - t = *f.ElementType - } - return fmt.Sprintf("%s: %s%s", strcase.ToLowerCamel(f.Name), t, m) -}