class Embork::Generator

Constants

BLUEPRINT_DIR

Attributes

dot_files[R]
erb_files[R]
logger[R]
project_path[R]

Public Class Methods

new(package_name, options) click to toggle source
# File lib/embork/generator.rb, line 14
def initialize(package_name, options)
  @package_name = package_name
  @logger = Embork::Logger.new STDOUT, :simple
  read_erb_files
  read_dot_files
  set_project_path(options[:directory])
end

Public Instance Methods

generate() click to toggle source
# File lib/embork/generator.rb, line 45
def generate
  check_for_npm
  print_banner
  copy_files
  process_erb
  move_dot_files
  install_npm_deps
  install_bower_deps
  bundle
  init_git
  print_success
end
read_dot_files() click to toggle source
# File lib/embork/generator.rb, line 27
def read_dot_files
  list_file_path = File.join(BLUEPRINT_DIR, 'dotfiles')
  @dot_files = File.readlines(list_file_path).map{ |f| f.strip }
end
read_erb_files() click to toggle source
# File lib/embork/generator.rb, line 22
def read_erb_files
  list_file_path = File.join(BLUEPRINT_DIR, 'erbfiles')
  @erb_files = File.readlines(list_file_path).map{ |f| f.strip }
end
set_project_path(directory) click to toggle source
# File lib/embork/generator.rb, line 32
def set_project_path(directory)
  if directory.nil?
    @project_path = File.join Dir.pwd, @package_name
  else
    p = Pathname.new directory
    if p.absolute?
      @project_path = p.to_s
    else
      @project_path = File.expand_path p.to_s, Dir.pwd
    end
  end
end

Protected Instance Methods

bundle() click to toggle source
# File lib/embork/generator.rb, line 122
def bundle
  logger.info 'Bundling app.'
  command = 'bundle install'
  # Clean up bundler before running bundle
  bundle_vars = %w(BUNDLE_GEMFILE RUBYOPT BUNDLE_BIN_PATH)
  bundled_env = ENV.to_hash
  bundle_vars.each{ |var| ENV.delete var }

  # Run bundle
  status, out = run_in_project_dir command

  # Restor bundler
  ENV.replace bundled_env
  print_error(command, out) unless status.success?
end
check_for_npm() click to toggle source
# File lib/embork/generator.rb, line 62
def check_for_npm
  status = Open3.popen3('which npm') do |stdin, stdout, stderr, wait_thr|
    stdin.close
    wait_thr.value
  end
  if !status.success?
    logger.fatal :banner
    logger.warn "Hey Person! Embork needs node and npm installed to work properly."
    logger.warn "Come back and try again when you get them set up!"
    logger.fatal :banner
    exit 1
  end
end
copy_files() click to toggle source
# File lib/embork/generator.rb, line 76
def copy_files
  FileUtils.mkdir_p project_path
  FileUtils.cp_r File.join(BLUEPRINT_DIR, '.'), project_path
end
init_git() click to toggle source
# File lib/embork/generator.rb, line 138
def init_git
  command = 'git init'
  status, out = run_in_project_dir command
  print_error(command, out) unless status.success?
end
install_bower_deps() click to toggle source
# File lib/embork/generator.rb, line 115
def install_bower_deps
  logger.info 'Fetching bower dependencies.'
  command = 'npm run bower-deps'
  status, out = run_in_project_dir command
  print_error(command, out) unless status.success?
end
install_npm_deps() click to toggle source
# File lib/embork/generator.rb, line 108
def install_npm_deps
  logger.info 'Fetching npm dependencies.'
  command = 'npm install'
  status, out = run_in_project_dir command
  print_error(command, out) unless status.success?
end
move_dot_files() click to toggle source
# File lib/embork/generator.rb, line 100
def move_dot_files
  Dir.chdir project_path do
    dot_files.each do |file|
      FileUtils.mv file, '.%s' % file
    end
  end
end
print_banner() click to toggle source
print_error(command, trace) click to toggle source
print_success() click to toggle source
process_erb() click to toggle source
# File lib/embork/generator.rb, line 89
def process_erb
  helpers = ErbHelpers.new
  helpers.namespace = @package_name
  Dir.chdir project_path do
    erb_files.each do |file|
      processed_file = ERB.new(File.read(file)).result helpers.get_binding
      File.open(file, 'w'){ |f| f.write processed_file }
    end
  end
end
run_in_project_dir(command) click to toggle source
# File lib/embork/generator.rb, line 144
def run_in_project_dir(command)
  out = status = nil
  Dir.chdir project_path do
    out = ''
    status = Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
      stdin.close
      out = stdout.read + stderr.read
      wait_thr.value
    end
  end
  [ status, out ]
end