Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor *scalaConfig to //pkg/scalaconfig #118

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ load(":workspace_deps.bzl", "workspace_deps")

workspace_deps()

# ---------------------------------------------------------------
# zig dependencies
# ---------------------------------------------------------------

load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")

zig_toolchains()

register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.28",
"@zig_sdk//toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//toolchain:darwin_amd64",
"@zig_sdk//toolchain:darwin_arm64",
)

# ----------------------------------------------------
# @rules_proto
# ----------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions language/scala/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ go_library(
"package_marker_rule.go",
"progress.go",
"resolve.go",
"scala_config.go",
"scala_package.go",
"scala_rule.go",
"scope.go",
Expand All @@ -42,6 +41,7 @@ go_library(
"//pkg/protobuf",
"//pkg/provider",
"//pkg/resolver",
"//pkg/scalaconfig",
"//pkg/scalarule",
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//label:go_default_library",
Expand All @@ -51,7 +51,6 @@ go_library(
"@bazel_gazelle//rule:go_default_library",
"@build_stack_rules_proto//pkg/protoc",
"@com_github_bazelbuild_buildtools//build:go_default_library",
"@com_github_bmatcuk_doublestar_v4//:doublestar",
"@com_github_pcj_mobyprogress//:mobyprogress",
],
)
Expand Down Expand Up @@ -83,7 +82,6 @@ go_test(
"golden_test.go",
"language_test.go",
"loads_test.go",
"scala_config_test.go",
"scala_package_test.go",
"scala_rule_test.go",
],
Expand All @@ -93,6 +91,7 @@ go_test(
"//build/stack/gazelle/scala/parse",
"//pkg/resolver",
"//pkg/resolver/mocks",
"//pkg/scalaconfig",
"//pkg/scalarule",
"//pkg/testutil",
"@bazel_gazelle//config:go_default_library",
Expand All @@ -102,7 +101,6 @@ go_test(
"@bazel_gazelle//testtools:go_default_library",
"@build_stack_rules_proto//pkg/goldentest",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
"@com_github_stretchr_testify//mock",
],
)
Expand Down
3 changes: 2 additions & 1 deletion language/scala/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
)

