/
generator.rb
124 lines (96 loc) · 2.97 KB
/
generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module Forge
class Generator
class << self
def run(project, layout='default')
generator = self.new(project, layout)
generator.run
end
end
def initialize(project, layout='default')
@project = project
@task = project.task
@layout = layout
end
def create_structure
paths = [
['.forge'],
['assets', 'images'],
['assets', 'javascripts'],
['assets', 'stylesheets'],
['functions', 'inc'],
['templates', 'core'],
['templates', 'custom', 'pages'],
['templates', 'custom', 'partials']
]
paths.each do |path|
@task.empty_directory File.join(@project.root, path)
end
self
end
def copy_stylesheets
source = [@layout, 'stylesheets', 'style.css.scss.erb']
target = [@project.assets_path, 'stylesheets', 'style.css.scss']
write_template(source, target)
self
end
def copy_templates
source = File.expand_path(File.join(self.layout_path, 'templates'))
target = File.expand_path(File.join(@project.root, 'templates'))
Dir.glob("#{source}/**/*") do |file|
unless File.directory?(file)
source_file = file.gsub(source, '')
target_file = File.join(target, source_file)
if source_file.end_with? ".erb"
target_file = target_file.slice(0..-5)
content = render_erb(file)
else
content = File.open(file).read
end
@task.create_file target_file do
content
end
end
end
self
end
def copy_settings_library
settings_path = @task.find_in_source_paths(File.join('lib', 'forge-settings', 'classes'))
source = File.expand_path(settings_path)
target = File.expand_path(File.join(@project.root, 'functions', 'inc', 'forge-settings', '.'))
@task.directory(source, target)
end
def copy_functions
source = File.expand_path(File.join(self.layout_path, 'functions', 'functions.php.erb'))
target = File.expand_path(File.join(@project.root, 'functions', 'functions.php'))
write_template(source, target)
end
def layout_path
@layout_path ||= File.join(Forge::ROOT, 'layouts', @layout)
end
def run
write_config
create_structure
copy_stylesheets
copy_templates
copy_functions
copy_settings_library
return self
end
def write_config
write_template(['config', 'config.json.erb'], @project.config_file)
self
end
def write_template(source, target)
source = File.join(source)
template = File.expand_path(@task.find_in_source_paths((source)))
target = File.expand_path(File.join(target))
@task.create_file target do
render_erb(template)
end
end
protected
def render_erb(file)
ERB.new(::File.binread(file), nil, '-', '@output_buffer').result(@project.get_binding)
end
end
end