rails_apps_composer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/README.textile +313 -0
  2. data/bin/rails_apps_composer +7 -0
  3. data/lib/rails_wizard.rb +10 -0
  4. data/lib/rails_wizard/command.rb +79 -0
  5. data/lib/rails_wizard/config.rb +86 -0
  6. data/lib/rails_wizard/recipe.rb +106 -0
  7. data/lib/rails_wizard/recipes.rb +38 -0
  8. data/lib/rails_wizard/template.rb +58 -0
  9. data/recipes/action_mailer.rb +41 -0
  10. data/recipes/activerecord.rb +37 -0
  11. data/recipes/add_user.rb +87 -0
  12. data/recipes/add_user_name.rb +74 -0
  13. data/recipes/application_layout.rb +45 -0
  14. data/recipes/ban_spiders.rb +27 -0
  15. data/recipes/capybara.rb +34 -0
  16. data/recipes/cleanup.rb +36 -0
  17. data/recipes/css_setup.rb +42 -0
  18. data/recipes/cucumber.rb +62 -0
  19. data/recipes/devise.rb +76 -0
  20. data/recipes/devise_navigation.rb +93 -0
  21. data/recipes/env_yaml.rb +54 -0
  22. data/recipes/git.rb +38 -0
  23. data/recipes/haml.rb +23 -0
  24. data/recipes/heroku.rb +58 -0
  25. data/recipes/home_page.rb +45 -0
  26. data/recipes/home_page_users.rb +47 -0
  27. data/recipes/hoptoad.rb +34 -0
  28. data/recipes/jammit.rb +43 -0
  29. data/recipes/jquery.rb +70 -0
  30. data/recipes/less.rb +12 -0
  31. data/recipes/mongo_mapper.rb +18 -0
  32. data/recipes/mongohq.rb +59 -0
  33. data/recipes/mongoid.rb +39 -0
  34. data/recipes/mootools.rb +23 -0
  35. data/recipes/navigation.rb +68 -0
  36. data/recipes/omniauth.rb +152 -0
  37. data/recipes/omniauth_email.rb +82 -0
  38. data/recipes/pow.rb +12 -0
  39. data/recipes/prototype.rb +11 -0
  40. data/recipes/rails_admin.rb +21 -0
  41. data/recipes/redis.rb +17 -0
  42. data/recipes/redistogo.rb +40 -0
  43. data/recipes/rightjs.rb +17 -0
  44. data/recipes/rspec.rb +112 -0
  45. data/recipes/sass.rb +13 -0
  46. data/recipes/seed_database.rb +42 -0
  47. data/recipes/sequel.rb +13 -0
  48. data/recipes/settingslogic.rb +43 -0
  49. data/recipes/slim.rb +11 -0
  50. data/recipes/test_unit.rb +11 -0
  51. data/recipes/users_page.rb +103 -0
  52. data/spec/rails_wizard/config_spec.rb +99 -0
  53. data/spec/rails_wizard/recipe_spec.rb +103 -0
  54. data/spec/rails_wizard/recipes/sanity_spec.rb +30 -0
  55. data/spec/rails_wizard/recipes_spec.rb +24 -0
  56. data/spec/rails_wizard/template_spec.rb +48 -0
  57. data/spec/spec_helper.rb +11 -0
  58. data/spec/support/rails_directory.rb +17 -0
  59. data/spec/support/template_runner.rb +28 -0
  60. data/templates/helpers.erb +45 -0
  61. data/templates/layout.erb +69 -0
  62. data/templates/recipe.erb +10 -0
  63. data/version.rb +3 -0
  64. metadata +206 -0
