class Shaf::Command::New

Attributes

project_name[RW]

Public Instance Methods

call() click to toggle source
# File lib/shaf/command/new.rb, line 12
def call
  self.project_name = args.first
  if project_name.nil? || project_name.empty?
    raise ArgumentError,
      "Please provide a project name when using command 'new'!"
  end

  create_dir project_name
  Dir.chdir(project_name) do
    copy_templates
    create_gemfile
    create_settings_file
    write_shaf_version
    create_ruby_version_file
  end
end

Private Instance Methods

copy_template(template) click to toggle source
# File lib/shaf/command/new.rb, line 69
def copy_template(template)
  target = target_for(template)
  create_dir File.dirname(target)
  FileUtils.cp(template, target)
end
copy_templates() click to toggle source
# File lib/shaf/command/new.rb, line 59
def copy_templates
  template_files.each do |template|
    copy_template(template)
  end
end
create_dir(name) click to toggle source
# File lib/shaf/command/new.rb, line 33
def create_dir(name)
  return if Dir.exist? name
  FileUtils.mkdir_p(name)
rescue SystemCallError
  exit_with_error("Failed to create directory #{name}", 2)
end
create_gemfile() click to toggle source
# File lib/shaf/command/new.rb, line 40
def create_gemfile
  template_file = File.expand_path('../templates/Gemfile.erb', __FILE__)
  content = File.read(template_file)
  File.write "Gemfile", erb(content)
end
create_ruby_version_file() click to toggle source
# File lib/shaf/command/new.rb, line 65
def create_ruby_version_file
  File.write '.ruby-version', RUBY_VERSION
end
create_settings_file() click to toggle source
# File lib/shaf/command/new.rb, line 46
def create_settings_file
  settings_file = 'config/settings.yml'
  template_file = File.expand_path("../templates/#{settings_file}.erb", __FILE__)
  content = File.read(template_file)
  File.write settings_file,
             erb(content, project_name: project_name.capitalize)
end
erb(content, locals = {}) click to toggle source
# File lib/shaf/command/new.rb, line 54
def erb(content, locals = {})
  return ERB.new(content, 0, '%-<>').result_with_hash(locals) if RUBY_VERSION < "2.6.0"
  ERB.new(content, trim_mode: '-<>').result_with_hash(locals)
end
target_for(template) click to toggle source
# File lib/shaf/command/new.rb, line 85
def target_for(template)
  template.sub("#{template_dir}/", "")
end
template_dir() click to toggle source
# File lib/shaf/command/new.rb, line 75
def template_dir
  File.expand_path('../../../../templates', __FILE__)
end
template_files() click to toggle source
# File lib/shaf/command/new.rb, line 79
def template_files
  Dir["#{template_dir}/**/{*,.*}"].reject do |file|
    File.directory?(file)
  end
end