Class: Mygen::Generator

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

Direct Known Subclasses

Plugins::MygenGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Naming

#camel_case, #cc_name, #d_name, #dash_case, #method_name, #no_case, #s_name, #snake_case

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



9
10
11
# File 'lib/mygen/generator.rb', line 9

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.



7
8
9
# File 'lib/mygen/generator.rb', line 7

def dest_dir
  @dest_dir
end

#dry_runObject

Returns the value of attribute dry_run.



7
8
9
# File 'lib/mygen/generator.rb', line 7

def dry_run
  @dry_run
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/mygen/generator.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/mygen/generator.rb', line 7

def options
  @options
end

#template_source_dirObject

Returns the value of attribute template_source_dir.



7
8
9
# File 'lib/mygen/generator.rb', line 7

def template_source_dir
  @template_source_dir
end

Class Method Details

.descendantsObject



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

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Instance Method Details

#descriptionObject



17
18
19
# File 'lib/mygen/generator.rb', line 17

def description
  "Plugin has no description"
end

#file_destination(file, bindings) ⇒ Object



111
112
113
114
115
# File 'lib/mygen/generator.rb', line 111

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



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

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

#generator_nameObject



21
22
23
# File 'lib/mygen/generator.rb', line 21

def generator_name
  snake_case(self.class.name)
end

#internal_template_filesObject



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

def internal_template_files
  template_files(internal_template_source_dir)
end

#internal_template_source_dirObject



41
42
43
# File 'lib/mygen/generator.rb', line 41

def internal_template_source_dir
  File.join(Mygen.root, "templates", generator_name)
end

#make_template_tree(internal = false) ⇒ Object



50
51
52
53
54
# File 'lib/mygen/generator.rb', line 50

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

#make_template_tree_in_current_dirObject



45
46
47
48
# File 'lib/mygen/generator.rb', line 45

def make_template_tree_in_current_dir
  fileutils.mkdir_p(dest_dir) unless File.exist?(dest_dir)
  fileutils.cp_r(Dir.glob(File.join(template_source_dir, "/*")), dest_dir)
end

#move_file_in_place(src, dest) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mygen/generator.rb', line 90

def move_file_in_place(src, dest)
  sf = File.absolute_path(src)
  df = File.absolute_path(dest)
  file_exist = File.exist?(df)
  fileutils.mv(sf, df) if sf != df && !file_exist

  if file_exist && sf != df
    Dir.glob(File.join(sf, "/*")).each do |f|
      next if f == sf
      file = f.sub(sf, '')
      fileutils.mv(f, File.join(df, file))
    end
    fileutils.rm_rf(sf)
  end
end

#parent_dirs_dont_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/mygen/generator.rb', line 78

def parent_dirs_dont_exist?(path)
  parent_dir = File.expand_path("..", path)
  return false if path.include?('__')
  return !File.exist?(path)
end

#parse(file, bindings) ⇒ Object



84
85
86
87
88
# File 'lib/mygen/generator.rb', line 84

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



106
107
108
109
# File 'lib/mygen/generator.rb', line 106

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mygen/generator.rb', line 60

def parse_templates(bindings)
  template_dirs(File.join(dest_dir)).each do |dir|
    dest = file_destination(dir, bindings)
    parent_dir = File.expand_path("..", dest)
    fileutils.mkdir_p(parent_dir) if parent_dirs_dont_exist?(dest)
    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



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

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



29
30
31
# File 'lib/mygen/generator.rb', line 29

def template_files(path = template_source_dir)
  Dir.glob(File.join(path, "**/*")).select { |f| File.file? f }
end