Merge Task::DSL into Task
This commit is contained in:
@@ -23,7 +23,7 @@ module Producer
|
||||
end
|
||||
|
||||
def task(name, *args, &block)
|
||||
@tasks << Task.evaluate(name, env, *args, &block)
|
||||
@tasks << Task.evaluate(env, name, *args, &block)
|
||||
end
|
||||
|
||||
def macro(name, &block)
|
||||
|
@@ -2,28 +2,54 @@ module Producer
|
||||
module Core
|
||||
class Task
|
||||
class << self
|
||||
def evaluate(name, env, *args, &block)
|
||||
dsl = DSL.new(env, &block)
|
||||
dsl.evaluate(*args)
|
||||
Task.new(name, dsl.actions, dsl.condition)
|
||||
def define_action(keyword, klass)
|
||||
define_method(keyword) do |*args|
|
||||
@actions << klass.new(@env, *args)
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate(env, name, *args, &block)
|
||||
new(env, name).tap { |o| o.instance_exec *args, &block }
|
||||
end
|
||||
end
|
||||
|
||||
define_action :echo, Actions::Echo
|
||||
define_action :sh, Actions::ShellCommand
|
||||
|
||||
define_action :mkdir, Actions::Mkdir
|
||||
define_action :file_append, Actions::FileAppend
|
||||
define_action :file_replace_content, Actions::FileReplaceContent
|
||||
define_action :file_write, Actions::FileWriter
|
||||
|
||||
attr_reader :name, :actions, :condition
|
||||
|
||||
def initialize(name, actions = [], condition = true)
|
||||
def initialize(env, name, actions = [], condition = true)
|
||||
@env = env
|
||||
@name = name
|
||||
@actions = actions
|
||||
@condition = condition
|
||||
end
|
||||
|
||||
def to_s
|
||||
name.to_s
|
||||
@name.to_s
|
||||
end
|
||||
|
||||
def condition_met?
|
||||
!!@condition
|
||||
end
|
||||
|
||||
def condition(&block)
|
||||
@condition = Condition.evaluate(@env, &block) if block
|
||||
@condition
|
||||
end
|
||||
|
||||
def ask(question, choices, prompter: Prompter.new(@env.input, @env.output))
|
||||
prompter.prompt(question, choices)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
@env[key]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -1,50 +0,0 @@
|
||||
module Producer
|
||||
module Core
|
||||
class Task
|
||||
class DSL
|
||||
class << self
|
||||
def define_action(keyword, klass)
|
||||
define_method(keyword) do |*args|
|
||||
@actions << klass.new(@env, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
define_action :echo, Actions::Echo
|
||||
define_action :sh, Actions::ShellCommand
|
||||
|
||||
define_action :mkdir, Actions::Mkdir
|
||||
|
||||
define_action :file_append, Actions::FileAppend
|
||||
define_action :file_replace_content, Actions::FileReplaceContent
|
||||
define_action :file_write, Actions::FileWriter
|
||||
|
||||
attr_reader :env, :block, :actions
|
||||
|
||||
def initialize(env, &block)
|
||||
@env = env
|
||||
@block = block
|
||||
@actions = []
|
||||
@condition = true
|
||||
end
|
||||
|
||||
def evaluate(*args)
|
||||
instance_exec *args, &@block
|
||||
end
|
||||
|
||||
def condition(&block)
|
||||
@condition = Condition.evaluate(@env, &block) if block
|
||||
@condition
|
||||
end
|
||||
|
||||
def ask(question, choices, prompter: Prompter)
|
||||
prompter.new(env.input, env.output).prompt(question, choices)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
env[key]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -4,27 +4,25 @@ module Producer
|
||||
DRY_RUN_WARNING =
|
||||
'running in dry run mode, actions will NOT be applied'.freeze
|
||||
|
||||
attr_accessor :env
|
||||
|
||||
def initialize(env)
|
||||
@env = env
|
||||
end
|
||||
|
||||
def process(tasks)
|
||||
env.log DRY_RUN_WARNING, :warn if env.dry_run?
|
||||
@env.log DRY_RUN_WARNING, :warn if @env.dry_run?
|
||||
|
||||
tasks.each { |t| process_task t }
|
||||
end
|
||||
|
||||
def process_task(task)
|
||||
if task.condition_met?
|
||||
env.log "Task: `#{task}' applying..."
|
||||
@env.log "Task: `#{task}' applying..."
|
||||
task.actions.each do |e|
|
||||
env.log " action: #{e}"
|
||||
e.apply unless env.dry_run?
|
||||
@env.log " action: #{e}"
|
||||
e.apply unless @env.dry_run?
|
||||
end
|
||||
else
|
||||
env.log "Task: `#{task}' skipped"
|
||||
@env.log "Task: `#{task}' skipped"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user