Skip to content

Commit

Permalink
WIP [ignore]: implement convention checker interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-french committed Aug 28, 2024
1 parent 2d6805c commit 1786849
Show file tree
Hide file tree
Showing 10 changed files with 913 additions and 5 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ filegroup(
"repository.rst",
"//cmd:all_files",
"//config:all_files",
"//convention:all_files",
"//flag:all_files",
"//internal:all_files",
"//label:all_files",
Expand Down
1 change: 1 addition & 0 deletions cmd/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//config",
"//convention",
"//flag",
"//internal/wspace",
"//label",
Expand Down
26 changes: 21 additions & 5 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/bazelbuild/bazel-gazelle/internal/wspace"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/convention"
"github.com/bazelbuild/bazel-gazelle/merger"
"github.com/bazelbuild/bazel-gazelle/repo"
"github.com/bazelbuild/bazel-gazelle/resolve"
Expand All @@ -55,6 +56,7 @@ type updateConfig struct {
patchBuffer bytes.Buffer
print0 bool
profile profiler
useConventions bool
}

type emitFunc func(c *config.Config, f *rule.File) error
Expand Down Expand Up @@ -90,6 +92,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.BoolVar(&uc.useConventions, "use_conventions", false, "when true, gazelle will enable convention checking in fix-update.")
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.StringVar(&ucr.cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
Expand Down Expand Up @@ -263,7 +266,8 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
&config.CommonConfigurer{},
&updateConfigurer{},
&walk.Configurer{},
&resolve.Configurer{})
&resolve.Configurer{},
&convention.Configurer{})

for _, lang := range languages {
cexts = append(cexts, lang)
Expand Down Expand Up @@ -313,6 +317,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}
}()

conventionChecker := convention.NewChecker(c, uc.dirs, mrslv.Resolver, exts...)
var errorsFromWalk []error
walk.Walk(c, cexts, uc.dirs, uc.walkMode, func(dir, rel string, c *config.Config, update bool, f *rule.File, subdirs, regularFiles, genFiles []string) {
// If this file is ignored or if Gazelle was not asked to update this
Expand All @@ -321,9 +326,14 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
for _, repl := range c.KindMap {
mrslv.MappedKind(rel, repl)
}
if c.IndexLibraries && f != nil {
if f != nil {
for _, r := range f.Rules {
ruleIndex.AddRule(c, r, f)
if c.IndexLibraries {
ruleIndex.AddRule(c, r, f)
}
if uc.useConventions {
conventionChecker.AddRule(c, r, f)
}
}
}
return
Expand Down Expand Up @@ -448,10 +458,13 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
})

// Add library rules to the dependency resolution table.
if c.IndexLibraries {
for _, r := range f.Rules {
for _, r := range f.Rules {
if c.IndexLibraries {
ruleIndex.AddRule(c, r, f)
}
if uc.useConventions {
conventionChecker.AddRule(c, r, f)
}
}
})

Expand All @@ -475,6 +488,9 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}

// Finish building the index for dependency resolution.
if uc.useConventions {
conventionChecker.Finish(c, ruleIndex)
}
ruleIndex.Finish()

// Resolve dependencies.
Expand Down
55 changes: 55 additions & 0 deletions convention/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "convention",
srcs = [
"check.go",
"config.go",
"dir_set.go",
],
importpath = "github.com/bazelbuild/bazel-gazelle/convention",
visibility = ["//visibility:public"],
deps = [
"//config",
"//label",
"//resolve",
"//rule",
],
)

alias(
name = "go_default_library",
actual = ":convention",
visibility = ["//visibility:public"],
)

go_test(
name = "convention_test",
srcs = [
"check_test.go",
"config_test.go",
],
embed = [":convention"],
deps = [
"//config",
"//label",
"//language/go",
"//language/proto",
"//resolve",
"//rule",
],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"check.go",
"check_test.go",
"config.go",
"config_test.go",
"dir_set.go",
],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 1786849

Please sign in to comment.