@@ -43,6 +43,7 @@ type Data struct {
4343 TemplatePath string
4444 DestinationPath string
4545 ClientPackagePath string
46+ PackageName string
4647}
4748
4849var ValidGenerationTargets = map [string ]func (* Data ) error {
@@ -82,12 +83,38 @@ func (cfg *ExtensionsConfig) Validate() error {
8283 )
8384}
8485
86+ func getPackageNameFromPath (clientPath string ) (clientName string , err error ) {
87+ if clientPath == "" {
88+ err = commonerrors .Newf (commonerrors .ErrUnexpected , "missing client package path, provide a client package path using '-c' or '--client_path'" )
89+ return
90+ }
91+ isDir , err := filesystem .IsDir (clientPath )
92+ if err != nil {
93+ return
94+ }
95+
96+ if ! isDir {
97+ err = commonerrors .Newf (commonerrors .ErrUnexpected , "client path '%s' is not a directory" , clientPath )
98+ return
99+ }
100+
101+ clientName = filepath .Base (clientPath )
102+
103+ return
104+ }
105+
85106func GenerateDataStruct (cfg ExtensionsConfig ) (d * Data , err error ) {
107+ clientName , err := getPackageNameFromPath (cfg .ClientPackagePath )
108+ if err != nil {
109+ return
110+ }
111+
86112 return & Data {
87113 DestinationPath : cfg .Output ,
88114 SpecPath : cfg .Input ,
89115 TemplatePath : cfg .Template ,
90116 ClientPackagePath : cfg .ClientPackagePath ,
117+ PackageName : clientName ,
91118 }, nil
92119}
93120
@@ -135,7 +162,7 @@ func generateSourceCode(ctx context.Context, data *Data, template *template.Temp
135162 }
136163
137164 var tempBuffer bytes.Buffer
138- err = template .Execute (& tempBuffer , data . Params )
165+ err = template .Execute (& tempBuffer , data )
139166 if err != nil {
140167 return
141168 }
@@ -200,7 +227,7 @@ func isExtensionFlagSet(props openapi3.ExtensionProps, flagKey string) (isSet bo
200227 return
201228}
202229
203- func CopyStaticFiles (ctx context.Context , destination string ) error {
230+ func CopyStaticFiles (ctx context.Context , clientName string , destination string ) ( err error ) {
204231 efs , fsErr := filesystem .NewEmbedFileSystem (& static )
205232 if fsErr != nil {
206233 return commonerrors .Newf (commonerrors .ErrUnexpected , "failed to create a filesystem for directory `%s`: %s" , destination , fsErr .Error ())
@@ -216,12 +243,27 @@ func CopyStaticFiles(ctx context.Context, destination string) error {
216243 return commonerrors .Newf (commonerrors .ErrUnexpected , "could not create directory `%s`: %s" , destination , mkdirErr .Error ())
217244 }
218245
219- sfs := filesystem .NewStandardFileSystem ()
220246 for _ , f := range files {
247+ t , tmplErr := template .
248+ New (filepath .Base (f )).
249+ ParseFS (static , f )
250+ if tmplErr != nil {
251+ return tmplErr
252+ }
253+
221254 resultFileName := strings .TrimPrefix (f , "static/" ) // The embedded filesystem always uses Unix-style paths, regardless of the target platform
222255 resultFileName = strings .TrimSuffix (resultFileName , ".static" )
223- filesystem .CopyBetweenFS (ctx , efs , f , sfs , filepath .Join (destination , resultFileName ))
256+
257+ d := Data {
258+ DestinationPath : filepath .Join (destination , resultFileName ),
259+ PackageName : clientName ,
260+ }
261+ err = generateSourceCode (ctx , & d , t )
262+ if err != nil {
263+ return
264+ }
265+
224266 }
225267
226- return nil
268+ return
227269}
0 commit comments