lib/bently/recipe.rb in bently-0.1.0 vs lib/bently/recipe.rb in bently-1.0.0
- old
+ new
@@ -1,63 +1,110 @@
module Bently
-
- module RecipeMethods
- def self.extended(base)
- base.send(:include, InstanceMethods) if base.is_a? Class
+ class Recipe
+
+ class Operation
+ def say; nil end
+ def after; nil end
end
- def step(*args)
- step_args << args
+ class Say < Operation
+ attr_reader :message, :status, :color
+ def initialize *args
+ @message, @status, @color = args
+ end
+ def args
+ @status ? [@status,@message,@color] : [@status]
+ end
end
- def step_args
- @steps_args ||= []
+ class Run < Operation
+ attr_reader :command
+ def initialize *args
+ @command = args.shift
+ end
end
- private
+ class Create < Operation
+ attr_reader :args, :file, :data
+ def initialize *args
+ @file, @data = @args = args
+ end
+ def after
+ @data.each_line.each_with_index.map{|l,i| [i+1, l] }
+ end
+ end
- def inherited(subclass)
- super
- subclass.step_args.concat self.step_args
+ class Modify < Operation
+ attr_reader :args, :file, :pattern, :replace
+ def initialize *args
+ @file, @pattern, @replace = @args = args
+ end
+ def after
+ [[1,@pattern.inspect], [2,@replace.inspect]]
+ end
end
- module InstanceMethods
- def steps
- @steps ||= parse_steps
+ class Append < Operation
+ attr_reader :file, :data
+ def initialize *args
+ @file, @data = args
end
+ def args; [@file, @data] end
+ def after
+ @data.each_line.each_with_index.map{|l,i| [i+1, l] }
+ end
+ end
- protected
+ class Prepend < Operation
+ attr_reader :args, :file, :data
+ def initialize *args
+ @file, @data = @args = args
+ end
+ def after
+ @data.each_line.each_with_index.map{|l,i| [i+1, l] }
+ end
+ end
- def parse_steps
- self.class.step_args.map do |args|
- self.send(args.shift, *args)
- end
+ class Insert < Operation
+ attr_reader :args, :file, :data, :options
+ def initialize *args
+ @file, @data, @options = @args = args
end
+ def after
+ @data.each_line.each_with_index.map{|l,i| [i+1, l] }
+ end
end
- end
- class Recipe
- extend RecipeMethods
+ class Remove < Operation
+ attr_reader :args, :file
+ def initialize *args
+ @args = args
+ @file = @args[0]
+ end
+ end
- # step :shell, command
- def shell(*args); StepShell.new(*args) end
+ def self.breakdown
+ new.operations
+ end
- # step :touch, :file => filename, :with => data
- def touch(*args) StepTouch.new(*args) end
+ def self.name(name) @name ||= name; end
+ def self.category(category) @category ||= category; end
+ def self.description(description) @description ||= description; end
+ def self.homepage(homepage=nil) @homepage ||= homepage; end
+ def self.version(version) @version ||= version; end
- # step :modify, :file => filename, :from => pattern, :to => replacement
- def modify(*args) StepModify.new(*args) end
+ def operations; @operations ||= []; end
- # step :append, :file => filename, :with => data
- def append(*args) StepAppend.new(*args) end
-
- # step :prepend, :file => filename, :with => data
- def prepend(*args) StepPrepend.new(*args) end
-
- # step :insert, :file => filename, :with => data, :after => some_text
- def insert(*args) StepInsert.new(*args) end
-
- # step :remove, :file => filename
- def remove(*args) StepRemove.new(*args) end
+ def say(*args) ; operations << Say.new(*args) end
+ def run(*args) ; operations << Run.new(*args) end
+ def create(*args) ; operations << Create.new(*args) end
+ def modify(*args) ; operations << Modify.new(*args) end
+ def append(*args) ; operations << Append.new(*args) end
+ def prepend(*args) ; operations << Prepend.new(*args) end
+ def insert(*args) ; operations << Insert.new(*args) end
+ def remove(*args) ; operations << Remove.new(*args) end
+ def operate(op, *args) ; operations << op.new(*args) end
+ def todo(name) ; say(name, 'TODO', :red) end
+ def warn(text) ; say(text, 'WARNING', :red) end
+ def requirement(text) ; say(text, 'REQUIRED', :red) end
end
-
end