From 4099a2dc4df0fa9576cfae069620c8addcb43fe0 Mon Sep 17 00:00:00 2001 From: Denis Mikhaylov Date: Wed, 10 Mar 2021 10:27:39 +0300 Subject: [PATCH] Support Pants 1.30.x --- src/python/scalapb/pants/register.py | 6 +++- .../scalapb/pants/targets/scalapb_library.py | 10 +++--- .../scalapb/pants/targets/target_types.py | 34 +++++++++++++++++++ src/python/scalapb/pants/tasks/scalapb_gen.py | 7 ++-- 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/python/scalapb/pants/targets/target_types.py diff --git a/src/python/scalapb/pants/register.py b/src/python/scalapb/pants/register.py index 33bec75..ad438a7 100644 --- a/src/python/scalapb/pants/register.py +++ b/src/python/scalapb/pants/register.py @@ -1,14 +1,18 @@ from pants.build_graph.build_file_aliases import BuildFileAliases from scalapb.pants.targets.scalapb_library import ScalaPBLibrary +from scalapb.pants.targets.target_types import ScalaPBLibraryTarget from scalapb.pants.tasks.scalapb_gen import ScalaPBGen from pants.goal.task_registrar import TaskRegistrar as task def build_file_aliases(): return BuildFileAliases( targets={ - 'scalapb_library': ScalaPBLibrary, + ScalaPBLibrary.alias(): ScalaPBLibrary, } ) def register_goals(): task(name='scalapb-gen', action=ScalaPBGen).install('gen') + +def target_types(): + return [ScalaPBLibraryTarget] diff --git a/src/python/scalapb/pants/targets/scalapb_library.py b/src/python/scalapb/pants/targets/scalapb_library.py index 5cbe753..02aa3aa 100644 --- a/src/python/scalapb/pants/targets/scalapb_library.py +++ b/src/python/scalapb/pants/targets/scalapb_library.py @@ -1,18 +1,12 @@ from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) -import logging - from pants.backend.jvm.targets.import_jars_mixin import ImportJarsMixin from pants.backend.jvm.targets.jvm_target import JvmTarget from pants.base.payload import Payload from pants.base.payload_field import PrimitiveField - import os - -logger = logging.getLogger(__name__) - class ScalaPBLibrary(ImportJarsMixin, JvmTarget): """A Java library generated from Protocol Buffer IDL files.""" @@ -39,6 +33,10 @@ def __init__(self, }) super(ScalaPBLibrary, self).__init__(payload=payload, **kwargs) + @classmethod + def alias(cls): + return "scalapb_library" + @property def source_root(self): return os.path.normpath( diff --git a/src/python/scalapb/pants/targets/target_types.py b/src/python/scalapb/pants/targets/target_types.py new file mode 100644 index 0000000..599fe0a --- /dev/null +++ b/src/python/scalapb/pants/targets/target_types.py @@ -0,0 +1,34 @@ +from pants.engine.target import ( + COMMON_TARGET_FIELDS, + Dependencies, + Sources, + StringField, + StringSequenceField, + Target, +) + +from pants.backend.jvm.target_types import ( + COMMON_JVM_FIELDS, +) + +class Imports(StringSequenceField): + alias = "imports" + +class SourceRoot(StringField): + alias = "source_root" + +class ProtobufSources(Sources): + default = ("*.proto",) + expected_file_extensions = (".proto",) + +class ScalaPBLibraryTarget(Target): + """A Scala library generated from Protobuf IDL files.""" + + alias = "scalapb_library" + core_fields = ( + *COMMON_JVM_FIELDS, + Imports, + SourceRoot, + ProtobufSources + ) + v1_only = True diff --git a/src/python/scalapb/pants/tasks/scalapb_gen.py b/src/python/scalapb/pants/tasks/scalapb_gen.py index 36893b3..ed64be4 100644 --- a/src/python/scalapb/pants/tasks/scalapb_gen.py +++ b/src/python/scalapb/pants/tasks/scalapb_gen.py @@ -35,7 +35,7 @@ def register_options(cls, register): super(ScalaPBGen, cls).register_options(register) cls.register_jvm_tool(register, 'scalapbc') register('--protoc-version', fingerprint=True, - help='Set a specific protoc version to use.', default='330') + help='Set a specific protoc version to use.', default='3.3.0') def synthetic_target_type(self, target): return ScalaLibrary @@ -48,6 +48,8 @@ def execute_codegen(self, target, target_workdir): source_roots = self._calculate_source_roots(target) source_roots.update(self._proto_path_imports([target])) + # This is needed when used with unpacked jars + source_roots.add(target._sources_field.sources.rel_root) scalapb_options = [] if target.payload.java_conversions: @@ -69,7 +71,8 @@ def execute_codegen(self, target, target_workdir): source_roots = list(source_roots) source_roots.sort() for source_root in source_roots: - args.append('--proto_path={0}'.format(source_root)) + if (source_root != '.'): + args.append('--proto_path={0}'.format(source_root)) classpath = self.tool_classpath('scalapbc')