class Ronin::Gen::Generator

The {Generator} class is a generate base-class for all file, source-code or directory generators.

# Extending

To create a new type of generator one can extend {Generator}, {FileGenerator} or {DirGenerator} classes. The new generator can define it’s own ‘class_options`, which are made available to other classes that extend our generator. The functionality of the generator is defined via instance methods, which are called sequentially when the generator is invoked.

require 'ronin/gen/file_generator'

module Ronin
  module Gen
    module Generators
      class MyGenerator < FileGenerator

        # generator options
        parameter :stuff,    :type => true
        parameter :syntax,   :type => String
        parameter :includes, :type => Array

        #
        # Performs the generation.
        #
        def generate
          template 'some_template.erb', path
        end

      end
    end
  end
end

# Invoking

To invoke the generator from ruby, one can call the {generate} class method with the options and arguments to run the generator with:

MyGenerator.generate(
  :stuff    => true,
  :syntax   => 'bla',
  :includes => ['other']
  :path     => 'path/to/file',
)

To make your generator accessible to the ‘ronin-gen` command, simply place your generator file within the `ronin/gen/generators` directory of any Ronin library. If your generator class is named `MyGenerator`, than it’s ruby file must be named ‘my_generator.rb`.

To run the generator using the ‘ronin-gen` command, simply specify it’s underscored name:

ronin-gen my_generator path/to/file --stuff \
                                    --syntax bla \
                                    --includes other

Public Class Methods

generate(*arguments,&block) click to toggle source

Invokes the generator.

@param [Array] arguments

Arguments for {#initialize}.

@yield [generator]

The given block will be passed the new generator.

@yieldparam [Generator] generator

The newly created generator object.

@return [Generator]

The generate object.

@example

gen.generate

@since 0.2.0

@api public

# File lib/ronin/gen/generator.rb, line 158
def self.generate(*arguments,&block)
  generator = new(*arguments,&block)

  generator.generate!
  return generator
end
generator_name() click to toggle source

The name of the generator.

@return [String]

The generator name.

@since 1.1.0

@api semipublic

# File lib/ronin/gen/generator.rb, line 130
def self.generator_name
  @generator_name ||= Support::Inflector.underscore(
    self.name.sub('Ronin::Gen::Generators::','').gsub('::',':')
  )
end
new(options={}) { |self| ... } click to toggle source

Initializes the generator.

@param [Hash{Symbol => Object}] options

The options for the generator.

@yield [generator]

The given block will be passed the newly created generator.

@yieldparam [Generator]

The newly created generator.

@api semipublic

# File lib/ronin/gen/generator.rb, line 110
def initialize(options={})
  initialize_params(options)

  if self.class.data_dir
    self.template_dirs << find_data_dir(self.class.data_dir)
  end

  yield self if block_given?
end

Protected Class Methods

data_dir(new_dir=nil) click to toggle source

The default data directory of the generator.

@param [String] new_dir

The new data directory.

@return [String, nil]

The data directory that the generator will search for source files
within.

@since 1.1.0

@api semipublic

# File lib/ronin/gen/generator.rb, line 217
def self.data_dir(new_dir=nil)
  if new_dir
    @data_dir = new_dir
  else
    @data_dir ||= if superclass < Generator
                    superclass.data_dir
                  end
  end
end

Public Instance Methods

generate() click to toggle source

Default generator method.

@since 0.2.0

@api semipublic

# File lib/ronin/gen/generator.rb, line 198
def generate
end
generate!() click to toggle source

Sets up the generator and calls {#generate}.

@see setup @see generate

@since 1.1.0

@api public

# File lib/ronin/gen/generator.rb, line 175
def generate!
  setup
  generate
end
setup() click to toggle source

Default method to initialize any instance variables before any of the tasks are invoked.

@since 1.0.0

@api semipublic

# File lib/ronin/gen/generator.rb, line 188
def setup
end

Protected Instance Methods

data_dir(path) click to toggle source

Searches for a directory within the Generators {data_dir}.

@param [String] path

The relative path to search for.

@return [String]

The path to the directory.

@raise [StandardError]

The directory could not be found in the Generators {data_dir}.

@since 1.1.0

@api private

# File lib/ronin/gen/generator.rb, line 288
def data_dir(path)
  unless (full_path = find_data_dir(data_path(path)))
    raise(StandardError,"cannot find generator directory: #{path.dump}")
  end

  return full_path
end
data_dirs(path,&block) click to toggle source

Searches for all matching directories within the Generators {data_dir}.

@param [String] path

The relative directory path to search for.

@yield [dir]

The given block will be passed each found directory.

@yieldparam [String] dir

A directory with the same relative path.

@since 1.1.0

@api private

# File lib/ronin/gen/generator.rb, line 312
def data_dirs(path,&block)
  each_data_dir(data_path(directory),&block)
end
data_file(path) click to toggle source

Searches for a file within the Generators {data_dir}.

@param [String] path

The relative path to search for.

@return [String]

The path to the file.

@raise [StandardError]

The file could not be found in the Generators {data_dir}.

@since 1.1.0

@api private

# File lib/ronin/gen/generator.rb, line 264
def data_file(path)
  unless (full_path = find_data_file(data_path(path)))
    raise(StandardError,"cannot find generator file: #{path.dump}")
  end

  return full_path
end
data_path(path) click to toggle source

Joins the path with the Generators {data_dir}.

@param [String] path

A relative path.

@return [String]

The full `data/` directory path.

@since 1.1.0

@api private

# File lib/ronin/gen/generator.rb, line 240
def data_path(path)
  if self.class.data_dir
    path = File.join(self.class.data_dir,path)
  end

  return path
end