Implement task evaluation feature

This commit is contained in:
Thibault Jouan 2013-07-27 23:53:12 +00:00
parent c7295fb977
commit baaa957e9e
6 changed files with 77 additions and 0 deletions

12
features/task.feature Normal file
View File

@ -0,0 +1,12 @@
Feature: tasks
Scenario: evaluates ruby code grouped in task blocks
Given a recipe with:
"""
task :hello do
puts 'hello from recipe'
end
"""
When I execute the recipe
Then the exit status must be 0
And the output must contain "hello from recipe"

View File

@ -1,3 +1,4 @@
require 'producer/core/cli' require 'producer/core/cli'
require 'producer/core/recipe' require 'producer/core/recipe'
require 'producer/core/task'
require 'producer/core/version' require 'producer/core/version'

View File

@ -13,11 +13,15 @@ module Producer
def evaluate def evaluate
dsl = DSL.new(@code) dsl = DSL.new(@code)
dsl.tasks.each.map(&:evaluate)
end end
class DSL class DSL
attr_reader :tasks
def initialize(code = nil, &block) def initialize(code = nil, &block)
@tasks = []
if code if code
instance_eval code instance_eval code
else else
@ -31,6 +35,10 @@ module Producer
def source(filepath) def source(filepath)
instance_eval File.read("./#{filepath}.rb") instance_eval File.read("./#{filepath}.rb")
end end
def task(name, &block)
@tasks << Task.new(name, &block)
end
end end
end end
end end

16
lib/producer/core/task.rb Normal file
View File

@ -0,0 +1,16 @@
module Producer
module Core
class Task
attr_reader :name
def initialize(name, &block)
@name = name
@block = block
end
def evaluate
instance_eval &@block
end
end
end
end

View File

@ -54,6 +54,23 @@ module Producer::Core
expect { dsl }.to raise_error(RuntimeError, 'error from recipe') expect { dsl }.to raise_error(RuntimeError, 'error from recipe')
end end
end end
describe '#tasks' do
let(:code) { Proc.new { task(:some_task) } }
it 'returns registered tasks' do
expect(dsl.tasks[0].name).to eq :some_task
end
end
describe '#task' do
let(:code) { Proc.new { task(:one); task(:two) } }
it 'registers tasks in declaration order' do
expect(dsl.tasks[0].name).to eq :one
expect(dsl.tasks[1].name).to eq :two
end
end
end end
end end
end end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
module Producer::Core
describe Task do
let(:name) { :some_task }
let(:block) { }
subject(:task) { Task.new(name, &block) }
describe '#name' do
it 'returns its name' do
expect(task.name).to eq name
end
end
describe 'evaluate' do
let(:block) { Proc.new { raise 'error from task' } }
it 'evaluates its block' do
expect { task.evaluate }.to raise_error(RuntimeError, 'error from task')
end
end
end
end