class Pliny::Commands::Creator

Attributes

args[RW]
opts[RW]
stream[RW]

Public Class Methods

new(args = {}, opts = {}, stream = $stdout) click to toggle source
# File lib/pliny/commands/creator.rb, line 16
def initialize(args = {}, opts = {}, stream = $stdout)
  @args = args
  @opts = opts
  @stream = stream
end
run(args, opts = {}, stream = $stdout) click to toggle source
# File lib/pliny/commands/creator.rb, line 12
def self.run(args, opts = {}, stream = $stdout)
  new(args, opts, stream).run!
end

Public Instance Methods

run!() click to toggle source
# File lib/pliny/commands/creator.rb, line 22
def run!
  abort("#{name} already exists") if File.exist?(app_dir)

  FileUtils.copy_entry template_dir, app_dir
  FileUtils.rm_rf("#{app_dir}/.git")
  parse_erb_files
  display 'Pliny app created. To start, run:'
  display "cd #{app_dir} && bin/setup"
end

Protected Instance Methods

app_dir() click to toggle source
# File lib/pliny/commands/creator.rb, line 61
def app_dir
  Pathname.new(name).expand_path
end
display(msg) click to toggle source
# File lib/pliny/commands/creator.rb, line 49
def display(msg)
  stream.puts msg
end
name() click to toggle source
# File lib/pliny/commands/creator.rb, line 53
def name
  args.first
end
parse_erb_files() click to toggle source
# File lib/pliny/commands/creator.rb, line 34
def parse_erb_files
  Dir.glob("#{app_dir}/{*,.*}.erb").each do |file|
    static_file = file.gsub(/\.erb$/, '')

    template = ERB.new(File.read(file), 0)
    context = OpenStruct.new(app_name: name)
    content = template.result(context.instance_eval { binding })

    File.open(static_file, "w") do |f|
      f.write content
    end
    FileUtils.rm(file)
  end
end
template_dir() click to toggle source
# File lib/pliny/commands/creator.rb, line 57
def template_dir
  File.expand_path('../../template', File.dirname(__FILE__))
end