This commit is contained in:
Mike Schwörer 2023-06-07 12:36:41 +02:00
parent a4b5f33d15
commit fe9ebf0bab
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 47 additions and 3 deletions

View File

@ -32,6 +32,8 @@ echo "> Current Version: ${curr_vers}"
echo "> Next Version: ${next_ver}"
echo ""
printf "package goext\n\nconst GoextVersion = \"%s\"\n\nconst GoextVersionTimestamp = \"%s\"\n" "${next_ver}" "$( date +"%Y-%m-%dT%H:%M:%S%z" )" > "goextVersion.go"
git add --verbose .
msg="v${next_ver}"

View File

@ -3,7 +3,9 @@ package bfcodegen
import (
"errors"
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext"
"gogs.mikescher.com/BlackForestBytes/goext/cmdext"
"gogs.mikescher.com/BlackForestBytes/goext/cryptext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rext"
"io"
@ -32,6 +34,8 @@ var rexEnumDef = rext.W(regexp.MustCompile("^\\s*type\\s+(?P<name>[A-Za-z0-9_]+)
var rexValueDef = rext.W(regexp.MustCompile("^\\s*(?P<name>[A-Za-z0-9_]+)\\s+(?P<type>[A-Za-z0-9_]+)\\s*=\\s*(?P<value>(\"[A-Za-z0-9_:]+\"|[0-9]+))\\s*(//(?P<descr>.*))?.*$"))
var rexChecksumConst = rext.W(regexp.MustCompile("const ChecksumGenerator = \"(?P<cs>[A-Za-z0-9_]*)\""))
func GenerateEnumSpecs(sourceDir string, destFile string) error {
files, err := os.ReadDir(sourceDir)
@ -39,6 +43,36 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
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 := rexChecksumConst.MatchFirst(string(content)); ok {
oldChecksum = m.GroupByName("cs").Value()
}
}
newChecksumStr := goext.GoextVersion
langext.SortBy(files, func(v os.DirEntry) string { return v.Name() })
for _, f := range files {
content, err := os.ReadFile(destFile)
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 := ""
@ -67,7 +101,7 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
return errors.New("no package name found in any file")
}
err = os.WriteFile(destFile, []byte(fmtOutput(allEnums, pkgname)), 0o755)
err = os.WriteFile(destFile, []byte(fmtOutput(newChecksum, allEnums, pkgname)), 0o755)
if err != nil {
return err
}
@ -159,8 +193,8 @@ func processFile(fn string) ([]EnumDef, string, error) {
return enums, pkgname, nil
}
func fmtOutput(enums []EnumDef, pkgname string) string {
str := "// Code generated by permissions_gen.sh DO NOT EDIT.\n"
func fmtOutput(cs string, enums []EnumDef, pkgname string) string {
str := "// Code generated by enum-generate.go DO NOT EDIT.\n"
str += "\n"
str += "package " + pkgname + "\n"
str += "\n"
@ -168,6 +202,9 @@ func fmtOutput(enums []EnumDef, pkgname string) string {
str += "import \"gogs.mikescher.com/BlackForestBytes/goext/langext\"" + "\n"
str += "\n"
str += "const ChecksumGenerator = \"" + cs + "\"" + "\n"
str += "\n"
str += "type Enum interface {" + "\n"
str += " Valid() bool" + "\n"
str += " ValuesAny() []any" + "\n"

5
goextVersion.go Normal file
View File

@ -0,0 +1,5 @@
package goext
const GoextVersion = "0.0.143"
const GoextVersionTimestamp = "2023-06-07T12:36:41+0200"