Class: Twittbot::Generators::AppGenerator

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/twittbot/generators/twittbot/app/app_generator.rb

Overview

Class to generate a new Twittbot bot.

Instance Method Summary collapse

Constructor Details

#initialize(app_name, options = {}) ⇒ AppGenerator

Returns a new instance of AppGenerator.



13
14
15
16
17
18
19
# File 'lib/twittbot/generators/twittbot/app/app_generator.rb', line 13

def initialize(app_name, options = {})
  @options = {
      'template_dir' => Twittbot::TEMPLATE_DIR
  }.merge!(options)
  @app_name = app_name
  @options['template_dir'] = File.expand_path @options['template_dir']
end

Instance Method Details

#createObject

Creates the bot.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/twittbot/generators/twittbot/app/app_generator.rb', line 22

def create
  path = File.expand_path "./#{@app_name}"
  if File.exist?(@app_name)
    say "#{File.directory?(@app_name) ? 'Directory' : 'File'} #{@app_name} already exists", :red
    exit 1
  end
  FileUtils.mkdir_p(path)

  # build the template
  files = Dir["#{@options['template_dir']}/**/*"]
  files.each do |file|
    real_filename = file.sub(/^#{@options['template_dir']}\//, '').sub(/^_/, '.')
    real_path = "#{path}/#{real_filename}"
    say_status :create, real_filename, :green
    if File.directory? file
      FileUtils.mkdir_p real_path
    else
      erb = Erubis::Eruby.new File.read(file)
      File.open real_path, 'w' do |f|
        f.write erb.result(binding)
      end
    end
  end
  generate_config path
end