Repository URL to install this package:
|
Version:
1.26.0.dev0+gite506aa5f ▾
|
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from abc import abstractmethod
from pants.backend.jvm.subsystems.scalafmt import Scalafmt
from pants.backend.jvm.tasks.rewrite_base import RewriteBase
from pants.base.deprecated import resolve_conflicting_options
from pants.base.exceptions import TaskError
from pants.java.jar.jar_dependency import JarDependency
from pants.option.custom_types import file_option
from pants.task.fmt_task_mixin import FmtTaskMixin
from pants.task.lint_task_mixin import LintTaskMixin
class ScalafmtTask(RewriteBase):
"""Abstract class to run ScalaFmt commands.
Classes that inherit from this should override additional_args and process_result to run
different scalafmt commands.
"""
def _resolve_conflicting_options(self, *, old_option: str, new_option: str):
return resolve_conflicting_options(
old_option=old_option,
new_option=new_option,
old_scope="fmt-scalafmt",
new_scope="scalafmt",
old_container=self.get_options(),
new_container=Scalafmt.global_instance().options,
)
def _resolve_conflicting_skip(self, *, old_scope: str):
# Skip mypy because this is a temporary hack, and mypy doesn't follow the inheritance chain
# properly.
return self.resolve_conflicting_skip_options( # type: ignore
old_scope=old_scope, new_scope="scalafmt", subsystem=Scalafmt.global_instance(),
)
@classmethod
def subsystem_dependencies(cls):
return super().subsystem_dependencies() + (Scalafmt,)
@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--configuration",
advanced=True,
type=file_option,
fingerprint=True,
removal_version="1.27.0.dev0",
removal_hint="Use `--scalafmt-config` instead.",
help="Path to scalafmt config file, if not specified default scalafmt config used",
)
cls.register_jvm_tool(
register,
"scalafmt",
classpath=[JarDependency(org="com.geirsson", name="scalafmt-cli_2.11", rev="1.5.1")],
)
@classmethod
def target_types(cls):
return ["scala_library", "junit_tests"]
@classmethod
def source_extension(cls):
return ".scala"
@classmethod
def implementation_version(cls):
return super().implementation_version() + [("ScalaFmt", 5)]
def invoke_tool(self, absolute_root, target_sources):
args = list(self.additional_args)
config = self._resolve_conflicting_options(old_option="configuration", new_option="config")
if config is not None:
args.extend(["--config", config])
args.extend([source for _target, source in target_sources])
return self.runjava(
classpath=self.tool_classpath("scalafmt"),
main="org.scalafmt.cli.Cli",
args=args,
workunit_name="scalafmt",
jvm_options=self.get_options().jvm_options,
)
@property
@abstractmethod
def additional_args(self):
"""Returns the arguments used to run Scalafmt command.
The return value should be an array of strings. For example, to run the Scalafmt help
command: ['--help']
"""
class ScalaFmtCheckFormat(LintTaskMixin, ScalafmtTask):
"""This Task checks that all scala files in the target are formatted correctly.
If the files are not formatted correctly an error is raised
including the command to run to format the files correctly
:API: public
"""
sideeffecting = False
additional_args = ["--test"]
@property
def skip_execution(self):
return super()._resolve_conflicting_skip(old_scope="lint-scalafmt")
def process_result(self, result):
if result != 0:
raise TaskError(
"Scalafmt failed with exit code {}; to fix run: "
"`./pants fmt <targets>`".format(result),
exit_code=result,
)
class ScalaFmtFormat(FmtTaskMixin, ScalafmtTask):
"""This Task reads all scala files in the target and emits the source in a standard style as
specified by the configuration file.
This task mutates the underlying flies.
:API: public
"""
sideeffecting = True
additional_args = ["-i"]
@property
def skip_execution(self):
return super()._resolve_conflicting_skip(old_scope="fmt-scalafmt")
def process_result(self, result):
# Processes the results of running the scalafmt command.
if result != 0:
raise TaskError("Scalafmt failed to format files", exit_code=result)