Skip to content

Commit 2382ffa

Browse files
committed
init
1 parent e50c9c3 commit 2382ffa

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

cmd/init.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ package cmd
33
import (
44
"codegen/internal"
55
"codegen/lang"
6+
"codegen/tmpl"
67
"encoding/json"
78
"github.com/spf13/cobra"
89
"os"
9-
"path"
1010
"strings"
1111
)
1212

13-
var initArgs = &internal.Args{}
13+
var initArgs = &internal.Args{
14+
Name: "swagger",
15+
Endpoint: "http://localhost:8080/v3/api-docs",
16+
Output: "src/api.ts",
17+
ClientOutput: "src/client.ts",
18+
Lang: "",
19+
Style: "",
20+
Version: "",
21+
}
1422

1523
func init() {
1624
//init
@@ -51,13 +59,15 @@ var initCmd = &cobra.Command{
5159
"structPackage": "",
5260
},
5361
Generics: &internal.Generics{
54-
Enable: false,
55-
Expressions: map[string][]string{},
62+
Enable: false,
63+
Unfold: false,
64+
Expressions: map[string][]string{
65+
"ApiResult": {"data"},
66+
},
5667
},
5768
}
5869
//当前执行目录
59-
pwd, _ := os.Getwd()
60-
envFile := path.Join(pwd, defaultEnvFileName)
70+
envFile := tmpl.PwdJoinPath(defaultEnvFileName)
6171

6272
envs := make([]*internal.Env, 0)
6373
envs = append(envs, defaultEnv)

cmd/reload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package cmd
22

33
import (
44
"codegen/internal"
5+
"codegen/tmpl"
56
"encoding/json"
67
"fmt"
78
"github.com/samber/lo"
89
"github.com/spf13/cobra"
910
"os"
10-
"path"
1111
"strings"
1212
)
1313

@@ -30,9 +30,9 @@ var reloadCmd = &cobra.Command{
3030

3131
//当前执行目录
3232
if envFile == "" {
33-
pwd, _ := os.Getwd()
34-
envFile = path.Join(pwd, defaultEnvFileName)
33+
envFile = defaultEnvFileName
3534
}
35+
envFile = tmpl.PwdJoinPath(envFile)
3636

3737
data, err := os.ReadFile(envFile)
3838
if err != nil {

internal/cmd.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ type Args struct {
2626

2727
type Env struct {
2828
*Args
29-
Ignore []string `json:"ignore"`
30-
Filter []string `json:"filter"`
31-
Alias Alias `json:"alias"`
32-
Variables map[string]string `json:"variables"`
33-
Generics *Generics `json:"generics"`
29+
Ignore []string `json:"ignore"`
30+
Filter []string `json:"filter"`
31+
Alias Alias `json:"alias"`
32+
Variables map[string]string `json:"variables"`
33+
Generics *Generics `json:"generics"`
34+
RepeatableOperationId bool `json:"repeatable_operation_id"`
3435
}
3536

3637
type Alias struct {
@@ -257,7 +258,6 @@ func (e *Executor) Run(cmd *Args) error {
257258
return cmp.Compare(a.Name, b.Name)
258259
})
259260

260-
//统一累计到参数集
261261
if path.Request != nil {
262262
path.Parameters = append(path.Parameters, &tmpl.Parameter{
263263
Name: "body",
@@ -279,6 +279,16 @@ func (e *Executor) Run(cmd *Args) error {
279279
if path.Response != nil {
280280
path.Response = findType(path.Response)
281281
}
282+
283+
//方法名 不同Tag下 方法重名下容易生成 add_1,update_39
284+
path.Name = lo.TernaryF(e.env.RepeatableOperationId, func() string {
285+
return strings.FieldsFunc(path.Name, func(r rune) bool {
286+
return r == '_' || r == '-'
287+
})[0]
288+
}, func() string {
289+
return path.Name
290+
})
291+
282292
}
283293

284294
api.Paths = paths

internal/writer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (w *stdWriter) api(output string, name string, engine *template.Template) e
7777

7878
}
7979

80+
output = tmpl.PwdJoinPath(output)
8081
dir := path.Dir(output)
8182
_ = os.MkdirAll(dir, os.ModePerm)
8283

tmpl/template.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package tmpl
33
import (
44
_ "embed"
55
"errors"
6+
"os"
7+
"path/filepath"
68
"text/template"
79
)
810

@@ -35,7 +37,7 @@ func NewEngine(lang string, style string) (*template.Template, error) {
3537
tp = tp.Funcs(tmplFunc)
3638

3739
if style != "" {
38-
return tp.ParseFiles(style)
40+
return tp.ParseFiles(PwdJoinPath(style))
3941
}
4042

4143
switch lang {
@@ -55,3 +57,11 @@ func NewEngine(lang string, style string) (*template.Template, error) {
5557
return nil, errors.New("invalid language")
5658
}
5759
}
60+
61+
func PwdJoinPath(name string) string {
62+
if filepath.IsAbs(name) {
63+
return filepath.Clean(name)
64+
}
65+
pwd, _ := os.Getwd()
66+
return filepath.Join(pwd, name)
67+
}

0 commit comments

Comments
 (0)