-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathpackage_make_command.go
89 lines (71 loc) · 2.38 KB
/
package_make_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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, "-", "_"), ".", "_")
}