Skip to content

Commit

Permalink
add a file suffix option (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
abice authored Dec 18, 2023
1 parent 2f16976 commit 919e61c
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 3 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ jobs:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
run: |
make cover
- name: Coveralls
if: (matrix.go == '1.21')
env:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
run: |
if [[ -n "$COVERALLS_TOKEN" ]]; then
make coveralls
fi
- name: Upload coverage
uses: actions/upload-artifact@v3
if: (matrix.go == '1.21')
uses: actions/upload-artifact@v4
with:
name: coverage
path: coverage.*
Expand Down
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 919e61c

Please sign in to comment.