Class: KISSGen::Generator
- Inherits:
-
Object
- Object
- KISSGen::Generator
- Defined in:
- lib/kissgen/generator.rb
Overview
Defined Under Namespace
Classes: SetupFailure
Instance Attribute Summary collapse
-
#copy_path ⇒ Object
readonly
Returns the value of attribute copy_path.
-
#explain_created ⇒ Object
Returns the value of attribute explain_created.
-
#explain_footer ⇒ Object
Returns the value of attribute explain_footer.
-
#explain_pretend ⇒ Object
Returns the value of attribute explain_pretend.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#setup_file_path ⇒ Object
Returns the value of attribute setup_file_path.
Instance Method Summary collapse
-
#directory(directory_path, relative_copy_path = directory_path, options = {}) ⇒ Object
Recursively adds a directory to the list of files to generate.
-
#file(relative_file_path, relative_copy_path = relative_file_path, options = {}) ⇒ Object
Adds a file to the list of files to generate.
- #generate(options = {}) ⇒ Object
-
#import_setup ⇒ Object
Import setup file which is located in /path/to/generator/setup.rb.
-
#initialize(path, copy_path) ⇒ Generator
constructor
A new instance of Generator.
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_path ⇒ Object (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_created ⇒ Object
Returns the value of attribute explain_created.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def explain_created @explain_created end |
#explain_footer ⇒ Object
Returns the value of attribute explain_footer.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def @explain_footer end |
#explain_pretend ⇒ Object
Returns the value of attribute explain_pretend.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def explain_pretend @explain_pretend end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
11 12 13 |
# File 'lib/kissgen/generator.rb', line 11 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/kissgen/generator.rb', line 11 def path @path end |
#setup_file_path ⇒ Object
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, = {}) 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, = {}) 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( = {}) puts([:pretend] ? @explain_pretend : @explain_generate) @files.each do |file| puts([:pretend] ? "#{file.copy_path}" : file.create) end puts @explain_footer end |
#import_setup ⇒ Object
Import setup file which is located in /path/to/generator/setup.rb
31 32 33 34 |
# File 'lib/kissgen/generator.rb', line 31 def import_setup raise SetupFailure, "Setup file does not exist in #{File.(setup_file_path)}" unless File.exists?(setup_file_path) instance_eval(File.new(setup_file_path).read) end |