Skip to content

Commit

Permalink
Merge pull request #377 from tdeo/require_relative
Browse files Browse the repository at this point in the history
Add option to have require_relative statements
  • Loading branch information
film42 committed Sep 6, 2017
2 parents 24b44e3 + 2ab80d9 commit b9c7140
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/protobuf/generators/file_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def print_import_requires
header "Imports"

descriptor.dependency.each do |dependency|
print_require(convert_filename(dependency))
print_require(convert_filename(dependency), ENV.key?('PB_REQUIRE_RELATIVE'))
end

puts
Expand Down
4 changes: 2 additions & 2 deletions lib/protobuf/generators/printable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def print_module(name, &block)
#
# print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
#
def print_require(file)
puts "require '#{file}'"
def print_require(file, relative = false)
puts "require#{'_relative' if relative} '#{file}'"
end

# Puts the given message prefixed by the indent level.
Expand Down
11 changes: 9 additions & 2 deletions spec/lib/protobuf/generators/file_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
end

it 'prints a ruby require for each dependency' do
expect(subject).to receive(:print_require).with('test/bar.pb')
expect(subject).to receive(:print_require).with('test/baz.pb')
expect(subject).to receive(:print_require).with('test/bar.pb', false)
expect(subject).to receive(:print_require).with('test/baz.pb', false)
subject.print_import_requires
end

it 'prints a ruby require_relative for each dependency if environment variable was set' do
expect(subject).to receive(:print_require).with('test/bar.pb', true)
expect(subject).to receive(:print_require).with('test/baz.pb', true)
ENV['PB_REQUIRE_RELATIVE'] = 'true'
subject.print_import_requires
ENV.delete('PB_REQUIRE_RELATIVE')
end
end

describe '#compile' do
Expand Down

0 comments on commit b9c7140

Please sign in to comment.