Class: Templater::CLI::Generator

### Home Classes Methods

### In Files

- lib/templater/cli/generator.rb

### Parent

Object

### Methods

- ::new
- #conflict_menu
- #help
- #output_diff_line
- #run
- #say_status
- #step_through_templates
- #version

### Files

- History.txt
- Manifest.txt

### Class/Module Index

Quicksearch

- Templater
- Templater::ActionDescription
- Templater::Actions
- Templater::Actions::Action
- Templater::Actions::Directory
- Templater::Actions::EmptyDirectory
- Templater::Actions::File
- Templater::Actions::Template
- Templater::ArgumentDescription
- Templater::CLI
- Templater::CLI::Generator
- Templater::CLI::Manifold
- Templater::Description
- Templater::Discovery
- Templater::Generator
- Templater::InvocationDescription
- Templater::Manifold
- Templater::Spec
- Templater::Spec::Helpers
- Templater::Spec::Helpers::CreateMatcher
- Templater::Spec::Helpers::InvokeMatcher
- Kernel
- String

No matching classes.

# Templater::CLI::Generator

### Public Class Methods

new(generator_name, generator_class, destination_root, name, version) click to toggle source

    # File lib/templater/cli/generator.rb, line 7
    def initialize(generator_name, generator_class, destination_root, name, version)
      @generator_name = generator_name
      @destination_root = destination_root
      @generator_class = generator_class
      @name = name
      @version = version
    end

### Public Instance Methods

help() click to toggle source

outputs a helpful message and quits

    # File lib/templater/cli/generator.rb, line 21
    def help
      puts "Usage: #{@name} #{@generator_name} [options] [args]"
      puts ''
      puts @generator_class.desc
      puts ''
      puts @options[:opts]
      puts ''
      exit
    end

run(arguments) click to toggle source

    # File lib/templater/cli/generator.rb, line 31
    def run(arguments)
      generator_class = @generator_class # FIXME: closure wizardry, there has got to be a better way than this?

      @options = Templater::CLI::Parser.parse(arguments) do |opts, options|
        opts.separator "Options specific for this generator:"
        # the reason this is reversed is so that the 'main' generator will always have the last word
        # on the description of the option
        generator_class.generators.reverse.each do |generator|
          # Loop through this generator's options and add them as valid command line options
          # so that they show up in help messages and such
          generator.options.each do |option|
            name = option.name.to_s.gsub('_', '-')
            if option.options[:as] == :boolean
              opts.on("--#{name}", option.options[:desc]) do |s|
                options[option.name] = s
              end
            else
              opts.on("--#{name}=value", option.options[:desc]) do |s|
                options[option.name] = s.gsub('-', '_').to_sym
              end
            end
          end
        end
      end

      self.help if @options[:help]
      self.version if @options[:version]

      # Try to instantiate a generator, if the arguments to it were incorrect: show a help message
      begin
        @generator = @generator_class.new(@destination_root, @options, *arguments)
      rescue Templater::ArgumentError => e
        if @options[:debug]
          raise e
        else
          self.help
        end
      end

      if @options[:pretend]
        puts "Generating with #{@generator_name} generator (just pretending):"
      else
        puts "Generating with #{@generator_name} generator:"
      end
      step_through_templates

      # Run hooks
      @generator.after_run
      @generator.after_generation unless @options[:delete]
      @generator.after_deletion   if     @options[:delete]
    end

step_through_templates() click to toggle source

    # File lib/templater/cli/generator.rb, line 83
    def step_through_templates
      @generator.all_actions.each do |action|
        if @options[:delete]
          action.revoke! unless @options[:pretend]
          say_status('deleted', action, :red)
        else
          if action.identical?
            say_status('identical', action, :blue)
          elsif action.exists?
            if @options[:force]
              action.invoke! unless @options[:pretend]
              say_status('forced', action, :yellow)
            elsif @options[:skip]
              say_status('skipped', action, :yellow)
            else
              say_status('conflict', action, :red)
              conflict_menu(action)
            end
          else
            action.invoke! unless @options[:pretend]
            say_status('added', action, :green)
          end
        end
      end
    end

version() click to toggle source

    # File lib/templater/cli/generator.rb, line 15
    def version
      puts @version
      exit
    end

### Protected Instance Methods

output_diff_line(diff) click to toggle source

    # File lib/templater/cli/generator.rb, line 163
    def output_diff_line(diff)
      case diff.action
      when '-'
        say "<%= color( %(-  #{diff.element.chomp}), :red) %>"
      when '+'
        say "<%= color( %(+  #{diff.element.chomp}), :green) %>"
      else
        say "#{diff.action}  #{diff.element.chomp}"
      end
    end

say_status(status, template, color = nil) click to toggle source

    # File lib/templater/cli/generator.rb, line 154
    def say_status(status, template, color = nil)
      status_flag = "[#{status.to_s.upcase}]".rjust(12)
      if color and not @options[:no_color]
        say "<%= color('#{status_flag}', :#{color}) %>  " + template.relative_destination
      else
        say "#{status_flag}  " + template.relative_destination
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.