Skip to content

Commit 69dc1c6

Browse files
committed
Implement logic to exclude the copying of defined static files
1 parent 02cee1b commit 69dc1c6

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

.github/workflows/update-client.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ jobs:
9393
run: |
9494
cd generator
9595
go mod tidy
96-
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_entities.gen.go -t templates/entities.go.tmpl -g collections -c ../client -d false
97-
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_jobs.gen.go -t templates/jobs.go.tmpl -g jobs -c ../client -d false
98-
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_link_followers.gen.go -t templates/linkfollowers.go.tmpl -g links -c ../client -d false
96+
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_entities.gen.go -t templates/entities.go.tmpl -g collections -c ../client
97+
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_jobs.gen.go -t templates/jobs.go.tmpl -g jobs -c ../client
98+
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_link_followers.gen.go -t templates/linkfollowers.go.tmpl -g links -c ../client
9999
- name: Copy extensions to client
100100
run: |
101101
if [ -d ./extensions/ ] ; then cp -r extensions/. ${{ env.go_module }}/ ; fi

generator/cmd/root.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"path/filepath"
87

98
"github.com/spf13/cobra"
109
"github.com/spf13/viper"
@@ -16,13 +15,13 @@ import (
1615
)
1716

1817
const (
19-
app = "generate_code"
20-
inputArg = "input"
21-
outputArg = "output"
22-
templateArg = "template"
23-
GenerateType = "generate"
24-
clientPath = "client_path"
25-
disableLinks = "disable_links" // TODO: Naming
18+
app = "generate_code"
19+
inputArg = "input"
20+
outputArg = "output"
21+
templateArg = "template"
22+
GenerateType = "generate"
23+
clientPath = "client-path"
24+
disableLinksArg = "disable-links"
2625
)
2726

