Skip to content

Commit

Permalink
port JVM-based subsystems to use ExportableTool
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed May 18, 2024
1 parent 40e8bc9 commit a9bdae1
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 172 deletions.
19 changes: 4 additions & 15 deletions src/python/pants/backend/codegen/avro/java/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pants.backend.java.target_types import JavaSourceField
from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
from pants.build_graph.address import Address
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.engine.fs import (
AddPrefix,
CreateDigest,
Expand Down Expand Up @@ -53,7 +53,7 @@
from pants.jvm.jdk_rules import InternalJdk, JvmProcess
from pants.jvm.resolve import jvm_tool
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.jvm.subsystems import JvmSubsystem
from pants.jvm.target_types import JvmResolveField, PrefixedJvmJdkField, PrefixedJvmResolveField
from pants.source.source_root import SourceRoot, SourceRootRequest
Expand All @@ -66,10 +66,6 @@ class GenerateJavaFromAvroRequest(GenerateSourcesRequest):
output = JavaSourceField


class AvroToolLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = AvroSubsystem.options_scope


@dataclass(frozen=True)
class CompileAvroSourceRequest:
digest: Digest
Expand Down Expand Up @@ -116,7 +112,7 @@ async def compile_avro_source(
output_dir = "_generated_files"
toolcp_relpath = "__toolcp"

lockfile_request = await Get(GenerateJvmLockfileFromTool, AvroToolLockfileSentinel())
lockfile_request = GenerateJvmLockfileFromTool.create(avro_tools)
tool_classpath, subsetted_input_digest, empty_output_dir = await MultiGet(
Get(ToolClasspath, ToolClasspathRequest(lockfile=lockfile_request)),
Get(
Expand Down Expand Up @@ -222,13 +218,6 @@ def make_avro_process(
return CompiledAvroSource(normalized_digest)


@rule
def generate_avro_tools_lockfile_request(
_: AvroToolLockfileSentinel, tool: AvroSubsystem
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(tool)


@dataclass(frozen=True)
class AvroRuntimeDependencyInferenceFieldSet(FieldSet):
required_fields = (
Expand Down Expand Up @@ -326,7 +315,7 @@ def rules():
*jvm_tool.rules(),
*jdk_rules.rules(),
UnionRule(GenerateSourcesRequest, GenerateJavaFromAvroRequest),
UnionRule(GenerateToolLockfileSentinel, AvroToolLockfileSentinel),
UnionRule(ExportableTool, AvroSubsystem),
UnionRule(InferDependenciesRequest, InferAvroRuntimeDependencyRequest),
AvroSourceTarget.register_plugin_field(PrefixedJvmJdkField),
AvroSourcesGeneratorTarget.register_plugin_field(PrefixedJvmJdkField),
Expand Down
25 changes: 8 additions & 17 deletions src/python/pants/backend/codegen/protobuf/java/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from pants.backend.experimental.java.register import rules as java_backend_rules
from pants.backend.java.target_types import JavaSourceField
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.core.util_rules.external_tool import DownloadedExternalTool, ExternalToolRequest
from pants.core.util_rules.source_files import SourceFilesRequest
from pants.core.util_rules.stripped_source_files import StrippedSourceFiles
Expand All @@ -42,7 +42,7 @@
)
from pants.engine.unions import UnionRule
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.jvm.target_types import PrefixedJvmJdkField, PrefixedJvmResolveField
from pants.source.source_root import SourceRoot, SourceRootRequest
from pants.util.logging import LogLevel
Expand All @@ -53,19 +53,18 @@ class GenerateJavaFromProtobufRequest(GenerateSourcesRequest):
output = JavaSourceField


class GrpcJavaToolLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = JavaProtobufGrpcSubsystem.options_scope


@dataclass(frozen=True)
class ProtobufJavaGrpcPlugin:
digest: Digest
path: str


@rule
async def resolve_protobuf_java_grpc_plugin(platform: Platform) -> ProtobufJavaGrpcPlugin:
lockfile_request = await Get(GenerateJvmLockfileFromTool, GrpcJavaToolLockfileSentinel())
async def resolve_protobuf_java_grpc_plugin(
platform: Platform,
tool: JavaProtobufGrpcSubsystem,
) -> ProtobufJavaGrpcPlugin:
lockfile_request = GenerateJvmLockfileFromTool.create(tool)
classpath = await Get(
ToolClasspath,
ToolClasspathRequest(lockfile=lockfile_request),
Expand Down Expand Up @@ -200,21 +199,13 @@ async def generate_java_from_protobuf(
return GeneratedSources(source_root_restored)


@rule
async def generate_grpc_java_lockfile_request(
_: GrpcJavaToolLockfileSentinel,
tool: JavaProtobufGrpcSubsystem,
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(tool)


def rules():
return [
*collect_rules(),
*dependency_inference.rules(),
*symbol_mapper.rules(),
UnionRule(GenerateSourcesRequest, GenerateJavaFromProtobufRequest),
UnionRule(GenerateToolLockfileSentinel, GrpcJavaToolLockfileSentinel),
UnionRule(ExportableTool, JavaProtobufGrpcSubsystem),
ProtobufSourceTarget.register_plugin_field(PrefixedJvmJdkField),
ProtobufSourcesGeneratorTarget.register_plugin_field(PrefixedJvmJdkField),
ProtobufSourceTarget.register_plugin_field(PrefixedJvmResolveField),
Expand Down
25 changes: 7 additions & 18 deletions src/python/pants/backend/codegen/protobuf/scala/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
ScalaArtifactsForVersionResult,
ScalaVersion,
)
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.core.util_rules import distdir
from pants.core.util_rules.external_tool import DownloadedExternalTool, ExternalToolRequest
from pants.core.util_rules.source_files import SourceFilesRequest
Expand Down Expand Up @@ -53,7 +53,7 @@
from pants.jvm.jdk_rules import InternalJdk, JvmProcess
from pants.jvm.resolve.common import ArtifactRequirements, GatherJvmCoordinatesRequest
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.jvm.target_types import PrefixedJvmJdkField, PrefixedJvmResolveField
from pants.source.source_root import SourceRoot, SourceRootRequest
from pants.util.logging import LogLevel
Expand All @@ -66,10 +66,6 @@ class GenerateScalaFromProtobufRequest(GenerateSourcesRequest):
output = ScalaSourceField


class ScalapbcToolLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = ScalaPBSubsystem.options_scope


class ScalaPBShimCompiledClassfiles(ClasspathEntry):
pass

Expand Down Expand Up @@ -118,7 +114,7 @@ async def generate_scala_from_protobuf(
plugins_relpath = "__plugins"
protoc_relpath = "__protoc"

lockfile_request = await Get(GenerateJvmLockfileFromTool, ScalapbcToolLockfileSentinel())
lockfile_request = GenerateJvmLockfileFromTool.create(scalapb)
(
downloaded_protoc_binary,
tool_classpath,
Expand Down Expand Up @@ -258,9 +254,9 @@ async def setup_scalapb_shim_classfiles(

scalapb_shim_source = FileContent("ScalaPBShim.scala", scalapb_shim_content)

lockfile_request, scala_artifacts = await MultiGet(
Get(GenerateJvmLockfileFromTool, ScalapbcToolLockfileSentinel()),
Get(ScalaArtifactsForVersionResult, ScalaArtifactsForVersionRequest(SHIM_SCALA_VERSION)),
lockfile_request = GenerateJvmLockfileFromTool.create(scalapb)
scala_artifacts = await Get(
ScalaArtifactsForVersionResult, ScalaArtifactsForVersionRequest(SHIM_SCALA_VERSION)
)
tool_classpath, shim_classpath, source_digest = await MultiGet(
Get(
Expand Down Expand Up @@ -310,21 +306,14 @@ async def setup_scalapb_shim_classfiles(
return ScalaPBShimCompiledClassfiles(digest=stripped_classfiles_digest)


@rule
def generate_scalapbc_lockfile_request(
_: ScalapbcToolLockfileSentinel, tool: ScalaPBSubsystem
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(tool)


def rules():
return [
*collect_rules(),
*lockfile.rules(),
*dependency_inference.rules(),
*symbol_mapper.rules(),
UnionRule(GenerateSourcesRequest, GenerateScalaFromProtobufRequest),
UnionRule(GenerateToolLockfileSentinel, ScalapbcToolLockfileSentinel),
UnionRule(ExportableTool, ScalaPBSubsystem),
ProtobufSourceTarget.register_plugin_field(PrefixedJvmJdkField),
ProtobufSourcesGeneratorTarget.register_plugin_field(PrefixedJvmJdkField),
ProtobufSourceTarget.register_plugin_field(PrefixedJvmResolveField),
Expand Down
19 changes: 4 additions & 15 deletions src/python/pants/backend/codegen/soap/java/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from pants.backend.java.target_types import JavaSourceField
from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.engine.fs import (
AddPrefix,
CreateDigest,
Expand All @@ -41,7 +41,7 @@
from pants.jvm.jdk_rules import InternalJdk, JvmProcess
from pants.jvm.resolve import jvm_tool
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.jvm.target_types import PrefixedJvmJdkField, PrefixedJvmResolveField
from pants.source.source_root import SourceRoot, SourceRootRequest
from pants.util.logging import LogLevel
Expand All @@ -52,10 +52,6 @@ class GenerateJavaFromWsdlRequest(GenerateSourcesRequest):
output = JavaSourceField


class JaxWsToolsLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = JaxWsTools.options_scope


@dataclass(frozen=True)
class CompileWsdlSourceRequest:
digest: Digest
Expand Down Expand Up @@ -111,7 +107,7 @@ async def compile_wsdl_source(
output_dir = "_generated_files"
toolcp_relpath = "__toolcp"

lockfile_request = await Get(GenerateJvmLockfileFromTool, JaxWsToolsLockfileSentinel())
lockfile_request = GenerateJvmLockfileFromTool.create(jaxws)
tool_classpath, subsetted_input_digest, empty_output_dir = await MultiGet(
Get(
ToolClasspath,
Expand Down Expand Up @@ -174,13 +170,6 @@ async def compile_wsdl_source(
return CompiledWsdlSource(normalized_digest)


@rule
async def generate_jaxws_lockfile_request(
_: JaxWsToolsLockfileSentinel, jaxws: JaxWsTools
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(jaxws)


def rules():
return [
*collect_rules(),
Expand All @@ -190,7 +179,7 @@ def rules():
*jvm_tool.rules(),
*jdk_rules.rules(),
UnionRule(GenerateSourcesRequest, GenerateJavaFromWsdlRequest),
UnionRule(GenerateToolLockfileSentinel, JaxWsToolsLockfileSentinel),
UnionRule(ExportableTool, JaxWsTools),
WsdlSourceTarget.register_plugin_field(PrefixedJvmJdkField),
WsdlSourcesGeneratorTarget.register_plugin_field(PrefixedJvmJdkField),
WsdlSourceTarget.register_plugin_field(PrefixedJvmResolveField),
Expand Down
19 changes: 4 additions & 15 deletions src/python/pants/backend/codegen/thrift/scrooge/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ThriftSourcesGeneratorTarget,
ThriftSourceTarget,
)
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import CreateDigest, Digest, Directory, MergeDigests, RemovePrefix, Snapshot
from pants.engine.internals.selectors import Get, MultiGet
Expand All @@ -26,7 +26,7 @@
from pants.jvm.goals import lockfile
from pants.jvm.jdk_rules import InternalJdk, JvmProcess
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.jvm.target_types import PrefixedJvmJdkField, PrefixedJvmResolveField
from pants.source.source_root import SourceRootsRequest, SourceRootsResult
from pants.util.logging import LogLevel
Expand All @@ -44,10 +44,6 @@ class GeneratedScroogeThriftSources:
snapshot: Snapshot


class ScroogeToolLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = ScroogeSubsystem.options_scope


@rule
async def generate_scrooge_thrift_sources(
request: GenerateScroogeThriftSourcesRequest,
Expand All @@ -57,7 +53,7 @@ async def generate_scrooge_thrift_sources(
output_dir = "_generated_files"
toolcp_relpath = "__toolcp"

lockfile_request = await Get(GenerateJvmLockfileFromTool, ScroogeToolLockfileSentinel())
lockfile_request = GenerateJvmLockfileFromTool.create(scrooge)
tool_classpath, transitive_targets, empty_output_dir_digest, wrapped_target = await MultiGet(
Get(ToolClasspath, ToolClasspathRequest(lockfile=lockfile_request)),
Get(TransitiveTargets, TransitiveTargetsRequest([request.thrift_source_field.address])),
Expand Down Expand Up @@ -141,19 +137,12 @@ async def generate_scrooge_thrift_sources(
return GeneratedScroogeThriftSources(output_snapshot)


@rule
def generate_scrooge_lockfile_request(
_: ScroogeToolLockfileSentinel, scrooge: ScroogeSubsystem
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(scrooge)


def rules():
return [
*collect_rules(),
*additional_fields.rules(),
*lockfile.rules(),
UnionRule(GenerateToolLockfileSentinel, ScroogeToolLockfileSentinel),
UnionRule(ExportableTool, ScroogeSubsystem),
ThriftSourceTarget.register_plugin_field(PrefixedJvmJdkField),
ThriftSourcesGeneratorTarget.register_plugin_field(PrefixedJvmJdkField),
ThriftSourceTarget.register_plugin_field(PrefixedJvmResolveField),
Expand Down
21 changes: 4 additions & 17 deletions src/python/pants/backend/java/lint/google_java_format/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pants.backend.java.lint.google_java_format.subsystem import GoogleJavaFormatSubsystem
from pants.backend.java.target_types import JavaSourceField
from pants.core.goals.fmt import FmtResult, FmtTargetsRequest
from pants.core.goals.generate_lockfiles import GenerateToolLockfileSentinel
from pants.core.goals.resolves import ExportableTool
from pants.core.util_rules.partitions import PartitionerType
from pants.engine.internals.selectors import Get
from pants.engine.process import ProcessResult
Expand All @@ -17,7 +17,7 @@
from pants.jvm.jdk_rules import InternalJdk, JvmProcess
from pants.jvm.resolve import jvm_tool
from pants.jvm.resolve.coursier_fetch import ToolClasspath, ToolClasspathRequest
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool, GenerateJvmToolLockfileSentinel
from pants.jvm.resolve.jvm_tool import GenerateJvmLockfileFromTool
from pants.util.logging import LogLevel
from pants.util.strutil import pluralize

Expand All @@ -41,19 +41,13 @@ class GoogleJavaFormatRequest(FmtTargetsRequest):
partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION


class GoogleJavaFormatToolLockfileSentinel(GenerateJvmToolLockfileSentinel):
resolve_name = GoogleJavaFormatSubsystem.options_scope


@rule(desc="Format with Google Java Format", level=LogLevel.DEBUG)
async def google_java_format_fmt(
request: GoogleJavaFormatRequest.Batch,
tool: GoogleJavaFormatSubsystem,
jdk: InternalJdk,
) -> FmtResult:
lockfile_request = await Get(
GenerateJvmLockfileFromTool, GoogleJavaFormatToolLockfileSentinel()
)
lockfile_request = GenerateJvmLockfileFromTool.create(tool)
tool_classpath = await Get(ToolClasspath, ToolClasspathRequest(lockfile=lockfile_request))

toolcp_relpath = "__toolcp"
Expand Down Expand Up @@ -97,17 +91,10 @@ async def google_java_format_fmt(
return await FmtResult.create(request, result)


@rule
def generate_google_java_format_lockfile_request(
_: GoogleJavaFormatToolLockfileSentinel, tool: GoogleJavaFormatSubsystem
) -> GenerateJvmLockfileFromTool:
return GenerateJvmLockfileFromTool.create(tool)


def rules():
return [
*collect_rules(),
*jvm_tool.rules(),
*GoogleJavaFormatRequest.rules(),
UnionRule(GenerateToolLockfileSentinel, GoogleJavaFormatToolLockfileSentinel),
UnionRule(ExportableTool, GoogleJavaFormatSubsystem),
]
Loading

0 comments on commit a9bdae1

Please sign in to comment.