Skip to content

Commit

Permalink
chore: optimize make commands (#597)
Browse files Browse the repository at this point in the history
* chore: optimize make commands

* fix test cases

* fix test cases

* fix test cases
  • Loading branch information
hwbrzzl authored Aug 14, 2024
1 parent 1fb6d9a commit 2e271fa
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 644 deletions.
40 changes: 4 additions & 36 deletions auth/console/policy_make_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

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

Expand All @@ -10,7 +9,6 @@ import (
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

type PolicyMakeCommand struct {
Expand Down Expand Up @@ -46,13 +44,13 @@ func (receiver *PolicyMakeCommand) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *PolicyMakeCommand) Handle(ctx console.Context) error {
name, err := supportconsole.GetName(ctx, "policy", ctx.Argument(0), receiver.getPath)
m, err := supportconsole.NewMake(ctx, "policy", ctx.Argument(0), filepath.Join("app", "policies"))
if err != nil {
color.Red().Println(err)
return nil
}

if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil {
return err
}

Expand All @@ -66,39 +64,9 @@ func (receiver *PolicyMakeCommand) getStub() string {
}

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

stub = strings.ReplaceAll(stub, "DummyPolicy", str.Case2Camel(policyName))
func (receiver *PolicyMakeCommand) populateStub(stub string, packageName, structName string) string {
stub = strings.ReplaceAll(stub, "DummyPolicy", structName)
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

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

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
}
40 changes: 4 additions & 36 deletions console/console/make_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

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

Expand All @@ -10,7 +9,6 @@ import (
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

type MakeCommand struct {
Expand Down Expand Up @@ -39,13 +37,13 @@ func (receiver *MakeCommand) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *MakeCommand) Handle(ctx console.Context) error {
name, err := supportconsole.GetName(ctx, "command", ctx.Argument(0), receiver.getPath)
m, err := supportconsole.NewMake(ctx, "command", ctx.Argument(0), filepath.Join("app", "console", "commands"))
if err != nil {
color.Red().Println(err)
return nil
}

if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil {
return err
}

Expand All @@ -59,39 +57,9 @@ func (receiver *MakeCommand) getStub() string {
}

// populateStub Populate the place-holders in the command stub.
func (receiver *MakeCommand) populateStub(stub string, name string) string {
commandName, packageName, _ := receiver.parseName(name)

stub = strings.ReplaceAll(stub, "DummyCommand", str.Case2Camel(commandName))
func (receiver *MakeCommand) populateStub(stub string, packageName, structName string) string {
stub = strings.ReplaceAll(stub, "DummyCommand", structName)
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

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

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
}
39 changes: 4 additions & 35 deletions database/console/factory_make_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

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

Expand All @@ -10,7 +9,6 @@ import (
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

type FactoryMakeCommand struct {
Expand Down Expand Up @@ -46,13 +44,13 @@ func (receiver *FactoryMakeCommand) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *FactoryMakeCommand) Handle(ctx console.Context) error {
name, err := supportconsole.GetName(ctx, "factory", ctx.Argument(0), receiver.getPath)
m, err := supportconsole.NewMake(ctx, "factory", ctx.Argument(0), filepath.Join("database", "factories"))
if err != nil {
color.Red().Println(err)
return nil
}

if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil {
return err
}

Expand All @@ -66,38 +64,9 @@ func (receiver *FactoryMakeCommand) getStub() string {
}

// populateStub Populate the place-holders in the command stub.
func (receiver *FactoryMakeCommand) populateStub(stub string, name string) string {
modelName, packageName, _ := parseName(name, "factories")

stub = strings.ReplaceAll(stub, "DummyFactory", str.Case2Camel(modelName))
func (receiver *FactoryMakeCommand) populateStub(stub string, packageName, structName string) string {
stub = strings.ReplaceAll(stub, "DummyFactory", structName)
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

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

modelName, _, folderPath := parseName(name, "factories")

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

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

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

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

folderPath := ""

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

return modelName, packageName, folderPath
}
40 changes: 4 additions & 36 deletions database/console/model_make_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

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

Expand All @@ -10,7 +9,6 @@ import (
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

type ModelMakeCommand struct {
Expand Down Expand Up @@ -46,13 +44,13 @@ func (receiver *ModelMakeCommand) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *ModelMakeCommand) Handle(ctx console.Context) error {
name, err := supportconsole.GetName(ctx, "model", ctx.Argument(0), receiver.getPath)
m, err := supportconsole.NewMake(ctx, "model", ctx.Argument(0), filepath.Join("app", "models"))
if err != nil {
color.Red().Println(err)
return nil
}

if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil {
return err
}

Expand All @@ -66,39 +64,9 @@ func (receiver *ModelMakeCommand) getStub() string {
}

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

stub = strings.ReplaceAll(stub, "DummyModel", str.Case2Camel(modelName))
func (receiver *ModelMakeCommand) populateStub(stub string, packageName, structName string) string {
stub = strings.ReplaceAll(stub, "DummyModel", structName)
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

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

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
}
40 changes: 4 additions & 36 deletions database/console/observer_make_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package console

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

Expand All @@ -10,7 +9,6 @@ import (
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

type ObserverMakeCommand struct {
Expand Down Expand Up @@ -46,13 +44,13 @@ func (receiver *ObserverMakeCommand) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *ObserverMakeCommand) Handle(ctx console.Context) error {
name, err := supportconsole.GetName(ctx, "observer", ctx.Argument(0), receiver.getPath)
m, err := supportconsole.NewMake(ctx, "observer", ctx.Argument(0), filepath.Join("app", "observers"))
if err != nil {
color.Red().Println(err)
return nil
}

if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil {
return err
}

Expand All @@ -66,39 +64,9 @@ func (receiver *ObserverMakeCommand) getStub() string {
}

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

stub = strings.ReplaceAll(stub, "DummyObserver", str.Case2Camel(observerName))
func (receiver *ObserverMakeCommand) populateStub(stub string, packageName, structName string) string {
stub = strings.ReplaceAll(stub, "DummyObserver", structName)
stub = strings.ReplaceAll(stub, "DummyPackage", packageName)

return stub
}

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

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
}
Loading

0 comments on commit 2e271fa

Please sign in to comment.