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
|
# File 'lib/generators/refinery_engine/refinery_engine_generator.rb', line 12
def generate
if singular_name == plural_name
puts ""
if singular_name.singularize != singular_name
puts "Please specify the singular name '#{singular_name.singularize}' instead of '#{plural_name}'."
else
puts "The engine name you specified will not work as the singular name is equal to the plural name."
end
puts ""
exit(1)
end
unless attributes.empty? and self.behavior != :revoke
if (engine = attributes.detect{|a| a.type.to_s == 'engine'}).present? and attributes.reject!{|a| a.type.to_s == 'engine'}.present?
engine = engine.name.pluralize
end
Pathname.glob(Pathname.new(self.class.source_root).join('**', '**')).reject{|f| f.directory?}.sort.each do |path|
unless (engine_path = engine_path_for(path, engine)).nil?
template path, engine_path
end
end
if engine.present?
tmp_directories = []
Dir.glob(File.expand_path('../templates/{config/locales/*.yml,config/routes.rb,features/support/paths.rb}', __FILE__), File::FNM_DOTMATCH).sort.each do |path|
new_file_path = Rails.root.join(engine_path_for(path, engine))
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
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{/features/support.*paths.rb$}
paths_file = (file_parts = current_path.read.to_s.split("\n"))[0..4]
paths_file << file_parts[5..-8].join("\n")
new_file_parts = new_file_path.read.split("\n")
paths_file << new_file_parts[5..-4].join("\n")
paths_file << file_parts[-3..-1]
new_contents = paths_file.join("\n")
end
current_path.open('w+') { |f| f.puts new_contents } unless new_contents.nil?
end
tmp_directories.uniq.each{|d| d.rmtree unless d.nil? or !d.exist?}
end
if self.behavior != :revoke and !self.options['pretend']
unless Rails.env.test?
Rails.root.join('Gemfile').open('a') do |f|
f.write "\ngem 'refinerycms-#{plural_name}', '1.0', :path => 'vendor/engines'"
end unless engine.present?
puts "------------------------"
puts "Now run:"
puts "bundle install"
unless engine.present?
puts "rails generate refinerycms_#{plural_name}"
else
puts "rails generate refinerycms_#{engine} #{plural_name}"
end
puts "rake db:migrate"
puts "------------------------"
end
elsif self.behavior == :revoke
lines = Rails.root.join('Gemfile').open('r').read.split("\n")
Rails.root.join('Gemfile').open('w').puts(lines.reject {|l|
l =~ %r{refinerycms-#{plural_name}}
}.join("\n"))
::Refinery::Generators::Migrations.revoke({
:pattern => File.expand_path('../templates/db/migrate/*.rb', __FILE__),
:plural_name => plural_name,
:singular_name => singular_name
})
end
else
puts "You must specify at least one field. For help: rails generate refinery_engine"
end
end
|