Class: Mygen::Generator
- Inherits:
-
Object
show all
- Includes:
- Naming
- Defined in:
- lib/mygen/generator.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Naming
#camel_case, #cc_name, #d_name, #dash_case, #no_case, #s_name, #snake_case
Constructor Details
Returns a new instance of Generator.
7
8
9
|
# File 'lib/mygen/generator.rb', line 7
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.
5
6
7
|
# File 'lib/mygen/generator.rb', line 5
def dest_dir
@dest_dir
end
|
#dry_run ⇒ Object
Returns the value of attribute dry_run.
5
6
7
|
# File 'lib/mygen/generator.rb', line 5
def dry_run
@dry_run
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/mygen/generator.rb', line 5
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/mygen/generator.rb', line 5
def options
@options
end
|
#template_source_dir ⇒ Object
Returns the value of attribute template_source_dir.
5
6
7
|
# File 'lib/mygen/generator.rb', line 5
def template_source_dir
@template_source_dir
end
|
Class Method Details
.descendants ⇒ Object
11
12
13
|
# File 'lib/mygen/generator.rb', line 11
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
|
Instance Method Details
#description ⇒ Object
15
16
17
|
# File 'lib/mygen/generator.rb', line 15
def description
"Plugin has no description"
end
|
#file_destination(file, bindings) ⇒ Object
93
94
95
96
97
|
# File 'lib/mygen/generator.rb', line 93
def file_destination(file, bindings)
new_file = file.gsub(template_source_dir, '')
replaced_filename(new_file, bindings)
end
|
#fileutils ⇒ Object
23
24
25
|
# File 'lib/mygen/generator.rb', line 23
def fileutils
dry_run ? FileUtils::DryRun : FileUtils::Verbose
end
|
#generator_name ⇒ Object
19
20
21
|
# File 'lib/mygen/generator.rb', line 19
def generator_name
snake_case(self.class.name)
end
|
#internal_template_files ⇒ Object
35
36
37
|
# File 'lib/mygen/generator.rb', line 35
def internal_template_files
template_files(internal_template_source_dir)
end
|
#internal_template_source_dir ⇒ Object
39
40
41
|
# File 'lib/mygen/generator.rb', line 39
def internal_template_source_dir
File.join(Mygen.root, "templates", generator_name)
end
|
#make_template_tree(internal = false) ⇒ Object
48
49
50
51
52
|
# File 'lib/mygen/generator.rb', line 48
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_dir ⇒ Object
43
44
45
46
|
# File 'lib/mygen/generator.rb', line 43
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
82
83
84
85
86
|
# File 'lib/mygen/generator.rb', line 82
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
76
77
78
79
80
|
# File 'lib/mygen/generator.rb', line 76
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
88
89
90
91
|
# File 'lib/mygen/generator.rb', line 88
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.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/mygen/generator.rb', line 58
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) unless File.exist?(parent_dir)
move_file_in_place(dir, dest)
end
template_files(File.join(dest_dir)).each do |file|
dest = file_destination(file, bindings)
if file.end_with? 'erb'
parse(file, bindings)
end
move_file_in_place(file, dest)
end
end
|
#template_dirs(path = template_source_dir) ⇒ Object
31
32
33
|
# File 'lib/mygen/generator.rb', line 31
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
27
28
29
|
# File 'lib/mygen/generator.rb', line 27
def template_files(path = template_source_dir)
Dir.glob(File.join(path, "**/*")).select { |f| File.file? f }
end
|