diff --git a/CHANGELOG.md b/CHANGELOG.md index 820e851..406039d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/gen/bobgen-mysql/templates/models/singleton/bob_mysql_blocks.go.tpl b/gen/bobgen-mysql/templates/models/singleton/bob_mysql_blocks.bob.go.tpl similarity index 100% rename from gen/bobgen-mysql/templates/models/singleton/bob_mysql_blocks.go.tpl rename to gen/bobgen-mysql/templates/models/singleton/bob_mysql_blocks.bob.go.tpl diff --git a/gen/bobgen-psql/templates/models/singleton/bob_psql_blocks.go.tpl b/gen/bobgen-psql/templates/models/singleton/bob_psql_blocks.bob.go.tpl similarity index 100% rename from gen/bobgen-psql/templates/models/singleton/bob_psql_blocks.go.tpl rename to gen/bobgen-psql/templates/models/singleton/bob_psql_blocks.bob.go.tpl diff --git a/gen/bobgen-sqlite/templates/models/singleton/bob_sqlite_blocks.go.tpl b/gen/bobgen-sqlite/templates/models/singleton/bob_sqlite_blocks.bob.go.tpl similarity index 100% rename from gen/bobgen-sqlite/templates/models/singleton/bob_sqlite_blocks.go.tpl rename to gen/bobgen-sqlite/templates/models/singleton/bob_sqlite_blocks.bob.go.tpl diff --git a/gen/output.go b/gen/output.go index 9321288..deb703f 100644 --- a/gen/output.go +++ b/gen/output.go @@ -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) } } @@ -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) @@ -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() @@ -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) } @@ -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 @@ -461,10 +482,6 @@ func getOutputFilename(schema, tableName string, isTest, isGo bool) string { output += "." + schema } - if isTest { - output += "_test" - } - return output } @@ -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" @@ -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 @@ -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 } diff --git a/gen/output_test.go b/gen/output_test.go index ad3b61a..ad14f84 100644 --- a/gen/output_test.go +++ b/gen/output_test.go @@ -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) @@ -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) - } } } @@ -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) - } }) } } diff --git a/gen/templates/factory/singleton/bobfactory_context.go.tpl b/gen/templates/factory/singleton/bobfactory_context.bob.go.tpl similarity index 100% rename from gen/templates/factory/singleton/bobfactory_context.go.tpl rename to gen/templates/factory/singleton/bobfactory_context.bob.go.tpl diff --git a/gen/templates/factory/singleton/bobfactory_enums.go.tpl b/gen/templates/factory/singleton/bobfactory_enums.bob.go.tpl similarity index 100% rename from gen/templates/factory/singleton/bobfactory_enums.go.tpl rename to gen/templates/factory/singleton/bobfactory_enums.bob.go.tpl diff --git a/gen/templates/factory/singleton/bobfactory_main.go.tpl b/gen/templates/factory/singleton/bobfactory_main.bob.go.tpl similarity index 100% rename from gen/templates/factory/singleton/bobfactory_main.go.tpl rename to gen/templates/factory/singleton/bobfactory_main.bob.go.tpl diff --git a/gen/templates/factory/singleton/bobfactory_random.go.tpl b/gen/templates/factory/singleton/bobfactory_random.bob.go.tpl similarity index 100% rename from gen/templates/factory/singleton/bobfactory_random.go.tpl rename to gen/templates/factory/singleton/bobfactory_random.bob.go.tpl diff --git a/gen/templates/factory/singleton/bobfactory_random_test.go.tpl b/gen/templates/factory/singleton/bobfactory_random_test.bob.go.tpl similarity index 100% rename from gen/templates/factory/singleton/bobfactory_random_test.go.tpl rename to gen/templates/factory/singleton/bobfactory_random_test.bob.go.tpl diff --git a/gen/templates/models/singleton/bob_enums.go.tpl b/gen/templates/models/singleton/bob_enums.bob.go.tpl similarity index 100% rename from gen/templates/models/singleton/bob_enums.go.tpl rename to gen/templates/models/singleton/bob_enums.bob.go.tpl diff --git a/gen/templates/models/singleton/bob_main.go.tpl b/gen/templates/models/singleton/bob_main.bob.go.tpl similarity index 100% rename from gen/templates/models/singleton/bob_main.go.tpl rename to gen/templates/models/singleton/bob_main.bob.go.tpl diff --git a/gen/templates/models/singleton/bob_main_test.go.tpl b/gen/templates/models/singleton/bob_main_test.bob.go.tpl similarity index 100% rename from gen/templates/models/singleton/bob_main_test.go.tpl rename to gen/templates/models/singleton/bob_main_test.bob.go.tpl