2827
var (
@@ -50,14 +49,14 @@ func init() {
5049
rootCmd.Flags().StringP(templateArg, "t", "", "path to the template file if defaults need overriding")
5150
rootCmd.Flags().StringP(GenerateType, "g", "", "what to generate")
5251
rootCmd.Flags().StringP(clientPath, "c", "", "path to the client go package generated by OpenAPI Generator")
53-
rootCmd.Flags().StringP(disableLinks, "d", "", "disable the generation of links")
52+
rootCmd.Flags().StringP(disableLinksArg, "d", "", "disable the generation of HAL link data")
5453

5554
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_INPUT", rootCmd.Flags().Lookup(inputArg))
5655
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_OUTPUT", rootCmd.Flags().Lookup(outputArg))
5756
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_TEMPLATE", rootCmd.Flags().Lookup(templateArg))
5857
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_GENERATE_TYPE", rootCmd.Flags().Lookup(GenerateType))
5958
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_CLIENT_PATH", rootCmd.Flags().Lookup(clientPath))
60-
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_DISABLE_LINKS", rootCmd.Flags().Lookup(disableLinks))
59+
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_DISABLE_LINKS", rootCmd.Flags().Lookup(disableLinksArg))
6160
}
6261

6362
func RunCLI(ctx context.Context) (err error) {
@@ -84,7 +83,12 @@ func RunCLI(ctx context.Context) (err error) {
8483
return
8584
}
8685

87-
err = codegen.CopyStaticFiles(ctx, d.PackageName, filepath.Dir(d.DestinationPath))
86+
staticFileConfig, err := codegen.GenerateStaticFileConfigStruct(extensionConfig)
87+
if err != nil {
88+
return
89+
}
90+
91+
err = codegen.CopyStaticFiles(ctx, staticFileConfig)
8892
if err != nil {
8993
return
9094
}

generator/codegen/common.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/getkin/kin-openapi/openapi3"
1515
"golang.org/x/tools/imports"
1616

17+
"slices"
18+
1719
"github.com/ARM-software/golang-utils/utils/commonerrors"
1820
configUtils "github.com/ARM-software/golang-utils/utils/config"
1921
"github.com/ARM-software/golang-utils/utils/filesystem"
@@ -44,6 +46,7 @@ type Data struct {
4446
DestinationPath string
4547
ClientPackagePath string
4648
PackageName string
49+
DisableLinks bool
4750
}
4851

4952
var ValidGenerationTargets = map[string]func(*Data) error{
@@ -58,6 +61,13 @@ type ExtensionsConfig struct {
5861
Template string `mapstructure:"template"`
5962
GenerateType string `mapstructure:"generate_type"`
6063
ClientPackagePath string `mapstructure:"client_path"`
64+
DisableLinks bool `mapstructure:"disable_links"`
65+
}
66+
67+
type StaticFileConfig struct {
68+
ClientName string
69+
Destination string
70+
Exclusions []string
6171
}
6272

6373
func DefaultExtensionsConfig() *ExtensionsConfig {
@@ -118,6 +128,24 @@ func GenerateDataStruct(cfg ExtensionsConfig) (d *Data, err error) {
118128
}, nil
119129
}
120130

131+
func GenerateStaticFileConfigStruct(cfg ExtensionsConfig) (d *StaticFileConfig, err error) {
132+
clientName, err := getPackageNameFromPath(cfg.ClientPackagePath)
133+
if err != nil {
134+
return
135+
}
136+
137+
var exclusions []string
138+
if cfg.DisableLinks {
139+
exclusions = append(exclusions, "static/extension_model_hal_link_data.go.static")
140+
}
141+
142+
return &StaticFileConfig{
143+
ClientName: clientName,
144+
Destination: filepath.Dir(cfg.Output),
145+
Exclusions: exclusions,
146+
}, nil
147+
}
148+
121149
func GenerateTemplateFile(ctx context.Context, d *Data) (err error) {
122150
t, err := template.
123151
New(filepath.Base(d.TemplatePath)).
@@ -227,23 +255,28 @@ func isExtensionFlagSet(props openapi3.ExtensionProps, flagKey string) (isSet bo
227255
return
228256
}
229257

230-
func CopyStaticFiles(ctx context.Context, clientName string, destination string) (err error) {
258+
func CopyStaticFiles(ctx context.Context, staticFileConfig *StaticFileConfig) (err error) {
231259
efs, fsErr := filesystem.NewEmbedFileSystem(&static)
232260
if fsErr != nil {
233-
return commonerrors.Newf(commonerrors.ErrUnexpected, "failed to create a filesystem for directory `%s`: %s", destination, fsErr.Error())
261+
return commonerrors.Newf(commonerrors.ErrUnexpected, "failed to create a filesystem for directory `%s`: %s", staticFileConfig.Destination, fsErr.Error())
234262
}
235263

236264
files, lsErr := efs.FindAll(".", "go.static")
237265
if lsErr != nil {
238-
return commonerrors.Newf(commonerrors.ErrUnexpected, "no files with the '.go.static' extension were found in the directory `%s`", destination)
266+
return commonerrors.Newf(commonerrors.ErrUnexpected, "no files with the '.go.static' extension were found in the directory `%s`", staticFileConfig.Destination)
239267
}
240268

241-
mkdirErr := filesystem.MkDir(destination)
269+
mkdirErr := filesystem.MkDir(staticFileConfig.Destination)
242270
if mkdirErr != nil {
243-
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", destination, mkdirErr.Error())
271+
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", staticFileConfig.Destination, mkdirErr.Error())
244272
}
245273

246274
for _, f := range files {
275+
// Skip the file if it's contained in exclusions
276+
if slices.Contains(staticFileConfig.Exclusions, f) {
277+
continue
278+
}
279+
247280
t, tmplErr := template.
248281
New(filesystem.FilePathBase(efs, f)).
249282
ParseFS(static, f)
@@ -255,8 +288,8 @@ func CopyStaticFiles(ctx context.Context, clientName string, destination string)
255288
resultFileName = strings.TrimSuffix(resultFileName, ".static")
256289

257290
d := Data{
258-
DestinationPath: filepath.Join(destination, resultFileName),
259-
PackageName: clientName,
291+
DestinationPath: filepath.Join(staticFileConfig.Destination, resultFileName),
292+
PackageName: staticFileConfig.ClientName,
260293
}
261294
err = generateSourceCode(ctx, &d, t)
262295
if err != nil {

0 commit comments

Comments
 (0)