You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2022/07/20 10:48:10 UTC

[incubator-devlake] 06/07: fix: purpose camel case and snake case

This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit fd56d9cecead3dd849daceebd978f58770b73210
Author: abeizn <zi...@merico.dev>
AuthorDate: Wed Jul 20 17:55:05 2022 +0800

    fix: purpose camel case and snake case
---
 generator/cmd/create_migration.go                     | 19 +++++--------------
 .../migrationscripts/add_init_tables.go-template      |  2 +-
 .../template/migrationscripts/migration.go-template   |  2 +-
 .../migration_with_config.go-template                 |  2 +-
 4 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/generator/cmd/create_migration.go b/generator/cmd/create_migration.go
index 79977ee7..26e7f513 100644
--- a/generator/cmd/create_migration.go
+++ b/generator/cmd/create_migration.go
@@ -24,12 +24,12 @@ import (
 	"os"
 	"path/filepath"
 	"regexp"
-	"strings"
 	"time"
 
 	"github.com/apache/incubator-devlake/generator/util"
 	"github.com/manifoldco/promptui"
 	"github.com/spf13/cobra"
+	"github.com/stoewer/go-strcase"
 )
 
 func init() {
@@ -92,7 +92,7 @@ If framework passed, generator will create a new migration in models/migrationsc
 		// create vars
 		values := map[string]string{}
 		values[`Date`] = time.Now().Format(`20060102`)
-		values[`Purpose`] = purpose
+		values[`Purpose`] = strcase.LowerCamelCase(purpose)
 		existMigrations, err := ioutil.ReadDir(migrationPath)
 		cobra.CheckErr(err)
 		values[`Count`] = fmt.Sprintf(`%06d`, len(existMigrations))
@@ -100,9 +100,9 @@ If framework passed, generator will create a new migration in models/migrationsc
 		// read template
 		templates := map[string]string{}
 		if withConfig == `Yes` {
-			templates[values[`Date`]+`_`+ToSnakeCase(values[`Purpose`])+`.go`] = util.ReadTemplate("generator/template/migrationscripts/migration_with_config.go-template")
+			templates[values[`Date`]+`_`+strcase.SnakeCase(values[`Purpose`])+`.go`] = util.ReadTemplate("generator/template/migrationscripts/migration_with_config.go-template")
 		} else {
-			templates[values[`Date`]+`_`+ToSnakeCase(values[`Purpose`])+`.go`] = util.ReadTemplate("generator/template/migrationscripts/migration.go-template")
+			templates[values[`Date`]+`_`+strcase.SnakeCase(values[`Purpose`])+`.go`] = util.ReadTemplate("generator/template/migrationscripts/migration.go-template")
 		}
 		values = util.DetectExistVars(templates, values)
 		println(`vars in template:`, fmt.Sprint(values))
@@ -114,7 +114,7 @@ If framework passed, generator will create a new migration in models/migrationsc
 			util.ReplaceVarInFile(
 				filepath.Join(migrationPath, `register.go`),
 				regexp.MustCompile(`(return +\[]migration\.Script ?\{ ?\n?)((\s*[\w.()]+,\n?)*)(\s*})`),
-				fmt.Sprintf("$1$2\t\tnew(%s),\n$4", values[`Purpose`]),
+				fmt.Sprintf("$1$2\t\tnew(%s),\n$4", strcase.LowerCamelCase(values[`Purpose`])),
 			)
 		}
 	},
@@ -131,12 +131,3 @@ func purposeNotExistValidate(input string) error {
 
 	return nil
 }
-
-var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
-var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
-
-func ToSnakeCase(str string) string {
-	snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
-	snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
-	return strings.ToLower(snake)
-}
diff --git a/generator/template/migrationscripts/add_init_tables.go-template b/generator/template/migrationscripts/add_init_tables.go-template
index 5e4f68e6..44c9c103 100644
--- a/generator/template/migrationscripts/add_init_tables.go-template
+++ b/generator/template/migrationscripts/add_init_tables.go-template
@@ -35,5 +35,5 @@ func (*addInitTables) Version() uint64 {
 }
 
 func (*addInitTables) Name() string {
-	return "{{ .pluginName }} init schemas {{ .Date }}"
+	return "{{ .pluginName }} init schemas"
 }
diff --git a/generator/template/migrationscripts/migration.go-template b/generator/template/migrationscripts/migration.go-template
index 3a93a128..f3a3a5d0 100644
--- a/generator/template/migrationscripts/migration.go-template
+++ b/generator/template/migrationscripts/migration.go-template
@@ -34,5 +34,5 @@ func (*{{ .Purpose }}) Version() uint64 {
 }
 
 func (*{{ .Purpose }}) Name() string {
-	return "UpdateSchemas for {{ .Purpose }} in {{ .Date }}"
+	return "UpdateSchemas for {{ .Purpose }}"
 }
diff --git a/generator/template/migrationscripts/migration_with_config.go-template b/generator/template/migrationscripts/migration_with_config.go-template
index d4db8db5..3d599f9a 100644
--- a/generator/template/migrationscripts/migration_with_config.go-template
+++ b/generator/template/migrationscripts/migration_with_config.go-template
@@ -42,5 +42,5 @@ func (*{{ .Purpose }}) Version() uint64 {
 }
 
 func (*{{ .Purpose }}) Name() string {
-	return "UpdateSchemas for {{ .Purpose }} in {{ .Date }}"
+	return "UpdateSchemas for {{ .Purpose }}"
 }