github.com/goravel/framework@v1.13.9/foundation/console/package_make_command.go (about)

     1  package console
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/gookit/color"
     8  
     9  	"github.com/goravel/framework/contracts/console"
    10  	"github.com/goravel/framework/contracts/console/command"
    11  	"github.com/goravel/framework/support/file"
    12  )
    13  
    14  type PackageMakeCommand struct{}
    15  
    16  func NewPackageMakeCommand() *PackageMakeCommand {
    17  	return &PackageMakeCommand{}
    18  }
    19  
    20  // Signature The name and signature of the console command.
    21  func (receiver *PackageMakeCommand) Signature() string {
    22  	return "make:package"
    23  }
    24  
    25  // Description The console command description.
    26  func (receiver *PackageMakeCommand) Description() string {
    27  	return "Create a package template"
    28  }
    29  
    30  // Extend The console command extend.
    31  func (receiver *PackageMakeCommand) Extend() command.Extend {
    32  	return command.Extend{
    33  		Category: "make",
    34  		Flags: []command.Flag{
    35  			&command.StringFlag{
    36  				Name:    "root",
    37  				Aliases: []string{"r"},
    38  				Usage:   "The root path of package, default: packages",
    39  				Value:   "packages",
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  // Handle Execute the console command.
    46  func (receiver *PackageMakeCommand) Handle(ctx console.Context) error {
    47  	pkg := ctx.Argument(0)
    48  	if pkg == "" {
    49  		color.Redln("Not enough arguments (missing: name)")
    50  
    51  		return nil
    52  	}
    53  
    54  	pkg = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(pkg, "/", "_"), "-", "_"), ".", "_")
    55  	root := ctx.Option("root") + "/" + pkg
    56  	if file.Exists(root) {
    57  		color.Redf("Package %s already exists\n", pkg)
    58  
    59  		return nil
    60  	}
    61  
    62  	packageName := packageName(pkg)
    63  	packageMakeCommandStubs := NewPackageMakeCommandStubs(pkg, root)
    64  	files := map[string]func() string{
    65  		"README.md":                        packageMakeCommandStubs.Readme,
    66  		"service_provider.go":              packageMakeCommandStubs.ServiceProvider,
    67  		packageName + ".go":                packageMakeCommandStubs.Main,
    68  		"config/" + packageName + ".go":    packageMakeCommandStubs.Config,
    69  		"contracts/" + packageName + ".go": packageMakeCommandStubs.Contracts,
    70  		"facades/" + packageName + ".go":   packageMakeCommandStubs.Facades,
    71  	}
    72  
    73  	for path, content := range files {
    74  		if err := file.Create(filepath.Join(root, path), content()); err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	color.Green.Printf("Package created successfully: %s\n", root)
    80  
    81  	return nil
    82  }
    83  
    84  func packageName(name string) string {
    85  	nameSlice := strings.Split(name, "/")
    86  	lastName := nameSlice[len(nameSlice)-1]
    87  
    88  	return strings.ReplaceAll(strings.ReplaceAll(lastName, "-", "_"), ".", "_")
    89  }