Skip to content

Commit

Permalink
Add -print0 to print names of rewritten files (bazel-contrib#1213)
Browse files Browse the repository at this point in the history
This CL adds a config to fix/update routine to output files being
rewrittenby gazelle when using fix mode. This will make scripting
around gazelle much easier, eg. run gazelle in a pre-commit hook then
commit changed BUILD files.
  • Loading branch information
dr-dime authored Apr 9, 2022
1 parent 3d712ae commit e0dc92a
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ platforms:
- "-//docs:all"
# Fails to execute, apparently due to command-line length limit.
- "-//internal:bazel_test"
# gazelle prints file paths with backward slashes on windows,
# which doesn't match the golden files generated on *nix.
- "-//tests:fix_mode_print0"
2 changes: 2 additions & 0 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type updateConfig struct {
walkMode walk.Mode
patchPath string
patchBuffer bytes.Buffer
print0 bool
}

type emitFunc func(c *config.Config, f *rule.File) error
Expand Down Expand Up @@ -81,6 +82,7 @@ func (ucr *updateConfigurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *conf
fs.StringVar(&ucr.mode, "mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff")
fs.BoolVar(&ucr.recursive, "r", true, "when true, gazelle will update subdirectories recursively")
fs.StringVar(&uc.patchPath, "patch", "", "when set with -mode=diff, gazelle will write to a file instead of stdout")
fs.BoolVar(&uc.print0, "print0", false, "when set with -mode=fix, gazelle will print the names of rewritten files separated with \\0 (NULL)")
fs.Var(&gzflag.MultiFlag{Values: &ucr.knownImports}, "known_import", "import path for which external resolution is skipped (can specify multiple times)")
fs.StringVar(&ucr.repoConfigPath, "repo_config", "", "file where Gazelle should load repository configuration. Defaults to WORKSPACE.")
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/gazelle/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -38,5 +39,8 @@ func fixFile(c *config.Config, f *rule.File) error {
return err
}
f.Content = newContent
if getUpdateConfig(c).print0 {
fmt.Printf("%s\x00", outPath)
}
return nil
}
1 change: 1 addition & 0 deletions tests/fix_mode_print0/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:lang disabled
1 change: 1 addition & 0 deletions tests/fix_mode_print0/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:lang disabled
3 changes: 3 additions & 0 deletions tests/fix_mode_print0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fix mode with -print0

Prints all fixed files with \0 as delimiter.
Empty file added tests/fix_mode_print0/WORKSPACE
Empty file.
6 changes: 6 additions & 0 deletions tests/fix_mode_print0/already_fixed/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rule(
name = "good",
visibility = [
"test",
],
)
6 changes: 6 additions & 0 deletions tests/fix_mode_print0/already_fixed/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rule(
name = "good",
visibility = [
"test",
],
)
1 change: 1 addition & 0 deletions tests/fix_mode_print0/arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-print0
Binary file added tests/fix_mode_print0/expectedStdout.txt
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/fix_mode_print0/need_fix/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule( name= "really_bad_formatting",

visibility=[




"test"
]
)
6 changes: 6 additions & 0 deletions tests/fix_mode_print0/need_fix/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rule(
name = "really_bad_formatting",
visibility = [
"test",
],
)

0 comments on commit e0dc92a

Please sign in to comment.