]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add go generate -skip flag
authorRuss Cox <rsc@golang.org>
Fri, 5 Aug 2022 19:44:15 +0000 (15:44 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 17 Aug 2022 03:24:24 +0000 (03:24 +0000)
Following proposal discussion in #38687, add go generate -skip
to allow easier skipping of specific //go:generate directives.

Fixes #38687.

Change-Id: Ied5b4042965dd6a2b93c1c517045fccae2d95c3f
Reviewed-on: https://go-review.googlesource.com/c/go/+/421440
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/go/alldocs.go
src/cmd/go/internal/generate/generate.go
src/cmd/go/testdata/script/generate.txt

index a3c1fecb91b6dda01c78ccb91176899d8cb9acf0..ce152171fda2ed32c8156bc4ded57bf558ee6542 100644 (file)
 //
 // The generator is run in the package's source directory.
 //
-// Go generate accepts one specific flag:
+// Go generate accepts two specific flags:
 //
 //     -run=""
 //             if non-empty, specifies a regular expression to select
 //             any trailing spaces and final newline) matches the
 //             expression.
 //
+//     -skip=""
+//             if non-empty, specifies a regular expression to suppress
+//             directives whose full original source text (excluding
+//             any trailing spaces and final newline) matches the
+//             expression. If a directive matches both the -run and
+//             the -skip arguments, it is skipped.
+//
 // It also accepts the standard build flags including -v, -n, and -x.
 // The -v flag prints the names of packages and files as they are
 // processed.
index 65e7148aa87057eb55cf6172912998851df9a607..3eda6c71453f16d84acbeac206f15cb94e105c63 100644 (file)
@@ -133,7 +133,7 @@ all further processing for that package.
 
 The generator is run in the package's source directory.
 
-Go generate accepts one specific flag:
+Go generate accepts two specific flags:
 
        -run=""
                if non-empty, specifies a regular expression to select
@@ -141,6 +141,13 @@ Go generate accepts one specific flag:
                any trailing spaces and final newline) matches the
                expression.
 
+       -skip=""
+               if non-empty, specifies a regular expression to suppress
+               directives whose full original source text (excluding
+               any trailing spaces and final newline) matches the
+               expression. If a directive matches both the -run and
+               the -skip arguments, it is skipped.
+
 It also accepts the standard build flags including -v, -n, and -x.
 The -v flag prints the names of packages and files as they are
 processed.
@@ -156,11 +163,15 @@ For more about specifying packages, see 'go help packages'.
 var (
        generateRunFlag string         // generate -run flag
        generateRunRE   *regexp.Regexp // compiled expression for -run
+
+       generateSkipFlag string         // generate -skip flag
+       generateSkipRE   *regexp.Regexp // compiled expression for -skip
 )
 
 func init() {
        work.AddBuildFlags(CmdGenerate, work.DefaultBuildFlags)
        CmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "")
+       CmdGenerate.Flag.StringVar(&generateSkipFlag, "skip", "", "")
 }
 
 func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
@@ -171,6 +182,13 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
                        log.Fatalf("generate: %s", err)
                }
        }
+       if generateSkipFlag != "" {
+               var err error
+               generateSkipRE, err = regexp.Compile(generateSkipFlag)
+               if err != nil {
+                       log.Fatalf("generate: %s", err)
+               }
+       }
 
        cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "generate")
 
@@ -291,10 +309,11 @@ func (g *Generator) run() (ok bool) {
                if !isGoGenerate(buf) {
                        continue
                }
-               if generateRunFlag != "" {
-                       if !generateRunRE.Match(bytes.TrimSpace(buf)) {
-                               continue
-                       }
+               if generateRunFlag != "" && !generateRunRE.Match(bytes.TrimSpace(buf)) {
+                       continue
+               }
+               if generateSkipFlag != "" && generateSkipRE.Match(bytes.TrimSpace(buf)) {
+                       continue
                }
 
                g.setEnv()
index 73f5bbd57a91d35177b5ccaed75e0d1496cebffe..58777c5865a3f18280a1fe6992c5ac1bd127a24e 100644 (file)
@@ -17,11 +17,19 @@ stdout 'Now is the time for all good men'
 go generate './generate/substitution.go'
 stdout $GOARCH' substitution.go:7 pabc xyzp/substitution.go/123'
 
-# Test go generate's run flag
+# Test go generate's run and skip flags
 go generate -run y.s './generate/flag.go'
 stdout 'yes' # flag.go should select yes
 ! stdout 'no' # flag.go should not select no
 
+go generate -skip th..sand './generate/flag.go'
+stdout 'yes' # flag.go should select yes
+! stdout 'no' # flag.go should not select no
+
+go generate -run . -skip th..sand './generate/flag.go'
+stdout 'yes' # flag.go should select yes
+! stdout 'no' # flag.go should not select no
+
 # Test go generate provides the right "$GOPACKAGE" name in an x_test
 go generate './generate/env_test.go'
 stdout 'main_test'