Skip to content

Commit

Permalink
Upstream the scala_jar and scala_artifact helpers to suffix the scala…
Browse files Browse the repository at this point in the history
… platform version.

Add helpers for suffixing the scala platform version on libraries. We've been using this internally to ease scala upgrades by centralizing the platform version. We could of course do much, much more with this, so consider this revision a strawman.

- Add scala_jar for use with remote dependencies
- Add scala_artifact for use in provides clauses

Most likely what this should do instead would be to define classpaths for each platform, with the major version as a key.

Testing Done:
#2273

Reviewed at https://rbcommons.com/s/twitter/r/2891/
  • Loading branch information
stuhood committed Sep 28, 2015
1 parent 509060b commit 43f4b2d
Show file tree
Hide file tree
Showing 40 changed files with 198 additions and 78 deletions.
14 changes: 7 additions & 7 deletions 3rdparty/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ jar_library(name='protobuf-test-import',
jar_library(name='scrooge-core',
jars = [
# used by scrooge-generator in BUILD.tools:scrooge-gen
jar(org='com.twitter', name='scrooge-core_2.10', rev='3.20.0'),
scala_jar(org='com.twitter', name='scrooge-core', rev='3.20.0'),
])

jar_library(name='shapeless',
jars=[
jar(org='com.chuusai', name='shapeless_2.10.4', rev='2.0.0'),
scala_jar(org='com.chuusai', name='shapeless', rev='2.2.5'),
])

jar_library(name='slf4j-api',
jars = [
jar(org='org.slf4j', name='slf4j-api', rev='1.6.1'),
])

jar_library(name = 'spindle-runtime',
jars = [
jar(org = 'com.foursquare', name = 'spindle-runtime_2.10', rev = '3.0.0-M7'),
jar_library(name='spindle-runtime',
jars=[
scala_jar(org='com.foursquare', name='spindle-runtime', rev='3.0.0-M7'),
],
dependencies = [
dependencies=[
'contrib/spindle/3rdparty:rogue',
])

Expand Down Expand Up @@ -158,5 +158,5 @@ jar_library(name='junit',

jar_library(name='scalatest',
jars=[
jar('org.scalatest', 'scalatest_2.10', '2.2.4')
scala_jar('org.scalatest', 'scalatest', '2.2.4')
])
4 changes: 2 additions & 2 deletions 3rdparty/jvm/com/twitter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

jar_library(name='finagle-thrift',
jars=[
jar(org='com.twitter', name='finagle-thrift_2.10', rev='6.28.0',
scala_jar(org='com.twitter', name='finagle-thrift', rev='6.28.0',
excludes=[
exclude(org = 'org.apache.thrift', name = 'libthrift'),
]),
Expand All @@ -15,7 +15,7 @@ jar_library(name='finagle-thrift',

jar_library(name='scrooge-core',
jars=[
jar(org='com.twitter', name='scrooge-core_2.10', rev='3.20.0',
scala_jar(org='com.twitter', name='scrooge-core', rev='3.20.0',
excludes=[
exclude(org = 'org.apache.thrift', name = 'libthrift'),
]),
Expand Down
24 changes: 12 additions & 12 deletions BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ jar_library(name = 'scala-repl',

jar_library(name = 'scalastyle',
jars = [
jar(org='org.scalastyle', name='scalastyle_2.10', rev='0.3.2')
scala_jar(org='org.scalastyle', name='scalastyle', rev='0.3.2')
])

jar_library(name = 'scrooge-gen',
jars = [
jar(org='com.twitter', name='scrooge-generator_2.10', rev='3.20.0',
excludes=[
# scrooge requires libthrift 0.5.0-1 which is not available on
# the default maven repos. Force scrooge to use thrift-0.6.1, which
# is compatible, instead.
exclude(org = 'org.apache.thrift', name = 'libthrift')
])
scala_jar(org='com.twitter', name='scrooge-generator', rev='3.20.0',
excludes=[
# scrooge requires libthrift 0.5.0-1 which is not available on
# the default maven repos. Force scrooge to use thrift-0.6.1, which
# is compatible, instead.
exclude(org = 'org.apache.thrift', name = 'libthrift')
])
],
dependencies = [
'3rdparty:thrift-0.6.1',
])

jar_library(name = 'scrooge-linter',
jars = [
jar(org='com.twitter', name='scrooge-linter_2.10', rev='3.20.0',
excludes=[
exclude(org = 'org.apache.thrift', name = 'libthrift')
])
scala_jar(org='com.twitter', name='scrooge-linter', rev='3.20.0',
excludes=[
exclude(org = 'org.apache.thrift', name = 'libthrift')
])
],
dependencies = [
'3rdparty:thrift-0.6.1',
Expand Down
10 changes: 5 additions & 5 deletions contrib/spindle/3rdparty/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
rogue_rev = '3.0.0-beta13.1'
jar_library(name = 'rogue',
jars = [
jar(org = 'com.foursquare', name = 'rogue-core_2.10', rev = rogue_rev),
jar(org = 'com.foursquare', name = 'rogue-lift_2.10', rev = rogue_rev),
jar(org = 'com.foursquare', name = 'rogue-spindle_2.10', rev = rogue_rev),
jar(org = 'com.foursquare', name = 'rogue-index_2.10', rev = rogue_rev),
jar(org = 'com.foursquare', name = 'rogue-field_2.10', rev = '2.2.1'),
scala_jar(org = 'com.foursquare', name = 'rogue-core', rev = rogue_rev),
scala_jar(org = 'com.foursquare', name = 'rogue-lift', rev = rogue_rev),
scala_jar(org = 'com.foursquare', name = 'rogue-spindle', rev = rogue_rev),
scala_jar(org = 'com.foursquare', name = 'rogue-index', rev = rogue_rev),
scala_jar(org = 'com.foursquare', name = 'rogue-field', rev = '2.2.1'),
],
)
6 changes: 3 additions & 3 deletions examples/src/scala/org/pantsbuild/example/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ scala_library(name = 'jvm-run-example-lib',
'examples/src/scala/org/pantsbuild/example/hello/welcome',
],
sources = ['JvmRunExample.scala'],
provides = artifact(org='org.pantsbuild.example',
name='jvm-example-lib',
repo=public,)
provides = scala_artifact(org='org.pantsbuild.example',
name='jvm-example-lib',
repo=public,)
)

jvm_binary(name ='jvm-run-example',
Expand Down
6 changes: 3 additions & 3 deletions examples/src/scala/org/pantsbuild/example/hello/welcome/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ scala_library(name='welcome',
resources = [
'examples/src/resources/org/pantsbuild/example/hello',
],
provides = artifact(org='org.pantsbuild.example.hello',
name='welcome',
repo=public,),
provides = scala_artifact(org='org.pantsbuild.example.hello',
name='welcome',
repo=public,),
)
11 changes: 11 additions & 0 deletions src/python/pants/backend/jvm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ python_library(
':artifact',
':ossrh_publication_metadata',
':repository',
':scala_artifact',
'src/python/pants/backend/core/tasks:group_task',
'src/python/pants/backend/jvm/subsystems:shader',
'src/python/pants/backend/jvm/targets:all',
Expand Down Expand Up @@ -65,6 +66,16 @@ python_library(
sources=['repository.py'],
)

python_library(
name='scala_artifact',
sources=['scala_artifact.py'],
dependencies=[
':artifact',
'src/python/pants/backend/jvm/subsystems:scala_platform',
'src/python/pants/util:memo',
],
)

python_library(
name='ossrh_publication_metadata',
sources=['ossrh_publication_metadata.py'],
Expand Down
10 changes: 9 additions & 1 deletion src/python/pants/backend/jvm/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ def __init__(self, org, name, repo, publication_metadata=None):
.format(PublicationMetadata, type(publication_metadata)))

self.org = org
self.name = name
self._base_name = name
self.repo = repo
self.publication_metadata = publication_metadata

@property
def name(self):
return self._base_name

@name.setter
def name(self, value):
self._base_name = value

def __eq__(self, other):
return (type(other) == Artifact and
self.org == other.org and
Expand Down
9 changes: 9 additions & 0 deletions src/python/pants/backend/jvm/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pants.backend.jvm.ossrh_publication_metadata import (Developer, License,
OSSRHPublicationMetadata, Scm)
from pants.backend.jvm.repository import Repository as repo
from pants.backend.jvm.scala_artifact import ScalaArtifact
from pants.backend.jvm.subsystems.scala_platform import ScalaPlatform
from pants.backend.jvm.subsystems.shader import Shading
from pants.backend.jvm.targets.annotation_processor import AnnotationProcessor
from pants.backend.jvm.targets.benchmark import Benchmark
Expand All @@ -22,6 +24,7 @@
from pants.backend.jvm.targets.java_tests import JavaTests
from pants.backend.jvm.targets.jvm_app import Bundle, DirectoryReMapper, JvmApp
from pants.backend.jvm.targets.jvm_binary import Duplicate, JarRules, JvmBinary, Skip
from pants.backend.jvm.targets.scala_jar_dependency import ScalaJarDependency
from pants.backend.jvm.targets.scala_library import ScalaLibrary
from pants.backend.jvm.targets.scalac_plugin import ScalacPlugin
from pants.backend.jvm.targets.unpacked_jars import UnpackedJars
Expand Down Expand Up @@ -80,6 +83,7 @@ def build_file_aliases():
},
objects={
'artifact': Artifact,
'scala_artifact': ScalaArtifact,
'ossrh': OSSRHPublicationMetadata,
'license': License,
'scm': Scm,
Expand All @@ -89,6 +93,7 @@ def build_file_aliases():
'Duplicate': Duplicate,
'exclude': Exclude,
'jar': JarDependency,
'scala_jar': ScalaJarDependency,
'jar_rules': JarRules,
'Repository': Repository,
'repository': repo,
Expand All @@ -104,6 +109,10 @@ def build_file_aliases():
)


def global_subsystems():
return (ScalaPlatform,)


# TODO https://github.com/pantsbuild/pants/issues/604 register_goals
def register_goals():
ng_killall = task(name='ng-killall', action=NailgunKillall)
Expand Down
17 changes: 17 additions & 0 deletions src/python/pants/backend/jvm/scala_artifact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# coding=utf-8
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.backend.jvm.artifact import Artifact
from pants.backend.jvm.subsystems.scala_platform import ScalaPlatform


class ScalaArtifact(Artifact):
"""Extends Artifact to append the configured Scala version."""

@property
def name(self):
return ScalaPlatform.global_instance().suffix_version(self._base_name)
1 change: 1 addition & 0 deletions src/python/pants/backend/jvm/subsystems/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ python_library(
':jvm_tool_mixin',
'src/python/pants/option',
'src/python/pants/subsystem',
'src/python/pants/util:memo',
],
)

Expand Down
19 changes: 19 additions & 0 deletions src/python/pants/backend/jvm/subsystems/scala_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,30 @@ def register_options(cls, register):
# dependency for the scala_library target.
register('--runtime', advanced=True, type=list_option, default=['//:scala-library'],
help='Target specs pointing to the scala runtime libraries.')
# TODO: The choice of a platform version should likely drive automatic selection of the
# appropriate scala-library and scala-compiler dependencies.
register('--version', advanced=True, default='2.10',
help='The scala "platform version", which is suffixed onto all published '
'libraries. This should match the declared compiler/library versions.')
cls.register_jvm_tool(register, 'scalac', classpath_spec='//:scala-compiler')

def compiler_classpath(self, products):
return self.tool_classpath_from_products(products, 'scalac', scope=self.options_scope)

@property
def version(self):
return self.get_options().version

def suffix_version(self, name):
"""Appends the platform version to the given artifact name.
Also validates that the name doesn't already end with the version.
"""
if name.endswith(self.version):
raise ValueError('The name "{0}" should not be suffixed with the scala platform version '
'({1}): it will be added automatically.'.format(name, self.version))
return '{0}_{1}'.format(name, self.version)

@property
def runtime(self):
return self.get_options().runtime
4 changes: 3 additions & 1 deletion src/python/pants/backend/jvm/targets/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ python_library(
sources = [
'scala_library.py',
'scalac_plugin.py',
'scala_jar_dependency.py',
],
dependencies = [
':jvm',
'3rdparty/python/twitter/commons:twitter.common.collections',
':jvm',
'src/python/pants/backend/jvm/subsystems:scala_platform',
'src/python/pants/base:address',
'src/python/pants/base:exceptions',
'src/python/pants/base:target',
'src/python/pants/base:validation',
'src/python/pants/util:memo',
],
)
10 changes: 9 additions & 1 deletion src/python/pants/backend/jvm/targets/jar_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, org, name, rev=None, force=False, ext=None, url=None, apidocs
:type excludes: list of :class:`pants.backend.jvm.targets.exclude.Exclude`
"""
self.org = org
self.name = name
self._base_name = name
self.rev = rev
self.force = force

Expand Down Expand Up @@ -77,6 +77,14 @@ def exclude(self, org, name=None):
self.excludes += (Exclude(org, name),)
return self

@property
def name(self):
return self._base_name

@name.setter
def name(self, value):
self._base_name = value

def __str__(self):
return 'JarDependency({})'.format(self.coordinate)

Expand Down
22 changes: 22 additions & 0 deletions src/python/pants/backend/jvm/targets/scala_jar_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# coding=utf-8
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.backend.jvm.subsystems.scala_platform import ScalaPlatform
from pants.backend.jvm.targets.exclude import Exclude
from pants.backend.jvm.targets.jar_dependency import JarDependency


class ScalaJarDependency(JarDependency):
"""A JarDependency with the configured 'scala-compile: platform-version' automatically appended.
This allows for more natural consumption of cross-published scala libraries, which have their
scala version/platform appended to the artifact name.
"""

@property
def name(self):
return ScalaPlatform.global_instance().suffix_version(self._base_name)
2 changes: 1 addition & 1 deletion src/scala/org/pantsbuild/zinc/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala_library(
name='zinc',
provides=artifact(
provides=scala_artifact(
org='org.pantsbuild',
name='zinc',
repo=public,
Expand Down
2 changes: 1 addition & 1 deletion src/scala/org/pantsbuild/zinc/cache/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala_library(
name='cache',
provides=artifact(
provides=scala_artifact(
org='org.pantsbuild',
name='zinc-cache',
repo=public,
Expand Down
2 changes: 1 addition & 1 deletion src/scala/org/pantsbuild/zinc/logging/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala_library(
name='logging',
provides=artifact(
provides=scala_artifact(
org='org.pantsbuild',
name='zinc-logging',
repo=public,
Expand Down
2 changes: 1 addition & 1 deletion src/scala/sbt/compiler/javac/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala_library(
name='javac',
provides=artifact(
provides=scala_artifact(
org='org.pantsbuild',
name='zinc-sbt-compiler-javac',
repo=public,
Expand Down
2 changes: 1 addition & 1 deletion src/scala/sbt/inc/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scala_library(
name='inc',
provides=artifact(
provides=scala_artifact(
org='org.pantsbuild',
name='zinc-sbt-inc',
repo=public,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ scala_library(name = 'jvm-run-example-lib',
'testprojects/src/scala/org/pantsbuild/testproject/publish/hello/welcome',
],
sources = ['JvmRunExample.scala'],
provides = artifact(org='org.pantsbuild.testproject.publish',
name='jvm-example-lib',
repo=testing,)
provides = scala_artifact(org='org.pantsbuild.testproject.publish',
name='jvm-example-lib',
repo=testing,)
)

0 comments on commit 43f4b2d

Please sign in to comment.