Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add make package command #162

Merged
merged 2 commits into from
Jun 5, 2023
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
1 change: 1 addition & 0 deletions foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (app *Application) Boot() {
app.registerConfiguredServiceProviders()
app.bootConfiguredServiceProviders()
app.registerCommands([]consolecontract.Command{
console.NewPackageMakeCommand(),
console.NewVendorPublishCommand(app.publishes, app.publishGroups),
})
app.bootArtisan()
Expand Down
89 changes: 89 additions & 0 deletions foundation/console/package_make_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package console

import (
"path/filepath"
"strings"

"github.com/gookit/color"

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/file"
)

type PackageMakeCommand struct{}

func NewPackageMakeCommand() *PackageMakeCommand {
return &PackageMakeCommand{}
}

//Signature The name and signature of the console command.
func (receiver *PackageMakeCommand) Signature() string {
return "make:package"
}

//Description The console command description.
func (receiver *PackageMakeCommand) Description() string {
return "Create a package template"
}

//Extend The console command extend.
func (receiver *PackageMakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
Flags: []command.Flag{
&command.StringFlag{
Name: "root",
Aliases: []string{"r"},
Usage: "The root path of package, default: packages",
Value: "packages",
},
},
}
}

//Handle Execute the console command.
func (receiver *PackageMakeCommand) Handle(ctx console.Context) error {
pkg := ctx.Argument(0)
if pkg == "" {
color.Redln("Not enough arguments (missing: name)")

return nil
}

pkg = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(pkg, "/", "_"), "-", "_"), ".", "_")
root := filepath.Join(ctx.Option("root"), pkg)
if file.Exists(root) {
color.Redf("Package %s already exists\n", pkg)

return nil
}

packageName := packageName(pkg)
packageMakeCommandStubs := NewPackageMakeCommandStubs(pkg, root)
files := map[string]func() string{
"README.md": packageMakeCommandStubs.Readme,
"service_provider.go": packageMakeCommandStubs.ServiceProvider,
packageName + ".go": packageMakeCommandStubs.Main,
"config/" + packageName + ".go": packageMakeCommandStubs.Config,
"contracts/" + packageName + ".go": packageMakeCommandStubs.Contracts,
"facades/" + packageName + ".go": packageMakeCommandStubs.Facades,
}

for path, content := range files {
if err := file.Create(filepath.Join(root, path), content()); err != nil {
return err
}
}

color.Green.Printf("Package created successfully: /%s\n", root)

return nil
}

func packageName(name string) string {
nameSlice := strings.Split(name, "/")
lastName := nameSlice[len(nameSlice)-1]

return strings.ReplaceAll(strings.ReplaceAll(lastName, "-", "_"), ".", "_")
}
124 changes: 124 additions & 0 deletions foundation/console/package_make_command_stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package console

import (
"strings"

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

type PackageMakeCommandStubs struct {
pkg string
root string
name string
}

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

func (r PackageMakeCommandStubs) Readme() string {
content := `# DummyName
`

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

func (r PackageMakeCommandStubs) ServiceProvider() string {
content := `package DummyName

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

const Binding = "DummyPackage"

var App foundation.Application

type ServiceProvider struct {
}

func (receiver *ServiceProvider) Register(app foundation.Application) {
App = app

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

func (receiver *ServiceProvider) Boot(app foundation.Application) {

}
`

content = strings.ReplaceAll(content, "DummyPackage", r.pkg)
content = strings.ReplaceAll(content, "DummyName", r.name)

return content
}

func (r PackageMakeCommandStubs) Main() string {
content := `package DummyName

type DummyCamelName struct {}
`

content = strings.ReplaceAll(content, "DummyName", r.name)
content = strings.ReplaceAll(content, "DummyCamelName", str.Case2Camel(r.name))

return content
}

func (r PackageMakeCommandStubs) Config() 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

type DummyCamelName interface {}
`

return strings.ReplaceAll(content, "DummyCamelName", str.Case2Camel(r.name))
}

func (r PackageMakeCommandStubs) Facades() string {
content := `package facades

import (
"log"

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

func DummyCamelName() contracts.DummyCamelName {
instance, err := DummyName.App.Make(DummyName.Binding)
if err != nil {
log.Fatalln(err)
return nil
}

return instance.(contracts.DummyCamelName)
}
`

content = strings.ReplaceAll(content, "DummyRoot", r.root)
content = strings.ReplaceAll(content, "DummyName", r.name)
content = strings.ReplaceAll(content, "DummyCamelName", str.Case2Camel(r.name))

return content
}
88 changes: 88 additions & 0 deletions foundation/console/package_make_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package console

import (
"testing"

"github.com/stretchr/testify/assert"

consolemocks "github.com/goravel/framework/contracts/console/mocks"
"github.com/goravel/framework/support/file"
)

func TestPackageMakeCommand(t *testing.T) {
var (
mockContext *consolemocks.Context
)

beforeEach := func() {
mockContext = &consolemocks.Context{}
}

tests := []struct {
name string
setup func()
assert func()
}{
{
name: "name is empty",
setup: func() {
mockContext.On("Argument", 0).Return("").Once()
},
},
{
name: "name is sms and use default root",
setup: func() {
mockContext.On("Argument", 0).Return("sms").Once()
mockContext.On("Option", "root").Return("packages").Once()
},
assert: func() {
assert.True(t, file.Exists("packages/sms/README.md"))
assert.True(t, file.Exists("packages/sms/service_provider.go"))
assert.True(t, file.Exists("packages/sms/sms.go"))
assert.True(t, file.Exists("packages/sms/config/sms.go"))
assert.True(t, file.Exists("packages/sms/contracts/sms.go"))
assert.True(t, file.Exists("packages/sms/facades/sms.go"))

file.Remove("packages")
},
},
{
name: "name is github.com/goravel/sms and use other root",
setup: func() {
mockContext.On("Argument", 0).Return("github.com/goravel/sms-aws").Once()
mockContext.On("Option", "root").Return("package").Once()
},
assert: func() {
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/README.md"))
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/service_provider.go"))
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/github_com_goravel_sms_aws.go"))
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/config/github_com_goravel_sms_aws.go"))
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/contracts/github_com_goravel_sms_aws.go"))
assert.True(t, file.Exists("package/github_com_goravel_sms_aws/facades/github_com_goravel_sms_aws.go"))

file.Remove("package")
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
beforeEach()
test.setup()
_ = NewPackageMakeCommand().Handle(mockContext)
if test.assert != nil {
test.assert()
}
mockContext.AssertExpectations(t)
})
}
}

func TestPackageName(t *testing.T) {
input := "github.com/example/package-name"
expected := "package_name"
assert.Equal(t, expected, packageName(input))

input2 := "example.com/another_package.name"
expected2 := "another_package_name"
assert.Equal(t, expected2, packageName(input2))
}