-
Notifications
You must be signed in to change notification settings - Fork 51
DSL: passing output data between steps #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # typed: false | ||
| # frozen_string_literal: true | ||
|
|
||
| # How do we pass information between steps? | ||
| # Demonstrate by passing result of a command output to another step | ||
|
|
||
| config do | ||
| cmd(:echo) { display! } | ||
| end | ||
|
|
||
| execute do | ||
| cmd(:ls) { "ls -al" } | ||
| cmd(:echo) do | ||
| # TODO: this is a bespoke output object for cmd, is there a generic one we can offer | ||
| first_line = cmd(:ls).command_output.split("\n").second | ||
| "echo '#{first_line}'" | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,14 @@ | |
| module Roast | ||
| module DSL | ||
| class Cog | ||
| class CogAlreadyRanError < StandardError; end | ||
|
|
||
| class << self | ||
| def on_create | ||
| eigen = self | ||
| proc do |instance_name = Random.uuid, &action| | ||
| #: self as Roast::DSL::ExecutionContext | ||
| add_cog_instance(instance_name, eigen.new(action)) | ||
| #: self as Roast::DSL::WorkflowExecutionContext | ||
| add_cog_instance(instance_name, eigen.new(instance_name, action)) | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -39,23 +41,28 @@ def find_child_config_or_default | |
| end | ||
| end | ||
|
|
||
| attr_reader :output | ||
| attr_reader :name, :output | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes pls |
||
|
|
||
| def initialize(cog_input_proc) | ||
| def initialize(name, cog_input_proc) | ||
| @name = name | ||
| @cog_input_proc = cog_input_proc | ||
| @finished = false | ||
| end | ||
|
|
||
| def input | ||
| @cog_input_proc.call | ||
| end | ||
| def run!(config, cog_execution_context) | ||
| raise CogAlreadyRanError if ran? | ||
|
|
||
| def run!(config) | ||
| @config = config | ||
| @output = execute | ||
| @output = execute(cog_execution_context.instance_exec(&@cog_input_proc)) | ||
| @finished = true | ||
| end | ||
|
|
||
| def ran? | ||
| @finished | ||
| end | ||
|
|
||
| # Inheriting cog must implement this | ||
| def execute | ||
| def execute(input) | ||
| raise NotImplementedError | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # typed: true | ||
| # frozen_string_literal: true | ||
|
|
||
| module Roast | ||
| module DSL | ||
| # Contains the cogs already executed in this run. | ||
| class CogExecutionContext | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be called something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll drop a rename PR on top of this |
||
| # Raises if you access a cog in an execution block that hasn't already been run. | ||
| class IncompleteCogExecutionAccessError < StandardError; end | ||
|
|
||
| def initialize(cogs, bound_names) | ||
| @cogs = cogs | ||
| bind_cog_methods(bound_names) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def bind_cog_methods(bound_names) | ||
| bound_names.map do |name| | ||
| define_singleton_method(name.to_sym, ->(name) do | ||
| @cogs[name].tap do |cog| | ||
| raise IncompleteCogExecutionAccessError unless cog.ran? | ||
| end.output | ||
| end) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i love this!