data/README.textile ADDED
@@ -0,0 +1,313 @@
1
+ h1. Rails Apps Composer Gem
2
+
3
+ A gem with recipes to create Rails application templates you can use to generate Rails starter apps. Creates ready-to-run Rails web applications. Makes it easy to create and maintain a starter app using your own preferred options.
4
+
5
+ h4. Based on Michael Bleigh's RailsWizard Gem
6
+
7
+ The rails_apps_composer gem is a fork of "Michael Bleigh's RailsWizard gem":https://github.com/intridea/rails_wizard (see credits below). The purpose of this fork is to provide recipes for ready-to-run Rails starter apps. Several recipes provided by the rails_apps_composer gem are different from those provided by the rails_wizard gem. Applications generated with the rails_apps_composer gem are more complete; for example, they may include a home page with sign-in, sign-out navigation links.
8
+
9
+ Any issues? Please create a "GitHub issue":http://github.com/RailsApps/rails_apps_composer/issues.
10
+
11
+ h2. !http://twitter-badges.s3.amazonaws.com/t_logo-a.png(Follow on Twitter)!:http://www.twitter.com/rails_apps Follow on Twitter !http://railsapps.github.com/images/mailing-list-icon.jpg(Join the Mailing List)!:http://eepurl.com/dQx3o Join the Mailing List
12
+
13
+ Follow the project on Twitter: "http://twitter.com/rails_apps":http://twitter.com/rails_apps. Tweet some praise if you like what you've found.
14
+
15
+ <a href="http://eepurl.com/dQx3o">Join the email list</a> (low volume, announcements only) for project updates and my tips about Rails resources.
16
+
17
+ h2. Suggested Use
18
+
19
+ Any developer can quickly generate a Rails web application using the @rails new@ command. In practice, experienced Rails developers typically add an assortment of useful additional packages (gems) before beginning development of any web application. A developer often uses the same set of packages to get started and may create a "starter app" that can be copied and reused for any new project.
20
+
21
+ It can be a hassle to integrate some of the most commonly used gems, particularly when new versions are released and there are minor "gotchas" that interfere with gems working together. Despite the apparent convenience of creating a starter app, it can be time consuming to maintain and update a starter app as component packages evolve. This project aims to simplify the process of building and maintaining a starter app by providing mix-and-match recipes to assemble the most commonly used Rails packages.
22
+
23
+ If you use this gem to create your Rails starter app, you can expect the pieces to work together. If they don't, you can report problems and look for identified "issues":http://github.com/RailsApps/rails_apps_composer/issues (and perhaps contributed fixes).
24
+
25
+ If you use this gem to create a reusable application template (see the instructions below), you can check for changes to recipes here and reassemble your application template as packages evolve.
26
+
27
+ h2. Dependencies
28
+
29
+ Before generating a new Rails app, you will need:
30
+
31
+ * The Ruby language (version 1.8.7 or 1.9.2)
32
+ * Rails (version 3.0.4 or newer)
33
+
34
+ I recommend installing rvm, the "Ruby Version Manager":http://rvm.beginrescueend.com/, to manage multiple versions of Rails.
35
+
36
+ If you are using rvm, you can see a list of the Ruby versions currently installed:
37
+ @$ rvm list@
38
+
39
+ Check that appropriate versions of Ruby and Rails are installed in your development environment:
40
+ @$ ruby -v@
41
+ @$ rails -v@
42
+
43
+ h2. Installation
44
+
45
+ Installation is simple:
46
+
47
+ @$ gem install rails_apps_composer@
48
+
49
+ h2. Usage
50
+
51
+ h4. List Recipes
52
+
53
+ You can display a list of recipes:
54
+
55
+ @$ rails_apps_composer list@
56
+
57
+ You'll find more details about the available recipes by browsing the repository "recipes directory":https://github.com/RailsApps/rails_apps_composer/tree/master/recipes.
58
+
59
+ h4. Generate a Starter App
60
+
61
+ There are two ways to use the rails_apps_composer gem to generate a starter app.
62
+
63
+ If you want to build a starter app for one-time use, you can build an application by providing a list of recipes with the @-r@ option. This will automatically generate an application using the specified recipes.
64
+
65
+ If you want to create and save an application template that you can reuse as needed to "clone" identical starter apps, you can download the rails_apps_composer project, customize recipes as needed, and use a @rake@ task to save a reusable application template file.
66
+
67
+ Each of these approaches is described below.
68
+
69
+ h2. Make Your Own Starter App for One-Time Use
70
+
71
+ h4. Select Recipes
72
+
73
+ You can mix and match recipes to create your own customized starter app. Browse the repository "recipes directory":https://github.com/RailsApps/rails_apps_composer/tree/master/recipes to see what is available. Then provide your list of recipes to the rails_apps_composer gem using the @-r@ option and generate an app as needed. Here's an example that creates a simple app using jquery and haml:
74
+
75
+ <pre>
76
+ $ rails_apps_composer new APP_NAME -r jquery haml
77
+ </pre>
78
+
79
+ To build the "rails3-mongoid-devise":http://github.com/RailsApps/rails3-mongoid-devise/ example application, run the command:
80
+
81
+ <pre>
82
+ $ rails_apps_composer new APP_NAME -r jquery haml rspec cucumber mongoid action_mailer devise add_user home_page home_page_users seed_database users_page css_setup application_layout devise_navigation cleanup ban_spiders git
83
+ </pre>
84
+
85
+ h2. Make Your Own Starter App with a Reusable Application Template
86
+
87
+ You can modify the recipes and save an application template that creates your own customized starter app.
88
+
89
+ First, you'll need to make your own copy of the rails_apps_composer gem.
90
+
91
+ @$ git clone git://github.com/RailsApps/rails_apps_composer.git@
92
+
93
+ @$ cd rails_apps_composer@
94
+
95
+ The "mg":https://github.com/sr/mg "minimal gem" is required for development of the rails_apps_composer gem. Several other gems are needed as well:
96
+
97
+ @$ gem install i18n activesupport thor rspec mg@
98
+
99
+ h4. Customize the Recipes
100
+
101
+ Modify or write new recipes as you wish (see below for details about writing recipes). You can run @rake spec@ to make sure your recipes conform to the required syntax.
102
+
103
+ h4. Save the Application Template
104
+
105
+ The rails_apps_composer gem creates an application template as an intermediate step before generating an application. You can generate and save the application template. Here's an example of generating an application template and saving the template to a file:
106
+
107
+ <pre>
108
+ $ rake print --silent RECIPES=recipe1,recipe2 > ~/Desktop/template.txt
109
+ </pre>
110
+
111
+ If you don't include the @--silent@ option, the rake task will generate the application template with an extraneous first line. Open the template file and remove the first line if you encounter this problem.
112
+
113
+ The rails_apps_composer gem creates an application template that can be used by the @rails new@ command with the @-m@ option. For example:
114
+
115
+ <pre>
116
+ $ rails new testapp -m ~/Desktop/template.txt
117
+ </pre>
118
+
119
+ You can specify the @-T -O -J@ flags as needed to skip Test::Unit files and Active Record files. For Rails 3.0, you can also specify the @-J@ flag to skip Prototype files.
120
+
121
+ That's all it takes. You'll have a ready-to-customize Rails web application in minutes.
122
+
123
+ h4. Template for Rails 3 + Devise + RSpec + Cucumber
124
+
125
+ To build a reusable application template for the "rails3-devise-rspec-cucumber":http://github.com/RailsApps/rails3-devise-rspec-cucumber/ example application, run the command:
126
+
127
+ <pre>
128
+ $ rake print --silent RECIPES=jquery,haml,rspec,cucumber,action_mailer,devise,add_user,home_page,home_page_users,seed_database,users_page,css_setup,application_layout,devise_navigation,cleanup,ban_spiders,git > ~/Desktop/template.txt
129
+ </pre>
130
+
131
+ Then generate the application using the @-T@ flag. Add the @-J@ flag for Rails 3.0.
132
+
133
+ <pre>
134
+ $ rails new testapp -m ~/Desktop/template.txt -T -J
135
+ </pre>
136
+
137
+ h4. Template for Rails 3 + Mongoid + Devise
138
+
139
+ To build a reusable application template for the "rails3-mongoid-devise":http://github.com/RailsApps/rails3-mongoid-devise/ example application, run the command:
140
+
141
+ <pre>
142
+ $ rake print --silent RECIPES=jquery,haml,rspec,cucumber,mongoid,action_mailer,devise,add_user,home_page,home_page_users,seed_database,users_page,css_setup,application_layout,devise_navigation,cleanup,ban_spiders,git > ~/Desktop/template.txt
143
+ </pre>
144
+
145
+ Then generate the application using the @-T -O@ flags. Add the @-J@ flag for Rails 3.0.
146
+
147
+ <pre>
148
+ $ rails new testapp -m ~/Desktop/template.txt -T -O -J
149
+ </pre>
150
+
151
+ h4. Template for Rails 3 + Mongoid + OmniAuth
152
+
153
+ To build a reusable application template for the "rails3-mongoid-omniauth":http://github.com/RailsApps/rails3-mongoid-omniauth/ example application, run the command:
154
+
155
+ <pre>
156
+ $ rake print --silent RECIPES=jquery,haml,rspec,cucumber,mongoid,seed_database,add_user,omniauth,home_page,home_page_users,css_setup,application_layout,navigation,users_page,omniauth_email,cleanup,ban_spiders,git > ~/Desktop/template.txt
157
+ </pre>
158
+
159
+ Then generate the application using the @-T -O@ flags. Add the @-J@ flag for Rails 3.0.
160
+
161
+ <pre>
162
+ $ rails new testapp -m ~/Desktop/template.txt -T -O -J
163
+ </pre>
164
+
165
+ h2. The Recipes
166
+
167
+ The following recipes are available.
168
+
169
+ h4. Recipes for General Use
170
+
171
+ |_. File |_. |_. Dependencies |_. Authors |
172
+ | "action_mailer.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/action_mailer.rb | Changes ActionMailer defaults | none | dkehoe |
173
+ | "ban_spiders.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/ban_spiders.rb | Ban spiders from your site by changing robots.txt | none | dkehoe |
174
+ | "cleanup.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/cleanup.rb | Remove unnecessary files, add READMEs | none | fnichol, dkehoe |
175
+ | "git.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/git.rb | Add a gitignore file, initialize new Git repo | Git | fnichol, dkehoe |
176
+ | "sample_readme.textile":https://github.com/RailsApps/rails-template-recipes/raw/master/sample_readme.textile | Placeholder text for a README file (textile markup) | none | dkehoe |
177
+ | "sample_readme.txt":https://github.com/RailsApps/rails-template-recipes/raw/master/sample_readme.txt | Placeholder text for a README file (ASCII) | none | dkehoe |
178
+
179
+ h4. Recipes for Testing and BDD
180
+
181
+ |_. File |_. |_. Dependencies |_. Authors |
182
+ | "rspec.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/rspec.rb | Install RSpec | none | rbrooker, dkehoe |
183
+ | "cucumber.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/cucumber.rb | Install Cucumber | none | rbrooker, dkehoe |
184
+
185
+ h4. Recipes for Mongoid
186
+
187
+ |_. File |_. |_. Dependencies |_. Authors |
188
+ | "mongoid.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/mongoid.rb | Installs Mongoid with the bson_ext gem | Mongoid | dkehoe |
189
+
190
+ h4. Recipes for jQuery
191
+
192
+ |_. File |_. |_. Dependencies |_. Authors |
193
+ | "jquery.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/jquery_fix.rb | Installs jQuery | none | dkehoe |
194
+
195
+ h4. Recipes for Devise
196
+
197
+ |_. File |_. |_. Dependencies |_. Authors |
198
+ | "devise.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/devise.rb | Installs Devise | none | dkehoe |
199
+
200
+ h4. Recipes for OmniAuth
201
+
202
+ |_. File |_. |_. Dependencies |_. Authors |
203
+ | "omniauth.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/omniauth.rb | OmniAuth for authentication | none | dkehoe |
204
+ | "omniauth_email.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/omniauth_email.rb | Requests a new uer's email address when authenticating with Twitter, etc. | OmniAuth | dkehoe |
205
+
206
+ h4. Recipes for Haml
207
+
208
+ |_. File |_. |_. Dependencies |_. Authors |
209
+ | "haml.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/haml.rb | Installs Haml | none | dkehoe |
210
+
211
+ h4. Recipes for the Application Layout
212
+
213
+ |_. File |_. |_. Dependencies |_. Authors |
214
+ | "application_layout.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/application_layout.rb | Adds application layout with flash messages | ERB or Haml | dkehoe |
215
+ | "css_setup.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/css_setup.rb | Add a stylesheet with styles for a horizontal menu and flash messages | none | dkehoe |
216
+
217
+ h4. Recipes for the Example Apps
218
+
219
+ |_. File |_. |_. Dependencies |_. Authors |
220
+ | "add_user.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/add_user.rb | adds User model for use with OmniAuth or Devise | none | dkehoe |
221
+ | "navigation.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/_navigation.rb | Add navigation links to the default application layout | none | dkehoe |
222
+ | "devise_navigation.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/devise_navigation.rb | Add navigation links to the default application layout | Devise | dkehoe |
223
+ | "home_page.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/home_page.rb | Create a home controller, route, and simple view | ERB or Haml | dkehoe |
224
+ | "home_page_users.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/home_page_users.rb | Modify a home page to display a list of users | Home controller, User model, ERB or Haml | dkehoe |
225
+ | "seed_database.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/seed_database.rb | Adds a db/seeds.rb file to initialize a database | none | dkehoe |
226
+ | "users_page.rb":https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/users_page.rb | Add a home page containing links to User pages | User model, ERB or Haml | dkehoe |
227
+
228
+ h2. Writing New Recipes
229
+
230
+ You can find the rails_apps_composer recipe collection in the GitHub repository's "recipes directory":https://github.com/RailsApps/rails_apps_composer/tree/master/recipes. If you find errors or improve a recipe you can contribute to the project by submitting a pull request or creating a "Github issue":http://github.com/RailsApps/rails_apps_composer/issues.
231
+
232
+ h4. Rails Wizard Basics
233
+
234
+ For more information on all available options for authoring recipes that can be read by the rails_wizard or rails_apps_composer gems, please see the "wiki for Michael Bleigh's RailsWizard gem":https://github.com/intridea/rails_wizard/wiki.
235
+
236
+ Recipes are made of up *template code* and *YAML back-matter* stored in a ruby file. The @__END__@ parsing convention is used so that each recipe is actually a valid, parseable Ruby file. The structure of a recipe looks something like this:
237
+
238
+ <pre>
239
+ gem 'supergem'
240
+
241
+ after_bundler do
242
+ generate &quot;supergem:install&quot;
243
+ end
244
+
245
+ __END__
246
+
247
+ category: templating
248
+ name: SuperGem
249
+ description: Installs SuperGem which is useful for things
250
+ author: mbleigh
251
+ </pre>
252
+
253
+ h4. Rails Apps Composer Differences
254
+
255
+ The rails_apps_composer gem is very similar to the rails_wizard gem, with one significant difference. The rails_wizard gem allows specification of execution order for recipes with @run_after@ and @run_after@ configuration flags. The rails_apps_composer gem does not support @run_after@ or @run_after@; instead, the order which you provide the recipes sets the execution order. This makes it easier to chain a series of recipes in the order you prefer. For example,
256
+
257
+ <pre>
258
+ $ rails_apps_composer new APP_NAME -r git jquery haml
259
+ </pre>
260
+
261
+ installs jquery before haml.
262
+
263
+ h4. Recipe Differences
264
+
265
+ Several recipes provided by the rails_apps_composer gem are different from those provided by the rails_wizard gem.
266
+
267
+ h4. Rails Wizard RSpec Tests
268
+
269
+ The gem has RSpec tests that automatically validate each recipe in the repository, so you should run @rake spec@ as a basic syntax check. Note that these don't verify that your recipe code itself works, just that the gem could properly parse and understand your recipe file.
270
+
271
+ h4. How It Works
272
+
273
+ Rails generators can use any methods provided by the "Thor::Actions":http://rdoc.info/github/wycats/thor/master/Thor/Actions module. The flexibility of mixing "recipes" for application templates comes from use of the @apply@ method from the Thor::Actions module. Given a web address or a local filepath, the "apply method":http://rdoc.info/github/wycats/thor/master/Thor/Actions#apply-instance_method loads and executes a file within the context of the generator script.
274
+
275
+ h2. Documentation and Support
276
+
277
+ This is the only documentation.
278
+
279
+ h4. Writing Recipes
280
+
281
+ To understand the code in these templates, take a look at "Thor::Actions":http://rdoc.info/github/wycats/thor/master/Thor/Actions. Your recipes can use any methods provided by "Thor::Actions":http://rdoc.info/github/wycats/thor/master/Thor/Actions or "Rails::Generators::Actions":http://railsapi.com/doc/rails-v3.0.3/classes/Rails/Generators/Actions.html.
282
+
283
+ h4. About Rails Application Templates
284
+
285
+ "Cooking Up A Custom Rails 3 Template (11 Oct 2010) by Andrea Singh":http://blog.madebydna.com/all/code/2010/10/11/cooking-up-a-custom-rails3-template.html
286
+ "Rails Application Templates (16 Sept 2010) by Collin Schaafsma":http://quickleft.com/blog/rails-application-templates
287
+ "Application templates in Rails 3 (18 Sept 2009) by Ben Scofield":http://benscofield.com/2009/09/application-templates-in-rails-3/
288
+ "Railscasts: App Templates in Rails 2.3 (9 Feb 2009) by Ryan Bates":http://railscasts.com/episodes/148-app-templates-in-rails-2-3
289
+ "Rails templates (4 Dec 2008) by Pratik Naik":http://m.onkey.org/rails-templates
290
+
291
+ h2. Issues
292
+
293
+ Any issues? Please create a "GitHub issue":http://github.com/RailsApps/rails_apps_composer/issues.
294
+
295
+ h2. Credits
296
+
297
+ This project is based on "Michael Bleigh's RailsWizard gem":https://github.com/intridea/rails_wizard. The original idea for a RailsWizard and the innovative implementation is the work of Michael Bleigh.
298
+
299
+ "Fletcher Nichol's":http://silversky.ca/ project "fnichol/rails-template-recipes":https://github.com/fnichol/rails-template-recipes provides the basis for several recipes.
300
+
301
+ RSpec, Cucumber, and Yard recipes were contributed by "Ramon Brooker":http://cogniton-mind.tumblr.com/.
302
+
303
+ Additional recipes by Daniel Kehoe, "http://danielkehoe.com/":http://danielkehoe.com/.
304
+
305
+ Is the gem useful to you? Follow the project on Twitter:
306
+ "http://twitter.com/rails_apps":http://twitter.com/rails_apps
307
+ and tweet some praise. I'd love to know you were helped out by the gem.
308
+
309
+ h2. License
310
+
311
+ h4. MIT License
312
+
313
+ The rails_apps_composer gem and its recipes are distributed under the MIT License.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ $:.push File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'rails_wizard/command'
6
+
7
+ RailsWizard::Command.start
@@ -0,0 +1,10 @@
1
+ require 'rails_wizard/recipes'
2
+ require 'rails_wizard/recipe'
3
+ require 'rails_wizard/config'
4
+ require 'rails_wizard/template'
5
+
6
+ Dir[File.dirname(__FILE__) + '/../recipes/*.rb'].each do |path|
7
+ key = File.basename(path, '.rb')
8
+ recipe = RailsWizard::Recipe.generate(key, File.open(path))
9
+ RailsWizard::Recipes.add(recipe)
10
+ end
@@ -0,0 +1,79 @@
1
+ require 'rails_wizard'
2
+ require 'thor'
3
+
4
+ module RailsWizard
5
+ class Command < Thor
6
+ include Thor::Actions
7
+ desc "new APP_NAME", "create a new Rails app"
8
+ method_option :recipes, :type => :array, :aliases => "-r"
9
+ def new(name)
10
+ if options[:recipes]
11
+ run_template(name, options[:recipes])
12
+ else
13
+ @recipes = []
14
+
15
+ while recipe = ask("#{print_recipes}#{bold}Which recipe would you like to add? #{clear}#{yellow}(blank to finish)#{clear}")
16
+ if recipe == ''
17
+ run_template(name, @recipes)
18
+ break
19
+ elsif RailsWizard::Recipes.list.include?(recipe)
20
+ @recipes << recipe
21
+ puts
22
+ puts "> #{green}Added '#{recipe}' to template.#{clear}"
23
+ else
24
+ puts
25
+ puts "> #{red}Invalid recipe, please try again.#{clear}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ desc "list [CATEGORY]", "list available recipes (optionally by category)"
32
+ def list(category = nil)
33
+ if category
34
+ recipes = RailsWizard::Recipes.for(category).map{|r| RailsWizard::Recipe.from_mongo(r) }
35
+ else
36
+ recipes = RailsWizard::Recipes.list_classes
37
+ end
38
+
39
+ recipes.each do |recipe|
40
+ puts recipe.key.ljust(15) + "# #{recipe.description}"
41
+ end
42
+ end
43
+
44
+ no_tasks do
45
+ def cyan; "\033[36m" end
46
+ def clear; "\033[0m" end
47
+ def bold; "\033[1m" end
48
+ def red; "\033[31m" end
49
+ def green; "\033[32m" end
50
+ def yellow; "\033[33m" end
51
+
52
+ def print_recipes
53
+ puts
54
+ puts
55
+ puts
56
+ if @recipes && @recipes.any?
57
+ puts "#{green}#{bold}Your Recipes:#{clear} " + @recipes.join(", ")
58
+ puts
59
+ end
60
+ puts "#{bold}#{cyan}Available Recipes:#{clear} " + RailsWizard::Recipes.list.join(', ')
61
+ puts
62
+ end
63
+
64
+ def run_template(name, recipes)
65
+ puts
66
+ puts
67
+ puts "#{bold}Generating and Running Template..."
68
+ puts
69
+ file = Tempfile.new('template')
70
+ template = RailsWizard::Template.new(recipes)
71
+ file.write template.compile
72
+ file.close
73
+ system "rails new #{name} -m #{file.path} #{template.args.join(' ')}"
74
+ ensure
75
+ file.unlink
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,86 @@
1
+ require 'active_support/ordered_hash'
2
+
3
+ module RailsWizard
4
+ class Config
5
+ attr_reader :questions
6
+
7
+ def initialize(schema)
8
+ @questions = ActiveSupport::OrderedHash.new
9
+ schema.each do |hash|
10
+ key = hash.keys.first
11
+ details = hash.values.first
12
+
13
+ kind = details['type']
14
+ raise ArgumentError, "Unrecognized question type, must be one of #{QUESTION_TYPES.keys.join(', ')}" unless QUESTION_TYPES.keys.include?(kind)
15
+ @questions[key] = QUESTION_TYPES[kind].new(details)
16
+ end
17
+ end
18
+
19
+ def compile(values = {})
20
+ result = []
21
+ result << "config = #{values.inspect}"
22
+ @questions.each_pair do |key, question|
23
+ result << "config['#{key}'] = #{question.compile} unless config.key?('#{key}')"
24
+ end
25
+ result.join("\n")
26
+ end
27
+
28
+ class Prompt
29
+ attr_reader :prompt, :details
30
+ def initialize(details)
31
+ @details = details
32
+ @prompt = details['prompt']
33
+ end
34
+
35
+ def compile
36
+ "#{question} if #{conditions}"
37
+ end
38
+
39
+ def question
40
+ "ask_wizard(#{prompt.inspect})"
41
+ end
42
+
43
+ def conditions
44
+ [config_conditions, recipe_conditions].join(' && ')
45
+ end
46
+
47
+ def config_conditions
48
+ if details['if']
49
+ "config['#{details['if']}']"
50
+ elsif details['unless']
51
+ "!config['#{details['unless']}']"
52
+ else
53
+ 'true'
54
+ end
55
+ end
56
+
57
+ def recipe_conditions
58
+ if details['if_recipe']
59
+ "recipe?('#{details['if_recipe']}')"
60
+ elsif details['unless_recipe']
61
+ "!recipe?('#{details['unless_recipe']}')"
62
+ else
63
+ 'true'
64
+ end
65
+ end
66
+ end
67
+
68
+ class TrueFalse < Prompt
69
+ def question
70
+ "yes_wizard?(#{prompt.inspect})"
71
+ end
72
+ end
73
+
74
+ class MultipleChoice < Prompt
75
+ def question
76
+ "multiple_choice(#{prompt.inspect}, #{@details['choices'].inspect})"
77
+ end
78
+ end
79
+
80
+ QUESTION_TYPES = {
81
+ 'boolean' => TrueFalse,
82
+ 'string' => Prompt,
83
+ 'multiple_choice' => MultipleChoice
84
+ }
85
+ end
86
+ end