Skip to content

Commit

Permalink
:allow plugins to provide builtin goals
Browse files Browse the repository at this point in the history
  • Loading branch information
tdyas committed May 13, 2024
1 parent 3ef507e commit f24f7f5
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/python/pants/backend/experimental/bsp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
13 changes: 13 additions & 0 deletions src/python/pants/backend/experimental/bsp/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.bsp.goal import BSPGoal
from pants.bsp.rules import rules as bsp_rules


def builtin_goals():
return (BSPGoal,)


def rules():
return (*bsp_rules(),)
4 changes: 4 additions & 0 deletions src/python/pants/backend/experimental/java/bsp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
29 changes: 29 additions & 0 deletions src/python/pants/backend/experimental/java/bsp/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.experimental.bsp.register import builtin_goals as bsp_builtin_goals
from pants.backend.experimental.bsp.register import rules as bsp_rules
from pants.backend.experimental.java.register import build_file_aliases as java_build_file_aliases
from pants.backend.experimental.java.register import rules as java_rules
from pants.backend.experimental.java.register import target_types as java_target_types
from pants.backend.java.bsp.rules import rules as java_bsp_rules


def builtin_goals():
return bsp_builtin_goals()


def target_types():
return java_target_types()


def rules():
return (
*java_rules(),
*bsp_rules(),
*java_bsp_rules(),
)


def build_file_aliases():
return java_build_file_aliases()
2 changes: 0 additions & 2 deletions src/python/pants/backend/experimental/java/register.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.java.bsp import rules as java_bsp_rules
from pants.backend.java.compile import javac
from pants.backend.java.dependency_inference import java_parser
from pants.backend.java.dependency_inference import rules as dependency_inference_rules
Expand Down Expand Up @@ -39,7 +38,6 @@ def rules():
*java_parser.rules(),
*dependency_inference_rules.rules(),
*tailor.rules(),
*java_bsp_rules.rules(),
*archive.rules(),
*target_types_rules(),
*jvm_common.rules(),
Expand Down
4 changes: 4 additions & 0 deletions src/python/pants/backend/experimental/scala/bsp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
28 changes: 28 additions & 0 deletions src/python/pants/backend/experimental/scala/bsp/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from pants.backend.experimental.bsp.register import builtin_goals as bsp_builtin_goals
from pants.backend.experimental.bsp.register import rules as bsp_rules
from pants.backend.experimental.scala.register import build_file_aliases as scala_build_file_aliases
from pants.backend.experimental.scala.register import rules as scala_rules
from pants.backend.experimental.scala.register import target_types as scala_target_types
from pants.backend.scala.bsp.rules import rules as scala_bsp_rules


def builtin_goals():
return bsp_builtin_goals()


def target_types():
return scala_target_types()


def rules():
return (
*scala_rules(),
*bsp_rules(),
*scala_bsp_rules(),
)


def build_file_aliases():
return scala_build_file_aliases()
2 changes: 0 additions & 2 deletions src/python/pants/backend/experimental/scala/register.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from pants.backend.scala.bsp.rules import rules as bsp_rules
from pants.backend.scala.compile import scalac
from pants.backend.scala.dependency_inference import rules as dep_inf_rules
from pants.backend.scala.goals import check, repl, tailor
Expand Down Expand Up @@ -50,7 +49,6 @@ def rules():
*dep_inf_rules.rules(),
*target_types_rules(),
*scala_lockfile_rules(),
*bsp_rules(),
*jvm_common.rules(),
*wrap_scala.rules,
]
Expand Down
18 changes: 18 additions & 0 deletions src/python/pants/build_graph/build_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ def register_target_types(
def register_remote_auth_plugin(self, remote_auth_plugin: Callable) -> None:
self._remote_auth_plugin = remote_auth_plugin

def register_builtin_goals(self, plugin_or_backend: str, builtin_goals: Iterable[type]):
"""Registers the given builtin goals."""
if not isinstance(builtin_goals, Iterable):
raise TypeError(
f"The entrypoint `builtin_goals` must return an iterable. "
f"Given {repr(builtin_goals)}"
)
# Import `BuiltinGoal` here to avoid import cycle.
from pants.goal.builtin_goal import BuiltinGoal

bad_elements = [goal for goal in builtin_goals if not issubclass(goal, BuiltinGoal)]
if bad_elements:
raise TypeError(
"Every element of the entrypoint `builtin_goals` must be a subclass of "
f"{BuiltinGoal.__name__}. Bad elements: {bad_elements}."
)
self.register_subsystems(plugin_or_backend, builtin_goals)

def allow_unknown_options(self, allow: bool = True) -> None:
"""Allows overriding whether Options parsing will fail for unrecognized Options.
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/core/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
These are always activated and cannot be disabled.
"""
from pants.backend.codegen import export_codegen_goal
from pants.bsp.rules import rules as bsp_rules
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.core.goals import (
check,
Expand Down Expand Up @@ -85,7 +84,6 @@ def rules():
*run.rules(),
*tailor.rules(),
*test.rules(),
*bsp_rules(),
# util_rules
*adhoc_binaries.rules(),
*anonymous_telemetry.rules(),
Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/goal/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import annotations

from pants.bsp.goal import BSPGoal
from pants.build_graph.build_configuration import BuildConfiguration
from pants.goal import help
from pants.goal.builtin_goal import BuiltinGoal
Expand All @@ -13,12 +12,12 @@


def register_builtin_goals(build_configuration: BuildConfiguration.Builder) -> None:
build_configuration.register_subsystems("pants.goal", builtin_goals())
# build_configuration.register_subsystems("pants.goal", builtin_goals())
build_configuration.register_builtin_goals("builtin", builtin_goals())


def builtin_goals() -> tuple[type[BuiltinGoal], ...]:
return (
BSPGoal,
CompletionBuiltinGoal,
ExplorerBuiltinGoal,
MigrateCallByNameBuiltinGoal,
Expand Down
6 changes: 6 additions & 0 deletions src/python/pants/init/extension_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def load_plugins(
f"register remote auth function {remote_auth_func.__module__}.{remote_auth_func.__name__} from plugin: {plugin}"
)
build_configuration.register_remote_auth_plugin(remote_auth_func)
if "builtin_goals" in entries:
builtin_goals = entries["builtin_goals"].load()()
build_configuration.register_builtin_goals(req.key, builtin_goals)

loaded[dist.as_requirement().key] = dist

Expand Down Expand Up @@ -168,3 +171,6 @@ def invoke_entrypoint(name: str):
f"register remote auth function {remote_auth_func.__module__}.{remote_auth_func.__name__} from backend: {backend_package}"
)
build_configuration.register_remote_auth_plugin(remote_auth_func)
builtin_goals = getattr(module, "builtin_goals", None)
if builtin_goals:
build_configuration.register_builtin_goals(backend_package, builtin_goals)

0 comments on commit f24f7f5

Please sign in to comment.