package bfcodegen import ( "bytes" _ "embed" "errors" "fmt" "go/format" "gogs.mikescher.com/BlackForestBytes/goext" "gogs.mikescher.com/BlackForestBytes/goext/cryptext" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/rext" "io" "os" "path" "path/filepath" "regexp" "strings" "text/template" ) type EnumDefVal struct { VarName string Value string Description *string } type EnumDef struct { File string FileRelative string EnumTypeName string Type string Values []EnumDefVal } var rexEnumPackage = rext.W(regexp.MustCompile(`^package\s+(?P[A-Za-z0-9_]+)\s*$`)) var rexEnumDef = rext.W(regexp.MustCompile(`^\s*type\s+(?P[A-Za-z0-9_]+)\s+(?P[A-Za-z0-9_]+)\s*//\s*(@enum:type).*$`)) var rexEnumValueDef = rext.W(regexp.MustCompile(`^\s*(?P[A-Za-z0-9_]+)\s+(?P[A-Za-z0-9_]+)\s*=\s*(?P("[A-Za-z0-9_:\s\-.]+"|[0-9]+))\s*(//(?P.*))?.*$`)) var rexEnumChecksumConst = rext.W(regexp.MustCompile(`const ChecksumEnumGenerator = "(?P[A-Za-z0-9_]*)"`)) //go:embed enum-generate.template var templateEnumGenerateText string func GenerateEnumSpecs(sourceDir string, destFile string) error { files, err := os.ReadDir(sourceDir) if err != nil { return err } oldChecksum := "N/A" if _, err := os.Stat(destFile); !os.IsNotExist(err) { content, err := os.ReadFile(destFile) if err != nil { return err } if m, ok := rexEnumChecksumConst.MatchFirst(string(content)); ok { oldChecksum = m.GroupByName("cs").Value() } } files = langext.ArrFilter(files, func(v os.DirEntry) bool { return v.Name() != path.Base(destFile) }) files = langext.ArrFilter(files, func(v os.DirEntry) bool { return strings.HasSuffix(v.Name(), ".go") }) files = langext.ArrFilter(files, func(v os.DirEntry) bool { return !strings.HasSuffix(v.Name(), "_gen.go") }) langext.SortBy(files, func(v os.DirEntry) string { return v.Name() }) newChecksumStr := goext.GoextVersion for _, f := range files { content, err := os.ReadFile(path.Join(sourceDir, f.Name())) if err != nil { return err } newChecksumStr += "\n" + f.Name() + "\t" + cryptext.BytesSha256(content) } newChecksum := cryptext.BytesSha256([]byte(newChecksumStr)) if newChecksum != oldChecksum { fmt.Printf("[EnumGenerate] Checksum has changed ( %s -> %s ), will generate new file\n\n", oldChecksum, newChecksum) } else { fmt.Printf("[EnumGenerate] Checksum unchanged ( %s ), nothing to do\n", oldChecksum) return nil } allEnums := make([]EnumDef, 0) pkgname := "" for _, f := range files { fmt.Printf("========= %s =========\n\n", f.Name()) fileEnums, pn, err := processEnumFile(sourceDir, path.Join(sourceDir, f.Name())) if err != nil { return err } fmt.Printf("\n") allEnums = append(allEnums, fileEnums...) if pn != "" { pkgname = pn } } if pkgname == "" { return errors.New("no package name found in any file") } fdata, err := format.Source([]byte(fmtEnumOutput(newChecksum, allEnums, pkgname))) if err != nil { return err } err = os.WriteFile(destFile, fdata, 0o755) if err != nil { return err } return nil } func processEnumFile(basedir string, fn string) ([]EnumDef, string, error) { file, err := os.Open(fn) if err != nil { return nil, "", err } defer func() { _ = file.Close() }() bin, err := io.ReadAll(file) if err != nil { return nil, "", err } lines := strings.Split(string(bin), "\n") enums := make([]EnumDef, 0) pkgname := "" for i, line := range lines { if i == 0 && strings.HasPrefix(line, "// Code generated by") { break } if match, ok := rexEnumPackage.MatchFirst(line); i == 0 && ok { pkgname = match.GroupByName("name").Value() continue } if match, ok := rexEnumDef.MatchFirst(line); ok { rfp, err := filepath.Rel(basedir, fn) if err != nil { return nil, "", err } def := EnumDef{ File: fn, FileRelative: rfp, EnumTypeName: match.GroupByName("name").Value(), Type: match.GroupByName("type").Value(), Values: make([]EnumDefVal, 0), } enums = append(enums, def) fmt.Printf("Found enum definition { '%s' -> '%s' }\n", def.EnumTypeName, def.Type) } if match, ok := rexEnumValueDef.MatchFirst(line); ok { typename := match.GroupByName("type").Value() def := EnumDefVal{ VarName: match.GroupByName("name").Value(), Value: match.GroupByName("value").Value(), Description: match.GroupByNameOrEmpty("descr").ValueOrNil(), } found := false for i, v := range enums { if v.EnumTypeName == typename { enums[i].Values = append(enums[i].Values, def) found = true if def.Description != nil { fmt.Printf("Found enum value [%s] for '%s' ('%s')\n", def.Value, def.VarName, *def.Description) } else { fmt.Printf("Found enum value [%s] for '%s'\n", def.Value, def.VarName) } break } } if !found { fmt.Printf("Found non-enum value [%s] for '%s' ( looks like enum value, but no matching @enum:type )\n", def.Value, def.VarName) } } } return enums, pkgname, nil } func fmtEnumOutput(cs string, enums []EnumDef, pkgname string) string { templ := template.New("enum-generate") templ = templ.Funcs(template.FuncMap{ "boolToStr": func(b bool) string { return langext.Conditional(b, "true", "false") }, "deref": func(v *string) string { return *v }, "trimSpace": func(str string) string { return strings.TrimSpace(str) }, "hasStr": func(v EnumDef) bool { return v.Type == "string" }, "hasDescr": func(v EnumDef) bool { return langext.ArrAll(v.Values, func(val EnumDefVal) bool { return val.Description != nil }) }, }) templ = template.Must(templ.Parse(templateEnumGenerateText)) buffer := bytes.Buffer{} err := templ.Execute(&buffer, langext.H{ "PkgName": pkgname, "Checksum": cs, "GoextVersion": goext.GoextVersion, "Enums": enums, }) if err != nil { panic(err) } return buffer.String() }