Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generators): generate option-validator-auto (#191) #193

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ linters:
- wsl

run:
skip-dirs:
- /generators/gola/templates
skip-files:
- .*\.tmpl$
issues-exit-code: 1
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"beezledub",
"bindnative",
"bodyclose",
"colors",
Expand All @@ -11,6 +12,7 @@
"errcheck",
"exportloopref",
"extendio",
"faydeaudeau",
"fieldalignment",
"foobar",
"goconst",
Expand All @@ -32,6 +34,7 @@
"ipnet",
"linters",
"nakedret",
"nolint",
"nolintlint",
"paramset",
"prealloc",
Expand All @@ -45,11 +48,15 @@
"stylecheck",
"templatise",
"thelper",
"tmpl",
"tparallel",
"unconvert",
"unparam",
"vactive",
"Validatable",
"varcheck"
]
],
"files.associations": {
"**/*.tmpl": "go"
}
}
9 changes: 7 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ tasks:
ov-gen-t:
cmds:
- mkdir -p {{.GEN_TEST_OUTPUT_DIR}}
- cobrass-gen -test -cwd ./
- cobrass-gen -test -cwd ./ -templates generators/gola -write
- go fmt ./generators/gola/out/assistant/*.go

ov-gen:
cmds:
- go generate ./...

clear-gen-t:
cmds:
- rm -f ./generators/gola/out/assistant/*auto*.go

# === build/deploy code generator ===========================

b-gen-linux:
Expand All @@ -97,7 +102,7 @@ tasks:
- GOOS={{.TARGET_OS}} GOARCH={{.TARGET_ARCH}} go build -o {{.DIST_DIR}}/{{.TARGET_OS}}/{{.GEN_BINARY_NAME}} -v {{.APPLICATION_ENTRY}}

sources:
- ./generators/gola/*.go
- ./generators/gola/**/*.go

generates:
- "{{.DIST_DIR}}/{{.TARGET_OS}}/{{.GEN_BINARY_NAME}}"
Expand Down
2 changes: 1 addition & 1 deletion generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cobrass

//go:generate cobrass-gen -cwd ./
//go:generate cobrass-gen -cwd ./ -templates gola/templates/
42 changes: 15 additions & 27 deletions generators/gola/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const (
)

var (
testFlag = flag.Bool("test", false, "generate code in test location?")
cwdFlag = flag.String("cwd", "", "current working directory")
testPath = filepath.Join("generators", "gola", "out", "assistant")
sourcePath = filepath.Join("src", "assistant")
outputPathNotFound = "Output path '%v', not found"
testFlag = flag.Bool("test", false, "generate code in test location?")
cwdFlag = flag.String("cwd", "", "current working directory")
templatesSubPath = flag.String("templates", "", "templates sub path")
write = flag.Bool("write", false, "write generated code?")
testPath = filepath.Join("generators", "gola", "out", "assistant")
sourcePath = filepath.Join("src", "assistant")
)

func Usage() {
Expand All @@ -42,9 +43,6 @@ func fail(reason string, callback ...func()) {
os.Exit(outputPathNotFoundExitCode)
}

// ???
// https://askgolang.com/how-to-get-current-directory-in-golang/

func main() {
flag.Usage = Usage
flag.Parse()
Expand All @@ -58,18 +56,18 @@ func main() {
absolutePath, _ := filepath.Abs(*cwdFlag)
absolutePath = filepath.Join(absolutePath, outputPath)

if !utils.FileExists(absolutePath) {
if !utils.FolderExists(absolutePath) {
callback := func() {
fmt.Printf("💥 ---> CWD: '%v' \n", *cwdFlag)
fmt.Printf("💥 ---> OUTPUT: '%v' \n", outputPath)
fmt.Printf("💥 ---> RESOLVED: '%v' \n", absolutePath)
}
fail(fmt.Sprintf(outputPathNotFound, absolutePath), callback)
fail(fmt.Sprintf("Output path '%v', not found", absolutePath), callback)

return
}

sourceCode := gola.NewSourceCodeContainer()
sourceCode := gola.NewSourceCodeContainer(absolutePath, *templatesSubPath)
mode := lo.Ternary(*testFlag, "🧪 Test", "🎁 Source")

fmt.Printf("☑️ ---> CWD: '%v' \n", *cwdFlag)
Expand All @@ -78,12 +76,12 @@ func main() {
fmt.Printf("---> 🐲 cobrass generator (%v, to: %v)\n", mode, absolutePath)

if !*testFlag {
if sourceCode.AnyMissing(absolutePath) {
sourceCode.Verify(absolutePath, func(entry *gola.SourceCodeData) {
exists := entry.Exists(absolutePath)
if sourceCode.AnyMissing() {
sourceCode.Verify(func(entry *gola.SourceCodeData) {
exists := entry.Exists()
indicator := lo.Ternary(exists, "✔️", "❌")
status := lo.Ternary(exists, "exists", "missing")
path := entry.FullPath(absolutePath)
path := entry.FullPath()
message := fmt.Sprintf("%v source file: '%v' %v", indicator, path, status)

fmt.Printf("%v\n", message)
Expand All @@ -93,17 +91,7 @@ func main() {
}
}

sourceCode.Generator().Run()

logicalEnum := gola.LogicalType{
TypeName: "Enum",
GoType: "string",
DisplayType: "enum",
UnderlyingTypeName: "String",
FlagName: "Format",
Short: "f",
Def: "xml",
if err := sourceCode.Generator(*write).Run(); err != nil {
fail(err.Error())
}

fmt.Printf("---> 🐲 cobrass generator (enum: %+v)\n", logicalEnum)
}
6 changes: 6 additions & 0 deletions generators/gola/gola-defs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gola

type executionInfo struct {
Spec *TypeSpec
Operators Operators
}
8 changes: 4 additions & 4 deletions generators/gola/gomega-matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m *AreAllSourceCodeFilesPresentMatcher) Match(actual interface{}) (bool, e
return false, fmt.Errorf("matcher expected a SourceCodeContainer value (actual: '%v')", actual)
}

return !sourceCode.AnyMissing(m.directory), nil
return !sourceCode.AnyMissing(), nil
}

func (m *AreAllSourceCodeFilesPresentMatcher) report(
Expand All @@ -41,11 +41,11 @@ func (m *AreAllSourceCodeFilesPresentMatcher) report(
fmt.Sprintf("🔥 Expected all source code files %vto be present\n", not),
)

sourceCode.ReportAll(func(entry *gola.SourceCodeData) {
exists := entry.Exists(m.directory)
sourceCode.ForEach(func(entry *gola.SourceCodeData) {
exists := entry.Exists()
indicator := lo.Ternary(exists, "✔️", "❌")
status := lo.Ternary(exists, "exists", "missing")
path := entry.FullPath(m.directory)
path := entry.FullPath()
message := fmt.Sprintf("%v source file: '%v' %v\n", indicator, path, status)
builder.WriteString(message)
})
Expand Down
Loading