-
-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Following the per-language `*_protobuf_library` pattern, here we add `go_protobuf_library` support and as simple example showing how to use it.
- Loading branch information
1 parent
06aa372
commit ce19e86
Showing
17 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
contrib/go/3rdparty/go/github.com/golang/protobuf/proto/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
go_remote_library(rev='v1.1.0') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
# This target must be build with protoc that recognizes `option go_package` | ||
# ./pants run contrib/go/examples/src/go/distance/ --protoc-version=3.5.1 | ||
go_binary( | ||
dependencies=[ | ||
'contrib/go/examples/src/protobuf/org/pantsbuild/example/route:route-go', | ||
] | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
"pantsbuild/example/distance" | ||
"pantsbuild/example/route" | ||
) | ||
|
||
func main() { | ||
r := &route.Route{ | ||
Name: proto.String("example_route"), | ||
Distances: []*distance.Distance{ | ||
{ | ||
Unit: proto.String("parsecs"), | ||
Number: proto.Int64(27), | ||
}, | ||
{ | ||
Unit: proto.String("mm"), | ||
Number: proto.Int64(2), | ||
}, | ||
}, | ||
} | ||
println(r.String()) | ||
} |
5 changes: 5 additions & 0 deletions
5
contrib/go/examples/src/protobuf/org/pantsbuild/example/distance/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
go_protobuf_library(name='distance-go') | ||
|
15 changes: 15 additions & 0 deletions
15
contrib/go/examples/src/protobuf/org/pantsbuild/example/distance/distances.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.distance; | ||
|
||
option go_package = "pantsbuild/example/distance"; | ||
|
||
/** | ||
* Structure for expressing distance measures: 8mm, 12 parsecs, etc. | ||
* Not so useful on its own. | ||
*/ | ||
message Distance { | ||
optional string unit = 1; | ||
required int64 number = 2; | ||
} |
9 changes: 9 additions & 0 deletions
9
contrib/go/examples/src/protobuf/org/pantsbuild/example/route/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
go_protobuf_library( | ||
name='route-go', | ||
dependencies=[ | ||
'contrib/go/examples/src/protobuf/org/pantsbuild/example/distance:distance-go', | ||
], | ||
) |
18 changes: 18 additions & 0 deletions
18
contrib/go/examples/src/protobuf/org/pantsbuild/example/route/route.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.route; | ||
|
||
import "org/pantsbuild/example/distance/distances.proto"; | ||
|
||
option go_package = "pantsbuild/example/route"; | ||
|
||
|
||
/** | ||
* Structure for expressing a simple route with distances | ||
* Not so useful on its own. | ||
*/ | ||
message Route { | ||
optional string name = 1; | ||
repeated org.pantsbuild.example.distance.Distance distances = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
contrib/go/src/python/pants/contrib/go/subsystems/protoc_gen_go.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 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) | ||
|
||
import logging | ||
import os | ||
|
||
from pants.backend.codegen.protobuf.subsystems.protoc import Protoc | ||
from pants.base.workunit import WorkUnitLabel | ||
from pants.scm.git import Git | ||
from pants.subsystem.subsystem import Subsystem, SubsystemError | ||
from pants.util.dirutil import safe_mkdir | ||
from pants.util.memo import memoized_method | ||
|
||
from pants.contrib.go.subsystems.go_distribution import GoDistribution | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ProtocGenGo(Subsystem): | ||
"""A compiled protobuf plugin that generates Go code. | ||
For details, see https://github.com/golang/protobuf | ||
""" | ||
options_scope = 'protoc-gen-go' | ||
|
||
@classmethod | ||
def register_options(cls, register): | ||
super(ProtocGenGo, cls).register_options(register) | ||
register('--version', default='v1.1.0', | ||
help='Version of protoc-gen-go plugin to use when generating code') | ||
|
||
@classmethod | ||
def subsystem_dependencies(cls): | ||
return super(ProtocGenGo, cls).subsystem_dependencies() + (Protoc.scoped(cls), GoDistribution,) | ||
|
||
@memoized_method | ||
def select(self, context): | ||
self.get_options() | ||
workdir = os.path.join(self.get_options().pants_workdir, self.options_scope, | ||
'versions', self.get_options().version) | ||
tool_path = os.path.join(workdir, 'bin/protoc-gen-go') | ||
|
||
if not os.path.exists(tool_path): | ||
safe_mkdir(workdir, clean=True) | ||
|
||
# Checkout the git repo at a given version. `go get` always gets master. | ||
repo = Git.clone('https://github.com/golang/protobuf.git', | ||
os.path.join(workdir, 'src/github.com/golang/protobuf')) | ||
repo.set_state(self.get_options().version) | ||
|
||
go = GoDistribution.global_instance() | ||
result, go_cmd = go.execute_go_cmd( | ||
cmd='install', | ||
gopath=workdir, | ||
args=['github.com/golang/protobuf/protoc-gen-go'], | ||
workunit_factory=context.new_workunit, | ||
workunit_labels=[WorkUnitLabel.BOOTSTRAP], | ||
) | ||
|
||
if result != 0: | ||
raise SubsystemError('{} failed with exit code {}'.format(go_cmd, result)) | ||
|
||
logger.info('Selected {} binary bootstrapped to: {}'.format(self.options_scope, tool_path)) | ||
return tool_path |
55 changes: 55 additions & 0 deletions
55
contrib/go/src/python/pants/contrib/go/targets/go_protobuf_library.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 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.base.payload import Payload | ||
from pants.build_graph.target import Target | ||
|
||
from pants.contrib.go.targets.go_local_source import GoLocalSource | ||
from pants.contrib.go.targets.go_target import GoTarget | ||
|
||
|
||
class GoProtobufLibrary(Target): | ||
"""A Go library generated from Protobuf IDL files.""" | ||
|
||
default_sources_globs = '*.proto' | ||
|
||
def __init__(self, | ||
address=None, | ||
payload=None, | ||
sources=None, | ||
**kwargs): | ||
""" | ||
:param sources: protobuf source files | ||
:type sources: :class:`pants.source.wrapped_globs.FilesetWithSpec` or list of strings. Paths | ||
are relative to the BUILD file's directory. | ||
""" | ||
payload = payload or Payload() | ||
payload.add_field('sources', | ||
self.create_sources_field(sources, address.spec_path, key_arg='sources')) | ||
|
||
super(GoProtobufLibrary, self).__init__(payload=payload, address=address, **kwargs) | ||
|
||
@classmethod | ||
def alias(cls): | ||
return "go_protobuf_library" | ||
|
||
|
||
class GoProtobufGenLibrary(GoTarget): | ||
|
||
def __init__(self, sources=None, address=None, payload=None, **kwargs): | ||
payload = payload or Payload() | ||
payload.add_fields({ | ||
'sources': self.create_sources_field(sources=sources, | ||
sources_rel_path=address.spec_path, | ||
key_arg='sources'), | ||
}) | ||
super(GoProtobufGenLibrary, self).__init__(address=address, payload=payload, **kwargs) | ||
|
||
@property | ||
def import_path(self): | ||
"""The import path as used in import statements in `.go` source files.""" | ||
return GoLocalSource.local_import_path(self.target_base, self.address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.