-
-
Notifications
You must be signed in to change notification settings - Fork 636
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
[internal] refactor scrooge codegen to allow for multiple languages #14030
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() | ||
|
||
python_tests(name="tests") |
File renamed without changes.
72 changes: 72 additions & 0 deletions
72
src/python/pants/backend/codegen/thrift/scrooge/scala/rules.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from pants.backend.codegen.thrift.scrooge.rules import ( | ||
GeneratedScroogeThriftSources, | ||
GenerateScroogeThriftSourcesRequest, | ||
) | ||
from pants.backend.codegen.thrift.scrooge.scala.subsystem import ScroogeScalaSubsystem | ||
from pants.backend.codegen.thrift.target_types import ThriftDependenciesField, ThriftSourceField | ||
from pants.backend.java.target_types import JavaSourceField | ||
from pants.engine.addresses import Addresses, UnparsedAddressInputs | ||
from pants.engine.fs import AddPrefix, Digest, Snapshot | ||
from pants.engine.internals.selectors import Get | ||
from pants.engine.rules import collect_rules, rule | ||
from pants.engine.target import ( | ||
GeneratedSources, | ||
GenerateSourcesRequest, | ||
InjectDependenciesRequest, | ||
InjectedDependencies, | ||
) | ||
from pants.engine.unions import UnionRule | ||
from pants.source.source_root import SourceRoot, SourceRootRequest | ||
from pants.util.logging import LogLevel | ||
|
||
|
||
class GenerateScalaFromThriftRequest(GenerateSourcesRequest): | ||
input = ThriftSourceField | ||
output = JavaSourceField | ||
|
||
|
||
class InjectScroogeScalaDependencies(InjectDependenciesRequest): | ||
inject_for = ThriftDependenciesField | ||
|
||
|
||
@rule(desc="Generate Scala from Thrift with Scrooge", level=LogLevel.DEBUG) | ||
async def generate_scala_from_thrift_with_scrooge( | ||
request: GenerateScalaFromThriftRequest, | ||
) -> GeneratedSources: | ||
result = await Get( | ||
GeneratedScroogeThriftSources, | ||
GenerateScroogeThriftSourcesRequest( | ||
thrift_source_field=request.protocol_target[ThriftSourceField], | ||
lang_id="scala", | ||
lang_name="Scala", | ||
), | ||
) | ||
|
||
source_root = await Get( | ||
SourceRoot, SourceRootRequest, SourceRootRequest.for_target(request.protocol_target) | ||
) | ||
|
||
source_root_restored = ( | ||
await Get(Snapshot, AddPrefix(result.snapshot.digest, source_root.path)) | ||
if source_root.path != "." | ||
else await Get(Snapshot, Digest, result.snapshot.digest) | ||
) | ||
return GeneratedSources(source_root_restored) | ||
|
||
|
||
@rule | ||
async def inject_scrooge_scala_dependencies( | ||
_: InjectScroogeScalaDependencies, scrooge: ScroogeScalaSubsystem | ||
) -> InjectedDependencies: | ||
addresses = await Get(Addresses, UnparsedAddressInputs, scrooge.runtime_dependencies) | ||
return InjectedDependencies(addresses) | ||
|
||
|
||
def rules(): | ||
return ( | ||
*collect_rules(), | ||
UnionRule(GenerateSourcesRequest, GenerateScalaFromThriftRequest), | ||
UnionRule(InjectDependenciesRequest, InjectScroogeScalaDependencies), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/python/pants/backend/codegen/thrift/scrooge/scala/subsystem.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from __future__ import annotations | ||
|
||
from pants.backend.codegen.thrift.scrooge.subsystem import ScroogeSubsystem | ||
from pants.engine.addresses import UnparsedAddressInputs | ||
from pants.option.custom_types import target_option | ||
from pants.option.subsystem import Subsystem | ||
|
||
|
||
class ScroogeScalaSubsystem(Subsystem): | ||
options_scope = "scrooge-scala" | ||
help = "Scala-specific options for the Scrooge Thrift IDL compiler (https://twitter.github.io/scrooge/)." | ||
|
||
@classmethod | ||
def register_options(cls, register): | ||
super().register_options(register) | ||
register( | ||
"--runtime-dependencies", | ||
type=list, | ||
member_type=target_option, | ||
help=( | ||
"A list of addresses to `jvm_artifact` targets for the runtime " | ||
"dependencies needed for generated Scala code to work. For example, " | ||
"`['3rdparty/jvm:scrooge-runtime']`. These dependencies will " | ||
"be automatically added to every `thrift_source` target. At the very least, " | ||
"this option must be set to a `jvm_artifact` for the " | ||
f"`com.twitter:scrooge-runtime_SCALAVER:{ScroogeSubsystem.default_version}` runtime library." | ||
), | ||
) | ||
|
||
@property | ||
def runtime_dependencies(self) -> UnparsedAddressInputs: | ||
return UnparsedAddressInputs(self.options.runtime_dependencies, owning_address=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like how you organize these backends :)