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: make command add subdirectories support #176

Merged
merged 14 commits into from
Jun 11, 2023
41 changes: 33 additions & 8 deletions auth/console/policy_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package console
import (
"errors"
"os"
"path/filepath"
"strings"

"github.com/gookit/color"
Expand All @@ -20,24 +21,24 @@ func NewPolicyMakeCommand() *PolicyMakeCommand {
return &PolicyMakeCommand{}
}

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

//Description The console command description.
// Description The console command description.
func (receiver *PolicyMakeCommand) Description() string {
return "Create a new policy class"
}

//Extend The console command extend.
// Extend The console command extend.
func (receiver *PolicyMakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
}
}

//Handle Execute the console command.
// Handle Execute the console command.
func (receiver *PolicyMakeCommand) Handle(ctx console.Context) error {
name := ctx.Argument(0)
if name == "" {
Expand All @@ -57,16 +58,40 @@ func (receiver *PolicyMakeCommand) getStub() string {
return PolicyStubs{}.Policy()
}

//populateStub Populate the place-holders in the command stub.
// populateStub Populate the place-holders in the command stub.
func (receiver *PolicyMakeCommand) populateStub(stub string, name string) string {
stub = strings.ReplaceAll(stub, "DummyPolicy", str.Case2Camel(name))
policyName, packageName, _ := receiver.parseName(name)

stub = strings.ReplaceAll(stub, "DummyPolicy", str.Case2Camel(policyName))
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

//getPath Get the full path to the command.
// getPath Get the full path to the command.
func (receiver *PolicyMakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()

return pwd + "/app/policies/" + str.Camel2Case(name) + ".go"
policyName, _, folderPath := receiver.parseName(name)

return filepath.Join(pwd, "app", "policies", folderPath, str.Camel2Case(policyName)+".go")
}

// parseName Parse the name to get the policy name, package name and folder path.
func (receiver *PolicyMakeCommand) parseName(name string) (string, string, string) {
name = strings.TrimSuffix(name, ".go")

segments := strings.Split(name, "/")

policyName := segments[len(segments)-1]

packageName := "policies"
folderPath := ""

if len(segments) > 1 {
folderPath = filepath.Join(segments[:len(segments)-1]...)
packageName = segments[len(segments)-2]
}

return policyName, packageName, folderPath
}
8 changes: 8 additions & 0 deletions auth/console/policy_make_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ func TestEventMakeCommand(t *testing.T) {
err = policyMakeCommand.Handle(mockContext)
assert.Nil(t, err)
assert.True(t, file.Exists("app/policies/user_policy.go"))

mockContext.On("Argument", 0).Return("User/AuthPolicy").Once()
err = policyMakeCommand.Handle(mockContext)
assert.Nil(t, err)
assert.True(t, file.Exists("app/policies/User/auth_policy.go"))
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
assert.True(t, file.Contain("app/policies/User/auth_policy.go", "package User"))
assert.True(t, file.Contain("app/policies/User/auth_policy.go", "type AuthPolicy struct {"))

assert.True(t, file.Remove("app"))
}
2 changes: 1 addition & 1 deletion auth/console/policy_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type PolicyStubs struct {
}

func (receiver PolicyStubs) Policy() string {
return `package policies
return `package DummyPackage

import (
"context"
Expand Down
43 changes: 35 additions & 8 deletions console/console/make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package console
import (
"errors"
"os"
"path/filepath"
"strings"

"github.com/goravel/framework/contracts/console"
Expand All @@ -18,24 +19,24 @@ func NewMakeCommand() *MakeCommand {
return &MakeCommand{}
}

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

//Description The console command description.
// Description The console command description.
func (receiver *MakeCommand) Description() string {
return "Create a new Artisan command"
}

//Extend The console command extend.
// Extend The console command extend.
func (receiver *MakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
}
}

//Handle Execute the console command.
// Handle Execute the console command.
func (receiver *MakeCommand) Handle(ctx console.Context) error {
name := ctx.Argument(0)
if name == "" {
Expand All @@ -49,14 +50,40 @@ func (receiver *MakeCommand) getStub() string {
return Stubs{}.Command()
}

//populateStub Populate the place-holders in the command stub.
// populateStub Populate the place-holders in the command stub.
func (receiver *MakeCommand) populateStub(stub string, name string) string {
return strings.ReplaceAll(stub, "DummyCommand", str.Case2Camel(name))
commandName, packageName, _ := receiver.parseName(name)

stub = strings.ReplaceAll(stub, "DummyCommand", str.Case2Camel(commandName))
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

//getPath Get the full path to the command.
// getPath Get the full path to the command.
func (receiver *MakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()

return pwd + "/app/console/commands/" + str.Camel2Case(name) + ".go"
commandName, _, folderPath := receiver.parseName(name)

return filepath.Join(pwd, "app", "console", "commands", folderPath, str.Camel2Case(commandName)+".go")
}

// parseName Parse the name to get the command name, package name and folder path.
func (receiver *MakeCommand) parseName(name string) (string, string, string) {
name = strings.TrimSuffix(name, ".go")

segments := strings.Split(name, "/")

commandName := segments[len(segments)-1]

packageName := "commands"
folderPath := ""

if len(segments) > 1 {
folderPath = filepath.Join(segments[:len(segments)-1]...)
packageName = segments[len(segments)-2]
}

return commandName, packageName, folderPath
}
26 changes: 26 additions & 0 deletions console/console/make_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package console

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestMakeCommand(t *testing.T) {
makeCommand := &MakeCommand{}
mockContext := &consolemocks.Context{}
mockContext.On("Argument", 0).Return("CleanCache").Once()
assert.Nil(t, makeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/console/commands/clean_cache.go"))

mockContext.On("Argument", 0).Return("Goravel/CleanCache").Once()
assert.Nil(t, makeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/console/commands/Goravel/clean_cache.go"))
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
assert.True(t, file.Contain("app/console/commands/Goravel/clean_cache.go", "package Goravel"))
assert.True(t, file.Contain("app/console/commands/Goravel/clean_cache.go", "type CleanCache struct"))

assert.True(t, file.Remove("app"))
}
2 changes: 1 addition & 1 deletion console/console/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Stubs struct {
}

func (receiver Stubs) Command() string {
return `package commands
return `package DummyPackage

import (
"github.com/goravel/framework/contracts/console"
Expand Down
29 changes: 27 additions & 2 deletions database/console/model_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package console

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

"github.com/gookit/color"
Expand Down Expand Up @@ -60,7 +61,10 @@ func (receiver *ModelMakeCommand) getStub() string {

// populateStub Populate the place-holders in the command stub.
func (receiver *ModelMakeCommand) populateStub(stub string, name string) string {
stub = strings.ReplaceAll(stub, "DummyModel", str.Case2Camel(name))
modelName, packageName, _ := receiver.parseName(name)

stub = strings.ReplaceAll(stub, "DummyModel", str.Case2Camel(modelName))
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}
Expand All @@ -69,5 +73,26 @@ func (receiver *ModelMakeCommand) populateStub(stub string, name string) string
func (receiver *ModelMakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()

return pwd + "/app/models/" + str.Camel2Case(name) + ".go"
modelName, _, folderPath := receiver.parseName(name)

return filepath.Join(pwd, "app", "models", folderPath, str.Camel2Case(modelName)+".go")
}

// parseName Parse the name to get the model name, package name and folder path.
func (receiver *ModelMakeCommand) parseName(name string) (string, string, string) {
name = strings.TrimSuffix(name, ".go")

segments := strings.Split(name, "/")

modelName := segments[len(segments)-1]

packageName := "models"
folderPath := ""

if len(segments) > 1 {
folderPath = filepath.Join(segments[:len(segments)-1]...)
packageName = segments[len(segments)-2]
}

return modelName, packageName, folderPath
}
7 changes: 7 additions & 0 deletions database/console/model_make_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ func TestModelMakeCommand(t *testing.T) {
mockContext.On("Argument", 0).Return("User").Once()
assert.Nil(t, modelMakeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/models/user.go"))

mockContext.On("Argument", 0).Return("User/Phone").Once()
assert.Nil(t, modelMakeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/models/User/phone.go"))
assert.True(t, file.Contain("app/models/User/phone.go", "package User"))
assert.True(t, file.Contain("app/models/User/phone.go", "type Phone struct"))

assert.True(t, file.Remove("app"))
}
29 changes: 27 additions & 2 deletions database/console/observer_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package console

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

"github.com/gookit/color"
Expand Down Expand Up @@ -60,7 +61,10 @@ func (receiver *ObserverMakeCommand) getStub() string {

// populateStub Populate the place-holders in the command stub.
func (receiver *ObserverMakeCommand) populateStub(stub string, name string) string {
stub = strings.ReplaceAll(stub, "DummyObserver", str.Case2Camel(name))
observerName, packageName, _ := receiver.parseName(name)

stub = strings.ReplaceAll(stub, "DummyObserver", str.Case2Camel(observerName))
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}
Expand All @@ -69,5 +73,26 @@ func (receiver *ObserverMakeCommand) populateStub(stub string, name string) stri
func (receiver *ObserverMakeCommand) getPath(name string) string {
pwd, _ := os.Getwd()

return pwd + "/app/observers/" + str.Camel2Case(name) + ".go"
observerName, _, folderPath := receiver.parseName(name)

return filepath.Join(pwd, "app", "observers", folderPath, str.Camel2Case(observerName)+".go")
}

// parseName Parse the name to get the observer name, package name and folder path.
func (receiver *ObserverMakeCommand) parseName(name string) (string, string, string) {
name = strings.TrimSuffix(name, ".go")

segments := strings.Split(name, "/")

observerName := segments[len(segments)-1]

packageName := "observers"
folderPath := ""

if len(segments) > 1 {
folderPath = filepath.Join(segments[:len(segments)-1]...)
packageName = segments[len(segments)-2]
}

return observerName, packageName, folderPath
}
7 changes: 7 additions & 0 deletions database/console/observer_make_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ func TestObserverMakeCommand(t *testing.T) {
mockContext.On("Argument", 0).Return("UserObserver").Once()
assert.Nil(t, observerMakeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/observers/user_observer.go"))

mockContext.On("Argument", 0).Return("User/PhoneObserver").Once()
assert.Nil(t, observerMakeCommand.Handle(mockContext))
assert.True(t, file.Exists("app/observers/User/phone_observer.go"))
assert.True(t, file.Contain("app/observers/User/phone_observer.go", "package User"))
assert.True(t, file.Contain("app/observers/User/phone_observer.go", "type PhoneObserver struct"))

assert.True(t, file.Remove("app"))
}
4 changes: 2 additions & 2 deletions database/console/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Stubs struct {
}

func (r Stubs) Model() string {
return `package models
return `package DummyPackage

import (
"github.com/goravel/framework/database/orm"
Expand All @@ -17,7 +17,7 @@ type DummyModel struct {
}

func (r Stubs) Observer() string {
return `package observers
return `package DummyPackage

import (
"github.com/goravel/framework/contracts/database/orm"
Expand Down
Loading