Class: Mortar::Generators::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/mortar/generators/generator_base.rb

Instance Method Summary collapse

Methods included from Helpers

#action, #ask, #confirm, #copy_if_not_present_at_dest, #default_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #display_with_indent, #download_to_file, #ensure_dir_exists, #error, error_with_failure, error_with_failure=, extended, extended_into, #format_bytes, #format_date, #format_with_bang, #full_host, #get_terminal_environment, #home_directory, #host, #hprint, #hputs, included, included_into, #installed_with_omnibus?, #json_decode, #json_encode, #line_formatter, #longest, #output_with_bang, #pending_github_team_state_message, #quantify, #redisplay, #retry_on_exception, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #test_name, #ticking, #time_ago, #truncate, #warning, #with_tty, #write_to_file

Constructor Details

#initializeBase

Returns a new instance of Base.



25
26
27
28
29
30
31
32
33
34
# File 'lib/mortar/generators/generator_base.rb', line 25

def initialize()
  # This is a really ugly way to turn the subclass name 'Mortar::Generators::ProjectGenerator'
  # into 'project' so we can use it to find the appropriate templates folder

  generator_type = self.class.name.split("::")[2].match(/.*(?=([A-Z]))/)[0].downcase
  @src_path = File.expand_path("../../templates/#{generator_type}", __FILE__)
  @dest_path = Dir.pwd
  @rel_path = ""
  @binding_variables = {}
end

Instance Method Details

#copy_file(src_file, dest_file, options = { :recursive => false }) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mortar/generators/generator_base.rb', line 43

def copy_file(src_file, dest_file, options={ :recursive => false })
  src_path = File.join(@src_path, @rel_path, src_file)
  dest_path = File.join(@dest_path, @rel_path, dest_file)
  msg = File.join(@rel_path, dest_file)[1..-1]

  if File.exists?(dest_path)
    if FileUtils.compare_file(src_path, dest_path)
      display_identical(msg)
    else
      display_conflict(msg)
    end 
  else
    display_create(msg)  
    FileUtils.mkdir_p(File.dirname(dest_path)) if options[:recursive]
    FileUtils.cp(src_path, dest_path)
  end
end

#generate_file(src_file, dest_file, options = { :recursive => false }) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mortar/generators/generator_base.rb', line 73

def generate_file(src_file, dest_file, options={ :recursive => false })
  src_path = File.join(@src_path, @rel_path, src_file)
  dest_path = File.join(@dest_path, @rel_path, dest_file)
  msg = File.join(@rel_path, dest_file)[1..-1]

  erb = ERB.new(File.read(src_path), 0, "%<>")

  result = erb.result(@script_binding)
  

  if File.exists?(dest_path)
    if result == File.read(dest_path) 
      display_identical(msg)
    else
      display_conflict(msg)
    end 
  else
    FileUtils.mkdir_p(File.dirname(dest_path)) if options[:recursive]
    file = File.new(dest_path, "w")
    file.write(result)
    file.close
    display_create(msg)  
  end
end

#inside(folder) ⇒ Object



36
37
38
39
40
41
# File 'lib/mortar/generators/generator_base.rb', line 36

def inside(folder)
  rel_backup = @rel_path
  @rel_path = File.join(@rel_path, folder)
  yield
  @rel_path = rel_backup
end

#mkdir(folder, options = { :verbose => true }) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mortar/generators/generator_base.rb', line 61

def mkdir(folder, options={ :verbose => true })
  dest_path = File.join(@dest_path, @rel_path, folder)
  msg = File.join(@rel_path, folder)[1..-1]

  if File.exists?(dest_path) 
    display_exists(options[:verbose] ? msg : "") 
  else
    display_create(options[:verbose] ? msg : "")
    FileUtils.mkdir(dest_path)
  end
end