Module: Lattice::Generator

Includes:
FileUtils
Included in:
Lattice::Generators::AppBase
Defined in:
lib/lattice/generator.rb

Defined Under Namespace

Classes: ERBRenderer

Instance Method Summary collapse

Instance Method Details

#generate_directory(path) ⇒ Object

Create a directory within the application if it doesn’t exit



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lattice/generator.rb', line 10

def generate_directory(path)
  if File.directory?(path)
    ohai "       exist".light_blue + "  #{path}"
  elsif File.exist?(path)
     ohai "    conflict".light_red + " #{output_path}"
     ohai "Template resumes #{output_path} is a directory!"
  else
    ohai "      create".light_green + "  #{path}"
    mkdir_p path
  end
end

#generate_file(input_path, output_path) ⇒ Object

Render a file template and generate the associated output



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lattice/generator.rb', line 23

def generate_file(input_path, output_path)
  renderer = ERBRenderer.new(input_path, app_const_base: @app_const_base)
  generated_file = renderer.render

  if File.exist?(output_path)
    existing_file = File.read(output_path)
    if generated_file == existing_file
      ohai "   identical".light_blue + "  #{output_path}"
    else
      ohai "    conflict".light_red + " #{output_path}"
      overwrite = prompt("Overwrite #{output_path}? [yn] ")
      if overwrite == "y"
        ohai "       force".light_yellow + "  #{output_path}"
        File.open(output_path, "w") { |f| f << generated_file }
      else
        ohai "        skip".light_yellow + "  #{output_path}"
      end
    end
  else
    directory = File.dirname(output_path) + "/"
    generate_directory(directory) unless File.directory?(directory)
    ohai "      create".light_green + "  #{output_path}"
    File.open(output_path, "w") { |f| f << generated_file }
  end
end

#ohai(message) ⇒ Object

Tell the user something



50
51
52
# File 'lib/lattice/generator.rb', line 50

def ohai(message)
  puts message
end