Module: Lightning::Generators

Defined in:
lib/lightning/generators.rb,
lib/lightning/generators/misc.rb,
lib/lightning/generators/ruby.rb

Overview

This module contains methods which are used to generate bolts with ‘lightning bolt generate’. Each method should return an array of bolt globs. The name of the method is the name given to the bolt.

Generator Plugins

Generator plugins are a way for users to define and share generators. A generator plugin is a .rb file in ~/.lightning/generators/. Each plugin can have multiple generators since a generator is just a method in Lightning::Generators.

A sample generator plugin looks like this:

module Lightning::Generators
  desc "Files in $PATH"
  def bin
    ENV['PATH'].split(":").uniq.map {|e| "#{e}/*" }
  end
end

To register a generator, desc must be placed before a method and given a generator description. A generator should produce an array of globs. If a generator is to be shared with others it should dynamically generate filesystem-specific globs based on environment variables and commands. Generated globs don’t have to expand ‘~’ as lightning expands that automatically to the user’s home.

For generator plugin examples read the source.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.desc(arg) ⇒ Object

Used before a generator method to give it a description



32
33
34
# File 'lib/lightning/generators.rb', line 32

def self.desc(arg)
  @next_desc = arg
end

.generatorsHash

Returns Maps generators to their descriptions.

Returns:

  • (Hash)

    Maps generators to their descriptions



27
28
29
# File 'lib/lightning/generators.rb', line 27

def self.generators
  @desc ||= {}
end

Instance Method Details

#`(*args) ⇒ Object

Overridden for generators to error elegantly when a generator calls a shell command that doesn’t exist



38
39
40
41
42
43
44
45
# File 'lib/lightning/generators.rb', line 38

def `(*args)
  cmd = args[0].split(/\s+/)[0] || ''
  if Util.shell_command_exists?(cmd)
    Kernel.`(*args)
  else
    raise "Command '#{cmd}' doesn't exist."
  end
end