Skip to content

Commit

Permalink
Feat: [#49] Support Package Development
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Jun 2, 2023
1 parent 92ccb16 commit 61168ad
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 21 deletions.
9 changes: 9 additions & 0 deletions contracts/foundation/application.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package foundation

import (
"github.com/goravel/framework/contracts/console"
)

//go:generate mockery --name=Application
type Application interface {
Container
Boot()
Commands([]console.Command)
ConfigPath(path string) string
DatabasePath(path string) string
PublicPath(path string) string
Publishes(packageName string, paths map[string]string, groups ...string)
}
67 changes: 64 additions & 3 deletions contracts/foundation/mocks/Application.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 65 additions & 3 deletions foundation/application.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package foundation

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/goravel/framework/config"
consolecontract "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/foundation/console"
"github.com/goravel/framework/support"
)

var App foundation.Application
var (
App foundation.Application
)

func init() {
setEnv()

app := &Application{Container: NewContainer()}
app := &Application{
Container: NewContainer(),
publishes: make(map[string]map[string]string),
publishGroups: make(map[string]map[string]string),
}
app.registerBaseServiceProviders()
app.bootBaseServiceProviders()
App = app
}

type Application struct {
foundation.Container
publishes map[string]map[string]string
publishGroups map[string]map[string]string
}

func NewApplication() foundation.Application {
Expand All @@ -32,11 +44,57 @@ func NewApplication() foundation.Application {
func (app *Application) Boot() {
app.registerConfiguredServiceProviders()
app.bootConfiguredServiceProviders()

app.registerCommands([]consolecontract.Command{
console.NewVendorPublishCommand(app.publishes, app.publishGroups),
})
app.bootArtisan()
setRootPath()
}

func (app *Application) Commands(commands []consolecontract.Command) {
app.registerCommands(commands)
}

func (app *Application) ConfigPath(path string) string {
return fmt.Sprintf("config%s%s", string(filepath.Separator), path)
}

func (app *Application) DatabasePath(path string) string {
return fmt.Sprintf("database%s%s", string(filepath.Separator), path)
}

func (app *Application) PublicPath(path string) string {
return fmt.Sprintf("public%s%s", string(filepath.Separator), path)
}

func (app *Application) Publishes(packageName string, paths map[string]string, groups ...string) {
app.ensurePublishArrayInitialized(packageName)

for key, value := range paths {
app.publishes[packageName][key] = value
}

for _, group := range groups {
app.addPublishGroup(group, paths)
}
}

func (app *Application) ensurePublishArrayInitialized(packageName string) {
if _, exist := app.publishes[packageName]; !exist {
app.publishes[packageName] = make(map[string]string)
}
}

func (app *Application) addPublishGroup(group string, paths map[string]string) {
if _, exist := app.publishGroups[group]; !exist {
app.publishGroups[group] = make(map[string]string)
}

for key, value := range paths {
app.publishGroups[group][key] = value
}
}

//bootArtisan Boot artisan command.
func (app *Application) bootArtisan() {
app.MakeArtisan().Run(os.Args, true)
Expand Down Expand Up @@ -88,6 +146,10 @@ func (app *Application) bootServiceProviders(serviceProviders []foundation.Servi
}
}

func (app *Application) registerCommands(commands []consolecontract.Command) {
app.MakeArtisan().Register(commands)
}

func setEnv() {
args := os.Args
if strings.HasSuffix(os.Args[0], ".test") {
Expand Down
67 changes: 67 additions & 0 deletions foundation/application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package foundation

import (
"testing"

"github.com/stretchr/testify/suite"
)

type ApplicationTestSuite struct {
suite.Suite
app *Application
}

func TestApplicationTestSuite(t *testing.T) {
suite.Run(t, new(ApplicationTestSuite))
}

func (s *ApplicationTestSuite) SetupTest() {
s.app = &Application{
publishes: make(map[string]map[string]string),
publishGroups: make(map[string]map[string]string),
}
}

func (s *ApplicationTestSuite) TestConfigPath() {
s.Equal("config/goravel.go", s.app.ConfigPath("goravel.go"))
}

func (s *ApplicationTestSuite) TestDatabasePath() {
s.Equal("database/goravel.go", s.app.DatabasePath("goravel.go"))
}

func (s *ApplicationTestSuite) TestPublicPath() {
s.Equal("public/goravel.go", s.app.PublicPath("goravel.go"))
}

func (s *ApplicationTestSuite) TestPublishes() {
s.app.Publishes("github.com/goravel/sms", map[string]string{
"config.go": "config.go",
})
s.Equal(1, len(s.app.publishes["github.com/goravel/sms"]))
s.Equal(0, len(s.app.publishGroups))

s.app.Publishes("github.com/goravel/sms", map[string]string{
"config.go": "config1.go",
"config1.go": "config1.go",
}, "public", "private")
s.Equal(2, len(s.app.publishes["github.com/goravel/sms"]))
s.Equal("config1.go", s.app.publishes["github.com/goravel/sms"]["config.go"])
s.Equal(2, len(s.app.publishGroups["public"]))
s.Equal("config1.go", s.app.publishes["public"]["config.go"])
s.Equal(2, len(s.app.publishGroups["private"]))
}

func (s *ApplicationTestSuite) TestAddPublishGroup() {
s.app.addPublishGroup("public", map[string]string{
"config.go": "config.go",
})
s.Equal(1, len(s.app.publishGroups["public"]))

s.app.addPublishGroup("public", map[string]string{
"config.go": "config1.go",
"config1.go": "config1.go",
})
s.Equal(2, len(s.app.publishGroups["public"]))
s.Equal("config1.go", s.app.publishGroups["public"]["config.go"])
}
Loading

0 comments on commit 61168ad

Please sign in to comment.