Permalink
Browse files

Bring template generation into the gem instead of the web app. Coming…

… along nicely.
  • Loading branch information...
1 parent b3540f4 commit 8417d5f8913d35081f4d3448a00e0b85919b7955 Michael Bleigh committed Mar 13, 2011
Showing with 1,390 additions and 19 deletions.
  1. +26 −0 Rakefile
  2. +1 −0 lib/rails_wizard.rb
  3. +7 −2 lib/rails_wizard/config.rb
  4. +13 −3 lib/rails_wizard/recipe.rb
  5. +30 −0 lib/rails_wizard/template.rb
  6. +1 −1 rails_wizard.gemspec
  7. +77 −0 sample.rb
  8. +17 −13 spec/rails_wizard/config_spec.rb
  9. +7 −0 spec/rails_wizard/template_spec.rb
  10. +43 −0 templates/helpers.erb
  11. +40 −0 templates/layout.erb
  12. +7 −0 templates/recipe.erb
  13. +4 −0 test_run/.gitignore
  14. +32 −0 test_run/Gemfile
  15. +256 −0 test_run/README
  16. +7 −0 test_run/Rakefile
  17. +3 −0 test_run/app/controllers/application_controller.rb
  18. +2 −0 test_run/app/helpers/application_helper.rb
  19. +14 −0 test_run/app/views/layouts/application.html.erb
  20. +4 −0 test_run/config.ru
  21. +49 −0 test_run/config/application.rb
  22. +6 −0 test_run/config/boot.rb
  23. +5 −0 test_run/config/environment.rb
  24. +26 −0 test_run/config/environments/development.rb
  25. +49 −0 test_run/config/environments/production.rb
  26. +35 −0 test_run/config/environments/test.rb
  27. +7 −0 test_run/config/initializers/backtrace_silencers.rb
  28. +2 −0 test_run/config/initializers/generators.rb
  29. +10 −0 test_run/config/initializers/inflections.rb
  30. +5 −0 test_run/config/initializers/mime_types.rb
  31. +7 −0 test_run/config/initializers/secret_token.rb
  32. +8 −0 test_run/config/initializers/session_store.rb
  33. +5 −0 test_run/config/locales/en.yml
  34. +58 −0 test_run/config/routes.rb
  35. +7 −0 test_run/db/seeds.rb
  36. +2 −0 test_run/doc/README_FOR_APP
  37. 0 test_run/lib/tasks/.gitkeep
  38. +26 −0 test_run/public/404.html
  39. +26 −0 test_run/public/422.html
  40. +26 −0 test_run/public/500.html
  41. 0 test_run/public/favicon.ico
  42. BIN test_run/public/images/rails.png
  43. +239 −0 test_run/public/index.html
  44. 0 test_run/public/javascripts/.gitkeep
  45. 0 test_run/public/javascripts/application.js
  46. +16 −0 test_run/public/javascripts/jquery.js
  47. +158 −0 test_run/public/javascripts/rails.js
  48. +5 −0 test_run/public/robots.txt
  49. 0 test_run/public/stylesheets/.gitkeep
  50. +6 −0 test_run/script/rails
  51. +9 −0 test_run/test/performance/browsing_test.rb
  52. +7 −0 test_run/test/test_helper.rb
  53. 0 test_run/vendor/plugins/.gitkeep
