Class: KISSGen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/kissgen/generator.rb

Overview

The generator class is where you configure path and import files

require 'rcgen'
@g = KISSGen::Generator.new "/path/to/generator", "/path/where/files/are/generated"
@g.directory = "/"
@g.assign :my_name, "lancelot"
@g.generate(:pretend => true)
@g.generate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, copy_path) ⇒ Generator

Returns a new instance of Generator.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kissgen/generator.rb', line 16

def initialize(path, copy_path)
  @path = path
  @copy_path = copy_path
  @files = []
  @assigns = {}
  @explain_pretend = "== PRETEND MODE: Would have created the following files: =="
  @explain_generate = "== Generated the following files: =="
  @explain_footer = "====== Generator Finished ======"
  
  import_setup
end

Instance Attribute Details

#assignsObject (readonly)

Returns the value of attribute assigns.



12
13
14
# File 'lib/kissgen/generator.rb', line 12

def assigns
  @assigns
end

#copy_pathObject (readonly)

Returns the value of attribute copy_path.



12
13
14
# File 'lib/kissgen/generator.rb', line 12

def copy_path
  @copy_path
end

#explain_createdObject

Returns the value of attribute explain_created.



13
14
15
# File 'lib/kissgen/generator.rb', line 13

def explain_created
  @explain_created
end

Returns the value of attribute explain_footer.



13
14
15
# File 'lib/kissgen/generator.rb', line 13

def explain_footer
  @explain_footer
end

#explain_pretendObject

Returns the value of attribute explain_pretend.



13
14
15
# File 'lib/kissgen/generator.rb', line 13

def explain_pretend
  @explain_pretend
end

#filesObject (readonly)

Returns the value of attribute files.



12
13
14
# File 'lib/kissgen/generator.rb', line 12

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/kissgen/generator.rb', line 12

def path
  @path
end

#setup_file_pathObject

Returns the value of attribute setup_file_path.



14
15
16
# File 'lib/kissgen/generator.rb', line 14

def setup_file_path
  @setup_file_path
end

Instance Method Details

#assign(name, value) ⇒ Object

assigns :app_file_name, “lancelot”



77
78
79
# File 'lib/kissgen/generator.rb', line 77

def assign(name, value)
  @assigns[name] = value
end

#deleteObject



81
82
83
# File 'lib/kissgen/generator.rb', line 81

def delete
  @files.each { |file| file.delete }
end

#directory(directory_path, relative_copy_path = directory_path, options = {}) ⇒ Object

Recursively adds a directory to the list of files to generate

directory “app”



65
66
67
68
69
70
71
72
73
74
# File 'lib/kissgen/generator.rb', line 65

def directory(directory_path, relative_copy_path = directory_path, options = {})
  Dir["#{File.join(@path, directory_path)}/**/*"].each do |file_path|
    unless FileTest.directory?(file_path)
      # Take the template file path and give relative paths
      relative_path = Pathname.new(file_path).relative_path_from(Pathname.new(File.join(@path, directory_path)))
      new_path = File.join(relative_copy_path, relative_path)
      @files << Template.new(self, File.join(directory_path, relative_path), new_path)
    end
  end
end

#file(relative_file_path, relative_copy_path = relative_file_path, options = {}) ⇒ Object

Adds a file to the list of files to generate

This will generate the template README file relative to the copy path

file "README"

or

file "README", "README_PLEASE"


56
57
58
59
60
# File 'lib/kissgen/generator.rb', line 56

def file(relative_file_path, relative_copy_path = relative_file_path, options = {})
  template = Template.new(self, relative_file_path, relative_copy_path)
  @files << template
  template
end

#generate(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/kissgen/generator.rb', line 37

def generate(options = {})
  puts(options[:pretend] ? @explain_pretend : @explain_generate)
  
  @files.each do |file|
    options[:pretend] ? puts("#{file.copy_path}") : file.create
  end
  
  puts @explain_footer
end

#import_setupObject

Import setup file which is located in /path/to/generator/setup.rb



33
34
35
# File 'lib/kissgen/generator.rb', line 33

def import_setup
  instance_eval(File.new(setup_file_path).read) if File.exists?(setup_file_path)
end