Module: Magicprotorb::IncludePath

Defined in:
lib/magicprotorb/include_path.rb

Overview

Resolves canonical proto paths against include roots, mirroring the ‘protoc -I` model: MAGICPROTORB_PATH first, then Ruby’s $LOAD_PATH.

This is the Ruby analogue of magicproto’s “MAGICPROTO_PATH then sys.path”: a library ships its protos as package data under its own lib directory, that directory is already on $LOAD_PATH, and the directory name namespaces the protos so two installed gems can’t collide.

Constant Summary collapse

ENV_VAR =
"MAGICPROTORB_PATH"

Class Method Summary collapse

Class Method Details

.resolve(proto_path) ⇒ Object

The first root under which proto_path exists, or nil. Used to produce a LoadError-shaped failure before invoking the compiler.



39
40
41
# File 'lib/magicprotorb/include_path.rb', line 39

def resolve(proto_path)
  roots.find { |root| File.file?(File.join(root, proto_path)) }
end

.rootsObject

Ordered, de-duplicated list of existing directories to search, highest priority first. These become the ‘-I` include roots handed to the compiler.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/magicprotorb/include_path.rb', line 18

def roots
  raw = []
  if (env = ENV.fetch(ENV_VAR, nil)) && !env.empty?
    raw.concat(env.split(File::PATH_SEPARATOR))
  end
  raw.concat($LOAD_PATH)

  seen = {}
  raw.each_with_object([]) do |dir, out|
    next if dir.nil? || dir.to_s.empty?

    path = File.expand_path(dir.to_s)
    next if seen[path] || !File.directory?(path)

    seen[path] = true
    out << path
  end
end