Module: Rails::Hyperdrive::GemspecLocator

Defined in:
lib/rails/hyperdrive/gemspec_locator.rb

Overview

Resolves the gemspec of the companion gem repo a dev task is running in. Companion-repo dev tooling: problems raise rather than fail open.

Constant Summary collapse

Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.load_spec(explicit, dir) ⇒ Object

Returns the loaded spec and the repo root its relative paths resolve against — the gemspec's own directory, not an installed gem path.

Raises:



12
13
14
15
16
17
18
# File 'lib/rails/hyperdrive/gemspec_locator.rb', line 12

def load_spec(explicit, dir)
  path = resolve(explicit, dir)
  spec = Gem::Specification.load(path)
  raise Error, "could not load gemspec #{path}" unless spec

  [spec, File.dirname(File.expand_path(path))]
end

.resolve(explicit, dir) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails/hyperdrive/gemspec_locator.rb', line 20

def resolve(explicit, dir)
  if explicit && !explicit.to_s.strip.empty?
    path = File.expand_path(explicit.to_s, dir)
    raise Error, "gemspec not found: #{path}" unless File.file?(path)
    return path
  end

  found = Dir.glob(File.join(dir, "*.gemspec"))
  case found.size
  when 1 then File.expand_path(found.first)
  when 0 then raise Error, "no .gemspec found in #{dir}; pass an explicit path as the task " \
                           "argument, e.g. rake \"<task>[path/to/name.gemspec]\""
  else raise Error, "multiple gemspecs found in #{dir} " \
                    "(#{found.map { |f| File.basename(f) }.join(", ")}); pass an explicit path"
  end
end