Module: Cog::Controllers::GeneratorController

Defined in:
lib/cog/controllers/generator_controller.rb

Overview

Manage a project’s generators

Class Method Summary collapse

Class Method Details

.create(name, opt = {}) ⇒ Boolean

Create a new generator

Parameters:

  • name (String)

    the name to use for the new generator

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :plugin_name (String) — default: nil

    plugin to use

Returns:

  • (Boolean)

    was the generator successfully created?

Raises:

  • (Errors::NoSuchPlugin)


11
12
13
14
15
16
17
18
19
20
# File 'lib/cog/controllers/generator_controller.rb', line 11

def self.create(name, opt={})
  plugin = Cog.plugin(opt[:plugin_name])
  prefix = Cog.project_generator_path
  raise Errors::NoSuchPlugin.new(opt[:plugin_name]) if plugin.nil?
  raise Errors::PluginMissingDefinition.new('stamp_generator') if plugin.stamp_generator_block.nil?
  raise Errors::ActionRequiresProjectGeneratorPath.new('create generator') unless prefix
  dest = File.join prefix, "#{name}.rb"
  raise Errors::DuplicateGenerator.new(dest) if File.exists?(dest)
  plugin.stamp_generator_block.call name.to_s, dest
end

.listArray<String>

List the available project generators

Returns:



24
25
26
# File 'lib/cog/controllers/generator_controller.rb', line 24

def self.list
  Helpers::CascadingSet.process_paths Cog.generator_path, :ext => 'rb'
end

.run(name) ⇒ nil

Run the generator with the given name

Parameters:

  • name (String)

    name of the generator to run

Returns:

  • (nil)

Raises:

  • (Errors::NoSuchGenerator)


31
32
33
34
35
36
37
38
39
40
# File 'lib/cog/controllers/generator_controller.rb', line 31

def self.run(name)
  Cog.generator_path.reverse.each do |root|
    path = File.join root, "#{name}.rb"
    if File.exists?(path)
      GeneratorSandbox.new(path).interpret
      return
    end
  end
  raise Errors::NoSuchGenerator.new(name)
end

.run_allnil

Run all generators

Returns:

  • (nil)


44
45
46
47
48
49
50
# File 'lib/cog/controllers/generator_controller.rb', line 44

def self.run_all
  Cog.generator_path.each do |root|
    Dir.glob("#{root}/*.rb").each do |path|
      GeneratorSandbox.new(path).interpret
    end
  end
end