rubigen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +3 -0
- data/License.txt +17 -0
- data/Manifest.txt +119 -0
- data/README.txt +204 -0
- data/Rakefile +142 -0
- data/bin/ruby_app +12 -0
- data/examples/rails_generators/applications/app/USAGE +16 -0
- data/examples/rails_generators/applications/app/app_generator.rb +177 -0
- data/examples/rails_generators/components/controller/USAGE +29 -0
- data/examples/rails_generators/components/controller/controller_generator.rb +37 -0
- data/examples/rails_generators/components/controller/templates/controller.rb +10 -0
- data/examples/rails_generators/components/controller/templates/functional_test.rb +18 -0
- data/examples/rails_generators/components/controller/templates/helper.rb +2 -0
- data/examples/rails_generators/components/controller/templates/view.html.erb +2 -0
- data/examples/rails_generators/components/integration_test/USAGE +8 -0
- data/examples/rails_generators/components/integration_test/integration_test_generator.rb +16 -0
- data/examples/rails_generators/components/integration_test/templates/integration_test.rb +10 -0
- data/examples/rails_generators/components/mailer/USAGE +16 -0
- data/examples/rails_generators/components/mailer/mailer_generator.rb +34 -0
- data/examples/rails_generators/components/mailer/templates/fixture.erb +3 -0
- data/examples/rails_generators/components/mailer/templates/fixture.rhtml +0 -0
- data/examples/rails_generators/components/mailer/templates/mailer.rb +13 -0
- data/examples/rails_generators/components/mailer/templates/unit_test.rb +37 -0
- data/examples/rails_generators/components/mailer/templates/view.erb +3 -0
- data/examples/rails_generators/components/mailer/templates/view.rhtml +0 -0
- data/examples/rails_generators/components/migration/USAGE +24 -0
- data/examples/rails_generators/components/migration/migration_generator.rb +20 -0
- data/examples/rails_generators/components/migration/templates/migration.rb +7 -0
- data/examples/rails_generators/components/model/USAGE +27 -0
- data/examples/rails_generators/components/model/model_generator.rb +38 -0
- data/examples/rails_generators/components/model/templates/fixtures.yml +15 -0
- data/examples/rails_generators/components/model/templates/migration.rb +14 -0
- data/examples/rails_generators/components/model/templates/model.rb +2 -0
- data/examples/rails_generators/components/model/templates/unit_test.rb +10 -0
- data/examples/rails_generators/components/observer/USAGE +13 -0
- data/examples/rails_generators/components/observer/observer_generator.rb +16 -0
- data/examples/rails_generators/components/observer/templates/observer.rb +2 -0
- data/examples/rails_generators/components/observer/templates/unit_test.rb +10 -0
- data/examples/rails_generators/components/plugin/USAGE +25 -0
- data/examples/rails_generators/components/plugin/plugin_generator.rb +39 -0
- data/examples/rails_generators/components/plugin/templates/MIT-LICENSE +20 -0
- data/examples/rails_generators/components/plugin/templates/README +13 -0
- data/examples/rails_generators/components/plugin/templates/Rakefile +22 -0
- data/examples/rails_generators/components/plugin/templates/USAGE +8 -0
- data/examples/rails_generators/components/plugin/templates/generator.rb +8 -0
- data/examples/rails_generators/components/plugin/templates/init.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/install.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/plugin.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/tasks.rake +4 -0
- data/examples/rails_generators/components/plugin/templates/uninstall.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/unit_test.rb +8 -0
- data/examples/rails_generators/components/resource/USAGE +23 -0
- data/examples/rails_generators/components/resource/resource_generator.rb +72 -0
- data/examples/rails_generators/components/resource/templates/USAGE +18 -0
- data/examples/rails_generators/components/resource/templates/controller.rb +2 -0
- data/examples/rails_generators/components/resource/templates/fixtures.yml +0 -0
- data/examples/rails_generators/components/resource/templates/functional_test.rb +20 -0
- data/examples/rails_generators/components/resource/templates/helper.rb +2 -0
- data/examples/rails_generators/components/scaffold/USAGE +25 -0
- data/examples/rails_generators/components/scaffold/scaffold_generator.rb +90 -0
- data/examples/rails_generators/components/scaffold/templates/controller.rb +85 -0
- data/examples/rails_generators/components/scaffold/templates/functional_test.rb +57 -0
- data/examples/rails_generators/components/scaffold/templates/helper.rb +2 -0
- data/examples/rails_generators/components/scaffold/templates/layout.html.erb +17 -0
- data/examples/rails_generators/components/scaffold/templates/style.css +74 -0
- data/examples/rails_generators/components/scaffold/templates/view_edit.html.erb +19 -0
- data/examples/rails_generators/components/scaffold/templates/view_index.html.erb +24 -0
- data/examples/rails_generators/components/scaffold/templates/view_new.html.erb +18 -0
- data/examples/rails_generators/components/scaffold/templates/view_show.html.erb +10 -0
- data/examples/rails_generators/components/session_migration/USAGE +10 -0
- data/examples/rails_generators/components/session_migration/session_migration_generator.rb +18 -0
- data/examples/rails_generators/components/session_migration/templates/migration.rb +16 -0
- data/examples/rails_generators/components/web_service/USAGE +24 -0
- data/examples/rails_generators/components/web_service/templates/api_definition.rb +5 -0
- data/examples/rails_generators/components/web_service/templates/controller.rb +8 -0
- data/examples/rails_generators/components/web_service/templates/functional_test.rb +19 -0
- data/examples/rails_generators/components/web_service/web_service_generator.rb +29 -0
- data/lib/rubigen/base.rb +168 -0
- data/lib/rubigen/commands.rb +632 -0
- data/lib/rubigen/generated_attribute.rb +40 -0
- data/lib/rubigen/generators/applications/ruby_app/USAGE +10 -0
- data/lib/rubigen/generators/applications/ruby_app/ruby_app_generator.rb +78 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/README.txt +1 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/configs/empty.log +0 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/fresh_rakefile +10 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/module.rb +5 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/script/generate +13 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/test_helper.rb +2 -0
- data/lib/rubigen/generators/components/test_unit/USAGE +14 -0
- data/lib/rubigen/generators/components/test_unit/templates/test +14 -0
- data/lib/rubigen/generators/components/test_unit/test_unit_generator.rb +26 -0
- data/lib/rubigen/helpers/generator_test_helper.rb +133 -0
- data/lib/rubigen/lookup.rb +296 -0
- data/lib/rubigen/manifest.rb +51 -0
- data/lib/rubigen/options.rb +136 -0
- data/lib/rubigen/scripts/destroy.rb +27 -0
- data/lib/rubigen/scripts/generate.rb +7 -0
- data/lib/rubigen/scripts/update.rb +12 -0
- data/lib/rubigen/scripts.rb +69 -0
- data/lib/rubigen/simple_logger.rb +44 -0
- data/lib/rubigen/spec.rb +42 -0
- data/lib/rubigen/version.rb +9 -0
- data/lib/rubigen.rb +44 -0
- data/script/destroy +9 -0
- data/script/generate +9 -0
- data/script/txt2html +65 -0
- data/setup.rb +1585 -0
- data/test/examples_from_rails/generator_test_helper.rb +195 -0
- data/test/examples_from_rails/test_rails_resource_generator.rb +106 -0
- data/test/examples_from_rails/test_rails_scaffold_generator.rb +185 -0
- data/test/test_generate_builtin_application.rb +24 -0
- data/test/test_generate_builtin_test_unit.rb +22 -0
- data/test/test_helper.rb +33 -0
- data/test/test_lookup.rb +103 -0
- data/website/index.html +352 -0
- data/website/index.txt +252 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +44 -0
- metadata +183 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
body { background-color: #fff; color: #333; }
|
|
2
|
+
|
|
3
|
+
body, p, ol, ul, td {
|
|
4
|
+
font-family: verdana, arial, helvetica, sans-serif;
|
|
5
|
+
font-size: 13px;
|
|
6
|
+
line-height: 18px;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
pre {
|
|
10
|
+
background-color: #eee;
|
|
11
|
+
padding: 10px;
|
|
12
|
+
font-size: 11px;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
a { color: #000; }
|
|
16
|
+
a:visited { color: #666; }
|
|
17
|
+
a:hover { color: #fff; background-color:#000; }
|
|
18
|
+
|
|
19
|
+
.fieldWithErrors {
|
|
20
|
+
padding: 2px;
|
|
21
|
+
background-color: red;
|
|
22
|
+
display: table;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#errorExplanation {
|
|
26
|
+
width: 400px;
|
|
27
|
+
border: 2px solid red;
|
|
28
|
+
padding: 7px;
|
|
29
|
+
padding-bottom: 12px;
|
|
30
|
+
margin-bottom: 20px;
|
|
31
|
+
background-color: #f0f0f0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#errorExplanation h2 {
|
|
35
|
+
text-align: left;
|
|
36
|
+
font-weight: bold;
|
|
37
|
+
padding: 5px 5px 5px 15px;
|
|
38
|
+
font-size: 12px;
|
|
39
|
+
margin: -7px;
|
|
40
|
+
background-color: #c00;
|
|
41
|
+
color: #fff;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#errorExplanation p {
|
|
45
|
+
color: #333;
|
|
46
|
+
margin-bottom: 0;
|
|
47
|
+
padding: 5px;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#errorExplanation ul li {
|
|
51
|
+
font-size: 12px;
|
|
52
|
+
list-style: square;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
div.uploadStatus {
|
|
56
|
+
margin: 5px;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
div.progressBar {
|
|
60
|
+
margin: 5px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
div.progressBar div.border {
|
|
64
|
+
background-color: #fff;
|
|
65
|
+
border: 1px solid grey;
|
|
66
|
+
width: 100%;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
div.progressBar div.background {
|
|
70
|
+
background-color: #333;
|
|
71
|
+
height: 18px;
|
|
72
|
+
width: 0%;
|
|
73
|
+
}
|
|
74
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<h1>Editing <%= singular_name %></h1>
|
|
2
|
+
|
|
3
|
+
<%%= error_messages_for :<%= singular_name %> %>
|
|
4
|
+
|
|
5
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
|
6
|
+
<% for attribute in attributes -%>
|
|
7
|
+
<p>
|
|
8
|
+
<b><%= attribute.column.human_name %></b><br />
|
|
9
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<% end -%>
|
|
13
|
+
<p>
|
|
14
|
+
<%%= f.submit "Update" %>
|
|
15
|
+
</p>
|
|
16
|
+
<%% end %>
|
|
17
|
+
|
|
18
|
+
<%%= link_to 'Show', @<%= singular_name %> %> |
|
|
19
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<h1>Listing <%= plural_name %></h1>
|
|
2
|
+
|
|
3
|
+
<table>
|
|
4
|
+
<tr>
|
|
5
|
+
<% for attribute in attributes -%>
|
|
6
|
+
<th><%= attribute.column.human_name %></th>
|
|
7
|
+
<% end -%>
|
|
8
|
+
</tr>
|
|
9
|
+
|
|
10
|
+
<%% for <%= singular_name %> in @<%= plural_name %> %>
|
|
11
|
+
<tr>
|
|
12
|
+
<% for attribute in attributes -%>
|
|
13
|
+
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
|
|
14
|
+
<% end -%>
|
|
15
|
+
<td><%%= link_to 'Show', <%= singular_name %> %></td>
|
|
16
|
+
<td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
|
17
|
+
<td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
|
18
|
+
</tr>
|
|
19
|
+
<%% end %>
|
|
20
|
+
</table>
|
|
21
|
+
|
|
22
|
+
<br />
|
|
23
|
+
|
|
24
|
+
<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<h1>New <%= singular_name %></h1>
|
|
2
|
+
|
|
3
|
+
<%%= error_messages_for :<%= singular_name %> %>
|
|
4
|
+
|
|
5
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
|
6
|
+
<% for attribute in attributes -%>
|
|
7
|
+
<p>
|
|
8
|
+
<b><%= attribute.column.human_name %></b><br />
|
|
9
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<% end -%>
|
|
13
|
+
<p>
|
|
14
|
+
<%%= f.submit "Create" %>
|
|
15
|
+
</p>
|
|
16
|
+
<%% end %>
|
|
17
|
+
|
|
18
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<% for attribute in attributes -%>
|
|
2
|
+
<p>
|
|
3
|
+
<b><%= attribute.column.human_name %>:</b>
|
|
4
|
+
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<% end -%>
|
|
8
|
+
|
|
9
|
+
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
|
|
10
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
Creates a migration to add the sessions table used by the Active Record
|
|
3
|
+
session store. Pass the migration name, either CamelCased or under_scored,
|
|
4
|
+
as an argument.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
`./script/generate session_migration CreateSessionTable`
|
|
8
|
+
|
|
9
|
+
With 4 existing migrations, this creates the AddSessionTable migration
|
|
10
|
+
in db/migrate/005_add_session_table.rb
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
class SessionMigrationGenerator < RubiGen::NamedBase
|
|
2
|
+
def initialize(runtime_args, runtime_options = {})
|
|
3
|
+
runtime_args << 'add_session_table' if runtime_args.empty?
|
|
4
|
+
super
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def manifest
|
|
8
|
+
record do |m|
|
|
9
|
+
m.migration_template 'migration.rb', 'db/migrate',
|
|
10
|
+
:assigns => { :session_table_name => default_session_table_name }
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
def default_session_table_name
|
|
16
|
+
ActiveRecord::Base.pluralize_table_names ? 'session'.pluralize : 'session'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :<%= session_table_name %> do |t|
|
|
4
|
+
t.string :session_id, :null => false
|
|
5
|
+
t.text :data
|
|
6
|
+
t.timestamps
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
add_index :<%= session_table_name %>, :session_id
|
|
10
|
+
add_index :<%= session_table_name %>, :updated_at
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.down
|
|
14
|
+
drop_table :<%= session_table_name %>
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
Stubs out the controller and API for a new web service using the deprecated
|
|
3
|
+
Action Web Service framework. Pass the web service name, either CamelCased
|
|
4
|
+
or under_scored, and a list of API methods as arguments. To create a web
|
|
5
|
+
service within a module, use a path like 'module_name/web_service_name'.
|
|
6
|
+
|
|
7
|
+
This generates a controller in app/controllers, an API definition
|
|
8
|
+
in app/apis, and functional tests in test/functional.
|
|
9
|
+
|
|
10
|
+
Example:
|
|
11
|
+
`./script/generate web_service User add edit list remove`
|
|
12
|
+
|
|
13
|
+
creates the User controller, API, and functional test:
|
|
14
|
+
Controller: app/controllers/user_controller.rb
|
|
15
|
+
API: app/apis/user_api.rb
|
|
16
|
+
Test: test/functional/user_api_test.rb
|
|
17
|
+
|
|
18
|
+
Modules Example:
|
|
19
|
+
`./script/generate web_service 'api/registration' register renew`
|
|
20
|
+
|
|
21
|
+
creates the Registration controller, API, and functional test:
|
|
22
|
+
Controller: app/controllers/api/registration_controller.rb
|
|
23
|
+
API: app/apis/api/registration_api.rb
|
|
24
|
+
Test: test/functional/api/registration_api_test.rb
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
|
|
2
|
+
require '<%= file_path %>_controller'
|
|
3
|
+
|
|
4
|
+
class <%= class_name %>Controller; def rescue_action(e) raise e end; end
|
|
5
|
+
|
|
6
|
+
class <%= class_name %>ControllerApiTest < Test::Unit::TestCase
|
|
7
|
+
def setup
|
|
8
|
+
@controller = <%= class_name %>Controller.new
|
|
9
|
+
@request = ActionController::TestRequest.new
|
|
10
|
+
@response = ActionController::TestResponse.new
|
|
11
|
+
end
|
|
12
|
+
<% for method_name in args -%>
|
|
13
|
+
|
|
14
|
+
def test_<%= method_name %>
|
|
15
|
+
result = invoke :<%= method_name %>
|
|
16
|
+
assert_equal nil, result
|
|
17
|
+
end
|
|
18
|
+
<% end -%>
|
|
19
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class WebServiceGenerator < RubiGen::NamedBase
|
|
2
|
+
def manifest
|
|
3
|
+
record do |m|
|
|
4
|
+
# Check for class naming collisions.
|
|
5
|
+
m.class_collisions class_path, "#{class_name}Api", "#{class_name}Controller", "#{class_name}ApiTest"
|
|
6
|
+
|
|
7
|
+
# API and test directories.
|
|
8
|
+
m.directory File.join('app/apis', class_path)
|
|
9
|
+
m.directory File.join('app/controllers', class_path)
|
|
10
|
+
m.directory File.join('test/functional', class_path)
|
|
11
|
+
|
|
12
|
+
# API definition, controller, and functional test.
|
|
13
|
+
m.template 'api_definition.rb',
|
|
14
|
+
File.join('app/apis',
|
|
15
|
+
class_path,
|
|
16
|
+
"#{file_name}_api.rb")
|
|
17
|
+
|
|
18
|
+
m.template 'controller.rb',
|
|
19
|
+
File.join('app/controllers',
|
|
20
|
+
class_path,
|
|
21
|
+
"#{file_name}_controller.rb")
|
|
22
|
+
|
|
23
|
+
m.template 'functional_test.rb',
|
|
24
|
+
File.join('test/functional',
|
|
25
|
+
class_path,
|
|
26
|
+
"#{file_name}_api_test.rb")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/rubigen/base.rb
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/options'
|
|
2
|
+
require File.dirname(__FILE__) + '/manifest'
|
|
3
|
+
require File.dirname(__FILE__) + '/spec'
|
|
4
|
+
require File.dirname(__FILE__) + '/generated_attribute'
|
|
5
|
+
|
|
6
|
+
# RubiGen is a code generation platform Ruby frameworks.
|
|
7
|
+
# Generators are easily invoked within Ruby framework instances
|
|
8
|
+
# to add and remove components such as library and test files.
|
|
9
|
+
#
|
|
10
|
+
# New generators are easy to create and may be distributed within RubyGems,
|
|
11
|
+
# user home directory, or within each Ruby framework that uses RubiGen.
|
|
12
|
+
#
|
|
13
|
+
# For example, newgem uses RubiGen to generate new RubyGems. Those
|
|
14
|
+
# generated RubyGems can then use RubiGen (via a generated script/generate
|
|
15
|
+
# application) to generate tests and executable apps, etc, for the RubyGem.
|
|
16
|
+
#
|
|
17
|
+
# Generators may subclass other generators to provide variations that
|
|
18
|
+
# require little or no new logic but replace the template files.
|
|
19
|
+
#
|
|
20
|
+
# For a RubyGem, put your generator classes and templates within subfolders
|
|
21
|
+
# of the +generators+ directory.
|
|
22
|
+
#
|
|
23
|
+
# The layout of generator files can be seen in the built-in
|
|
24
|
+
# +test_unit+ generator:
|
|
25
|
+
#
|
|
26
|
+
# generators/
|
|
27
|
+
# test_unit/
|
|
28
|
+
# test_unit_generator.rb
|
|
29
|
+
# templates/
|
|
30
|
+
# test_unit.rb
|
|
31
|
+
#
|
|
32
|
+
# The directory name (+test_unit+) matches the name of the generator file
|
|
33
|
+
# (test_unit_generator.rb) and class (+TestUnitGenerator+). The files
|
|
34
|
+
# that will be copied or used as templates are stored in the +templates+
|
|
35
|
+
# directory.
|
|
36
|
+
#
|
|
37
|
+
# The filenames of the templates don't matter, but choose something that
|
|
38
|
+
# will be self-explatatory since you will be referencing these in the
|
|
39
|
+
# +manifest+ method inside your generator subclass.
|
|
40
|
+
#
|
|
41
|
+
#
|
|
42
|
+
module RubiGen
|
|
43
|
+
class GeneratorError < StandardError; end
|
|
44
|
+
class UsageError < GeneratorError; end
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# The base code generator is bare-bones. It sets up the source and
|
|
48
|
+
# destination paths and tells the logger whether to keep its trap shut.
|
|
49
|
+
#
|
|
50
|
+
# It's useful for copying files such as stylesheets, images, or
|
|
51
|
+
# javascripts.
|
|
52
|
+
#
|
|
53
|
+
# For more comprehensive template-based passive code generation with
|
|
54
|
+
# arguments, you'll want RubiGen::NamedBase.
|
|
55
|
+
#
|
|
56
|
+
# Generators create a manifest of the actions they perform then hand
|
|
57
|
+
# the manifest to a command which replays the actions to do the heavy
|
|
58
|
+
# lifting (such as checking for existing files or creating directories
|
|
59
|
+
# if needed). Create, destroy, and list commands are included. Since a
|
|
60
|
+
# single manifest may be used by any command, creating new generators is
|
|
61
|
+
# as simple as writing some code templates and declaring what you'd like
|
|
62
|
+
# to do with them.
|
|
63
|
+
#
|
|
64
|
+
# The manifest method must be implemented by subclasses, returning a
|
|
65
|
+
# RubiGen::Manifest. The +record+ method is provided as a
|
|
66
|
+
# convenience for manifest creation. Example:
|
|
67
|
+
#
|
|
68
|
+
# class StylesheetGenerator < RubiGen::Base
|
|
69
|
+
# def manifest
|
|
70
|
+
# record do |m|
|
|
71
|
+
# m.directory('public/stylesheets')
|
|
72
|
+
# m.file('application.css', 'public/stylesheets/application.css')
|
|
73
|
+
# end
|
|
74
|
+
# end
|
|
75
|
+
# end
|
|
76
|
+
#
|
|
77
|
+
# See RubiGen::Commands::Create for a list of methods available
|
|
78
|
+
# to the manifest.
|
|
79
|
+
class Base
|
|
80
|
+
include Options
|
|
81
|
+
|
|
82
|
+
# Declare default options for the generator. These options
|
|
83
|
+
# are inherited to subclasses.
|
|
84
|
+
default_options :collision => :ask, :quiet => false
|
|
85
|
+
|
|
86
|
+
# A logger instance available everywhere in the generator.
|
|
87
|
+
cattr_accessor :logger
|
|
88
|
+
|
|
89
|
+
# Every generator that is dynamically looked up is tagged with a
|
|
90
|
+
# Spec describing where it was found.
|
|
91
|
+
class_inheritable_accessor :spec
|
|
92
|
+
|
|
93
|
+
attr_reader :source_root, :destination_root, :args
|
|
94
|
+
|
|
95
|
+
def initialize(runtime_args, runtime_options = {})
|
|
96
|
+
@args = runtime_args
|
|
97
|
+
parse!(@args, runtime_options)
|
|
98
|
+
|
|
99
|
+
# Derive source and destination paths.
|
|
100
|
+
@source_root = options[:source] || File.join(spec.path, 'templates')
|
|
101
|
+
if options[:destination]
|
|
102
|
+
@destination_root = options[:destination]
|
|
103
|
+
elsif defined? ::APP_ROOT
|
|
104
|
+
@destination_root = ::APP_ROOT
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Silence the logger if requested.
|
|
108
|
+
logger.quiet = options[:quiet]
|
|
109
|
+
|
|
110
|
+
# Raise usage error if help is requested.
|
|
111
|
+
usage if options[:help]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Generators must provide a manifest. Use the +record+ method to create
|
|
115
|
+
# a new manifest and record your generator's actions.
|
|
116
|
+
def manifest
|
|
117
|
+
raise NotImplementedError, "No manifest for '#{spec.name}' generator."
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Return the full path from the source root for the given path.
|
|
121
|
+
# Example for source_root = '/source':
|
|
122
|
+
# source_path('some/path.rb') == '/source/some/path.rb'
|
|
123
|
+
#
|
|
124
|
+
# The given path may include a colon ':' character to indicate that
|
|
125
|
+
# the file belongs to another generator. This notation allows any
|
|
126
|
+
# generator to borrow files from another. Example:
|
|
127
|
+
# source_path('model:fixture.yml') = '/model/source/path/fixture.yml'
|
|
128
|
+
def source_path(relative_source)
|
|
129
|
+
# Check whether we're referring to another generator's file.
|
|
130
|
+
name, path = relative_source.split(':', 2)
|
|
131
|
+
|
|
132
|
+
# If not, return the full path to our source file.
|
|
133
|
+
if path.nil?
|
|
134
|
+
File.join(source_root, name)
|
|
135
|
+
|
|
136
|
+
# Otherwise, ask our referral for the file.
|
|
137
|
+
else
|
|
138
|
+
# FIXME: this is broken, though almost always true. Others'
|
|
139
|
+
# source_root are not necessarily the templates dir.
|
|
140
|
+
File.join(self.class.lookup(name).path, 'templates', path)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Return the full path from the destination root for the given path.
|
|
145
|
+
# Example for destination_root = '/dest':
|
|
146
|
+
# destination_path('some/path.rb') == '/dest/some/path.rb'
|
|
147
|
+
def destination_path(relative_destination)
|
|
148
|
+
File.join(destination_root, relative_destination)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
protected
|
|
152
|
+
# Convenience method for generator subclasses to record a manifest.
|
|
153
|
+
def record
|
|
154
|
+
RubiGen::Manifest.new(self) { |m| yield m }
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Override with your own usage banner.
|
|
158
|
+
def banner
|
|
159
|
+
"Usage: #{$0} #{spec.name} [options]"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Read USAGE from file in generator base path.
|
|
163
|
+
def usage_message
|
|
164
|
+
File.read(File.join(spec.path, 'USAGE')) rescue ''
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
end
|