Skip to content

Instantly share code, notes, and snippets.

@Eric-Arellano

Eric-Arellano/register.py

Last active May 29, 2020
Embed
What would you like to do?
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.payload_field import PrimitiveField
from pants.build_graph.target import Target as TargetV1
from pants.engine.target import Target as TargetV2
from pants.engine.target import COMMON_TARGET_FIELDS, Dependencies, IntField, Sources
"""This sets up a new target called `elixir_tests`, with both a V1 definition and a V2 Target API definition.
In a BUILD file, this would look like:
elixir_tests(
name="demo",
sources=["test_demo.ex"],
timeout=120,
)
"""
class ElixirTestTargetV1(TargetV1):
default_sources_globs = ("test_*.ex",)
@classmethod
def alias(cls):
return "elixir_tests"
def __init__(self, sources=None, timeout=None, payload=None, **kwargs):
if timeout < 0:
raise ValueError(f"Invalid timeout field: {timeout}. Must be > 0.")
payload = payload or Payload()
payload.add_fields(
{
"sources": self.create_sources_field(
sources=sources, sources_rel_path=address.spec_path, key_arg="sources"
),
"timeout": PrimitiveField(timeout),
}
)
super().__init__(payload=payload, **kwargs)
class ElixirSources(Sources):
default = ("test_*.ex",)
class ElixirTestTimeout(IntField):
alias = "timeout"
class ElixirTestTargetV2(TargetV2):
alias = "elixir_tests"
core_fields = (*COMMON_TARGET_FIELDS, Dependencies, ElixirSources, ElixirTestTimeout)
def build_file_aliases():
return BuildFileAliases(targets={ElixirTestTargetV1.alias(): ElixirTestTargetV1})
def target_types():
return [ElixirTestTargetV2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment