Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion database/migration/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/goravel/framework/packages"
"github.com/goravel/framework/support/collect"
"github.com/goravel/framework/support/color"
"github.com/goravel/framework/support/env"
supportfile "github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)
Expand Down Expand Up @@ -53,10 +54,14 @@ func (r *Migrator) Create(name string, modelName string) (string, error) {

// Prepend timestamp to the file name.
fileName := r.creator.GetFileName(name)
facadesImport := packages.Paths().Facades().Import()
if !env.IsBootstrapSetup() {
facadesImport = "github.com/goravel/framework/facades"
}

templateData := StubData{
FacadesPackage: packages.Paths().Facades().Package(),
FacadesImport: packages.Paths().Facades().Import(),
FacadesImport: facadesImport,
Package: packages.Paths().Migrations().Package(),
SchemaFields: schemaFields,
Signature: fileName,
Expand Down
10 changes: 8 additions & 2 deletions foundation/console/package_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support"
"github.com/goravel/framework/support/env"
"github.com/goravel/framework/support/file"
)

Expand Down Expand Up @@ -78,8 +79,13 @@ func (r *PackageMakeCommand) Handle(ctx console.Context) error {
packageName + ".go": packageMakeCommandStubs.Main,
filepath.Join("contracts", packageName+".go"): packageMakeCommandStubs.Contracts,
filepath.Join("facades", packageName+".go"): packageMakeCommandStubs.Facades,
filepath.Join("setup", "stubs.go"): packageMakeCommandStubs.Config,
filepath.Join("setup", "setup.go"): packageMakeCommandStubs.Setup,
}
if env.IsBootstrapSetup() {
files[filepath.Join("setup", "stubs.go")] = packageMakeCommandStubs.Config
files[filepath.Join("setup", "setup.go")] = packageMakeCommandStubs.Setup
} else {
files[filepath.Join("setup", "config", packageName+".go")] = packageMakeCommandStubs.OldConfig
files[filepath.Join("setup", "setup.go")] = packageMakeCommandStubs.OldSetup
}

for path, content := range files {
Expand Down
17 changes: 15 additions & 2 deletions foundation/console/package_make_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ func (s *PackageMakeCommandTestSuite) TestHandle() {

beforeEach := func() {
mockContext = mocksconsole.NewContext(s.T())
// Create bootstrap/app.go to trigger IsBootstrapSetup() == true
_ = file.Create("bootstrap/app.go", `package bootstrap

import "github.com/goravel/framework/foundation"

func Boot() {
foundation.Setup().Start()
}
`)
}

s.T().Cleanup(func() {
_ = file.Remove("bootstrap")
})

tests := []struct {
name string
setup func()
Expand Down Expand Up @@ -106,8 +119,8 @@ func (s *PackageMakeCommandTestSuite) TestHandle() {
s.True(file.Exists("packages/sms/sms.go"))
s.True(file.Exists("packages/sms/contracts/sms.go"))
s.True(file.Exists("packages/sms/facades/sms.go"))
s.True(file.Contain("packages/sms/facades/sms.go", "goravel/packages/sms"))
s.True(file.Contain("packages/sms/facades/sms.go", "goravel/packages/sms/contracts"))
s.True(file.Contain("packages/sms/facades/sms.go", "github.com/goravel/framework/packages/sms"))
s.True(file.Contain("packages/sms/facades/sms.go", "github.com/goravel/framework/packages/sms/contracts"))
s.True(file.Exists("packages/sms/setup/stubs.go"))
s.True(file.Exists("packages/sms/setup/setup.go"))
s.NoError(file.Remove("packages"))
Expand Down
60 changes: 56 additions & 4 deletions foundation/console/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package console
import (
"strings"

"github.com/goravel/framework/support/env"
"github.com/goravel/framework/support/str"
)

Expand Down Expand Up @@ -78,13 +79,14 @@ func (r *DummyServiceProvider) Boot(app foundation.Application) {
}

type PackageMakeCommandStubs struct {
main string
pkg string
root string
name string
}

func NewPackageMakeCommandStubs(pkg, root string) *PackageMakeCommandStubs {
return &PackageMakeCommandStubs{pkg: pkg, root: root, name: packageName(pkg)}
return &PackageMakeCommandStubs{main: env.MainPath(), pkg: pkg, root: root, name: packageName(pkg)}
}

func (r PackageMakeCommandStubs) Readme() string {
Expand Down Expand Up @@ -123,7 +125,7 @@ func (r *ServiceProvider) Register(app foundation.Application) {
App = app

app.Bind(Binding, func(app foundation.Application) (any, error) {
return nil, nil
return &DummyCamelName{}, nil
})
}

Expand All @@ -135,6 +137,7 @@ func (r *ServiceProvider) Boot(app foundation.Application) {

content = strings.ReplaceAll(content, "DummyPackage", r.pkg)
content = strings.ReplaceAll(content, "DummyName", r.name)
content = strings.ReplaceAll(content, "DummyCamelName", str.Of(r.name).Studly().String())

return content
}
Expand Down Expand Up @@ -186,6 +189,24 @@ func config(configPackage string, facadesImport, facadesPackage string) string {
return content
}

func (r PackageMakeCommandStubs) OldConfig() string {
content := `package config

import (
"github.com/goravel/framework/facades"
)

func init() {
config := facades.Config()
config.Add("DummyName", map[string]any{

})
}
`

return strings.ReplaceAll(content, "DummyName", r.name)
}

func (r PackageMakeCommandStubs) Contracts() string {
content := `package contracts

Expand All @@ -201,8 +222,8 @@ func (r PackageMakeCommandStubs) Facades() string {
import (
"log"

"goravel/DummyRoot"
"goravel/DummyRoot/contracts"
"DummyMain/DummyRoot"
"DummyMain/DummyRoot/contracts"
)

func DummyCamelName() contracts.DummyCamelName {
Expand All @@ -216,6 +237,7 @@ func DummyCamelName() contracts.DummyCamelName {
}
`

content = strings.ReplaceAll(content, "DummyMain", r.main)
content = strings.ReplaceAll(content, "DummyRoot", r.root)
content = strings.ReplaceAll(content, "DummyName", r.name)
content = strings.ReplaceAll(content, "DummyCamelName", str.Of(r.name).Studly().String())
Expand Down Expand Up @@ -260,3 +282,33 @@ func main() {

return content
}

func (r PackageMakeCommandStubs) OldSetup() string {
content := `package main

import (
"os"

"github.com/goravel/framework/packages"
"github.com/goravel/framework/packages/match"
"github.com/goravel/framework/packages/modify"
"github.com/goravel/framework/support/path"
)

func main() {
setup := packages.Setup(os.Args)
setup.Install(
modify.GoFile(path.Config("app.go")).
Find(match.Imports()).Modify(modify.AddImport(setup.Paths().Module().Import())).
Find(match.ProvidersInConfig()).Modify(modify.Register("&DummyName.ServiceProvider{}")),
).Uninstall(
modify.GoFile(path.Config("app.go")).
Find(match.ProvidersInConfig()).Modify(modify.Unregister("&DummyName.ServiceProvider{}")).
Find(match.Imports()).Modify(modify.RemoveImport(setup.Paths().Module().Import())),
).Execute()
}
`
content = strings.ReplaceAll(content, "DummyName", r.name)

return content
}
14 changes: 3 additions & 11 deletions packages/match/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,9 @@ func Rules() []match.GoNode {
}
}

// Deprecated: ProvidersInConfig represents the old logic of registering
// service providers inside the `config/app.go` file.
//
// This pattern is deprecated and will be removed in future versions.
// Please migrate to the new Providers() function-based registration approach.
// NOTE: in the current internal setup files, ProvidersInConfig is only used for
// uninstall (not for install). It will continue to serve as an uninstall-only
// fallback for the next few releases — during which both Providers() and
// ProvidersInConfig matchers will be supported to ensure backward compatibility
// with older versions. After that deprecation period, ProvidersInConfig will
// be removed entirely.
// ProvidersInConfig represents the old logic of registering service providers inside the `config/app.go` file.
// If you are using the new code structure that registers service providers via the Providers() function in the
// bootstrap folder, you should use the Providers function instead.
func ProvidersInConfig() []match.GoNode {
return []match.GoNode{
Func(Ident("init")),
Expand Down
Loading