class Grundstein::Generator::Loader

This class encapsulates the logic to load a generator. It sets up a Generator::Envrionment and interacts with it. It is also responsible to determine paths.

Constants

DIR_EXPECTED_IN_PROJECT_ROOT
SCRIPT_NAME

Public Class Methods

new(generator_name) click to toggle source

Loads the generator.

# File lib/grundstein/generator/loader.rb, line 11
def initialize(generator_name)
  @generator_name = generator_name
  @env = load_environment
  @env.set_context(generator_path: generator_path, working_path: working_path, project_path: project_root_path)
end

Public Instance Methods

desc() click to toggle source

short hand for info

# File lib/grundstein/generator/loader.rb, line 45
def desc
  return info[:desc]
end
info() click to toggle source
# File lib/grundstein/generator/loader.rb, line 37
def info
  raise GeneratorMalformedError, "Generator script '#{@generator_name}' does not have an 'info' method." unless @env.respond_to?(:spec)
  spec = @env.spec
  raise GeneratorMalformedError, "Generator script '#{@generator_name}' does include :desc in the 'info' result." unless spec.is_a?(Hash) && spec[:desc].is_a?(String)
  return spec
end
name() click to toggle source
# File lib/grundstein/generator/loader.rb, line 33
def name
  return @generator_name
end
run() click to toggle source

Executes the generator's run method.

# File lib/grundstein/generator/loader.rb, line 18
def run # rubocop:disable Metrics/MethodLength
  raise GeneratorMalformedError, "Generator script '#{@generator_name}' does not have a 'run' method." unless @env.respond_to?(:run)
  begin
    puts "Running #{@generator_name.c_gen}"
    puts "Working path: #{working_path}"
    puts "Project path: #{project_root_path}"
    puts
    @env.run
    puts
    puts @env.caveats.c_warning
  rescue => e
    raise GeneratorRunError, "[#{@generator_name}] #{e.message}"
  end
end

Protected Instance Methods

generator_path() click to toggle source
# File lib/grundstein/generator/loader.rb, line 65
def generator_path
  return Generator::Repository.instance.generator_path(@generator_name)
end
generator_script_path() click to toggle source
# File lib/grundstein/generator/loader.rb, line 69
def generator_script_path
  return File.join(self.generator_path, SCRIPT_NAME)
end
load_environment() click to toggle source

Creates a Generator::Environment for the generator_name given in the constructor.

# File lib/grundstein/generator/loader.rb, line 52
def load_environment
  env = Grundstein::Generator::Environment.new
  path = self.generator_script_path
  raise GeneratorNotFoundError, "Generator named '#{@generator_name}' could not be found." unless Dir.exist?(File.dirname(path))
  raise GeneratorNotFoundError, "Generator named '#{@generator_name}' has no #{SCRIPT_NAME} script." unless File.exist?(path)
  env.extend_from_file(path)
  return env
end
project_root_path() click to toggle source
# File lib/grundstein/generator/loader.rb, line 73
def project_root_path
  return @project_root_path unless @project_root_path.nil?
  dir = Dir.pwd
  loop do
    raise GeneratorRunError, "Could not determine project path (no .git found here or above)." if dir == '/'
    if Dir.exist?(File.join(dir, DIR_EXPECTED_IN_PROJECT_ROOT))
      @project_root_path = dir
      return dir
    end
    dir = File.dirname(dir)
  end
end
working_path() click to toggle source
# File lib/grundstein/generator/loader.rb, line 61
def working_path
  return Dir.pwd
end