Skip to content

Instantly share code, notes, and snippets.

@mateor
Last active January 29, 2018 07:26
Show Gist options
  • Save mateor/e82226832edb63c20be5aa693318a43b to your computer and use it in GitHub Desktop.
Save mateor/e82226832edb63c20be5aa693318a43b to your computer and use it in GitHub Desktop.
Go-thrift Buildgen
mateo-2:pants mateo$ git add contrib/
mateo-2:pants mateo$ git diff HEAD contrib/go/src/python/pants/contrib/go/tasks/
diff --git a/contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py b/contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py
index 9e9c377da..cae3500d2 100644
--- a/contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py
+++ b/contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py
@@ -22,6 +22,7 @@ from pants.util.dirutil import safe_mkdir, safe_open
from pants.contrib.go.subsystems.fetcher_factory import FetcherFactory
from pants.contrib.go.targets.go_binary import GoBinary
from pants.contrib.go.targets.go_library import GoLibrary
+from pants.contrib.go.targets.go_thrift_library import GoThriftLibrary
from pants.contrib.go.targets.go_local_source import GoLocalSource
from pants.contrib.go.targets.go_remote_library import GoRemoteLibrary
from pants.contrib.go.tasks.go_task import GoTask
@@ -69,7 +70,7 @@ class GoTargetGenerator(object):
existing = self._build_graph.get_target(local_address)
if not existing:
self._build_graph.inject_synthetic_target(address=local_address, target_type=target_type)
- elif existing and not isinstance(existing, target_type):
+ elif existing and not isinstance(existing, target_type) and not isinstance(existing, GoThriftLibrary):
raise self.WrongLocalSourceTargetTypeError('{} should be a {}'
.format(existing, target_type.__name__))
@@ -107,6 +108,15 @@ class GoTargetGenerator(object):
src_path = os.path.join(gopath, 'src', import_path)
safe_mkdir(src_path)
package_src_root = os.path.join(get_buildroot(), local_address.spec_path)
+ internal = self._build_graph.get_target(local_address)
+
+ if isinstance(internal, GoThriftLibrary):
+
+ package = os.path.basename(import_path)
+ dummy_file = os.path.join(src_path, '{}.go'.format(package))
+ with safe_open(dummy_file, 'w') as fp:
+ fp.write('package {}'.format(package))
+
for source_file in os.listdir(package_src_root):
source_path = os.path.join(package_src_root, source_file)
if GoLocalSource.is_go_source(source_path):
diff --git a/contrib/go/src/python/pants/contrib/go/tasks/go_thrift_gen.py b/contrib/go/src/python/pants/contrib/go/tasks/go_thrift_gen.py
index d940ca08a..ba6cca346 100644
--- a/contrib/go/src/python/pants/contrib/go/tasks/go_thrift_gen.py
+++ b/contrib/go/src/python/pants/contrib/go/tasks/go_thrift_gen.py
@@ -31,7 +31,7 @@ class GoThriftGen(SimpleCodegenTask):
register('--strict', default=True, fingerprint=True, type=bool,
help='Run thrift compiler with strict warnings.')
- register('--gen-options', advanced=True, fingerprint=True,
+ register('--gen-options', type=str, advanced=True, fingerprint=True,
help='Use these apache thrift go gen options.')
register('--thrift-import', type=str, advanced=True, fingerprint=True,
help='Use this thrift-import gen option to thrift.')
@@ -54,7 +54,7 @@ class GoThriftGen(SimpleCodegenTask):
def _deps(self):
thrift_import_target = self.get_options().thrift_import_target
thrift_imports = self.context.resolve(thrift_import_target)
- return thrift_imports
+ return thrift_imports or []
@memoized_property
def _service_deps(self):
@@ -104,14 +104,16 @@ class GoThriftGen(SimpleCodegenTask):
@memoized_property
def _thrift_cmd(self):
cmd = [self._thrift_binary.path]
- thrift_import = 'thrift_import={}'.format(self.get_options().thrift_import)
gen_options = self.get_options().gen_options
+ thrift_import = self.get_options().thrift_import
+ opts = []
if gen_options:
- gen_options += ',' + thrift_import
- else:
- gen_options = thrift_import
- cmd.extend(('--gen', 'go:{}'.format(gen_options)))
-
+ opts.append(gen_options)
+ if thrift_import:
+ opts.append('thrift_import={}'.format(thrift_import))
+ if opts:
+ opts = ':{}'.format(','.join(opts))
+ cmd.extend(['--gen', 'go{}'.format(opts)])
if self.get_options().strict:
cmd.append('-strict')
if self.get_options().level == 'debug':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment