Skip to content

Commit

Permalink
add a file suffix option
Browse files Browse the repository at this point in the history
  • Loading branch information
abice committed Dec 18, 2023
1 parent 5c7021c commit ff4b8c6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
46 changes: 46 additions & 0 deletions example/suffix.enum.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions example/suffix.enum.gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions example/suffix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:generate ../bin/go-enum -b example --output-suffix .enum.gen

//go:build example
// +build example

package example

// Suffix ENUM(gen)
type Suffix string
29 changes: 29 additions & 0 deletions example/suffix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:generate ../bin/go-enum -b example --output-suffix .enum.gen

//go:build example
// +build example

package example

import (
"testing"

"github.com/stretchr/testify/assert"
)

// SuffixTest ENUM(some_item)
type SuffixTest string

func TestSuffix(t *testing.T) {
x := Suffix("")
assert.Equal(t, "", x.String())

assert.Equal(t, Suffix("gen"), SuffixGen)
}

func TestSuffixTest(t *testing.T) {
x := SuffixTest("")
assert.Equal(t, "", x.String())

assert.Equal(t, SuffixTest("some_item"), SuffixTestSomeItem)
}
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type rootT struct {
ForceLower bool
ForceUpper bool
NoComments bool
OutputSuffix string
}

func main() {
Expand Down Expand Up @@ -174,6 +175,11 @@ func main() {
Usage: "Adds build tags to a generated enum file.",
Destination: &argv.BuildTags,
},
&cli.StringFlag{
Name: "output-suffix",
Usage: "Changes the default filename suffix of _enum to something else. `.go` will be appended to the end of the string no matter what, so that `_test.go` cases can be accommodated ",
Destination: &argv.OutputSuffix,
},
},
Action: func(ctx *cli.Context) error {
aliases, err := generator.ParseAliases(argv.Aliases.Value())
Expand Down Expand Up @@ -262,14 +268,20 @@ func main() {
filenames = fn
}

outputSuffix := `_enum`
if argv.OutputSuffix != "" {
outputSuffix = argv.OutputSuffix
}

for _, fileName := range filenames {
originalName := fileName

out("go-enum started. file: %s\n", color.Cyan(originalName))
fileName, _ = filepath.Abs(fileName)
outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)))

outFilePath := fmt.Sprintf("%s%s.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)), outputSuffix)
if strings.HasSuffix(fileName, "_test.go") {
outFilePath = strings.Replace(outFilePath, "_test_enum.go", "_enum_test.go", 1)
outFilePath = strings.Replace(outFilePath, "_test"+outputSuffix+".go", outputSuffix+"_test.go", 1)
}

// Parse the file given in arguments
Expand Down

0 comments on commit ff4b8c6

Please sign in to comment.