View
@@ -7,3 +7,29 @@ desc "run specs"
RSpec::Core::RakeTask.new
task :default => :spec
+
+
+desc "Remove the test_run Rails app (if it's there)"
+task :clean do
+ system 'rm -rf test_run'
+end
+
+desc "Execute a test run with the specified recipes."
+task :run => :clean do
+ recipes = ENV['RECIPES'].split(',')
+
+ require 'tempfile'
+ require 'rails_wizard'
+
+ template = RailsWizard::Template.new(recipes)
+
+ begin
+ file = Tempfile.new('temporary_template')
+ file.write template.compile
+ file.close
+
+ system "rails new test_run -m #{file.path} #{template.args.join(' ')}"
+ ensure
+ file.unlink
+ end
+end
View
@@ -1,6 +1,7 @@
require 'rails_wizard/recipes'
require 'rails_wizard/recipe'
require 'rails_wizard/config'
+require 'rails_wizard/template'
Dir[File.dirname(__FILE__) + '/../recipes/*.rb'].each do |path|
key = File.basename(path, '.rb')
@@ -1,10 +1,15 @@
+require 'active_support/ordered_hash'
+
module RailsWizard
class Config
attr_reader :questions
def initialize(schema)
- @questions = {}
- schema.each_pair do |key, details|
+ @questions = ActiveSupport::OrderedHash.new
+ schema.each do |hash|
+ key = hash.keys.first
+ details = hash.values.first
+
kind = details['type']
raise ArgumentError, "Unrecognized question type, must be one of #{QUESTION_TYPES.keys.join(', ')}" unless QUESTION_TYPES.keys.include?(kind)
@questions[key] = QUESTION_TYPES[kind].new(details)
View
@@ -1,7 +1,8 @@
+require 'rails_wizard/config'
+
require 'active_support/inflector'
require 'yaml'
-
-require 'rails_wizard/config'
+require 'erb'
module RailsWizard
class Recipe
@@ -82,7 +83,16 @@ def self.to_mongo(value)
end
def self.from_mongo(key)
- RailsWizard::Recipes[key]
+ case key
+ when String
+ RailsWizard::Recipes[key]
+ when RailsWizard::Recipe
+ key
+ end
+ end
+
+ def self.get_binding
+ binding
end
end
end
@@ -0,0 +1,30 @@
+module RailsWizard
+ class Template
+ attr_reader :recipes
+
+ def initialize(recipes)
+ @recipes = recipes.map{|r| RailsWizard::Recipe.from_mongo(r)}
+ end
+
+ def self.template_root
+ File.dirname(__FILE__) + '/../../templates'
+ end
+
+ def self.render(template_name, binding = nil)
+ erb = ERB.new(File.open(template_root + '/' + template_name + '.erb').read)
+ erb.result(binding)
+ end
+ def render(template_name, binding = nil); self.class.render(template_name, binding) end
+
+ def compile
+ render 'layout', binding
+ end
+
+ def args
+ recipes.map(&:args).uniq
+ end
+
+ def custom_code?; false end
+ def custom_code; nil end
+ end
+end
View
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.add_dependency "activesupport", "~> 3.0.0"
s.add_development_dependency "rspec", "~> 2.5.0"
- s.files = Dir["lib/**/*.rb", "recipes/*.rb", "README.markdown", "version.rb"]
+ s.files = Dir["lib/**/*.rb", "recipes/*.rb", "README.markdown", "version.rb", "templates/*"]
s.test_files = Dir["spec/**/*"]
s.executables = Dir["bin/*"]
s.require_paths = ["lib"]
View
@@ -0,0 +1,77 @@
+# >---------------------------[ Install Command ]-----------------------------<
+# rails new APP_NAME -m http://railswizard.local/b9755a02fbf7886df8b2.rb
+# >---------------------------------------------------------------------------<
+#
+# _____ _ _ __ ___ _
+# | __ \ (_) | \ \ / (_) | |
+# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
+# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
+# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
+# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
+#
+# This template was generated by RailsWizard, the amazing and awesome Rails
+# application template builder. Get started at http://railswizard.org
+#
+# ___________________________________________________________________________
+# |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
+# | | |
+# | Name: | b9755a02fbf7886df8b2 |
+# | URL: | http://railswizard.local/b9755a02fbf7886df8b2.rb |
+# | Updated: | March 12, 2011 at 21:47PM |
+# | | |
+# |\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\|
+# ---------------------------------------------------------------------------
+
+# >----------------------------[ Initial Setup ]------------------------------<
+
+initializer 'generators.rb', <<-RUBY
+Rails.application.config.generators do |g|
+end
+RUBY
+
+recipes = ["activerecord", "devise", "prototype"]
+
+def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
+def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
+def say_wizard(text); say_custom('wizard', text) end
+def ask_wizard(question)
+ ask "\033[1m\033[30m\033[46m" + "prompt".rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
+end
+def yes_wizard?(question)
+ answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
+ case answer.downcase
+ when "yes", "y"
+ true
+ when "no", "n"
+ false
+ else
+ yes_wizard?(question)
+ end
+end
+def no_wizard?(question); !yes_wizard?(question) end
+def multiple_choice(question, choices)
+ say_custom('question', question)
+ values = {}
+ choices.each_with_index do |choice,i|
+ values[(i + 1).to_s] = choice[1]
+ say_custom (i + 1).to_s + '.', choice[0]
+ end
+ answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
+ values[answer]
+end
+
+@after_blocks = []
+def after_bundler(&block); @after_blocks << block; end
+
+
+say_wizard ask_wizard("What do you want to know?")
+say_wizard yes_wizard?("Is there a god?")
+say_wizard multiple_choice("What do you like?", [["Puppies","puppies"],["Kitties","kitties"]])
+
+# >-----------------------------[ Run Bundler ]-------------------------------<
+
+say_wizard "Running Bundler install. This will take a while."
+run 'bundle install'
+say_wizard "Running after Bundler callbacks."
+@after_blocks.each{|b| b.call}
+
@@ -2,30 +2,34 @@
describe RailsWizard::Config do
describe '#initialize' do
- subject{ RailsWizard::Config.new(@schema) }
+ subject{ RailsWizard::Config.new(YAML.load(@schema)) }
it 'should add a question key for each key of the schema' do
- @schema = {
- 'test' => {'type' => 'string'},
- 'foo' => {'type' => 'boolean'}
- }
+ @schema = <<-YAML
+ - test:
+ type: string
+ YAML
subject.questions.should be_key('test')
end
it 'should instantiate the correct question type for each question' do
- @schema = {
- 'string' => {'type' => 'string'},
- 'boolean' => {'type' => 'boolean'},
- 'multiple_choice' => {'type' => 'multiple_choice'}
- }
+ @schema = <<-YAML
+ - string:
+ type: string
+ - boolean:
+ type: boolean
+ - multiple_choice:
+ type: multiple_choice
+ YAML
subject.questions['string'].should be_kind_of(RailsWizard::Config::Prompt)
subject.questions['boolean'].should be_kind_of(RailsWizard::Config::TrueFalse)
subject.questions['multiple_choice'].should be_kind_of(RailsWizard::Config::MultipleChoice)
end
it 'should error on invalid question type' do
- @schema = {
- 'invalid' => {'type' => 'invalid'}
- }
+ @schema = <<-YAML
+ - invalid
+ type: invalid
+ YAML
lambda{ subject }.should raise_error(ArgumentError)
end
end
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe RailsWizard::Template do
+ it do
+ puts RailsWizard::Template.new(['jquery', 'heroku', 'mongo_mapper']).compile
+ end
+end
View
@@ -0,0 +1,43 @@
+def recipes; @recipes end
+def recipe?(name); @recipes.include?(name) end
+
+def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
+def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
+def say_wizard(text); say_custom('wizard', text) end
+
+def ask_wizard(question)
+ ask "\033[1m\033[30m\033[46m" + "prompt".rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
+end
+
+def yes_wizard?(question)
+ answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
+ case answer.downcase
+ when "yes", "y"
+ true
+ when "no", "n"
+ false
+ else
+ yes_wizard?(question)
+ end
+end
+
+def no_wizard?(question); !yes_wizard?(question) end
+
+def multiple_choice(question, choices)
+ say_custom('question', question)
+ values = {}
+ choices.each_with_index do |choice,i|
+ values[(i + 1).to_s] = choice[1]
+ say_custom (i + 1).to_s + '.', choice[0]
+ end
+ answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
+ values[answer]
+end
+
+@current_recipe = nil
+
+@after_blocks = []
+def after_bundler(&block); @after_blocks << block; end
+
+@before_configs = {}
+def before_config(&block); @before_configs[@current_recipe] = block; end
View
@@ -0,0 +1,40 @@
+# >---------------------------------------------------------------------------<
+#
+# _____ _ _ __ ___ _
+# | __ \ (_) | \ \ / (_) | |
+# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
+# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
+# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
+# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
+#
+# This template was generated by RailsWizard, the amazing and awesome Rails
+# application template builder. Get started at http://railswizard.org
+#
+# >---------------------------------------------------------------------------<
+
+# >----------------------------[ Initial Setup ]------------------------------<
+
+initializer 'generators.rb', <<-RUBY
+Rails.application.config.generators do |g|
+end
+RUBY
+
+@recipes = <%= recipes.map(&:key).inspect %>
+
+<%= render "helpers" %>
+
+<% recipes.each do |recipe| %>
+<%= render 'recipe', recipe.get_binding %>
+<% end %>
+
+<% if custom_code? %># >-----------------------------[ Custom Code ]-------------------------------<
+
+<%= custom_code %><% end %>
+
+# >-----------------------------[ Run Bundler ]-------------------------------<
+
+say_wizard "Running Bundler install. This will take a while."
+run 'bundle install'
+say_wizard "Running after Bundler callbacks."
+@after_blocks.each{|b| b.call}
+
View
@@ -0,0 +1,7 @@
+# ><%= "[ #{name} ]".center(75,'-') %><
+
+@current_recipe = "<%= key %>"
+@before_configs["<%= key %>"].call if @before_configs["<%= key %>"]
+say_recipe '<%= name %>'
+
+<%= template %>
View
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/
View
@@ -0,0 +1,32 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.4'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
+# gem 'ruby-debug'
+# gem 'ruby-debug19'
+
+# Bundle the extra gems:
+# gem 'bj'
+# gem 'nokogiri'
+# gem 'sqlite3-ruby', :require => 'sqlite3'
+# gem 'aws-s3', :require => 'aws/s3'
+
+# Bundle gems for the local environment. Make sure to
+# put test-only gems in this group so their generators
+# and rake tasks are available in development mode:
+# group :development, :test do
+# gem 'webrat'
+# end
+gem "bson_ext"
+gem "mongo_mapper", :git => "git://github.com/jnunemaker/mongomapper.git", :branch => "rails3"
Oops, something went wrong.

0 comments on commit 8417d5f

Please sign in to comment.