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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/refinery/extension_generation.rb', line 4
def self.included(base)
base.class_eval do
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
class_option :namespace, :type => :string, :default => nil, :banner => 'NAMESPACE', :required => false
class_option :extension, :type => :string, :default => nil, :banner => 'ENGINE', :required => false
class_option :i18n, :type => :array, :default => [], :required => false, :banner => "field field", :desc => 'Indicates generated fields'
remove_class_option :skip_namespace
def namespacing
@namespacing ||= if options[:namespace].present?
options[:namespace].to_s.camelize
else
class_name.pluralize
end
end
def extension_name
@extension_name ||= if options[:extension].present?
options[:extension].to_s
else
singular_name
end
end
def extension_class_name
@extension_class_name ||= extension_name.camelize
end
def extension_plural_class_name
@extension_plural_class_name ||= if options[:extension].present?
extension_class_name
else
extension_class_name.pluralize
end
end
def extension_plural_name
@extension_plural_name ||= if options[:extension].present?
extension_name
else
extension_name.pluralize
end
end
def localized?
localized_attributes.any?
end
def localized_attributes
@localized_attributes ||= attributes.select{|a| options[:i18n].include?(a.name)}
end
def attributes_for_translation_table
localized_attributes.inject([]) {|memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
end
protected
def append_extension_to_gemfile!
unless Rails.env.test? || (self.behavior != :revoke && extension_in_gemfile?)
path = extension_pathname.parent.relative_path_from(gemfile.parent)
append_file gemfile, "\ngem '#{gem_name}', :path => '#{path}'"
end
end
def clash_keywords
@clash_keywords ||= begin
clash_keywords = []
unless (clash_file = source_pathname.parent.join('clash_keywords.yml')).file?
clash_file = source_pathname.parent.parent.join('clash_keywords.yml')
end
clash_keywords = YAML.load_file(clash_file) if clash_file.file?
clash_keywords
end
end
def destination_pathname
@destination_pathname ||= Pathname.new(self.destination_root.to_s)
end
def extension_pathname
destination_pathname.join('vendor', 'extensions', extension_plural_name)
end
def extension_path_for(path, extension)
path = extension_pathname.join(path.to_s.gsub(%r{#{source_pathname}/?}, '')).to_s
path.gsub!('extension_plural_name', extension_plural_name)
path.gsub!('plural_name', plural_name)
path.gsub!('singular_name', singular_name)
path.gsub!('namespace', namespacing.underscore)
if options[:namespace].present? || options[:extension].present?
if path =~ %r{/migrate/\d+\w*.rb\z}
if last_migration = Dir["#{destination_pathname.join(path.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR), '*.rb')}"].sort.last
path.gsub!(%r{\d+_}) { |m| "#{last_migration.match(%r{migrate/(\d+)_})[1].to_i + 1}_" }
end
end
if extension.present? && File.exist?(path)
path = if path =~ %r{/locales/.*\.yml$} or path =~ %r{/routes.rb$} or path =~ %r{/#{gem_name}.rb$}
path.split(File::SEPARATOR).insert(-2, "tmp").join(File::SEPARATOR)
elsif path =~ %r{/readme.md$} or path =~ %r{/#{plural_name}.rb$}
nil
else
path
end
elsif extension.present? and path =~ /lib\/#{plural_name}.rb$/
path = nil
end
end
path
end
def erase_destination!
if Pathname.glob(extension_pathname.join('**', '*')).all?(&:directory?)
say_status :remove, relative_to_original_destination_root(extension_pathname.to_s), true
FileUtils.rm_rf extension_pathname unless options[:pretend]
end
end
def evaluate_templates!
Pathname.glob(source_pathname.join('**', '**')).reject{|f|
reject_template?(f)
}.sort.each do |path|
if (template_path = extension_path_for(path, extension_name)).present?
template path, template_path
end
end
end
def existing_extension?
options[:extension].present? && extension_pathname.directory? && extension_in_gemfile?
end
def exit_with_message!(message)
puts ""
puts message
puts ""
exit(1)
end
def extension_in_gemfile?
gemfile.read.scan(%r{#{gem_name}}).any?
end
def finalize_extension!
if self.behavior != :revoke and !self.options['pretend']
puts_instructions!
else
erase_destination!
end
end
def gem_name
"refinerycms-#{extension_plural_name}"
end
def gemfile
@gemfile ||= begin
Bundler.default_gemfile || destination_pathname.join('Gemfile')
end
end
def merge_locales!
if existing_extension?
tmp_directories = []
Dir.glob(source_pathname.join("{config/locales/*.yml,config/routes.rb,lib/refinerycms-extension_plural_name.rb}"), File::FNM_DOTMATCH).sort.each do |path|
new_file_path = destination_pathname.join(extension_path_for(path, extension_name))
tmp_directories << Pathname.new(new_file_path.to_s.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR)) current_path = Pathname.new(new_file_path.to_s.split(File::SEPARATOR).reject{|f| f == 'tmp'}.join(File::SEPARATOR))
new_contents = nil
if new_file_path.to_s =~ %r{.yml$}
new_contents = YAML::load(new_file_path.read).deep_merge(YAML::load(current_path.read)).to_yaml.gsub(%r{^---\n}, '')
elsif new_file_path.to_s =~ %r{/routes.rb$}
routes_file = [(file_parts = current_path.read.to_s.split("\n")).first]
routes_file += file_parts[1..-2]
routes_file += new_file_path.read.to_s.split("\n")[1..-2]
routes_file << file_parts.last
new_contents = routes_file.join("\n")
elsif new_file_path.to_s =~ %r{/#{gem_name}.rb$}
new_contents = current_path.read + new_file_path.read
end
current_path.open('w+') { |file| file.puts new_contents } if new_contents
end
tmp_directories.uniq.each{|dir| remove_dir(dir) if dir && dir.exist?}
end
end
def puts_instructions!
unless Rails.env.test?
puts "------------------------"
puts "Now run:"
puts "bundle install"
puts "rails generate refinery:#{extension_plural_name}"
puts "rake db:migrate"
puts "rake db:seed"
puts "------------------------"
end
end
def reject_file?(file)
!localized? && file.to_s.include?('locale_picker')
end
def reject_template?(file)
file.directory? || reject_file?(file)
end
def sanity_check!
if clash_keywords.member?(singular_name.downcase)
exit_with_message!("Please choose a different name. The generated code would fail for class '#{singular_name}' as it conflicts with a reserved keyword.")
end
if singular_name == plural_name
message = if singular_name.singularize == singular_name
"The extension name you specified will not work as the singular name is equal to the plural name."
else
"Please specify the singular name '#{singular_name.singularize}' instead of '#{plural_name}'."
end
exit_with_message!(message)
end
if attributes.empty? && self.behavior != :revoke
exit_with_message!("You must specify a name and at least one field. For help: #{generator_command}")
end
end
def source_pathname
@source_pathname ||= Pathname.new(self.class.source_root.to_s)
end
end
end
|