Skip to content

Commit

Permalink
Merge pull request #324 from stephenafamo/bob-file-suffix
Browse files Browse the repository at this point in the history
Generated files now end with `.bob.go`
  • Loading branch information
stephenafamo authored Dec 11, 2024
2 parents 45fc611 + 40df8b8 commit 1c4d4a0
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated error constant generation to employ specific error types for making error matching easier. (thanks @mbezhanov)
- Collation in `clause.OrderDef` is now a string not an expression and is always quoted
- Calling `UpdateAll`, `DeleteAll` and `ReloadAll` on an empty model slice now returns nil without running any queries.
- Generated files now end with `.bob.go` instead of `.go` and are always cleaned up before generating new files. Singleton templates are now required to have a `.bob.go.tpl` extension.

### Deprecated

- Deprecated the `wipe` option to delete all files in the output folder. Files are now generated with a `.bob.go` extension and are always cleaned up before generating new files.

### Removed

Expand Down
65 changes: 44 additions & 21 deletions gen/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,29 @@ type Output struct {
func (o *Output) initOutFolders(lazyTemplates []lazyTemplate, wipe bool) error {
if wipe {
if err := os.RemoveAll(o.OutFolder); err != nil {
return err
return fmt.Errorf("unable to wipe output folder: %w", err)
}
}

files, err := os.ReadDir(o.OutFolder)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("unable to read output folder: %w", err)
}

for _, d := range files {
if d.IsDir() {
continue
}

name := d.Name()
name = name[:len(name)-len(filepath.Ext(name))]

if !strings.HasSuffix(name, ".bob") && !strings.HasSuffix(name, ".bob_test") {
continue
}

if err := os.Remove(filepath.Join(o.OutFolder, d.Name())); err != nil {
return fmt.Errorf("unable to remove old file: %w", err)
}
}

Expand Down Expand Up @@ -249,7 +271,12 @@ func executeTemplates[T, C, I any](e executeTemplateData[T, C, I], goVersion str
}
}

fName := getOutputFilename(e.data.Table.Schema, e.data.Table.Name, e.isTest, isGo)
fName := getOutputFilename(e.data.Table.Schema, e.data.Table.Name, isGo)
fName += ".bob"
if e.isTest {
fName += "_test"
}

