Skip to content

Commit 3ae6ae6

Browse files
committedAug 10, 2012
Merge pull request #89 from tomykaira/custom_recipes
Custom recipes. Fix #86
2 parents f87dc31 + fba28e2 commit 3ae6ae6

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed
 

‎lib/rails_wizard.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44
require 'rails_wizard/diagnostics'
55
require 'rails_wizard/template'
66

7-
Dir[File.dirname(__FILE__) + '/../recipes/*.rb'].each do |path|
8-
key = File.basename(path, '.rb')
9-
recipe = RailsWizard::Recipe.generate(key, File.open(path))
10-
RailsWizard::Recipes.add(recipe)
11-
end
7+
RailsWizard::Recipes.add_from_directory(File.dirname(__FILE__) + '/../recipes')

‎lib/rails_wizard/command.rb

+10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Command < Thor
77
desc "new APP_NAME", "create a new Rails app"
88
method_option :recipes, :type => :array, :aliases => "-r"
99
method_option :defaults, :type => :string, :aliases => "-d"
10+
method_option :recipe_dirs, :type => :array, :aliases => "-l"
1011
def new(name)
12+
add_recipes
1113
args = ask_for_args
1214
recipes, defaults = load_defaults
1315
recipes = ask_for_recipes(recipes)
@@ -18,7 +20,9 @@ def new(name)
1820
desc "template TEMPLATE_FILE", "create a new Rails template"
1921
method_option :recipes, :type => :array, :aliases => "-r"
2022
method_option :defaults, :type => :string, :aliases => "-d"
23+
method_option :recipe_dirs, :type => :array, :aliases => "-l"
2124
def template(template_name)
25+
add_recipes
2226
recipes, defaults = load_defaults
2327
recipes = ask_for_recipes(recipes)
2428
gems = ask_for_gems
@@ -46,6 +50,12 @@ def red; "\033[31m" end
4650
def green; "\033[32m" end
4751
def yellow; "\033[33m" end
4852

53+
def add_recipes
54+
if dirs = options[:recipe_dirs]
55+
dirs.each { |d| Recipes.add_from_directory(d) }
56+
end
57+
end
58+
4959
def load_defaults
5060
# Load defaults from a file; if a file specifies recipes, they'll be run *before*
5161
# any on the command line (or prompted for)..

‎lib/rails_wizard/recipes.rb

+10
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,15 @@ def self.for(category)
3434
def self.remove_from_category(category, recipe)
3535
(@@categories[category.to_s] ||= []).delete(recipe.key)
3636
end
37+
38+
def self.add_from_directory(directory)
39+
Dir.foreach(directory) do |file|
40+
path = File.join(directory, file)
41+
next unless path.match /\.rb$/
42+
key = File.basename(path, '.rb')
43+
recipe = Recipe.generate(key, File.open(path))
44+
add(recipe)
45+
end
46+
end
3747
end
3848
end

‎spec/rails_wizard/recipes_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
RailsWizard::Recipes.for('test').should be_include('recipe_test')
2222
end
2323
end
24+
25+
it 'should add recipes in a directory with add_from_directory' do
26+
subject.add_from_directory(File.join(File.dirname(__FILE__), '..', 'test_recipes'))
27+
subject.list.should include 'test_recipe_in_file'
28+
end
2429
end
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
say_wizard "Hi! I am a test."
2+
3+
__END__
4+
5+
name: test_recipe_in_file
6+
description: "this is test file"
7+
author: githubname
8+
9+
category: test

0 commit comments

Comments
 (0)