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

FR: Allow disabling or customizing proto rule generation #180

Open
meowcakes opened this issue May 14, 2023 · 1 comment
Open

FR: Allow disabling or customizing proto rule generation #180

meowcakes opened this issue May 14, 2023 · 1 comment

Comments

@meowcakes
Copy link
Contributor

Currently this always generates the following rules when a proto_library rule is present:

java_proto_library(
    name = "my_java_proto",
    deps = [":my_proto"],
)

java_library(
    name = "my_java_library",
    visibility = ["//:__subpackages__"],
    exports = [":my_java_proto"],
)

It would be nice if this could be disabled by config or customizable so that it is compatible with other rules, e.g. https://rules-proto-grpc.com/en/latest/lang/java.html#java-proto-library

@illicitonion
Copy link
Collaborator

Thanks for filing the issue - I'd be happy to review a PR that did either or both of the following:

  1. Added a new directive, # gazelle:java_generate_proto, like # gazelle:go_generate_proto, which allowed disabling the java_proto_library generation
  2. Added a new directive # gazelle:java_proto_ruleset which accepted values rules_java and rules_proto_grpc and conditionally generated either target as you'd expect.

To do so, roughly we'd need to:

  1. Create a new directive and register it here:
    func (jc *Configurer) KnownDirectives() []string {
    and parse it into the config object here:
    func (jc *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
  2. Sniff the config out of cfg here and conditional-ise the codegen:
    jplName := strings.TrimSuffix(protoRuleName, "_proto") + "_java_proto"
    jglName := strings.TrimSuffix(protoRuleName, "_proto") + "_java_grpc"
    jlName := strings.TrimSuffix(protoRuleName, "_proto") + "_java_library"
    rjpl := rule.NewRule("java_proto_library", jplName)
    rjpl.SetAttr("deps", []string{":" + protoRuleName})
    res.Gen = append(res.Gen, rjpl)
    res.Imports = append(res.Imports, types.ResolveInput{})
    if protoPackage.HasServices {
    r := rule.NewRule("java_grpc_library", jglName)
    r.SetAttr("srcs", []string{":" + protoRuleName})
    r.SetAttr("deps", []string{":" + jplName})
    res.Gen = append(res.Gen, r)
    res.Imports = append(res.Imports, types.ResolveInput{})
    }
    rjl := rule.NewRule("java_library", jlName)
    rjl.SetAttr("visibility", []string{"//:__subpackages__"})
    var exports []string
    if protoPackage.HasServices {
    exports = append(exports, ":"+jglName)
    }
    rjl.SetAttr("exports", append(exports, ":"+jplName))
    packageName := types.NewPackageName(protoPackage.Options["java_package"])
    log.Debug().Str("pkg", packageName.Name).Msg("adding the proto import statement")
    rjl.SetPrivateAttr(packagesKey, []types.ResolvableJavaPackage{*types.NewResolvableJavaPackage(packageName, false, false)})
    res.Gen = append(res.Gen, rjl)
    res.Imports = append(res.Imports, types.ResolveInput{
    PackageNames: sorted_set.NewSortedSetFn([]types.PackageName{packageName}, types.PackageNameLess),
    })

illicitonion pushed a commit that referenced this issue May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants