Skip to content

Commit

Permalink
[internal] Rollout Options v2 to (other dirs) (pantsbuild#14643)
Browse files Browse the repository at this point in the history
[ci skip-rust]
  • Loading branch information
thejcannon authored Feb 27, 2022
1 parent 86157e1 commit 3e80b7b
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 540 deletions.
29 changes: 12 additions & 17 deletions src/python/pants/backend/codegen/avro/java/subsystem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from pants.jvm.resolve.jvm_tool import JvmToolBase
from pants.option.custom_types import target_option
from pants.option.option_types import TargetListOption
from pants.util.docutil import git_url


Expand All @@ -20,19 +20,14 @@ class AvroSubsystem(JvmToolBase):
)
default_lockfile_url = git_url(default_lockfile_path)

@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 Java code to work. For example, "
"`['3rdparty/jvm:avro-runtime']`. These dependencies will "
"be automatically added to every `avro_sources` target. At the very least, "
"this option must be set to a `jvm_artifact` for the "
f"`org.apache.avro:avro:{cls.default_version}` runtime library."
),
)
runtime_dependencies = TargetListOption(
"--runtime-dependencies",
help=lambda cls: (
"A list of addresses to `jvm_artifact` targets for the runtime "
"dependencies needed for generated Java code to work. For example, "
"`['3rdparty/jvm:avro-runtime']`. These dependencies will "
"be automatically added to every `avro_sources` target. At the very least, "
"this option must be set to a `jvm_artifact` for the "
f"`org.apache.avro:avro:{cls.default_version}` runtime library."
),
)
29 changes: 8 additions & 21 deletions src/python/pants/backend/codegen/protobuf/protoc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from typing import List, cast

from pants.core.util_rules.external_tool import TemplatedExternalTool
from pants.engine.platform import Platform
from pants.option.option_types import BoolOption


class Protoc(TemplatedExternalTool):
Expand All @@ -29,26 +29,13 @@ class Protoc(TemplatedExternalTool):
"macos_x86_64": "osx-x86_64",
}

@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--dependency-inference",
default=True,
type=bool,
help=(
"Infer Protobuf dependencies on other Protobuf files by analyzing import "
"statements."
),
)
dependency_inference = BoolOption(
"--dependency-inference",
default=True,
help=(
"Infer Protobuf dependencies on other Protobuf files by analyzing import statements."
),
)

def generate_exe(self, plat: Platform) -> str:
return "./bin/protoc"

@property
def runtime_targets(self) -> List[str]:
return cast(List[str], self.options.runtime_targets)

@property
def dependency_inference(self) -> bool:
return cast(bool, self.options.dependency_inference)
61 changes: 27 additions & 34 deletions src/python/pants/backend/codegen/protobuf/scala/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pants.engine.addresses import UnparsedAddressInputs
from pants.jvm.resolve.jvm_tool import JvmToolBase
from pants.option.custom_types import target_option
from pants.option.option_types import StrListOption, TargetListOption
from pants.util.docutil import git_url


Expand Down Expand Up @@ -42,42 +42,35 @@ class ScalaPBSubsystem(JvmToolBase):
)
default_lockfile_url = git_url(default_lockfile_path)

@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:scalapb-runtime']`. These dependencies will "
"be automatically added to every `protobuf_sources` target. At the very least, "
"this option must be set to a `jvm_artifact` for the "
f"`com.thesamet.scalapb:scalapb-runtime_SCALAVER:{cls.default_version}` runtime library."
),
)
register(
"--jvm-plugins",
type=list,
member_type=str,
help=(
"A list of JVM-based `protoc` plugins to invoke when generating Scala code from protobuf files. "
"The format for each plugin specifier is `NAME=ARTIFACT` where NAME is the name of the "
"plugin and ARTIFACT is either the address of a `jvm_artifact` target or the colon-separated "
"Maven coordinate for the plugin's jar artifact.\n\n"
"For example, to invoke the fs2-grpc protoc plugin, the following option would work: "
"`--scalapb-jvm-plugins=fs2=org.typelevel:fs2-grpc-codegen_2.12:2.3.1`. "
"(Note: you would also need to set --scalapb-runtime-dependencies appropriately "
"to include the applicable runtime libraries for your chosen protoc plugins.)"
),
)
_runtime_dependencies = TargetListOption(
"--runtime-dependencies",
help=lambda cls: (
"A list of addresses to `jvm_artifact` targets for the runtime "
"dependencies needed for generated Scala code to work. For example, "
"`['3rdparty/jvm:scalapb-runtime']`. These dependencies will "
"be automatically added to every `protobuf_sources` target. At the very least, "
"this option must be set to a `jvm_artifact` for the "
f"`com.thesamet.scalapb:scalapb-runtime_SCALAVER:{cls.default_version}` runtime library."
),
)
_jvm_plugins = StrListOption(
"--jvm-plugins",
help=(
"A list of JVM-based `protoc` plugins to invoke when generating Scala code from protobuf files. "
"The format for each plugin specifier is `NAME=ARTIFACT` where NAME is the name of the "
"plugin and ARTIFACT is either the address of a `jvm_artifact` target or the colon-separated "
"Maven coordinate for the plugin's jar artifact.\n\n"
"For example, to invoke the fs2-grpc protoc plugin, the following option would work: "
"`--scalapb-jvm-plugins=fs2=org.typelevel:fs2-grpc-codegen_2.12:2.3.1`. "
"(Note: you would also need to set --scalapb-runtime-dependencies appropriately "
"to include the applicable runtime libraries for your chosen protoc plugins.)"
),
)

@property
def runtime_dependencies(self) -> UnparsedAddressInputs:
return UnparsedAddressInputs(self.options.runtime_dependencies, owning_address=None)
return UnparsedAddressInputs(self._runtime_dependencies, owning_address=None)

@property
def jvm_plugins(self) -> tuple[PluginArtifactSpec, ...]:
return tuple(PluginArtifactSpec.from_str(pa_str) for pa_str in self.options.jvm_plugins)
return tuple(PluginArtifactSpec.from_str(pa_str) for pa_str in self._jvm_plugins)
89 changes: 32 additions & 57 deletions src/python/pants/backend/docker/lint/hadolint/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

from __future__ import annotations

from typing import cast

from pants.core.util_rules.config_files import ConfigFilesRequest
from pants.core.util_rules.external_tool import TemplatedExternalTool
from pants.option.custom_types import file_option, shell_str
from pants.option.option_types import ArgsListOption, BoolOption, FileOption
from pants.util.docutil import bin_name


Expand All @@ -33,66 +31,43 @@ class Hadolint(TemplatedExternalTool):
"linux_x86_64": "Linux-x86_64",
}

@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--skip",
type=bool,
default=False,
help=f"Don't use Hadolint when running `{bin_name()} lint`.",
)
register(
"--args",
type=list,
member_type=shell_str,
help=(
"Arguments to pass directly to Hadolint, e.g. `--hadolint-args='--format json'`.'"
),
)
register(
"--config",
type=file_option,
default=None,
advanced=True,
help=(
"Path to an YAML config file understood by Hadolint "
"(https://github.com/hadolint/hadolint#configure).\n\n"
f"Setting this option will disable `[{cls.options_scope}].config_discovery`. Use "
"this option if the config is located in a non-standard location."
),
)
register(
"--config-discovery",
type=bool,
default=True,
advanced=True,
help=(
"If true, Pants will include all relevant config files during runs "
"(`.hadolint.yaml` and `.hadolint.yml`).\n\n"
f"Use `[{cls.options_scope}].config` instead if your config is in a "
"non-standard location."
),
)

@property
def skip(self) -> bool:
return cast(bool, self.options.skip)

