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
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
|
require 'active_support/core_ext/hash/slice'
require "rails/generators/rails/app/app_generator"
module Rails
class PluginBuilder
def rakefile
template "Rakefile"
end
def readme
copy_file "README.rdoc"
end
def gemfile
template "Gemfile"
end
def license
template "MIT-LICENSE"
end
def gemspec
template "%name%.gemspec"
end
def gitignore
copy_file "gitignore", ".gitignore"
end
def lib
template "lib/%name%.rb"
if full?
template "lib/%name%/engine.rb"
end
end
def test
template "test/test_helper.rb"
template "test/%name%_test.rb"
if full?
template "test/integration/navigation_test.rb"
template "test/support/integration_case.rb"
end
end
def generate_test_dummy
opts = (options || {}).slice(:skip_active_record, :skip_javascript, :database, :javascript)
invoke Rails::Generators::AppGenerator,
[ File.expand_path(dummy_path, destination_root) ], opts
end
def test_dummy_config
template "rails/boot.rb", "#{dummy_path}/config/boot.rb", :force => true
template "rails/application.rb", "#{dummy_path}/config/application.rb", :force => true
end
def test_dummy_clean
inside dummy_path do
remove_file ".gitignore"
remove_file "db/seeds.rb"
remove_file "doc"
remove_file "Gemfile"
remove_file "lib/tasks"
remove_file "public/images/rails.png"
remove_file "public/index.html"
remove_file "public/robots.txt"
remove_file "README"
remove_file "test"
remove_file "vendor"
end
end
def script
directory "script" do |content|
"#{shebang}\n" + content
end
chmod "script", 0755, :verbose => false
end
def rakefile_test_tasks
<<-RUBY
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
RUBY
end
def dummy_path
"#{test_path}/dummy"
end
def test_path
"test"
end
end
module Generators
class PluginNewGenerator < AppBase
add_shared_options_for "plugin"
alias_method :plugin_path, :app_path
class_option :full, :type => :boolean, :default => false,
:desc => "Generate rails engine with integration tests"
def initialize(*args)
raise Error, "Options should be given after the plugin name. For details run: rails plugin --help" if args[0].blank?
super
end
public_task :create_root
def create_root_files
build(:readme)
build(:rakefile)
build(:gemspec)
build(:license)
build(:gitignore) unless options[:skip_git]
build(:gemfile) unless options[:skip_gemfile]
end
def create_config_files
build(:config)
end
def create_lib_files
build(:lib)
end
def create_script_files
build(:script)
end
def create_test_files
build(:test) unless options[:skip_test_unit]
end
def create_test_dummy_files
say_status :vendor_app, dummy_path
mute do
build(:generate_test_dummy)
end
end
def change_config_files
store_application_definition!
mute do
build(:test_dummy_config)
end
end
def remove_uneeded_rails_files
mute do
build(:test_dummy_clean)
end
end
def finish_template
build(:leftovers)
end
public_task :apply_rails_template, :bundle_if_dev_or_edge
protected
def full?
options[:full]
end
def self.banner
"rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
end
def name
@name ||= File.basename(destination_root)
end
def camelized
@camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize
end
def valid_const?
if camelized =~ /^\d/
raise Error, "Invalid plugin name #{name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(name)
raise Error, "Invalid plugin name #{name}. Please give a name which does not match one of the reserved rails words."
elsif Object.const_defined?(camelized)
raise Error, "Invalid plugin name #{name}, constant #{camelized} is already in use. Please choose another application name."
end
end
def application_definition
@application_definition ||= begin
dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
unless options[:pretend] || !File.exists?(dummy_application_path)
contents = File.read(dummy_application_path)
contents[(contents.index("module Dummy"))..-1]
end
end
end
alias :store_application_definition! :application_definition
def get_builder_class
defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
end
[:test_path, :dummy_path, :rakefile_test_tasks].each do |name|
define_method name do
builder.send(name) if builder.respond_to?(name)
end
end
def mute(&block)
shell.mute(&block)
end
end
end
end
|