Skip to content

Commit

Permalink
Add DoneGeneratingRules language hook (#1325)
Browse files Browse the repository at this point in the history
This allows for an explicit notification that it's safe to release
resources needed only for generation (e.g. shutting down background
servers).
  • Loading branch information
illicitonion authored Sep 22, 2022
1 parent e046297 commit ddd4fa7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}
})

for _, lang := range languages {
if finishable, ok := lang.(language.FinishableLanguage); ok {
finishable.DoneGeneratingRules()
}
}

// Finish building the index for dependency resolution.
ruleIndex.Finish()

Expand Down
4 changes: 4 additions & 0 deletions internal/language/test_filegroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ go_library(
importpath = "github.com/bazelbuild/bazel-gazelle/internal/language/test_filegroup",
visibility = ["//visibility:public"],
deps = [
"//config",
"//label",
"//language",
"//repo",
"//resolve",
"//rule",
],
)
Expand Down
22 changes: 21 additions & 1 deletion internal/language/test_filegroup/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ package test_filegroup
import (
"path"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/repo"
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
)

const testFilegroupName = "test_filegroup"

type testFilegroupLang struct {
language.BaseLang

sawDone bool
}

func NewLanguage() language.Language {
Expand All @@ -53,7 +59,11 @@ var kinds = map[string]rule.KindInfo{
},
}

func (*testFilegroupLang) GenerateRules(args language.GenerateArgs) language.GenerateResult {
func (l *testFilegroupLang) GenerateRules(args language.GenerateArgs) language.GenerateResult {
if l.sawDone {
panic("GenerateRules must not be called after DoneGeneratingRules")
}

r := rule.NewRule("filegroup", "all_files")
srcs := make([]string, 0, len(args.Subdirs)+len(args.RegularFiles))
srcs = append(srcs, args.RegularFiles...)
Expand All @@ -71,3 +81,13 @@ func (*testFilegroupLang) GenerateRules(args language.GenerateArgs) language.Gen
Imports: []interface{}{nil},
}
}

func (l *testFilegroupLang) DoneGeneratingRules() {
l.sawDone = true
}

func (l *testFilegroupLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, imports interface{}, from label.Label) {
if !l.sawDone {
panic("Expected a call to DoneGeneratingRules before Resolve")
}
}
2 changes: 2 additions & 0 deletions language/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ func (b *BaseLang) GenerateRules(args GenerateArgs) GenerateResult {
return GenerateResult{}
}

func (b *BaseLang) DoneGeneratingRules() {}

func (b *BaseLang) Fix(c *config.Config, f *rule.File) {}
12 changes: 12 additions & 0 deletions language/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ type Language interface {
Fix(c *config.Config, f *rule.File)
}

// FinishableLanguage allows a Language to be notified when Generate is finished
// being called.
type FinishableLanguage interface {
// DoneGeneratingRules is called when all calls to GenerateRules have been
// completed.
// This allows for hooks to be called, for instance to release resources
// such as shutting down a background server.
// No further calls will be made to GenerateRules on this Language instance
// after this method has been called.
DoneGeneratingRules()
}

// GenerateArgs contains arguments for language.GenerateRules. Arguments are
// passed in a struct value so that new fields may be added in the future
// without breaking existing implementations.
Expand Down

0 comments on commit ddd4fa7

Please sign in to comment.