Skip to content

Commit

Permalink
Fixed source roots in project info for IntelliJ plugin in case of Sca…
Browse files Browse the repository at this point in the history
…laLibrary with java_sources

Testing Done:
CI is passing.

Reviewed at https://rbcommons.com/s/twitter/r/955/
  • Loading branch information
fkorotkov authored and fkorotkov committed Sep 2, 2014
1 parent 069cbb3 commit c077a3d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/python/pants/backend/jvm/tasks/depmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from __future__ import (nested_scopes, generators, division, absolute_import, with_statement,
print_function, unicode_literals)
from collections import defaultdict
import itertools
import json
import os

from pants.backend.core.tasks.console_task import ConsoleTask
from pants.backend.core.targets.dependencies import Dependencies
from pants.backend.core.targets.resources import Resources
from pants.backend.jvm.targets.jar_dependency import JarDependency
from pants.backend.jvm.targets.scala_library import ScalaLibrary
from pants.base.build_environment import get_buildroot
from pants.base.exceptions import TaskError

Expand Down Expand Up @@ -279,9 +281,15 @@ def get_target_type(target):
info['targets'].append(self._address(dep.address))
resource_target_map[dep] = current_target

roots = list(set(
[os.path.dirname(source) for source in current_target.sources_relative_to_source_root()]
java_sources_targets = list(current_target.java_sources) if isinstance(current_target, ScalaLibrary) else list()
"""
:type java_sources_targets:list[pants.base.target.Target]
"""

roots = set(itertools.chain(
*[self._sources_for_target(t) for t in java_sources_targets + [current_target]]
))

info['roots'] = map(lambda source: {
'source_root': os.path.join(get_buildroot(), current_target.target_base, source),
'package_prefix': source.replace(os.sep, '.')
Expand Down Expand Up @@ -309,3 +317,11 @@ def _resolve_jars_info(self):
for module in dep.modules_by_ref.values():
mapping[self._jar_id(module.ref)] = [artifact.path for artifact in module.artifacts]
return mapping

@staticmethod
def _sources_for_target(target):
"""
:type target:pants.base.target.Target
"""
return [os.path.dirname(source) for source in target.sources_relative_to_source_root()]

28 changes: 21 additions & 7 deletions tests/python/pants_test/tasks/test_depmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pants.backend.jvm.register import build_file_aliases as register_jvm
from pants.backend.jvm.targets.jar_dependency import JarDependency
from pants.backend.jvm.targets.jar_library import JarLibrary
from pants.backend.jvm.targets.java_library import JavaLibrary
from pants.backend.jvm.targets.java_tests import JavaTests
from pants.backend.jvm.targets.jvm_binary import JvmApp, JvmBinary
from pants.backend.jvm.targets.jvm_target import JvmTarget
Expand Down Expand Up @@ -276,28 +277,35 @@ def setUp(self):
target_type=JarLibrary,
)

second = self.make_target(
'project_info:second',
jar_lib = self.make_target(
'project_info:jar_lib',
target_type=JarLibrary,
jars=[JarDependency('org.apache', 'apache-jar', '12.12.2012')],
)

self.make_target(
'project_info:java_lib',
target_type=JavaLibrary,
sources=['com/foo/Bar.java', 'com/foo/Baz.java'],
)

self.make_target(
'project_info:third',
target_type=ScalaLibrary,
dependencies=[second],
dependencies=[jar_lib],
java_sources=['project_info:java_lib'],
)

self.make_target(
'project_info:jvm_app',
target_type=JvmApp,
dependencies=[second],
dependencies=[jar_lib],
)

self.make_target(
'project_info:jvm_target',
target_type=JvmTarget,
dependencies=[second],
dependencies=[jar_lib],
sources=['this/is/a/source/Foo.scala', 'this/is/a/source/Bar.scala'],
)

Expand All @@ -310,15 +318,15 @@ def setUp(self):
self.make_target(
'project_info:java_test',
target_type=JavaTests,
dependencies=[second],
dependencies=[jar_lib],
sources=['this/is/a/test/source/FooTest.scala'],
resources=[test_resource],
)

jvm_binary = self.make_target(
'project_info:jvm_binary',
target_type=JvmBinary,
dependencies=[second],
dependencies=[jar_lib],
)

self.make_target(
Expand Down Expand Up @@ -352,6 +360,12 @@ def test_with_dependencies(self):
args=['--test-project-info'],
targets=[self.target('project_info:third')]
))
self.assertEqual(['org.apache:apache-jar:12.12.2012'], result['targets']['project_info:third']['libraries'])
self.assertEqual(1, len(result['targets']['project_info:third']['roots']))
self.assertEqual(
'com.foo',
result['targets']['project_info:third']['roots'][0]['package_prefix']
)
self.assertEqual(['org.apache:apache-jar:12.12.2012'],
result['targets']['project_info:third']['libraries'])

Expand Down

0 comments on commit c077a3d

Please sign in to comment.