-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
143 lines (133 loc) · 3.06 KB
/
build.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package build
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)
// CmdBuild build project command.
var CmdBuild = &cobra.Command{
Use: "build",
Short: "build project",
Long: "build project. Example: kratos run",
Run: Build,
}
var (
mainFilePath string
buildFilePath string
)
func init() {
CmdBuild.Flags().StringVarP(&mainFilePath, "main-file-path", "m", mainFilePath, "main file path")
CmdBuild.Flags().StringVarP(&buildFilePath, "build-file-path", "b", buildFilePath, "build file path")
}
// Build project.
func Build(_ *cobra.Command, args []string) {
var dir string
if len(args) > 0 {
dir = args[0]
}
base, err := os.Getwd()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
}
if dir == "" {
// find the directory containing the cmd/*
cmdPath, err := findCMD(base)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
}
switch len(cmdPath) {
case 0:
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The cmd directory cannot be found in the current directory")
return
case 1:
for _, v := range cmdPath {
dir = v
}
default:
var cmdPaths []string
for k := range cmdPath {
cmdPaths = append(cmdPaths, k)
}
prompt := &survey.Select{
Message: "Which directory do you want to run?",
Options: cmdPaths,
PageSize: 10,
}
e := survey.AskOne(prompt, &dir)
if e != nil || dir == "" {
return
}
dir = cmdPath[dir]
}
}
if len(mainFilePath) == 0 {
mainFilePath = "main/mono/main.go"
}
if len(buildFilePath) == 0 {
mainFilePath = "bin/mono"
}
fd := exec.Command("go", "build", "-v", "-o", buildFilePath, mainFilePath)
fd.Stdout = os.Stdout
fd.Stderr = os.Stderr
fd.Dir = dir
if err := fd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error())
return
}
}
func findCMD(base string) (map[string]string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
if !strings.HasSuffix(wd, "/") {
wd += "/"
}
var root bool
next := func(dir string) (map[string]string, error) {
cmdPath := make(map[string]string)
err = filepath.Walk(dir, func(walkPath string, info os.FileInfo, err error) error {
// multi level directory is not allowed under the cmdPath directory, so it is judged that the path ends with cmdPath.
if strings.HasSuffix(walkPath, "cmd") {
paths, err := os.ReadDir(walkPath)
if err != nil {
return err
}
for _, fileInfo := range paths {
if fileInfo.IsDir() {
abs := path.Join(walkPath, fileInfo.Name())
cmdPath[strings.TrimPrefix(abs, wd)] = abs
}
}
return nil
}
if info.Name() == "go.mod" {
root = true
}
return nil
})
return cmdPath, err
}
for i := 0; i < 5; i++ {
tmp := base
cmd, err := next(tmp)
if err != nil {
return nil, err
}
if len(cmd) > 0 {
return cmd, nil
}
if root {
break
}
_ = filepath.Join(base, "..")
}
return map[string]string{"": base}, nil
}