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'
KISSGen::Generator.new "/path/to/generator", "/path/where/files/are/generated"
KISSGen::Generator.generate(:pretend => true)
KISSGen::Generator.generate

Defined Under Namespace

Classes: SetupFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, copy_path) ⇒ Generator

Returns a new instance of Generator.



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

def initialize(path, copy_path)
  @path = path
  @copy_path = copy_path
  @files = []
  @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

#copy_pathObject (readonly)

Returns the value of attribute copy_path.



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

def copy_path
  @copy_path
end

#explain_createdObject

Returns the value of attribute explain_created.



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

def explain_created
  @explain_created
end

Returns the value of attribute explain_footer.



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

def explain_footer
  @explain_footer
end

#explain_pretendObject

Returns the value of attribute explain_pretend.



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

def explain_pretend
  @explain_pretend
end

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#setup_file_pathObject

Returns the value of attribute setup_file_path.



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

def setup_file_path
  @setup_file_path
end

Instance Method Details

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

Recursively adds a directory to the list of files to generate

directory “app”



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

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"


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

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



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

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

#import_setupObject

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

Raises:



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

def import_setup
  raise SetupFailure, "Setup file does not exist in #{File.expand_path(setup_file_path)}" unless File.exists?(setup_file_path)
  instance_eval(File.new(setup_file_path).read)
end