Skip to content

Commit c30124e

Browse files
authored
✨ Add support to disable generating extensions for HAL link data (#93)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description This adds support to disable generating extensions for HAL link data. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [ ] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent d14c683 commit c30124e

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

.secrets.baseline

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
{
6767
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
6868
},
69+
{
70+
"path": "detect_secrets.filters.common.is_baseline_file",
71+
"filename": ".secrets.baseline"
72+
},
6973
{
7074
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
7175
"min_level": 2
@@ -145,5 +149,5 @@
145149
}
146150
]
147151
},
148-
"generated_at": "2025-05-17T07:56:49Z"
152+
"generated_at": "2025-06-17T15:02:37Z"
149153
}

changes/20250617120442.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Add support to disable generating extensions for HAL link data

generator/cmd/root.go

Lines changed: 15 additions & 8 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,12 +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"
18+
app = "generate_code"
19+
inputArg = "input"
20+
outputArg = "output"
21+
templateArg = "template"
22+
GenerateType = "generate"
23+
clientPath = "client-path"
24+
disableLinksArg = "disable-links"
2525
)
2626

2727
var (
@@ -49,12 +49,14 @@ func init() {
4949
rootCmd.Flags().StringP(templateArg, "t", "", "path to the template file if defaults need overriding")
5050
rootCmd.Flags().StringP(GenerateType, "g", "", "what to generate")
5151
rootCmd.Flags().StringP(clientPath, "c", "", "path to the client go package generated by OpenAPI Generator")
52+
rootCmd.Flags().StringP(disableLinksArg, "d", "", "disable the generation of HAL link data")
5253

5354
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_INPUT", rootCmd.Flags().Lookup(inputArg))
5455
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_OUTPUT", rootCmd.Flags().Lookup(outputArg))
5556
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_TEMPLATE", rootCmd.Flags().Lookup(templateArg))
5657
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_GENERATE_TYPE", rootCmd.Flags().Lookup(GenerateType))
5758
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_CLIENT_PATH", rootCmd.Flags().Lookup(clientPath))
59+
_ = configUtils.BindFlagToEnv(viperSession, app, "GENERATE_CODE_DISABLE_LINKS", rootCmd.Flags().Lookup(disableLinksArg))
5860
}
5961

6062
func RunCLI(ctx context.Context) (err error) {
@@ -81,7 +83,12 @@ func RunCLI(ctx context.Context) (err error) {
8183
return
8284
}
8385

84-
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)
8592
if err != nil {
8693
return
8794
}

generator/codegen/common.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"go/format"
1010
"path/filepath"
11+
"slices"
1112
"strings"
1213
"text/template"
1314

@@ -44,6 +45,7 @@ type Data struct {
4445
DestinationPath string
4546
ClientPackagePath string
4647
PackageName string
48+
DisableLinks bool
4749
}
4850

4951
var ValidGenerationTargets = map[string]func(*Data) error{
@@ -58,6 +60,13 @@ type ExtensionsConfig struct {
5860
Template string `mapstructure:"template"`
5961
GenerateType string `mapstructure:"generate_type"`
6062
ClientPackagePath string `mapstructure:"client_path"`
63+
DisableLinks bool `mapstructure:"disable_links"`
64+
}
65+
66+
type StaticFileConfig struct {
67+
ClientName string
68+
Destination string
69+
Exclusions []string
6170
}
6271

6372
func DefaultExtensionsConfig() *ExtensionsConfig {
@@ -118,6 +127,24 @@ func GenerateDataStruct(cfg ExtensionsConfig) (d *Data, err error) {
118127
}, nil
119128
}
120129

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

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

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

241-
mkdirErr := filesystem.MkDir(destination)
268+
mkdirErr := filesystem.MkDir(staticFileConfig.Destination)
242269
if mkdirErr != nil {
243-
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", destination, mkdirErr.Error())
270+
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", staticFileConfig.Destination, mkdirErr.Error())
244271
}
245272

246273
for _, f := range files {
274+
// Skip the file if it's contained in exclusions
275+
if slices.Contains(staticFileConfig.Exclusions, f) {
276+
continue
277+
}
278+
247279
t, tmplErr := template.
248280
New(filesystem.FilePathBase(efs, f)).
249281
ParseFS(static, f)
@@ -255,8 +287,8 @@ func CopyStaticFiles(ctx context.Context, clientName string, destination string)
255287
resultFileName = strings.TrimSuffix(resultFileName, ".static")
256288

257289
d := Data{
258-
DestinationPath: filepath.Join(destination, resultFileName),
259-
PackageName: clientName,
290+
DestinationPath: filepath.Join(staticFileConfig.Destination, resultFileName),
291+
PackageName: staticFileConfig.ClientName,
260292
}
261293
err = generateSourceCode(ctx, &d, t)
262294
if err != nil {

0 commit comments

Comments
 (0)