Class: Shaf::Command::New

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/command/new.rb

Instance Attribute Summary

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

exit_with_error, identifier, inherited, #initialize, usage

Methods included from Utils

#bootstrap, #in_project_root, #in_project_root?, #is_project_root?, pluralize, #pluralize, #project_root, #project_root!

Constructor Details

This class inherits a constructor from Shaf::Command::Base

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shaf/command/new.rb', line 13

def call
  @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_shaf_version_file
    create_ruby_version_file
  end
end

#copy_template(template) ⇒ Object



58
59
60
61
62
# File 'lib/shaf/command/new.rb', line 58

def copy_template(template)
  target = target_for(template)
  create_dir File.dirname(target)
  FileUtils.cp(template, target)
end

#copy_templatesObject



43
44
45
46
47
# File 'lib/shaf/command/new.rb', line 43

def copy_templates
  template_files.each do |template|
    copy_template(template)
  end
end

#create_dir(name) ⇒ Object



29
30
31
32
33
34
# File 'lib/shaf/command/new.rb', line 29

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_gemfileObject



36
37
38
39
40
41
# File 'lib/shaf/command/new.rb', line 36

def create_gemfile
  template_file = File.expand_path('../templates/Gemfile.erb', __FILE__)
  content = File.read(template_file)
  File.write "Gemfile",
    ERB.new(content, 0, '%-<>').result
end

#create_ruby_version_fileObject



54
55
56
# File 'lib/shaf/command/new.rb', line 54

def create_ruby_version_file
  File.write '.ruby-version', RUBY_VERSION
end

#create_shaf_version_fileObject



49
50
51
52
# File 'lib/shaf/command/new.rb', line 49

def create_shaf_version_file
  File.write '.shaf',
    YAML.dump({'version' => Shaf::VERSION})
end

#target_for(template) ⇒ Object



74
75
76
# File 'lib/shaf/command/new.rb', line 74

def target_for(template)
  template.sub("#{template_dir}/", "")
end

#template_dirObject



64
65
66
# File 'lib/shaf/command/new.rb', line 64

def template_dir
  File.expand_path('../../../../templates', __FILE__)
end

#template_filesObject



68
69
70
71
72
# File 'lib/shaf/command/new.rb', line 68

def template_files
  Dir["#{template_dir}/**/{*,.*}"].reject do |file|
    File.directory?(file)
  end
end