Class: Mygen::Generator
- Inherits:
-
Object
- Object
- Mygen::Generator
- Defined in:
- lib/mygen/generator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dest_dir ⇒ Object
Returns the value of attribute dest_dir.
-
#dry_run ⇒ Object
Returns the value of attribute dry_run.
-
#name ⇒ Object
Returns the value of attribute name.
-
#template_source_dir ⇒ Object
Returns the value of attribute template_source_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #description ⇒ Object
- #file_destination(file, bindings) ⇒ Object
- #fileutils ⇒ Object
- #generator_name ⇒ Object
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
- #internal_template_files ⇒ Object
- #internal_template_source_dir ⇒ Object
- #make_template_tree(internal = false) ⇒ Object
- #move_file_in_place(src, dest) ⇒ Object
- #parse(file, bindings) ⇒ Object
- #parse_and_place_file(file, dest, bindings) ⇒ Object
-
#parse_templates(bindings) ⇒ Object
rename directories that should be filtered, from __name files should be from the destination, so no dirs needs to be filtered and only files need to be processed.
- #template_dirs(path = template_source_dir) ⇒ Object
- #template_files(path = template_source_dir) ⇒ Object
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
5 6 7 |
# File 'lib/mygen/generator.rb', line 5 def initialize @template_source_dir = File.join(ENV['HOME'], ".mygen", "plugins", generator_name, "templates") end |
Instance Attribute Details
#dest_dir ⇒ Object
Returns the value of attribute dest_dir.
3 4 5 |
# File 'lib/mygen/generator.rb', line 3 def dest_dir @dest_dir end |
#dry_run ⇒ Object
Returns the value of attribute dry_run.
3 4 5 |
# File 'lib/mygen/generator.rb', line 3 def dry_run @dry_run end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/mygen/generator.rb', line 3 def name @name end |
#template_source_dir ⇒ Object
Returns the value of attribute template_source_dir.
3 4 5 |
# File 'lib/mygen/generator.rb', line 3 def template_source_dir @template_source_dir end |
Class Method Details
.descendants ⇒ Object
9 10 11 |
# File 'lib/mygen/generator.rb', line 9 def self.descendants ObjectSpace.each_object(Class).select { |klass| klass < self } end |
Instance Method Details
#description ⇒ Object
13 14 15 |
# File 'lib/mygen/generator.rb', line 13 def description "Plugin has no description" end |
#file_destination(file, bindings) ⇒ Object
89 90 91 92 93 |
# File 'lib/mygen/generator.rb', line 89 def file_destination(file, bindings) # subtract template_source_dir from path, add dest_dir and substitute filenames new_file = file.gsub(template_source_dir, '') replaced_filename(new_file, bindings) end |
#fileutils ⇒ Object
26 27 28 |
# File 'lib/mygen/generator.rb', line 26 def fileutils dry_run ? FileUtils::DryRun : FileUtils::Verbose end |
#generator_name ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/mygen/generator.rb', line 17 def generator_name self.class.name.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). split('/').last. downcase end |
#internal_template_files ⇒ Object
38 39 40 |
# File 'lib/mygen/generator.rb', line 38 def internal_template_files template_files(internal_template_source_dir) end |
#internal_template_source_dir ⇒ Object
42 43 44 |
# File 'lib/mygen/generator.rb', line 42 def internal_template_source_dir File.join(Mygen.root, "templates", generator_name) end |
#make_template_tree(internal = false) ⇒ Object
46 47 48 49 50 |
# File 'lib/mygen/generator.rb', line 46 def make_template_tree(internal = false) @template_source_dir = internal_template_source_dir if internal fileutils.rm_rf(dest_dir) if File.exist?(dest_dir) fileutils.cp_r(template_source_dir, dest_dir) end |
#move_file_in_place(src, dest) ⇒ Object
78 79 80 81 82 |
# File 'lib/mygen/generator.rb', line 78 def move_file_in_place(src, dest) sf = File.absolute_path(src) df = File.absolute_path(dest) fileutils.mv(sf, df) unless sf == df end |
#parse(file, bindings) ⇒ Object
72 73 74 75 76 |
# File 'lib/mygen/generator.rb', line 72 def parse(file, bindings) erb = ERB.new(File.read(file)) result = erb.result bindings File.open(file, "w") { |f| f.write(result) } end |
#parse_and_place_file(file, dest, bindings) ⇒ Object
84 85 86 87 |
# File 'lib/mygen/generator.rb', line 84 def parse_and_place_file(file, dest, bindings) erb = ERB.new(File.read(file)) erb.result bindings end |
#parse_templates(bindings) ⇒ Object
rename directories that should be filtered, from __name files should be from the destination, so no dirs needs to be filtered and only files need to be processed.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mygen/generator.rb', line 56 def parse_templates(bindings) template_dirs(File.join(dest_dir)).each do |dir| dest = file_destination(dir, bindings) move_file_in_place(dir, dest) end # Filter files with erb template_files(File.join(dest_dir)).each do |file| dest = file_destination(file, bindings) # This is where you parse the erb files and fill in the contens if file.end_with? 'erb' parse(file, bindings) end move_file_in_place(file, dest) end end |
#template_dirs(path = template_source_dir) ⇒ Object
34 35 36 |
# File 'lib/mygen/generator.rb', line 34 def template_dirs(path = template_source_dir) Dir.glob(File.join(path, "**/*")).select { |f| File.directory? f } end |
#template_files(path = template_source_dir) ⇒ Object
30 31 32 |
# File 'lib/mygen/generator.rb', line 30 def template_files(path = template_source_dir) Dir.glob(File.join(path, "**/*")).select { |f| File.file? f } end |