Permalink
Browse files

Merge pull request #63 from bryanstearns/with_default_options

Configuration files for recipes and answers
  • Loading branch information...
2 parents 71ab5a6 + 3c5958a commit 2d6cdd73e9ff6394b4311da4b8e037536e52da8b @DanielKehoe DanielKehoe committed May 21, 2012
@@ -6,14 +6,20 @@ class Command < Thor
include Thor::Actions
desc "new APP_NAME", "create a new Rails app"
method_option :recipes, :type => :array, :aliases => "-r"
+ method_option :defaults, :type => :string, :aliases => "-d"
def new(name)
- run_template(name, ask_for_recipes, nil)
+ recipes, defaults = load_defaults
+ recipes = ask_for_recipes(recipes)
+ run_template(name, recipes, defaults, nil)
end
desc "template TEMPLATE_FILE", "create a new Rails template"
method_option :recipes, :type => :array, :aliases => "-r"
+ method_option :defaults, :type => :string, :aliases => "-d"
def template(template_name)
- run_template(nil, ask_for_recipes, template_name)
+ recipes, defaults = load_defaults
+ recipes = ask_for_recipes(recipes)
+ run_template(nil, recipes, defaults, template_name)
end
desc "list [CATEGORY]", "list available recipes (optionally by category)"
@@ -37,6 +43,18 @@ def red; "\033[31m" end
def green; "\033[32m" end
def yellow; "\033[33m" end
+ def load_defaults
+ # Load defaults from a file; if a file specifies recipes, they'll be run *before*
+ # any on the command line (or prompted for)..
+ defaults = if options[:defaults]
+ File.open(options[:defaults]) {|f| YAML.load(f) }
+ else
+ {}
+ end
+ recipes = defaults.delete('recipes') { [] }
+ [recipes, defaults]
+ end
+
def print_recipes(recipes)
puts
puts "#{bold}#{cyan}Available Recipes#{clear}:"
@@ -48,11 +66,10 @@ def print_recipes(recipes)
puts
end
- def ask_for_recipes
+ def ask_for_recipes(recipes)
if options[:recipes]
- return options[:recipes]
+ return recipes + options[:recipes]
end
- recipes=[]
while recipe = ask("#{print_recipes(recipes)}#{bold}Which recipe would you like to add? #{clear}#{yellow}(blank to finish)#{clear}")
if recipe == ''
break
@@ -70,17 +87,18 @@ def ask_for_recipes
#pass in name if you want to create a rails app
#pass in file_name if you want to create a template
- def run_template(name, recipes, file_name=nil)
+ def run_template(name, recipes, defaults, file_name=nil)
puts
puts
puts "#{bold}Generating#{name ? " and Running" : ''} Template..."
puts
+
if file_name
file = File.new(file_name,'w')
else
file = Tempfile.new('template')
end
- template = RailsWizard::Template.new(recipes)
+ template = RailsWizard::Template.new(recipes, defaults)
file.write template.compile
file.close
if name
@@ -2,10 +2,11 @@
module RailsWizard
class Config
- attr_reader :questions
+ attr_reader :questions, :defaults
- def initialize(schema)
+ def initialize(schema, defaults=nil)
@questions = ActiveSupport::OrderedHash.new
+ @defaults = defaults
schema.each do |hash|
key = hash.keys.first
details = hash.values.first
@@ -18,6 +19,7 @@ def initialize(schema)
def compile(values = {})
result = []
+ values.merge!(defaults) if defaults
result << "config = #{values.inspect}"
@questions.each_pair do |key, question|
result << "config['#{key}'] = #{question.compile} unless config.key?('#{key}')"
@@ -14,7 +14,7 @@ def self.<=>(another)
self.key <=> another.key
end
- ATTRIBUTES = %w(key args category name description template config exclusive tags run_before run_after requires)
+ ATTRIBUTES = %w(key args category name description template config exclusive tags run_before run_after requires defaults)
DEFAULT_ATTRIBUTES = {
:category => 'other',
:args => [],
@@ -73,7 +73,7 @@ def self.attributes=(hash)
def self.config
return nil unless attributes[:config]
- RailsWizard::Config.new(attributes[:config])
+ RailsWizard::Config.new(attributes[:config], attributes[:defaults])
end
def attributes
@@ -1,9 +1,10 @@
module RailsWizard
class Template
- attr_reader :recipes
+ attr_reader :recipes, :defaults
- def initialize(recipes)
+ def initialize(recipes, defaults={})
@recipes = recipes.map{|r| RailsWizard::Recipe.from_mongo(r)}
+ @defaults = defaults
end
def self.template_root
@@ -31,6 +32,7 @@ def resolve_recipes
end
end
+ list.each {|recipe| recipe.defaults = defaults[recipe.key] }
list
end
end
@@ -2,7 +2,8 @@
describe RailsWizard::Config do
describe '#initialize' do
- subject{ RailsWizard::Config.new(YAML.load(@schema)) }
+ let(:defaults) { nil }
+ subject{ RailsWizard::Config.new(YAML.load(@schema), defaults) }
it 'should add a question key for each key of the schema' do
@schema = <<-YAML
- test:
@@ -72,6 +73,14 @@
it 'should handle "unelss_recipe"' do
lines[3].should be_include("!recipe?('awesome')")
end
+
+ describe 'with defaults' do
+ let(:defaults) { { 'multiple_choice' => 'def' }}
+
+ it 'should process defaults' do
+ lines[0].should == 'config = {"multiple_choice"=>"def"}'
+ end
+ end
end
describe RailsWizard::Config::Prompt do
@@ -3,11 +3,16 @@
describe RailsWizard::Template do
subject{ RailsWizard::Template }
let(:recipe){ RailsWizard::Recipe.generate('name','# test') }
+ let(:defaults){ { "some_option" => "value" } }
describe '#initialize' do
it 'should work with classes' do
subject.new([recipe]).recipes.should == [recipe]
end
+
+ it 'should accept optional defaults' do
+ subject.new([recipe], defaults).defaults.should == defaults
+ end
end
describe '#resolve_dependencies' do

0 comments on commit 2d6cdd7

Please sign in to comment.