@property
def args(self) -> tuple[str, ...]:
return tuple(self.options.args)

@property
def config(self) -> str | None:
return cast("str | None", self.options.config)
skip = BoolOption(
"--skip",
default=False,
help=f"Don't use Hadolint when running `{bin_name()} lint`.",
)
args = ArgsListOption(
help=("Arguments to pass directly to Hadolint, e.g. `--hadolint-args='--format json'`.'"),
)
config = FileOption(
"--config",
default=None,
advanced=True,
help=lambda cls: (
"Path to an YAML config file understood by Hadolint "
"(https://github.com/hadolint/hadolint#configure).\n\n"
f"Setting this option will disable `[{cls.options_scope}].config_discovery`. Use "
"this option if the config is located in a non-standard location."
),
)
config_discovery = BoolOption(
"--config-discovery",
default=True,
advanced=True,
help=lambda cls: (
"If true, Pants will include all relevant config files during runs "
"(`.hadolint.yaml` and `.hadolint.yml`).\n\n"
f"Use `[{cls.options_scope}].config` instead if your config is in a "
"non-standard location."
),
)

def config_request(self) -> ConfigFilesRequest:
# Refer to https://github.com/hadolint/hadolint#configure for how config files are
# discovered.
return ConfigFilesRequest(
specified=self.config,
specified_option_name=f"[{self.options_scope}].config",
discovery=cast(bool, self.options.config_discovery),
discovery=self.config_discovery,
check_existence=[".hadolint.yaml", ".hadolint.yml"],
)
41 changes: 14 additions & 27 deletions src/python/pants/backend/java/lint/google_java_format/subsystem.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from typing import cast

from pants.jvm.resolve.jvm_tool import JvmToolBase
from pants.option.option_types import BoolOption
from pants.util.docutil import bin_name, git_url


Expand All @@ -19,29 +19,16 @@ class GoogleJavaFormatSubsystem(JvmToolBase):
default_lockfile_path = "src/python/pants/backend/java/lint/google_java_format/google_java_format.default.lockfile.txt"
default_lockfile_url = git_url(default_lockfile_path)

@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--skip",
type=bool,
default=False,
help=f"Don't use Google Java Format when running `{bin_name()} fmt` and `{bin_name()} lint`",
)
register(
"--aosp",
type=bool,
default=False,
help=(
"Use AOSP style instead of Google Style (4-space indentation). "
'("AOSP" is the Android Open Source Project.)'
),
)

@property
def skip(self) -> bool:
return cast(bool, self.options.skip)

@property
def aosp(self) -> bool:
return cast(bool, self.options.aosp)
skip = BoolOption(
"--skip",
default=False,
help=f"Don't use Google Java Format when running `{bin_name()} fmt` and `{bin_name()} lint`",
)
aosp = BoolOption(
"--aosp",
default=False,
help=(
"Use AOSP style instead of Google Style (4-space indentation). "
'("AOSP" is the Android Open Source Project.)'
),
)
17 changes: 5 additions & 12 deletions src/python/pants/backend/java/subsystems/junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.jvm.resolve.jvm_tool import JvmToolBase
from pants.option.custom_types import shell_str
from pants.option.option_types import ArgsListOption
from pants.util.docutil import git_url


Expand All @@ -20,14 +20,7 @@ class JUnit(JvmToolBase):
default_lockfile_path = "src/python/pants/jvm/test/junit.default.lockfile.txt"
default_lockfile_url = git_url(default_lockfile_path)

@classmethod
def register_options(cls, register):
super().register_options(register)

register(
"--args",
type=list,
member_type=shell_str,
passthrough=True,
help="Arguments to pass directly to JUnit, e.g. `--disable-ansi-colors`",
)
args = ArgsListOption(
passthrough=True,
help="Arguments to pass directly to JUnit, e.g. `--disable-ansi-colors`",
)
Loading

0 comments on commit 3e80b7b

Please sign in to comment.