Skip to content

Commit dba9529

Browse files
committed
Update the compile task to work with protoc v3
The Protocol Buffers v3 compiler (protoc) now comes with a builtin Ruby plugin. This conflicts with our local custom Ruby plugin. In order to use it, the custom plugin must be passed to the compiler, and invoked via the custom output generator: protoc --plugin=protoc-gen-ruby-protobuf=/path/to/bin/protoc-gen-ruby --ruby-protobuf_out=lib Update the compile task to take this new pattern into account so that it works with both the v2 and v3 compilers.
1 parent a3cafa7 commit dba9529

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

lib/protobuf/tasks/compile.rake

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
require 'fileutils'
1+
require "fileutils"
22

33
namespace :protobuf do
44

5-
desc 'Clean & Compile the protobuf source to ruby classes. Pass PB_NO_CLEAN=1 if you do not want to force-clean first.'
5+
desc "Clean & Compile the protobuf source to ruby classes. Pass PB_NO_CLEAN=1 if you do not want to force-clean first."
66
task :compile, [:package, :source, :destination, :plugin, :file_extension] do |_tasks, args|
7-
args.with_defaults(:destination => 'lib')
8-
args.with_defaults(:source => 'definitions')
9-
args.with_defaults(:plugin => 'ruby')
10-
args.with_defaults(:file_extension => '.pb.rb')
7+
binpath = ::File.expand_path("../../../../bin", __FILE__)
8+
9+
args.with_defaults(:destination => "lib")
10+
args.with_defaults(:source => "definitions")
11+
args.with_defaults(:plugin => "protoc-gen-ruby-protobuf=#{binpath}/protoc-gen-ruby")
12+
args.with_defaults(:file_extension => ".pb.rb")
13+
14+
# The local Ruby generator collides with the builtin Ruby generator
15+
#
16+
# From the protoc docs:
17+
#
18+
# --plugin=EXECUTABLE
19+
#
20+
# ...EXECUTABLE may be of the form NAME=PATH, in which case the given plugin name
21+
# is mapped to the given executable even if the executable"s own name differs.
22+
#
23+
# Use the NAME=PATH form to specify an alternative plugin name that avoids the name collision
24+
#
25+
plugin_name, _plugin_path = args[:plugin].split("=")
26+
27+
# The plugin name MUST have the protoc-gen- prefix in order to work, but that prefix is dropped
28+
# when using the plugin to generate definitions
29+
plugin_name.gsub!("protoc-gen-", "")
1130

1231
unless do_not_clean?
1332
force_clean!
@@ -16,22 +35,23 @@ namespace :protobuf do
1635

1736
command = []
1837
command << "protoc"
19-
command << "--#{args[:plugin]}_out=#{args[:destination]}"
38+
command << "--plugin=#{args[:plugin]}"
39+
command << "--#{plugin_name}_out=#{args[:destination]}"
2040
command << "-I #{args[:source]}"
2141
command << Dir["#{args[:source]}/#{args[:package]}/**/*.proto"].join(" ")
22-
full_command = command.join(' ')
42+
full_command = command.join(" ")
2343

2444
puts full_command
2545
system(full_command)
2646
end
2747

28-
desc 'Clean the generated *.pb.rb files from the destination package. Pass PB_FORCE_CLEAN=1 to skip confirmation step.'
48+
desc "Clean the generated *.pb.rb files from the destination package. Pass PB_FORCE_CLEAN=1 to skip confirmation step."
2949
task :clean, [:package, :destination, :file_extension] do |_task, args|
30-
args.with_defaults(:destination => 'lib')
31-
args.with_defaults(:file_extension => '.pb.rb')
50+
args.with_defaults(:destination => "lib")
51+
args.with_defaults(:file_extension => ".pb.rb")
3252

33-
file_extension = args[:file_extension].sub(/\*?\.+/, '')
34-
files_to_clean = ::File.join(args[:destination], args[:package], '**', "*.#{file_extension}")
53+
file_extension = args[:file_extension].sub(/\*?\.+/, "")
54+
files_to_clean = ::File.join(args[:destination], args[:package], "**", "*.#{file_extension}")
3555

3656
if force_clean? || permission_to_clean?(files_to_clean)
3757
::Dir.glob(files_to_clean).each do |file|
@@ -41,15 +61,15 @@ namespace :protobuf do
4161
end
4262

4363
def do_not_clean?
44-
! ::ENV.key?('PB_NO_CLEAN')
64+
! ::ENV.key?("PB_NO_CLEAN")
4565
end
4666

4767
def force_clean?
48-
::ENV.key?('PB_FORCE_CLEAN')
68+
::ENV.key?("PB_FORCE_CLEAN")
4969
end
5070

5171
def force_clean!
52-
::ENV['PB_FORCE_CLEAN'] = '1'
72+
::ENV["PB_FORCE_CLEAN"] = "1"
5373
end
5474

5575
def permission_to_clean?(files_to_clean)

0 commit comments

Comments
 (0)