fName += ext
if len(dir) != 0 {
fName = filepath.Join(dir, fName)
Expand Down Expand Up @@ -295,13 +322,11 @@ func executeSingletonTemplates[T, C, I any](e executeTemplateData[T, C, I], goVe
headerOut := e.output.templateHeaderByteBuffer
out := e.output.templateByteBuffer
for _, tplName := range e.templates.Templates() {
normalized, isSingleton, isGo, usePkg := outputFilenameParts(tplName)
normalized, isSingleton, isGo := outputFilenameParts(tplName)
if !isSingleton {
continue
}

dir, _ := filepath.Split(normalized)

headerOut.Reset()
out.Reset()
prevLen := out.Len()
Expand All @@ -327,12 +352,8 @@ func executeSingletonTemplates[T, C, I any](e executeTemplateData[T, C, I], goVe
imps := e.data.Importer.ToList()
version = goVersion

pkgName := e.output.PkgName
if !usePkg {
pkgName = filepath.Base(dir)
}
writeFileDisclaimer(headerOut)
writePackageName(headerOut, pkgName)
writePackageName(headerOut, e.output.PkgName)
writeImports(headerOut, imps)
}

Expand Down Expand Up @@ -447,7 +468,7 @@ func getLongExt(filename string) string {
return filename[index:]
}

func getOutputFilename(schema, tableName string, isTest, isGo bool) string {
func getOutputFilename(schema, tableName string, isGo bool) string {
output := tableName
if strings.HasPrefix(output, "_") {
output = "und" + output
Expand All @@ -461,10 +482,6 @@ func getOutputFilename(schema, tableName string, isTest, isGo bool) string {
output += "." + schema
}

if isTest {
output += "_test"
}

return output
}

Expand Down Expand Up @@ -508,7 +525,7 @@ func stringSliceToMap(slice []string) map[string]struct{} {
// templates_test/js/hello.js.tpl
//
//nolint:nonamedreturns
func outputFilenameParts(filename string) (normalized string, isSingleton, isGo, usePkg bool) {
func outputFilenameParts(filename string) (normalized string, isSingleton, isGo bool) {
fragments := strings.Split(filename, string(os.PathSeparator))
isSingleton = len(fragments) > 1 && fragments[len(fragments)-2] == "singleton"

Expand All @@ -522,15 +539,21 @@ func outputFilenameParts(filename string) (normalized string, isSingleton, isGo,
newFilename := remainingFragments[len(remainingFragments)-1]
newFilename = strings.TrimSuffix(newFilename, ".tpl")
newFilename = rgxRemoveNumberedPrefix.ReplaceAllString(newFilename, "")
remainingFragments[len(remainingFragments)-1] = newFilename

ext := filepath.Ext(newFilename)
isGo = ext == ".go"

usePkg = len(remainingFragments) == 1
remainingFragments[len(remainingFragments)-1] = newFilename
normalized = strings.Join(remainingFragments, string(os.PathSeparator))

return normalized, isSingleton, isGo, usePkg
if isSingleton {
fNameWithoutExt := newFilename[:len(newFilename)-len(ext)]
if !strings.HasSuffix(fNameWithoutExt, ".bob") &&
!strings.HasSuffix(fNameWithoutExt, ".bob_test") {
panic(fmt.Sprintf("singleton file name must end with .bob or .bob_test: %s", filename))
}
}

return normalized, isSingleton, isGo
}

type dirExtMap map[string]map[string][]string
Expand All @@ -541,7 +564,7 @@ func groupTemplates(templates *templateList) dirExtMap {
tplNames := templates.Templates()
dirs := make(map[string]map[string][]string)
for _, tplName := range tplNames {
normalized, isSingleton, _, _ := outputFilenameParts(tplName)
normalized, isSingleton, _ := outputFilenameParts(tplName)
if isSingleton {
continue
}
Expand Down
23 changes: 7 additions & 16 deletions gen/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,16 @@ func TestOutputFilenameParts(t *testing.T) {
Normalized string
IsSingleton bool
IsGo bool
UsePkg bool
}{
{"00_struct.go.tpl", "struct.go", false, true, true},
{"singleton/00_struct.go.tpl", "struct.go", true, true, true},
{"notpkg/00_struct.go.tpl", "notpkg/struct.go", false, true, false},
{"js/singleton/00_struct.js.tpl", "js/struct.js", true, false, false},
{"js/00_struct.js.tpl", "js/struct.js", false, false, false},
{"00_struct.go.tpl", "struct.go", false, true},
{"singleton/00_struct.bob.go.tpl", "struct.bob.go", true, true},
{"notpkg/00_struct.go.tpl", "notpkg/struct.go", false, true},
{"js/singleton/00_struct.bob.js.tpl", "js/struct.bob.js", true, false},
{"js/00_struct.js.tpl", "js/struct.js", false, false},
}

for i, test := range tests {
normalized, isSingleton, isGo, usePkg := outputFilenameParts(test.Filename)
normalized, isSingleton, isGo := outputFilenameParts(test.Filename)

if normalized != test.Normalized {
t.Errorf("%d) normalized wrong, want: %s, got: %s", i, test.Normalized, normalized)
Expand All @@ -87,9 +86,6 @@ func TestOutputFilenameParts(t *testing.T) {
if isGo != test.IsGo {
t.Errorf("%d) isGo wrong, want: %t, got: %t", i, test.IsGo, isGo)
}
if usePkg != test.UsePkg {
t.Errorf("%d) usePkg wrong, want: %t, got: %t", i, test.UsePkg, usePkg)
}
}
}

Expand Down Expand Up @@ -156,15 +152,10 @@ func TestGetOutputFilename(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
notTest := getOutputFilename(tc.SchemaName, tc.TableName, false, tc.IsGo)
notTest := getOutputFilename(tc.SchemaName, tc.TableName, tc.IsGo)
if diff := cmp.Diff(tc.Expected, notTest); diff != "" {
t.Fatal(diff)
}

isTest := getOutputFilename(tc.SchemaName, tc.TableName, true, tc.IsGo)
if diff := cmp.Diff(tc.Expected+"_test", isTest); diff != "" {
t.Fatal(diff)
}
})
}
}

0 comments on commit 1c4d4a0

Please sign in to comment.