class Emque::Consuming::Generators::Application

Constants

IGNORE

Attributes

current_dir[R]
name[RW]
options[RW]

Public Class Methods

new(options, name) click to toggle source
# File lib/emque/consuming/generators/application.rb, line 11
def initialize(options, name)
  self.name = Inflecto.underscore(name)
  self.options = options
end

Public Instance Methods

generate() click to toggle source
# File lib/emque/consuming/generators/application.rb, line 16
def generate
  context = Class.new(Object) { |obj|
    def initialize(options, name)
      @name = Inflecto.camelize(name)
      @options = options
    end

    def get_binding; binding; end
  }.new(options, name).get_binding

  @current_dir = File.realdirpath(Dir.pwd)

  recursively_copy_templates(
    File.realdirpath(
      File.join(
        File.dirname(__FILE__),
        "..",
        "..",
        "..",
        "templates"
      )
    ),
    [current_dir, name],
    context
  )
end
get_binding() click to toggle source
# File lib/emque/consuming/generators/application.rb, line 23
def get_binding; binding; end

Private Instance Methods

recursively_copy_templates(path, nesting, context) click to toggle source
# File lib/emque/consuming/generators/application.rb, line 52
def recursively_copy_templates(path, nesting, context)
  Dir.entries(path).each do |e|
    unless IGNORE.include?(e)
      loc = File.join(path, e)

      if Dir.exist?(loc)
        new_nest = nesting + [e]
        create_path = File.join(*new_nest)

        unless Dir.exist?(create_path)
          FileUtils.mkdir_p(create_path)
          puts "created directory #{relative_path(create_path)}"
        end

        recursively_copy_templates(loc, new_nest, context)
      elsif e =~ /\.tt$/
        filename = File.join(
          FileUtils.mkdir_p(File.join(*nesting)).first,
          e.gsub(".tt", "")
        )
        display = relative_path(filename)
        overwrite = "Y"

        if File.exist?(filename)
          print "#{display} exists, overwrite? (yN) "
          overwrite = $stdin.gets
        end

        if overwrite.upcase.chomp == "Y"
          File.open(filename, "w") do |f|
            f.write(ERB.new(File.read(loc)).result(context))
          end
          puts "created file #{display}"
        else
          puts "skipping file #{display}"
        end
      end
    end
  end
end
relative_path(path) click to toggle source
# File lib/emque/consuming/generators/application.rb, line 48
def relative_path(path)
  path.gsub(current_dir, ".")
end