-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ProtoBoeuf::AutoloaderGen will generate helper modules to autoload ou…
…r generated constants
- Loading branch information
1 parent
ecdc7b5
commit 001c6c2
Showing
6 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
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,95 @@ | ||
# frozen_string_literal: true | ||
|
||
require "erb" | ||
require "syntax_tree" | ||
require "pathname" | ||
|
||
module ProtoBoeuf | ||
class AutoloaderGen | ||
# This class generates top-level autoloader modules for our well known types. Given autogenerated .rb files like: | ||
# - lib/protoboeuf/google/protobuf/foo.rb | ||
# - lib/protoboeuf/google/protobuf/bar.rb | ||
# | ||
# generate lib/protoboeuf/google/protobuf.rb that looks like: | ||
# | ||
# module ProtoBoeuf | ||
# module Google | ||
# module Protobuf | ||
# autoload :FooMessage1, "protoboeuf/google/protobuf/foo" | ||
# autoload :FooMessage2, "protoboeuf/google/protobuf/foo" | ||
# autoload :BarConst1, "protoboeuf/google/protobuf/bar" | ||
# end | ||
# end | ||
# end | ||
|
||
BASE_LIB_DIR = File.expand_path("..", __dir__) | ||
|
||
attr_reader :module_filename, | ||
:child_ruby_filenames, | ||
:autoloader_module_name, | ||
:autoloader_module_parts, | ||
:child_constants_by_filename | ||
|
||
def initialize(module_filename) | ||
@module_filename = module_filename | ||
# Given lib/protoboeuf/google.rb, glob lib/protoboeuf/google/**/*.rb | ||
@child_ruby_filenames = Dir[module_filename.pathmap("%X/**/*.rb")] | ||
@autoloader_module_name = nil | ||
@child_constants_by_filename = child_ruby_filenames.each_with_object({}) do |filename, constants| | ||
child_constants = constants_for_child_ruby_filename(filename) | ||
# For the autoloader_module_name we can just pick the first child constant we come across and take the first | ||
# three parts. For example, ProtoBoeuf::Google::Api::FieldBehavior would be ProtoBoeuf::Google::Api. | ||
@autoloader_module_name = child_constants.first.split("::")[0..2].join("::") if @autoloader_module_name.nil? | ||
|
||
# Make our absolute filename relative to the base lib directory for our autoload calls. | ||
require_path = Pathname.new(filename).relative_path_from(BASE_LIB_DIR).sub_ext("") | ||
constants[require_path] = child_constants | ||
end | ||
|
||
@autoloader_module_parts = @autoloader_module_name.split("::") | ||
end | ||
|
||
def to_ruby | ||
SyntaxTree.format(ERB.new(<<~RUBY, trim_mode: "-").result(binding)) | ||
# frozen_string_literal: true | ||
# rubocop:disable all | ||
<%- autoloader_module_parts.each do |module_name| -%> | ||
module <%= module_name %> | ||
<%- end -%> | ||
<%- child_constants_by_filename.each do |require_path, constant_names| -%> | ||
<%- constant_names.each do |constant_name| -%> | ||
autoload :<%= constant_name.split("::").last %>, "<%= require_path %>" | ||
<%- end -%> | ||
<%- end -%> | ||
<%- autoloader_module_parts.each do |module_name| -%> | ||
end | ||
<%- end -%> | ||
RUBY | ||
end | ||
|
||
private | ||
|
||
def constants_for_child_ruby_filename(filename) | ||
@constants_for_child_ruby_filename ||= {} | ||
|
||
return @constants_for_child_ruby_filename[filename] if @constants_for_child_ruby_filename.key?(filename) | ||
|
||
loaded = Module.new do | ||
module_eval File.binread(filename) | ||
end | ||
|
||
@constants_for_child_ruby_filename[filename] = loaded::ProtoBoeuf::Google.constants.flat_map do |const_name| | ||
mod = loaded::ProtoBoeuf::Google.const_get(const_name) | ||
next unless mod.is_a?(Module) | ||
|
||
# The top-level module will be our anonymous Module we created above | ||
parent_module_name = mod.name.split("::")[1..].join("::") | ||
|
||
mod.constants.map { |const_name| "#{parent_module_name}::#{const_name}" } | ||
end | ||
end | ||
end | ||
end |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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