Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QOL make our gem requireable and autoload/eager load some constants #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed lib/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions lib/protoboeuf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module ProtoBoeuf
autoload :CodeGen, "protoboeuf/codegen"
autoload :Google, "protoboeuf/google"
end
5 changes: 5 additions & 0 deletions lib/protoboeuf/google.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

# There isn't a clean 1:1 mapping between constants and *.rb files, so eager load instead of autoload.

Dir[File.expand_path("google/**/*.rb", __dir__)].each { |file| require file }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we don't do this pattern, and rather generate the requires as part of our .proto to .rb step.

2 reasons:

  1. It's important to know what we're requiring
  2. I'm not sure if directory listings are sorted (they weren't in the past) and it can cause platform dependent problems.

If generating a file with the requires is to onerous, I can understand and we can go this route, but I'd prefer if we didn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give that a whirl

Copy link
Contributor Author

@davebenvenuti davebenvenuti Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new commit: 7dab057

We now generate modules in lib/protoboeuf/google/api.rb and lib/protoboeuf/google/protobuf.rb that autoload all of the constants defined in lib/protoboeuf/google/**/*.rb! Note that they're collapsed by default in the PR diff view because I marked them as autogenerated in .gitattributes. Big thanks to @rwstauner for helping me update the Rakefile in a way where I wouldn't embarrass myself.

https://github.com/Shopify/protoboeuf/blob/7dab057840f374538c061ce438f3b1db5a41f739/lib/protoboeuf/google/api.rb

https://github.com/Shopify/protoboeuf/blob/7dab057840f374538c061ce438f3b1db5a41f739/lib/protoboeuf/google/protobuf.rb

30 changes: 30 additions & 0 deletions test/gem_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "helper"

class GemTest < ProtoBoeuf::Test
def test_can_be_required
Tempfile.create do |file|
file.write(<<~RUBY)
require "bundler/inline"

gemfile do
gem "protoboeuf", path: "#{__dir__}/..", require: true
end

::ProtoBoeuf

# The following should auto/eagerload
::ProtoBoeuf::CodeGen
::ProtoBoeuf::Google::Api::FieldBehavior
::ProtoBoeuf::Google::Protobuf::Any

exit 0
RUBY

file.flush

assert(system(RbConfig.ruby, "-v", file.path), "Failed to require the gem")
end
end
end
Loading