Skip to content

Commit 72d6cc3

Browse files
doriableoliversun9bufdev
authored
Add clean to buf.gen.yaml (#3130)
As a quick follow-up to #3124 and in response to #3124 (comment), this adds a `clean` config key to `v2` `buf.gen.yaml`, for example: ```yaml version: v2 clean: true plugins: - local: custom-gen-go out: gen/go opt: paths=source_relative strategy: directory - protoc_builtin: java out: gen/java ``` When running `buf generate` with the above configs, the outs set to each plugin (e.g. `gen/go` and `gen/java`) will be removed before code generation is run. If `buf generate --clean` flag is set, then that will always take precedence, even if `clean: false` in the configuration. And likewise, if `buf generate --clean=false`, and `clean: true` in the configuration, then we would not delete the out directories. --------- Co-authored-by: Oliver Sun <[email protected]> Co-authored-by: Oliver Sun <[email protected]> Co-authored-by: bufdev <[email protected]> Co-authored-by: bufdev <[email protected]>
1 parent 06c2619 commit 72d6cc3

File tree

8 files changed

+104
-28
lines changed

8 files changed

+104
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
- Add `clean` as a top-level option in `buf.gen.yaml`, matching the `buf generate --clean` flag. If
6+
set to true, this will delete the directories, jar files, or zip files set to `out` for each
7+
plugin.
58
- Fix git input handling of annotated tags.
69
- Update `buf registry login` to complete the login flow in the browser by default. This allows
710
users to login with their browser and have the token automatically provided to the CLI.

private/buf/bufgen/bufgen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ func GenerateWithBaseOutDirPath(baseOutDirPath string) GenerateOption {
120120

121121
// GenerateWithDeleteOuts returns a new GenerateOption that results in the
122122
// output directories, zip files, or jar files being deleted before generation is run.
123-
func GenerateWithDeleteOuts() GenerateOption {
123+
func GenerateWithDeleteOuts(deleteOuts bool) GenerateOption {
124124
return func(generateOptions *generateOptions) {
125-
generateOptions.deleteOuts = true
125+
generateOptions.deleteOuts = &deleteOuts
126126
}
127127
}
128128

private/buf/bufgen/generator.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ func (g *generator) Generate(
105105
return err
106106
}
107107
}
108-
if generateOptions.deleteOuts {
108+
shouldDeleteOuts := config.CleanPluginOuts()
109+
if generateOptions.deleteOuts != nil {
110+
shouldDeleteOuts = *generateOptions.deleteOuts
111+
}
112+
if shouldDeleteOuts {
109113
if err := g.deleteOuts(
110114
ctx,
111115
generateOptions.baseOutDirPath,
@@ -484,7 +488,7 @@ func validateResponses(
484488

485489
type generateOptions struct {
486490
baseOutDirPath string
487-
deleteOuts bool
491+
deleteOuts *bool
488492
includeImportsOverride *bool
489493
includeWellKnownTypesOverride *bool
490494
}

private/buf/cmd/buf/command/generate/generate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Insertion points are processed in the order the plugins are specified in the tem
373373
type flags struct {
374374
Template string
375375
BaseOutDirPath string
376-
DeleteOuts bool
376+
DeleteOuts *bool
377377
ErrorFormat string
378378
Files []string
379379
Config string
@@ -427,10 +427,10 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
427427
".",
428428
`The base directory to generate to. This is prepended to the out directories in the generation template`,
429429
)
430-
flagSet.BoolVar(
431-
&f.DeleteOuts,
430+
bindBoolPointer(
431+
flagSet,
432432
deleteOutsFlagName,
433-
false,
433+
&f.DeleteOuts,
434434
`Prior to generation, delete the directories, jar files, or zip files that the plugins will write to. Allows cleaning of existing assets without having to call rm -rf`,
435435
)
436436
flagSet.StringVar(
@@ -522,10 +522,10 @@ func run(
522522
generateOptions := []bufgen.GenerateOption{
523523
bufgen.GenerateWithBaseOutDirPath(flags.BaseOutDirPath),
524524
}
525-
if flags.DeleteOuts {
525+
if flags.DeleteOuts != nil {
526526
generateOptions = append(
527527
generateOptions,
528-
bufgen.GenerateWithDeleteOuts(),
528+
bufgen.GenerateWithDeleteOuts(*flags.DeleteOuts),
529529
)
530530
}
531531
if flags.IncludeImportsOverride != nil {

private/buf/cmd/buf/command/generate/generate_test.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,24 @@ func testGenerateDeleteOuts(
908908
t *testing.T,
909909
baseOutDirPath string,
910910
outputPaths ...string,
911+
) {
912+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, nil, true, true, outputPaths)
913+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, nil, false, false, outputPaths)
914+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean"}, false, true, outputPaths)
915+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean"}, true, true, outputPaths)
916+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean=true"}, true, true, outputPaths)
917+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean=true"}, false, true, outputPaths)
918+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean=false"}, true, false, outputPaths)
919+
testGenerateDeleteOutsWithArgAndConfig(t, baseOutDirPath, []string{"--clean=false"}, false, false, outputPaths)
920+
}
921+
922+
func testGenerateDeleteOutsWithArgAndConfig(
923+
t *testing.T,
924+
baseOutDirPath string,
925+
cleanArgs []string,
926+
cleanOptionInConfig bool,
927+
expectedClean bool,
928+
outputPaths []string,
911929
) {
912930
// Just add more builtins to the plugins slice below if this goes off
913931
require.True(t, len(outputPaths) < 4, "we want to have unique plugins to work with and this test is only set up for three plugins max right now")
@@ -964,18 +982,25 @@ plugins:
964982
_, _ = templateBuilder.WriteString(outputPath)
965983
_, _ = templateBuilder.WriteString("\n")
966984
}
985+
if cleanOptionInConfig {
986+
templateBuilder.WriteString("clean: true\n")
987+
}
967988
testRunStdoutStderr(
968989
t,
969990
nil,
970991
0,
971992
``,
972993
``,
973-
filepath.Join("testdata", "simple"),
974-
"--template",
975-
templateBuilder.String(),
976-
"-o",
977-
filepath.Join(tmpDirPath, normalpath.Unnormalize(baseOutDirPath)),
978-
"--clean",
994+
append(
995+
[]string{
996+
filepath.Join("testdata", "simple"),
997+
"--template",
998+
templateBuilder.String(),
999+
"-o",
1000+
filepath.Join(tmpDirPath, normalpath.Unnormalize(baseOutDirPath)),
1001+
},
1002+
cleanArgs...,
1003+
)...,
9791004
)
9801005
for _, fullOutputPath := range fullOutputPaths {
9811006
switch normalpath.Ext(fullOutputPath) {
@@ -986,14 +1011,22 @@ plugins:
9861011
fullOutputPath,
9871012
)
9881013
require.NoError(t, err)
1014+
// Always expect non-fake data, because the existing ".jar" or ".zip"
1015+
// file is always replaced by the output. This is the existing and correct
1016+
// behavior.
9891017
require.True(t, len(data) > 1, "expected non-fake data at %q", fullOutputPath)
9901018
default:
991-
_, err := storage.ReadPath(
1019+
data, err := storage.ReadPath(
9921020
ctx,
9931021
storageBucket,
9941022
normalpath.Join(fullOutputPath, "foo.txt"),
9951023
)
996-
require.ErrorIs(t, err, fs.ErrNotExist)
1024+
if expectedClean {
1025+
require.ErrorIs(t, err, fs.ErrNotExist)
1026+
} else {
1027+
require.NoError(t, err, "expected foo.txt at %q", fullOutputPath)
1028+
require.NotNil(t, data, "expected foo.txt at %q", fullOutputPath)
1029+
}
9971030
}
9981031
}
9991032
}

private/bufpkg/bufconfig/buf_gen_yaml_file.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func writeBufGenYAMLFile(writer io.Writer, bufGenYAMLFile BufGenYAMLFile) error
258258
}
259259
externalBufGenYAMLFileV2 := externalBufGenYAMLFileV2{
260260
Version: FileVersionV2.String(),
261+
Clean: bufGenYAMLFile.GenerateConfig().CleanPluginOuts(),
261262
Plugins: externalPluginConfigsV2,
262263
Managed: externalManagedConfigV2,
263264
Inputs: externalInputConfigsV2,
@@ -481,8 +482,11 @@ type externalTypesConfigV1 struct {
481482

482483
// externalBufGenYAMLFileV2 represents the v2 buf.gen.yaml file.
483484
type externalBufGenYAMLFileV2 struct {
484-
Version string `json:"version,omitempty" yaml:"version,omitempty"`
485-
Managed externalGenerateManagedConfigV2 `json:"managed,omitempty" yaml:"managed,omitempty"`
485+
Version string `json:"version,omitempty" yaml:"version,omitempty"`
486+
Managed externalGenerateManagedConfigV2 `json:"managed,omitempty" yaml:"managed,omitempty"`
487+
// Clean, if set to true, will delete the output directories, zip files, or jar files
488+
// before generation is run.
489+
Clean bool `json:"clean,omitempty" yaml:"clean,omitempty"`
486490
Plugins []externalGeneratePluginConfigV2 `json:"plugins,omitempty" yaml:"plugins,omitempty"`
487491
Inputs []externalInputConfigV2 `json:"inputs,omitempty" yaml:"inputs,omitempty"`
488492
}

private/bufpkg/bufconfig/buf_gen_yaml_file_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ plugins:
106106
t,
107107
// input
108108
`version: v2
109+
clean: true
110+
plugins:
111+
- local: custom-gen-go
112+
out: gen/go
113+
opt: paths=source_relative
114+
strategy: directory
115+
`,
116+
// expected output
117+
`version: v2
118+
clean: true
119+
plugins:
120+
- local: custom-gen-go
121+
out: gen/go
122+
opt: paths=source_relative
123+
strategy: directory
124+
`,
125+
)
126+
testReadWriteBufGenYAMLFileRoundTrip(
127+
t,
128+
// input
129+
`version: v2
109130
managed:
110131
disable:
111132
- module: buf.build/googleapis/googleapis

private/bufpkg/bufconfig/generate_config.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222

2323
// GenerateConfig is a generation configuration.
2424
type GenerateConfig interface {
25+
// CleanPluginOuts is whether to delete the output directories, zip files, or jar files before
26+
// generation is run.
27+
CleanPluginOuts() bool
2528
// GeneratePluginConfigs returns the plugin configurations. This will always be
2629
// non-empty. Zero plugin configs will cause an error at construction time.
2730
GeneratePluginConfigs() []GeneratePluginConfig
@@ -38,6 +41,7 @@ type GenerateConfig interface {
3841

3942
// NewGenerateConfig returns a validated GenerateConfig.
4043
func NewGenerateConfig(
44+
cleanPluginOuts bool,
4145
pluginConfigs []GeneratePluginConfig,
4246
managedConfig GenerateManagedConfig,
4347
typeConfig GenerateTypeConfig,
@@ -46,18 +50,20 @@ func NewGenerateConfig(
4650
return nil, newNoPluginsError()
4751
}
4852
return &generateConfig{
49-
pluginConfigs: pluginConfigs,
50-
managedConfig: managedConfig,
51-
typeConfig: typeConfig,
53+
cleanPluginOuts: cleanPluginOuts,
54+
pluginConfigs: pluginConfigs,
55+
managedConfig: managedConfig,
56+
typeConfig: typeConfig,
5257
}, nil
5358
}
5459

5560
// *** PRIVATE ***
5661

5762
type generateConfig struct {
58-
pluginConfigs []GeneratePluginConfig
59-
managedConfig GenerateManagedConfig
60-
typeConfig GenerateTypeConfig
63+
cleanPluginOuts bool
64+
pluginConfigs []GeneratePluginConfig
65+
managedConfig GenerateManagedConfig
66+
typeConfig GenerateTypeConfig
6167
}
6268

6369
func newGenerateConfigFromExternalFileV1Beta1(
@@ -122,11 +128,16 @@ func newGenerateConfigFromExternalFileV2(
122128
return nil, err
123129
}
124130
return &generateConfig{
125-
managedConfig: managedConfig,
126-
pluginConfigs: pluginConfigs,
131+
cleanPluginOuts: externalFile.Clean,
132+
managedConfig: managedConfig,
133+
pluginConfigs: pluginConfigs,
127134
}, nil
128135
}
129136

137+
func (g *generateConfig) CleanPluginOuts() bool {
138+
return g.cleanPluginOuts
139+
}
140+
130141
func (g *generateConfig) GeneratePluginConfigs() []GeneratePluginConfig {
131142
return g.pluginConfigs
132143
}

0 commit comments

Comments
 (0)