@@ -2,7 +2,6 @@ package internal
22
33import (
44 "cmp"
5- "codegen/lang"
65 "codegen/tmpl"
76 v2 "codegen/v2"
87 v3 "codegen/v3"
@@ -11,27 +10,24 @@ import (
1110 "github.com/samber/lo"
1211 "slices"
1312 "strings"
14- "text/template"
1513)
1614
1715type Args struct {
18- Name string `json:"name"`
19- Endpoint string `json:"endpoint"`
20- Output string `json:"output"`
21- ClientOutput string `json:"client_output"`
22- Lang string `json:"lang"`
23- Style string `json:"style"`
24- Version string `json:"version"`
16+ Name string `json:"name"`
17+ Endpoint string `json:"endpoint"`
18+ Lang string `json:"lang"`
19+ Version string `json:"version"`
2520}
2621
2722type Env struct {
2823 * 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"`
34- RepeatableOperationId bool `json:"repeatable_operation_id"`
24+ Output map [string ]* tmpl.Output `json:"output"`
25+ Ignore []string `json:"ignore"`
26+ Filter []string `json:"filter"`
27+ Alias Alias `json:"alias"`
28+ Variables map [string ]string `json:"variables"`
29+ Generics * Generics `json:"generics"`
30+ RepeatableOperationId bool `json:"repeatable_operation_id"`
3531}
3632
3733type Alias struct {
@@ -49,29 +45,18 @@ type Generics struct {
4945
5046type Executor struct {
5147 env * Env
52- convert lang.TypeConvert
53- engine * template.Template
48+ convert tmpl.TypeConvert
5449}
5550
5651func New (env * Env , args []string ) (* Executor , error ) {
57-
58- tp , err := tmpl .NewEngine (env .Lang , env .Style )
59- if err != nil {
60- return nil , err
61- }
62-
6352 return & Executor {
64- env : env ,
65- engine : tp ,
53+ env : env ,
54+ convert : tmpl . NewConvert ( env . Lang , env . Alias . Types ) ,
6655 }, nil
6756}
6857
6958func (e * Executor ) Run (cmd * Args ) error {
7059
71- LANG := cmd .Lang
72- typeConvert := lang .NewConvert (LANG , e .env .Alias .Types )
73- e .convert = typeConvert
74-
7560 var outRefs []* tmpl.Ref
7661 var outApis []* tmpl.Api
7762 var err error
@@ -83,36 +68,11 @@ func (e *Executor) Run(cmd *Args) error {
8368 } else {
8469 return errors .New ("invalid version" )
8570 }
86-
8771 if err != nil {
8872 return err
8973 }
9074
91- // 过滤 path
92- filter := e .env .Filter
93- ignore := e .env .Ignore
94- lo .ForEach (outApis , func (item * tmpl.Api , index int ) {
95- item .Paths = lo .Filter (item .Paths , func (p * tmpl.Path , index int ) bool {
96- path := p .Path
97- _ , ignoreMatch := lo .Find (ignore , func (p string ) bool {
98- return strings .HasPrefix (path , p )
99- })
100- if ignoreMatch {
101- return false
102- }
103-
104- if len (filter ) > 0 {
105- _ , filterMatch := lo .Find (filter , func (p string ) bool {
106- return strings .HasPrefix (path , p )
107- })
108- return filterMatch
109- }
110- return true
111- })
112- })
113- outApis = lo .Filter (outApis , func (item * tmpl.Api , index int ) bool {
114- return len (item .Paths ) > 0
115- })
75+ outApis = e .filterApis (outApis )
11676
11777 slices .SortFunc (outRefs , func (a , b * tmpl.Ref ) int {
11878 return cmp .Compare (a .Name , b .Name )
@@ -122,6 +82,43 @@ func (e *Executor) Run(cmd *Args) error {
12282 return cmp .Compare (a .Name , b .Name )
12383 })
12484
85+ outRefs , err = e .buildRefs (outRefs )
86+ if err != nil {
87+ return err
88+ }
89+
90+ outApis , err = e .buildApis (outRefs , outApis )
91+ if err != nil {
92+ return err
93+ }
94+
95+ for name , output := range e .env .Output {
96+
97+ //合并 Variables
98+ for k , v := range e .env .Variables {
99+ if _ , ok := output .Variables [k ]; ok {
100+ continue
101+ }
102+ output .Variables [k ] = v
103+ }
104+
105+ writer := newWriter (name , e .env )
106+ engine , err := tmpl .NewEngine (e .env .Lang , name , output .Template , output .Variables )
107+ if err != nil {
108+ return err
109+ }
110+
111+ err = writer .write (output , outApis , outRefs , engine )
112+ if err != nil {
113+ return err
114+ }
115+ }
116+ return nil
117+ }
118+
119+ func (e * Executor ) buildRefs (outRefs []* tmpl.Ref ) ([]* tmpl.Ref , error ) {
120+ typeConvert := e .convert
121+
125122 //是否需要生成泛型
126123 if e .env .Generics .Enable {
127124
@@ -201,6 +198,48 @@ func (e *Executor) Run(cmd *Args) error {
201198 ref .Alias = cmp .Or (e .env .Alias .Modes [ref .Name ], ref .Name )
202199 }
203200
201+ //有顺序要求,重新排序
202+ slices .SortFunc (outRefs , func (a , b * tmpl.Ref ) int {
203+ return a .ReferenceLevel () - b .ReferenceLevel ()
204+ })
205+
206+ return outRefs , nil
207+ }
208+
209+ func (e * Executor ) filterApis (outApis []* tmpl.Api ) []* tmpl.Api {
210+ // 过滤 path
211+ filter := e .env .Filter
212+ ignore := e .env .Ignore
213+ lo .ForEach (outApis , func (item * tmpl.Api , index int ) {
214+ item .Paths = lo .Filter (item .Paths , func (p * tmpl.Path , index int ) bool {
215+ path := p .Path
216+ _ , ignoreMatch := lo .Find (ignore , func (p string ) bool {
217+ return strings .HasPrefix (path , p )
218+ })
219+ if ignoreMatch {
220+ return false
221+ }
222+
223+ if len (filter ) > 0 {
224+ _ , filterMatch := lo .Find (filter , func (p string ) bool {
225+ return strings .HasPrefix (path , p )
226+ })
227+ return filterMatch
228+ }
229+ return true
230+ })
231+ })
232+
233+ return lo .Filter (outApis , func (item * tmpl.Api , index int ) bool {
234+ return len (item .Paths ) > 0
235+ })
236+ }
237+
238+ func (e * Executor ) buildApis (outRefs []* tmpl.Ref , outApis []* tmpl.Api ) ([]* tmpl.Api , error ) {
239+
240+ LANG := e .env .Lang
241+ typeConvert := e .convert
242+
204243 //查询
205244 findType := func (currenType * tmpl.NamedType ) * tmpl.NamedType {
206245
@@ -228,7 +267,7 @@ func (e *Executor) Run(cmd *Args) error {
228267 })
229268
230269 if len (parameters ) > 0 {
231- return lang .Format (LANG , path .OriginalPath , e .env .Alias .Parameters )
270+ return tmpl .Format (LANG , path .OriginalPath , e .env .Alias .Parameters )
232271 }
233272 return fmt .Sprintf (`"%s"` , path .Path )
234273 }
@@ -294,16 +333,5 @@ func (e *Executor) Run(cmd *Args) error {
294333 api .Paths = paths
295334 }
296335
297- //写入接口文件
298- w := newWriter (e .env , outRefs , outApis )
299-
300- //写入接口
301- err = w .api (e .env .Output , "header" , e .engine )
302- if err != nil {
303- return err
304- }
305-
306- //写入接口适配
307- err = w .client (e .env .ClientOutput , "client" , e .engine )
308- return err
336+ return outApis , nil
309337}
0 commit comments