Class: Mygen::Generator

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

Direct Known Subclasses

Plugins::MygenGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

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_dirObject

Returns the value of attribute dest_dir.



3
4
5
# File 'lib/mygen/generator.rb', line 3

def dest_dir
  @dest_dir
end

#dry_runObject

Returns the value of attribute dry_run.



3
4
5
# File 'lib/mygen/generator.rb', line 3

def dry_run
  @dry_run
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/mygen/generator.rb', line 3

def name
  @name
end

#template_source_dirObject

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

.descendantsObject



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

#descriptionObject



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

#fileutilsObject



26
27
28
# File 'lib/mygen/generator.rb', line 26

def fileutils
  dry_run ? FileUtils::DryRun : FileUtils::Verbose
end

#generator_nameObject



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_filesObject



38
39
40
# File 'lib/mygen/generator.rb', line 38

def internal_template_files
  template_files(internal_template_source_dir)
end

#internal_template_source_dirObject



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