Skip to content
Browse files

Get template configs up and working.

  • Loading branch information...
2 parents f23369c + 6f31ba9 commit 8c37c87e3f77017cc2f2b9d2dc71ea354785fe89 Michael Bleigh committed Mar 15, 2011
Showing with 135 additions and 27 deletions.
  1. +9 −6 Rakefile
  2. +47 −10 lib/rails_wizard/config.rb
  3. +12 −9 recipes/activerecord.rb
  4. +63 −0 spec/rails_wizard/config_spec.rb
  5. +2 −2 spec/support/template_runner.rb
  6. +2 −0 templates/recipe.erb
View
15 Rakefile
@@ -24,13 +24,16 @@ task :run => :clean do
template = RailsWizard::Template.new(recipes)
begin
- file = Tempfile.new('temporary_template')
- file.write template.compile
- file.close
+ dir = Dir.mktmpdir "rails_template"
+ Dir.chdir(dir) do
+ file = File.open('template.rb', 'w')
+ file.write template.compile
+ file.close
- system "rails new test_run -m #{file.path} #{template.args.join(' ')}"
- ensure
- file.unlink
+ system "rails new test_run -m template.rb #{template.args.join(' ')}"
+
+ puts "\n\n cd #{dir} # look at the app"
+ end
end
end
View
57 lib/rails_wizard/config.rb
@@ -17,27 +17,64 @@ def initialize(schema)
end
def compile(values = {})
-
- end
-
- class Question
- def initialize(schema)
+ result = []
+ result << "config = #{values.inspect}"
+ @questions.each_pair do |key, question|
+ result << "config['#{key}'] = #{question.compile} unless config.key?('#{key}')"
end
+ result.join("\n")
end
-
+
class Prompt
+ attr_reader :prompt, :details
def initialize(details)
+ @details = details
+ @prompt = details['prompt']
+ end
+
+ def compile
+ "#{question} if #{conditions}"
+ end
+ def question
+ "ask_wizard(#{prompt.inspect})"
+ end
+
+ def conditions
+ [config_conditions, recipe_conditions].join(' && ')
+ end
+
+ def config_conditions
+ if details['if']
+ "config['#{details['if']}']"
+ elsif details['unless']
+ "!config['#{details['unless']}']"
+ else
+ 'true'
+ end
+ end
+
+ def recipe_conditions
+ if details['if_recipe']
+ "recipe?('#{details['if_recipe']}')"
+ elsif details['unless_recipe']
+ "!recipe?('#{details['unless_recipe']}')"
+ else
+ 'true'
+ end
end
end
- class TrueFalse < Question
- def compile
- "wizard_yes?(#{prompt.inspect})"
+ class TrueFalse < Prompt
+ def question
+ "yes_wizard?(#{prompt.inspect})"
end
end
- class MultipleChoice < Question
+ class MultipleChoice < Prompt
+ def question
+ "multiple_choice(#{prompt.inspect}, #{@details['choices'].inspect})"
+ end
end
QUESTION_TYPES = {
View
21 recipes/activerecord.rb
@@ -1,14 +1,14 @@
-config['database'] = 'oracle'
+if config['database']
+ say_wizard "Configuring '#{config['database']}' database settings..."
+ old_gem = gem_for_database
+ @options = @options.dup.merge(:database => config['database'])
+ gsub_file 'Gemfile', "gem '#{old_gem}'", "gem '#{gem_for_database}'"
+ template "config/databases/#{@options[:database]}.yml", "config/database.yml.new"
+ run 'mv config/database.yml.new config/database.yml'
+end
after_bundler do
- if config['database']
- say_wizard "Configuring '#{config['database']}' database settings..."
- old_gem = gem_for_database
- @options[:database] = config['database']
- gsub_file "gem '#{old_gem}'", "gem '#{gem_for_database}'"
- template "config/databases/#{@options[:database]}.yml", "config/database.yml"
- rake "db:create:all"
- end
+ rake "db:create:all" if config['auto_create']
end
__END__
@@ -32,3 +32,6 @@
- ["SQLite", sqlite3]
- ["Frontbase", frontbase]
- ["IBM DB", ibm_db]
+ - auto_create:
+ type: boolean
+ prompt: "Automatically create database with default configuration?"
View
63 spec/rails_wizard/config_spec.rb
@@ -32,5 +32,68 @@
YAML
lambda{ subject }.should raise_error(ArgumentError)
end
+
+ describe '#compile' do
+ let(:lines) { subject.compile.split("\n") }
+ before do
+ @schema = <<-YAML
+ - string:
+ type: string
+ prompt: Give me a string?
+ if: is_true
+ - boolean:
+ type: boolean
+ prompt: Yes or no?
+ unless: is_false
+ if_recipe: awesome
+ - multiple_choice:
+ type: multiple_choice
+ choices: [[ABC, abc], [DEF, def]]
+ unless_recipe: awesome
+ YAML
+ end
+
+ it 'should include all questions' do
+ lines.size.should == 4
+ end
+
+ it 'should handle "if"' do
+ lines[1].should be_include("config['is_true']")
+ end
+
+ it 'should handle "unless"' do
+ lines[2].should be_include("!config['is_false']")
+ end
+
+ it 'should handle "if_recipe"' do
+ lines[2].should be_include("recipe?('awesome')")
+ end
+
+ it 'should handle "unelss_recipe"' do
+ lines[3].should be_include("!recipe?('awesome')")
+ end
+ end
+
+ describe RailsWizard::Config::Prompt do
+ subject{ RailsWizard::Config::Prompt }
+ it 'should compile to a prompt' do
+ subject.new({'prompt' => "What's your favorite color?"}).question.should == 'ask_wizard("What\'s your favorite color?")'
+ end
+ end
+
+ describe RailsWizard::Config::TrueFalse do
+ subject{ RailsWizard::Config::TrueFalse }
+ it 'should compile to a yes? question' do
+ subject.new({'prompt' => 'Yes yes?'}).question.should == 'yes_wizard?("Yes yes?")'
+ end
+ end
+
+ describe RailsWizard::Config::MultipleChoice do
+ subject{ RailsWizard::Config::MultipleChoice }
+ it 'should compile into a multiple_choice' do
+ subject.new({'prompt' => 'What kind of fruit?', 'choices' => [['Apples', 'apples'], ['Bananas', 'bananas']]}).question.should ==
+ 'multiple_choice("What kind of fruit?", [["Apples", "apples"], ["Bananas", "bananas"]])'
+ end
+ end
end
end
View
4 spec/support/template_runner.rb
@@ -18,8 +18,8 @@ def run(app_name = 'rails_app')
@output
end
- def rails_directory
- RailsDirectory.new(File.join(@dir, @app_name))
+ def rails
+ RailsDirectory.new(@rails_dir)
end
def clean
View
2 templates/recipe.erb
@@ -4,4 +4,6 @@
@before_configs["<%= key %>"].call if @before_configs["<%= key %>"]
say_recipe '<%= name %>'
+<%= config.compile if config %>
+
<%= template %>

0 comments on commit 8c37c87

Please sign in to comment.
Something went wrong with that request. Please try again.