// Configure implements part of the language.Language interface
func (sl *scalaLang) Configure(c *config.Config, rel string, f *rule.File) {
if f != nil {
if err := getOrCreateScalaConfig(sl, c, rel).parseDirectives(f.Directives); err != nil {
if err := scalaconfig.GetOrCreate(sl, c, rel).ParseDirectives(f.Directives); err != nil {
log.Fatalf("parsing directives in package %q: %v", rel, err)
}
}
Expand Down
19 changes: 10 additions & 9 deletions language/scala/existing_scala_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bazelbuild/buildtools/build"

"github.com/stackb/scala-gazelle/pkg/resolver"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
"github.com/stackb/scala-gazelle/pkg/scalarule"
)

Expand Down Expand Up @@ -127,20 +128,20 @@ func (s *existingScalaRule) Resolve(rctx *scalarule.ResolveContext, importsRaw i
exports := scalaRule.ResolveExports(rctx)

r := rctx.Rule
sc := getScalaConfig(rctx.Config)
sc := scalaconfig.Get(rctx.Config)

// part 1a: deps

newImports := imports.Deps(sc.maybeRewrite(r.Kind(), rctx.From))
depLabels := sc.cleanDeps(rctx.From, r.Attr("deps"), newImports)
mergeDeps(r.Kind(), depLabels, newImports)
newImports := imports.Deps(sc.MaybeRewrite(r.Kind(), rctx.From))
depLabels := sc.CleanDeps(rctx.From, r.Attr("deps"), newImports)
scalaconfig.MergeDeps(r.Kind(), depLabels, newImports)
if len(depLabels.List) > 0 {
r.SetAttr("deps", depLabels)
} else {
r.DelAttr("deps")
}

if sc.shouldAnnotateImports() {
if sc.ShouldAnnotateImports() {
comments := r.AttrComments("srcs")
if comments != nil {
annotateImports(imports, comments, "import: ")
Expand All @@ -149,16 +150,16 @@ func (s *existingScalaRule) Resolve(rctx *scalarule.ResolveContext, importsRaw i

// part 1b: exports
if s.isLibrary {
newExports := exports.Deps(sc.maybeRewrite(r.Kind(), rctx.From))
exportLabels := sc.cleanExports(rctx.From, r.Attr("exports"), newExports)
mergeDeps(r.Kind(), exportLabels, newExports)
newExports := exports.Deps(sc.MaybeRewrite(r.Kind(), rctx.From))
exportLabels := sc.CleanExports(rctx.From, r.Attr("exports"), newExports)
scalaconfig.MergeDeps(r.Kind(), exportLabels, newExports)
if len(exportLabels.List) > 0 {
r.SetAttr("exports", exportLabels)
} else {
r.DelAttr("exports")
}

if sc.shouldAnnotateExports() {
if sc.ShouldAnnotateExports() {
comments := r.AttrComments("srcs")
if comments != nil {
annotateImports(exports, comments, "export: ")
Expand Down
73 changes: 0 additions & 73 deletions language/scala/existing_scala_rule_test.go
Original file line number Diff line number Diff line change
@@ -1,74 +1 @@
package scala

import (
"testing"

"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/google/go-cmp/cmp"
)

func TestScalaDepLabel(t *testing.T) {
for name, tc := range map[string]struct {
in string
want label.Label
}{
"degenerate": {
in: `
test(
expr = "",
)
`,
want: label.NoLabel,
},
"invalid label": {
in: `
test(
expr = "@@@",
)
`,
want: label.NoLabel,
},
"valid label": {
in: `
test(
expr = "@foo//bar:baz",
)
`,
want: label.New("foo", "bar", "baz"),
},
"invalid callexpr": {
in: `
test(
expr = fn("@foo//bar:baz"),
)
`,
want: label.NoLabel,
},
"valid callexpr": {
in: `
test(
expr = scala_dep("@foo//bar:baz"),
)
`,
want: label.New("foo", "bar", "baz"),
},
} {
t.Run(name, func(t *testing.T) {
file, err := rule.LoadData("<in-memory>", "BUILD", []byte(tc.in))
if err != nil {
t.Fatal(err)
}
if len(file.Rules) != 1 {
t.Fatalf("expected single in rule, got %d", len(file.Rules))
}
target := file.Rules[0]
expr := target.Attr("expr")
got := labelFromDepExpr(expr)

if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("label (-want +got):\n%s", diff)
}
})
}
}
3 changes: 2 additions & 1 deletion language/scala/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
)

const debugGenerate = false
Expand All @@ -24,7 +25,7 @@ func (sl *scalaLang) GenerateRules(args language.GenerateArgs) language.Generate
writeGenerateProgress(sl.progress, len(sl.packages), int(sl.cache.PackageCount))
}

sc := getScalaConfig(args.Config)
sc := scalaconfig.Get(args.Config)
pkg := newScalaPackage(args.Rel, args.File, sc, sl.ruleProviderRegistry, sl.parser, sl)
sl.packages[args.Rel] = pkg
sl.remainingPackages++
Expand Down
11 changes: 2 additions & 9 deletions language/scala/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stackb/scala-gazelle/pkg/parser"
"github.com/stackb/scala-gazelle/pkg/provider"
"github.com/stackb/scala-gazelle/pkg/resolver"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
"github.com/stackb/scala-gazelle/pkg/scalarule"
)

Expand Down Expand Up @@ -92,15 +93,7 @@ func (sl *scalaLang) Name() string { return scalaLangName }

// KnownDirectives implements part of the language.Language interface
func (*scalaLang) KnownDirectives() []string {
return []string{
resolveConflictsDirective,
resolveFileSymbolName,
resolveGlobDirective,
resolveKindRewriteNameDirective,
resolveWithDirective,
scalaDebugDirective,
scalaRuleDirective,
}
return scalaconfig.DirectiveNames()
}

// NewLanguage is called by Gazelle to install this language extension in a
Expand Down
8 changes: 4 additions & 4 deletions language/scala/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func ExampleLanguage_KnownDirectives() {
fmt.Println(d)
}
// output:
// scala_debug
// scala_rule
// resolve_glob
// resolve_conflicts
// resolve_with
// resolve_file_symbol_name
// resolve_glob
// resolve_kind_rewrite_name
// resolve_with
// scala_debug
// scala_rule
}
13 changes: 7 additions & 6 deletions language/scala/scala_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stackb/scala-gazelle/pkg/glob"
"github.com/stackb/scala-gazelle/pkg/parser"
"github.com/stackb/scala-gazelle/pkg/resolver"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
"github.com/stackb/scala-gazelle/pkg/scalarule"
)

Expand All @@ -36,7 +37,7 @@ type scalaPackage struct {
// the build file
file *rule.File
// the config for this package
cfg *scalaConfig
cfg *scalaconfig.Config
// the generated and empty rule providers
gen, empty []scalarule.RuleProvider
// rules is the final state of generated rules, by name.
Expand All @@ -50,7 +51,7 @@ type scalaPackage struct {
func newScalaPackage(
rel string,
file *rule.File,
cfg *scalaConfig,
cfg *scalaconfig.Config,
providerRegistry scalarule.ProviderRegistry,
parser parser.Parser,
universe resolver.Universe) *scalaPackage {
Expand All @@ -71,7 +72,7 @@ func newScalaPackage(
}

// Config returns the the underlying config.
func (s *scalaPackage) Config() *scalaConfig {
func (s *scalaPackage) Config() *scalaconfig.Config {
return s.cfg
}

Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *scalaPackage) generateRules(enabled bool) []scalarule.RuleProvider {
}
}

configuredRules := s.cfg.configuredRules()
configuredRules := s.cfg.ConfiguredRules()

for _, rc := range configuredRules {
// if enabled != rc.Enabled {
Expand Down Expand Up @@ -202,7 +203,7 @@ func (s *scalaPackage) ParseRule(r *rule.Rule, attrName string) (scalarule.Rule,
return nil, ErrRuleHasNoSrcs
}

from := s.cfg.maybeRewrite(r.Kind(), label.Label{Pkg: s.rel, Name: r.Name()})
from := s.cfg.MaybeRewrite(r.Kind(), label.Label{Pkg: s.rel, Name: r.Name()})

rule, err := s.parser.ParseScalaRule(r.Kind(), from, dir, srcs...)
if err != nil {
Expand All @@ -221,7 +222,7 @@ func (s *scalaPackage) ParseRule(r *rule.Rule, attrName string) (scalarule.Rule,

// repoRootDir return the root directory of the repo.
func (s *scalaPackage) repoRootDir() string {
return s.cfg.config.RepoRoot
return s.cfg.Config().RepoRoot
}

// Rules provides the aggregated rule list for the package.
Expand Down
3 changes: 2 additions & 1 deletion language/scala/scala_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/mock"

"github.com/stackb/scala-gazelle/pkg/resolver/mocks"
"github.com/stackb/scala-gazelle/pkg/scalaconfig"
"github.com/stackb/scala-gazelle/pkg/scalarule"
)

Expand All @@ -35,7 +36,7 @@ func TestScalaPackageParseRule(t *testing.T) {
Return(nil)

c := config.New()
cfg := newScalaConfig(universe, c, "")
cfg := scalaconfig.New(universe, c, "")

pkg := scalaPackage{
cfg: cfg,
Expand Down
Loading
Loading