Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 319 lines (257 sloc) 8.474 kb
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
1 require 'active_support/core_ext/hash/slice'
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
2 require "rails/generators/rails/app/app_generator"
94031ef Samuel Kadolph Add require 'date' to use Date.today in plugin template MIT-LICENSE
samuelkadolph authored
3 require 'date'
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
4
5 module Rails
6 class PluginBuilder
7 def rakefile
8 template "Rakefile"
9 end
10
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
11 def app
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
12 if mountable?
ed32c8f Piotr Sarnacki Generate default layout in engine with --mountable option
drogus authored
13 directory "app"
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
14 empty_directory_with_gitkeep "app/assets/images/#{name}"
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
15 elsif full?
16 empty_directory_with_gitkeep "app/models"
17 empty_directory_with_gitkeep "app/controllers"
18 empty_directory_with_gitkeep "app/views"
19 empty_directory_with_gitkeep "app/helpers"
d8e62a9 Dan Gebhardt Include empty app/mailers directory in mountable and full plugins
dgeb authored
20 empty_directory_with_gitkeep "app/mailers"
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
21 empty_directory_with_gitkeep "app/assets/images/#{name}"
ed32c8f Piotr Sarnacki Generate default layout in engine with --mountable option
drogus authored
22 end
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
23 end
24
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
25 def readme
bbcdc86 Santiago Pastorino README.rdoc is a template
spastorino authored
26 template "README.rdoc"
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
27 end
28
29 def gemfile
30 template "Gemfile"
31 end
32
33 def license
34 template "MIT-LICENSE"
35 end
36
37 def gemspec
38 template "%name%.gemspec"
39 end
40
41 def gitignore
4934908 Tyler Coville Fixed error with 'rails generate new plugin' where the .gitignore was no...
tylercoville authored
42 template "gitignore", ".gitignore"
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
43 end
44
45 def lib
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
46 template "lib/%name%.rb"
c159b50 Piotr Sarnacki Add example rake task to 'plugin new' generator
drogus authored
47 template "lib/tasks/%name%_tasks.rake"
d9b59c3 Dan Gebhardt Extracted version from gemspec and placed it in its own file. This is co...
dgeb authored
48 template "lib/%name%/version.rb"
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
49 if full?
50 template "lib/%name%/engine.rb"
51 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
52 end
53
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
54 def config
d75ff73 Santiago Pastorino plugin new generator should generate config/routes.rb file for full engi...
spastorino authored
55 template "config/routes.rb" if full?
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
56 end
57
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
58 def test
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
59 template "test/test_helper.rb"
60 template "test/%name%_test.rb"
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
61 append_file "Rakefile", <<-EOF
62 #{rakefile_test_tasks}
63
64 task :default => :test
65 EOF
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
66 if full?
67 template "test/integration/navigation_test.rb"
68 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
69 end
70
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
71 PASSTHROUGH_OPTIONS = [
72 :skip_active_record, :skip_javascript, :database, :javascript, :quiet, :pretend, :force, :skip
73 ]
74
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
75 def generate_test_dummy(force = false)
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
76 opts = (options || {}).slice(*PASSTHROUGH_OPTIONS)
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
77 opts[:force] = force
0a070a2 Santiago Pastorino Run bundle install once when using plugin_new --full --mountable. Closes...
spastorino authored
78 opts[:skip_bundle] = true
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
79
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
80 invoke Rails::Generators::AppGenerator,
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
81 [ File.expand_path(dummy_path, destination_root) ], opts
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
82 end
83
84 def test_dummy_config
85 template "rails/boot.rb", "#{dummy_path}/config/boot.rb", :force => true
86 template "rails/application.rb", "#{dummy_path}/config/application.rb", :force => true
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
87 if mountable?
88 template "rails/routes.rb", "#{dummy_path}/config/routes.rb", :force => true
89 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
90 end
91
92 def test_dummy_clean
93 inside dummy_path do
94 remove_file ".gitignore"
95 remove_file "db/seeds.rb"
96 remove_file "doc"
97 remove_file "Gemfile"
98 remove_file "lib/tasks"
d42e43b Arun Agrawal Fixed images path in plugin_new_generator
arunagw authored
99 remove_file "app/assets/images/rails.png"
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
100 remove_file "public/index.html"
101 remove_file "public/robots.txt"
102 remove_file "README"
103 remove_file "test"
104 remove_file "vendor"
105 end
106 end
107
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
108 def stylesheets
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
109 if mountable?
5952d4f Piotr Sarnacki Fix plugin new generator to work with new javascript/stylesheets convent...
drogus authored
110 copy_file "#{app_templates_dir}/app/assets/stylesheets/application.css",
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
111 "app/assets/stylesheets/#{name}/application.css"
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
112 elsif full?
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
113 empty_directory_with_gitkeep "app/assets/stylesheets/#{name}"
5952d4f Piotr Sarnacki Fix plugin new generator to work with new javascript/stylesheets convent...
drogus authored
114 end
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
115 end
116
117 def javascripts
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
118 return if options.skip_javascript?
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
119
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
120 if mountable?
baf2a9c Justin Dewyea properly require javascript libraries when generating mountable engine
jdewyea authored
121 template "#{app_templates_dir}/app/assets/javascripts/application.js.tt",
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
122 "app/assets/javascripts/#{name}/application.js"
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
123 elsif full?
4dc3bb9 Ryan Bates namespace engine assets
ryanb authored
124 empty_directory_with_gitkeep "app/assets/javascripts/#{name}"
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
125 end
126 end
127
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
128 def script(force = false)
e38d375 Stefan Sprenger Generate script/rails only if --full is given
flippingbits authored
129 return unless full?
130
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
131 directory "script", :force => force do |content|
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
132 "#{shebang}\n" + content
133 end
134 chmod "script", 0755, :verbose => false
135 end
136 end
137
138 module Generators
b37938e Piotr Sarnacki Refactored AppGenerator and PluginNewGenerator to inherit from AppBase.
drogus authored
139 class PluginNewGenerator < AppBase
d995953 Piotr Sarnacki DRY up app generator and plugin new generator. Moved shared options to A...
drogus authored
140 add_shared_options_for "plugin"
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
141
d995953 Piotr Sarnacki DRY up app generator and plugin new generator. Moved shared options to A...
drogus authored
142 alias_method :plugin_path, :app_path
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
143
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
144 class_option :dummy_path, :type => :string, :default => "test/dummy",
145 :desc => "Create dummy application at given path"
cc135e3 Piotr Sarnacki Allow to set dummy application path through options
drogus authored
146
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
147 class_option :full, :type => :boolean, :default => false,
148 :desc => "Generate rails engine with integration tests"
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
149
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
150 class_option :mountable, :type => :boolean, :default => false,
151 :desc => "Generate mountable isolated application"
7344332 Piotr Sarnacki Add skip-gemspec option to 'plugin new' generator
drogus authored
152
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
153 class_option :skip_gemspec, :type => :boolean, :default => false,
154 :desc => "Skip gemspec file"
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
155
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
156 def initialize(*args)
ae1debd Piotr Sarnacki Make tests for app and plugin generators more DRY
drogus authored
157 raise Error, "Options should be given after the plugin name. For details run: rails plugin --help" if args[0].blank?
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
158
dfa9e28 jack dempsey silence @dummy_path not defined warnings
jackdempsey authored
159 @dummy_path = nil
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
160 super
161 end
162
9cb9713 Piotr Sarnacki Refactor app and plugin generators to use new Thor API
drogus authored
163 public_task :create_root
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
164
165 def create_root_files
166 build(:readme)
167 build(:rakefile)
7344332 Piotr Sarnacki Add skip-gemspec option to 'plugin new' generator
drogus authored
168 build(:gemspec) unless options[:skip_gemspec]
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
169 build(:license)
170 build(:gitignore) unless options[:skip_git]
171 build(:gemfile) unless options[:skip_gemfile]
172 end
173
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
174 def create_app_files
175 build(:app)
176 end
177
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
178 def create_config_files
179 build(:config)
180 end
181
182 def create_lib_files
183 build(:lib)
184 end
185
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
186 def create_public_stylesheets_files
187 build(:stylesheets)
188 end
189
190 def create_javascript_files
191 build(:javascripts)
192 end
193
a1c31ec Prem Sichanugrist Add `app/assets/images` directory to plugin new generator
sikachu authored
194 def create_images_directory
195 build(:images)
196 end
197
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
198 def create_script_files
199 build(:script)
200 end
201
202 def create_test_files
203 build(:test) unless options[:skip_test_unit]
204 end
205
206 def create_test_dummy_files
3d9d9f8 Damien Mathieu create the dummy app if there's no test_unit but there's a dummy_path
dmathieu authored
207 return if options[:skip_test_unit] && options[:dummy_path] == 'test/dummy'
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
208 create_dummy_app
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
209 end
210
bcd414f Piotr Sarnacki Add support for templates for rails plugin new
drogus authored
211 def finish_template
212 build(:leftovers)
213 end
214
c88dddb Xavier Noria application and plugin generation run bundle check unless --skip-gemfile
fxn authored
215 public_task :apply_rails_template, :run_bundle
e51e9e2 Piotr Sarnacki Add --dev and --edge options to rails plugin new
drogus authored
216
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
217 protected
eb75f15 José Valim Generate app/* stubs directories for full plugins to show ththat it is p...
josevalim authored
218
978314c Piotr Sarnacki Generate javascripts in engine with --mountable option
drogus authored
219 def app_templates_dir
220 "../../app/templates"
221 end
222
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
223 def create_dummy_app(path = nil)
224 dummy_path(path) if path
225
fadad11 Piotr Sarnacki Properly skip test unit in 'plugin new' generator
drogus authored
226 say_status :vendor_app, dummy_path
227 mute do
228 build(:generate_test_dummy)
229 store_application_definition!
230 build(:test_dummy_config)
231 build(:test_dummy_clean)
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
232 # ensure that script/rails has proper dummy_path
233 build(:script, true)
fadad11 Piotr Sarnacki Properly skip test unit in 'plugin new' generator
drogus authored
234 end
235 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
236
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
237 def full?
cbe391b Piotr Sarnacki Add --mountable option to 'plugin new' generator which generates full mo...
drogus authored
238 options[:full] || options[:mountable]
239 end
240
241 def mountable?
242 options[:mountable]
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
243 end
244
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
245 def self.banner
246 "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
247 end
248
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
249 def original_name
250 @original_name ||= File.basename(destination_root)
251 end
252
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
253 def name
c220b4d Sergey Nartimov fix warning about instance variable in plugin generator
lest authored
254 @name ||= begin
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
255 # same as ActiveSupport::Inflector#underscore except not replacing '-'
c220b4d Sergey Nartimov fix warning about instance variable in plugin generator
lest authored
256 underscored = original_name.dup
257 underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
258 underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
259 underscored.downcase!
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
260
c220b4d Sergey Nartimov fix warning about instance variable in plugin generator
lest authored
261 underscored
262 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
263 end
264
265 def camelized
266 @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize
267 end
268
b37938e Piotr Sarnacki Refactored AppGenerator and PluginNewGenerator to inherit from AppBase.
drogus authored
269 def valid_const?
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
270 if camelized =~ /^\d/
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
271 raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers."
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
272 elsif RESERVED_NAMES.include?(name)
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
273 raise Error, "Invalid plugin name #{original_name}. Please give a name which does not match one of the reserved rails words."
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
274 elsif Object.const_defined?(camelized)
39d2251 Sergey Nartimov fix rails plugin new CamelCasedName bug
lest authored
275 raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name."
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
276 end
277 end
278
279 def application_definition
280 @application_definition ||= begin
671d146 Piotr Sarnacki Add --full option to 'plugin new' generator, which generates rails engin...
drogus authored
281
68295bc Piotr Sarnacki Remove integration tests and ActionModel/ActiveRecord calls from 'rake p...
drogus authored
282 dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
283 unless options[:pretend] || !File.exists?(dummy_application_path)
284 contents = File.read(dummy_application_path)
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
285 contents[(contents.index("module Dummy"))..-1]
286 end
287 end
288 end
289 alias :store_application_definition! :application_definition
b37938e Piotr Sarnacki Refactored AppGenerator and PluginNewGenerator to inherit from AppBase.
drogus authored
290
291 def get_builder_class
292 defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
293 end
b36fa51 Piotr Sarnacki Allow easy overriding of test framework in 'rake plugin new' generator, ...
drogus authored
294
f9e33fc Piotr Sarnacki create_dummy_app method that allows to easily create dummy application f...
drogus authored
295 def rakefile_test_tasks
296 <<-RUBY
297 require 'rake/testtask'
298
299 Rake::TestTask.new(:test) do |t|
300 t.libs << 'lib'
301 t.libs << 'test'
302 t.pattern = 'test/**/*_test.rb'
303 t.verbose = false
304 end
305 RUBY
306 end
307
308 def dummy_path(path = nil)
309 @dummy_path = path if path
cc135e3 Piotr Sarnacki Allow to set dummy application path through options
drogus authored
310 @dummy_path || options[:dummy_path]
b36fa51 Piotr Sarnacki Allow easy overriding of test framework in 'rake plugin new' generator, ...
drogus authored
311 end
489b279 Piotr Sarnacki Don't be verbose while creating dummy application in plugin new generato...
drogus authored
312
313 def mute(&block)
314 shell.mute(&block)
315 end
cfcea1d Piotr Sarnacki Added 'rails plugin new' generator which generates gem plugin skeleton.
drogus authored
316 end
317 end
318 end
Something went wrong with that request. Please try again.