Skip to content

Commit

Permalink
Add tailor support for Scala (pantsbuild#13565)
Browse files Browse the repository at this point in the history
Only creates `scala_sources` targets for now. Also moves `pants.backend.java.tailor` to `pants.backend.java.goals.tailor` to align with other backends.

Fixes pantsbuild#13529.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
stuhood authored Nov 10, 2021
1 parent ae6d6da commit 319a0ee
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/python/pants/backend/experimental/java/register.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.java import tailor
from pants.backend.java.compile import javac
from pants.backend.java.dependency_inference import (
import_parser,
Expand All @@ -10,7 +9,7 @@
package_mapper,
)
from pants.backend.java.dependency_inference import rules as dependency_inference_rules
from pants.backend.java.goals import check
from pants.backend.java.goals import check, tailor
from pants.backend.java.package import deploy_jar
from pants.backend.java.target_types import (
DeployJar,
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/backend/experimental/scala/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pants.backend.java.test import junit # TODO: Should move to the JVM package.
from pants.backend.scala.compile import scalac
from pants.backend.scala.dependency_inference import scala_parser
from pants.backend.scala.goals import check
from pants.backend.scala.goals import check, tailor
from pants.backend.scala.target_types import ScalaSourcesGeneratorTarget, ScalaSourceTarget
from pants.backend.scala.target_types import rules as target_types_rules
from pants.jvm import classpath, jdk_rules
Expand All @@ -36,6 +36,7 @@ def rules():
return [
*scalac.rules(),
*check.rules(),
*tailor.rules(),
*classpath.rules(),
*junit.rules(),
*deploy_jar.rules(),
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/backend/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()

python_tests(name="tests")
4 changes: 4 additions & 0 deletions src/python/pants/backend/java/goals/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()

python_tests(
name="tests",
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pytest

from pants.backend.java import tailor
from pants.backend.java.tailor import PutativeJavaTargetsRequest, classify_source_files
from pants.backend.java.goals import tailor
from pants.backend.java.goals.tailor import PutativeJavaTargetsRequest, classify_source_files
from pants.backend.java.target_types import JavaSourcesGeneratorTarget, JunitTestsGeneratorTarget
from pants.core.goals.tailor import (
AllOwnedSources,
Expand Down
65 changes: 65 additions & 0 deletions src/python/pants/backend/scala/goals/tailor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

import os
from dataclasses import dataclass
from typing import Iterable

from pants.backend.scala.target_types import ScalaSourcesGeneratorTarget
from pants.core.goals.tailor import (
AllOwnedSources,
PutativeTarget,
PutativeTargets,
PutativeTargetsRequest,
group_by_dir,
)
from pants.engine.fs import PathGlobs, Paths
from pants.engine.internals.selectors import Get
from pants.engine.rules import collect_rules, rule
from pants.engine.target import Target
from pants.engine.unions import UnionRule
from pants.util.logging import LogLevel


@dataclass(frozen=True)
class PutativeScalaTargetsRequest(PutativeTargetsRequest):
pass


def classify_source_files(paths: Iterable[str]) -> dict[type[Target], set[str]]:
"""Returns a dict of target type -> files that belong to targets of that type."""
# TODO: Until https://github.com/pantsbuild/pants/issues/13332 is fixed, we classify all scala
# files as `scala_sources`, and none as tests.
return {ScalaSourcesGeneratorTarget: set(paths)}


@rule(level=LogLevel.DEBUG, desc="Determine candidate Scala targets to create")
async def find_putative_targets(
req: PutativeScalaTargetsRequest,
all_owned_sources: AllOwnedSources,
) -> PutativeTargets:
all_scala_files_globs = req.search_paths.path_globs("*.scala")
all_scala_files = await Get(Paths, PathGlobs, all_scala_files_globs)
unowned_scala_files = set(all_scala_files.files) - set(all_owned_sources)
classified_unowned_scala_files = classify_source_files(unowned_scala_files)

putative_targets = []
for tgt_type, paths in classified_unowned_scala_files.items():
for dirname, filenames in group_by_dir(paths).items():
name = os.path.basename(dirname)
putative_targets.append(
PutativeTarget.for_target_type(
tgt_type, dirname, name, sorted(filenames), kwargs={}
)
)

return PutativeTargets(putative_targets)


def rules():
return [
*collect_rules(),
UnionRule(PutativeTargetsRequest, PutativeScalaTargetsRequest),
]

0 comments on commit 319a0ee

Please sign in to comment.