1 # Copyright (c) 2005 Jonathan Lim <snowblink@gmail.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 def complex_template(relative_source, relative_destination, template_options = {})
30 options = template_options.dup
31 options[:assigns] ||= {}
32 options[:assigns]['template_for_inclusion'] = render_template_part(template_options) if template_options[:mark_id]
33 options[:assigns]['license'] = render_license(template_options)
34 template(relative_source, relative_destination, options)
37 def render_license(template_options)
38 # Getting Sandbox to evaluate part template in it
39 part_binding = template_options[:sandbox].call.sandbox_binding
40 part_rel_path = template_options[:insert]
41 part_path = source_path(part_rel_path)
43 # Render inner template within Sandbox binding
44 template_file = File.readlines(part_path)
45 case template_options[:comment_style]
47 template_file.map! {|x| x.sub(/^/, '# ')}
49 rendered_part = ERB.new(template_file.join, nil, '-').result(part_binding)
58 class LicensingSandbox
59 include ActionView::Helpers::ActiveRecordHelper
75 print "Please enter the author's name: "
80 print "Please enter the author's email: "
85 "#{@name} <#{@email}>"
90 def initialize(source_root)
91 @source_root = source_root
96 # list all the licenses in the licenses directory
97 licenses = Dir.entries(File.join(@source_root, 'licenses')).select { |name| name !~ /^\./ }
98 puts "We can generate the following licenses automatically for you:"
99 licenses.sort.each_with_index do |license, index|
100 puts "#{index}) #{licenses[index]}"
102 print "Please select a license: "
103 while choice = gets.chomp
104 if (choice !~ /^[0-9]+$/)
105 print "Hint - you want to be typing a number.\nPlease select a license: "
108 break if choice.to_i >=0 && choice.to_i <= licenses.length
111 @license = licenses[choice.to_i]
112 puts "'#{@license}' selected"
116 File.join('licenses', @license)
121 class EngineGenerator < Rails::Generator::NamedBase
123 attr_reader :engine_class_name, :engine_underscored_name, :engine_start_name, :author
126 def initialize(runtime_args, runtime_options = {})
128 @engine_class_name = runtime_args.shift
130 # ensure that they've given us a valid class name
131 if @engine_class_name =~ /^[a-z]/
132 raise "'#{@engine_class_name}' should be a valid Ruby constant, e.g. 'MyEngine'; aborting generation..."
135 @engine_underscored_name = @engine_class_name.underscore
136 @engine_start_name = @engine_underscored_name.sub(/_engine$/, '')
138 @license = License.new(source_root)
143 m.directory File.join('vendor', 'plugins')
144 m.directory File.join('vendor', 'plugins', @engine_underscored_name)
145 m.complex_template 'README',
146 File.join('vendor', 'plugins', @engine_underscored_name, 'README'),
147 :sandbox => lambda {create_sandbox},
148 :insert => @license.to_s
150 m.file 'install.erb', File.join('vendor', 'plugins', @engine_underscored_name, 'install.rb')
152 m.complex_template 'init_engine.erb',
153 File.join('vendor', 'plugins', @engine_underscored_name, 'init_engine.rb'),
154 :sandbox => lambda {create_sandbox},
155 :insert => @license.to_s,
156 :comment_style => :rb
158 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'app')
159 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'app', 'models')
160 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'app', 'controllers')
161 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'app', 'helpers')
162 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'app', 'views')
163 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'db')
164 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'db', 'migrate')
165 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'lib')
166 m.complex_template File.join('lib', 'engine.erb'),
167 File.join('vendor', 'plugins', @engine_underscored_name, 'lib', "#{@engine_underscored_name}.rb"),
168 :sandbox => lambda {create_sandbox},
169 :insert => @license.to_s,
170 :comment_style => :rb
172 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'lib', @engine_underscored_name)
173 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'public')
174 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'public', 'javascripts')
175 m.template File.join('public', 'javascripts', 'engine.js'), File.join('vendor', 'plugins', @engine_underscored_name, 'public', 'javascripts', "#{@engine_underscored_name}.js")
176 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'public', 'stylesheets')
177 m.template File.join('public', 'stylesheets', 'engine.css'), File.join('vendor', 'plugins', @engine_underscored_name, 'public', 'stylesheets', "#{@engine_underscored_name}.css")
178 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'tasks')
179 m.template File.join('tasks', 'engine.rake'), File.join('vendor', 'plugins', @engine_underscored_name, 'tasks', "#{@engine_underscored_name}.rake")
180 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'test')
181 m.template File.join('test', 'test_helper.erb'), File.join('vendor', 'plugins', @engine_underscored_name, 'test', 'test_helper.rb')
182 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'test', 'fixtures')
183 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'test', 'functional')
184 m.directory File.join('vendor', 'plugins', @engine_underscored_name, 'test', 'unit')
190 "Usage: #{$0} #{spec.name} MyEngine [general options]"
194 sandbox = LicensingSandbox.new
195 sandbox.author = @author