From 26b7bdc2905d60c58b41c75b4dc3c32fc2d85521 Mon Sep 17 00:00:00 2001 From: Bowen Date: Fri, 12 Dec 2025 18:42:41 +0800 Subject: [PATCH] feat: [#423] Optimize setup --- go.mod | 2 +- go.sum | 4 +-- setup/setup.go | 80 ++++++++++++++++++++++++-------------------------- 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 6953cf8..e5591b4 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.25.5 require ( github.com/Masterminds/squirrel v1.5.4 - github.com/goravel/framework v1.16.1-0.20251204091854-4dcf6db5af43 + github.com/goravel/framework v1.16.1-0.20251210102050-e17cc7a766bd github.com/spf13/cast v1.10.0 github.com/stretchr/testify v1.11.1 gorm.io/driver/postgres v1.6.0 diff --git a/go.sum b/go.sum index b02bfc1..12d96d9 100644 --- a/go.sum +++ b/go.sum @@ -106,8 +106,8 @@ github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQ github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= -github.com/goravel/framework v1.16.1-0.20251204091854-4dcf6db5af43 h1:00SPBQnjk1n2hSDtQO8KrJBYfPkHOXrR0wR5wY2ZssU= -github.com/goravel/framework v1.16.1-0.20251204091854-4dcf6db5af43/go.mod h1:JBTmsY7yAM2i4j6iiPqBrK7H7YJYV/CYQdnOBwmsXoQ= +github.com/goravel/framework v1.16.1-0.20251210102050-e17cc7a766bd h1:/K1urDWnJgLi4Cy5oyZN0N5CjcBcmiOGK8nyhmhVxoE= +github.com/goravel/framework v1.16.1-0.20251210102050-e17cc7a766bd/go.mod h1:N4Q6ljvGDF4Kh9A4Bjajv5okcrfO94BHlPJglP+oUY8= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= diff --git a/setup/setup.go b/setup/setup.go index 1bafe25..a844252 100755 --- a/setup/setup.go +++ b/setup/setup.go @@ -11,6 +11,7 @@ import ( ) func main() { + setup := packages.Setup(os.Args) config := `map[string]any{ "host": config.Env("DB_HOST", "127.0.0.1"), "port": config.Env("DB_PORT", 5432), @@ -29,56 +30,53 @@ func main() { appConfigPath := path.Config("app.go") databaseConfigPath := path.Config("database.go") - modulePath := packages.GetModulePath() + moduleImport := setup.Paths().Module().Import() postgresServiceProvider := "&postgres.ServiceProvider{}" driverContract := "github.com/goravel/framework/contracts/database/driver" postgresFacades := "github.com/goravel/postgres/facades" databaseConnectionsConfig := match.Config("database.connections") databaseConfig := match.Config("database") - packages.Setup(os.Args). - Install( - // Add postgres service provider to app.go if not using bootstrap setup - modify.When(func(_ map[string]any) bool { - return !env.IsBootstrapSetup() - }, modify.GoFile(appConfigPath). - Find(match.Imports()).Modify(modify.AddImport(modulePath)). - Find(match.Providers()).Modify(modify.Register(postgresServiceProvider))), + setup.Install( + // Add postgres service provider to app.go if not using bootstrap setup + modify.When(func(_ map[string]any) bool { + return !env.IsBootstrapSetup() + }, modify.GoFile(appConfigPath). + Find(match.Imports()).Modify(modify.AddImport(moduleImport)). + Find(match.Providers()).Modify(modify.Register(postgresServiceProvider))), - // Add postgres service provider to providers.go if using bootstrap setup - modify.When(func(_ map[string]any) bool { - return env.IsBootstrapSetup() - }, modify.AddProviderApply(modulePath, postgresServiceProvider)), + // Add postgres service provider to providers.go if using bootstrap setup + modify.When(func(_ map[string]any) bool { + return env.IsBootstrapSetup() + }, modify.AddProviderApply(moduleImport, postgresServiceProvider)), - // Add postgres connection to database.go - modify.GoFile(databaseConfigPath).Find(match.Imports()).Modify( - modify.AddImport(driverContract), - modify.AddImport(postgresFacades, "postgresfacades"), - ). - Find(databaseConnectionsConfig).Modify(modify.AddConfig("postgres", config)). - Find(databaseConfig).Modify(modify.AddConfig("default", `"postgres"`)), + // Add postgres connection to database.go + modify.GoFile(databaseConfigPath).Find(match.Imports()).Modify( + modify.AddImport(driverContract), + modify.AddImport(postgresFacades, "postgresfacades"), ). - Uninstall( - // Remove postgres connection from database.go - modify.GoFile(databaseConfigPath). - Find(databaseConfig).Modify(modify.AddConfig("default", `""`)). - Find(databaseConnectionsConfig).Modify(modify.RemoveConfig("postgres")). - Find(match.Imports()).Modify( - modify.RemoveImport(driverContract), - modify.RemoveImport(postgresFacades, "postgresfacades"), - ), + Find(databaseConnectionsConfig).Modify(modify.AddConfig("postgres", config)). + Find(databaseConfig).Modify(modify.AddConfig("default", `"postgres"`)), + ).Uninstall( + // Remove postgres connection from database.go + modify.GoFile(databaseConfigPath). + Find(databaseConfig).Modify(modify.AddConfig("default", `""`)). + Find(databaseConnectionsConfig).Modify(modify.RemoveConfig("postgres")). + Find(match.Imports()).Modify( + modify.RemoveImport(driverContract), + modify.RemoveImport(postgresFacades, "postgresfacades"), + ), - // Remove postgres service provider from app.go if not using bootstrap setup - modify.When(func(_ map[string]any) bool { - return !env.IsBootstrapSetup() - }, modify.GoFile(appConfigPath). - Find(match.Providers()).Modify(modify.Unregister(postgresServiceProvider)). - Find(match.Imports()).Modify(modify.RemoveImport(modulePath))), + // Remove postgres service provider from app.go if not using bootstrap setup + modify.When(func(_ map[string]any) bool { + return !env.IsBootstrapSetup() + }, modify.GoFile(appConfigPath). + Find(match.Providers()).Modify(modify.Unregister(postgresServiceProvider)). + Find(match.Imports()).Modify(modify.RemoveImport(moduleImport))), - // Remove postgres service provider from providers.go if using bootstrap setup - modify.When(func(_ map[string]any) bool { - return env.IsBootstrapSetup() - }, modify.RemoveProviderApply(modulePath, postgresServiceProvider)), - ). - Execute() + // Remove postgres service provider from providers.go if using bootstrap setup + modify.When(func(_ map[string]any) bool { + return env.IsBootstrapSetup() + }, modify.RemoveProviderApply(moduleImport, postgresServiceProvider)), + ).Execute() }