Skip to content

Commit

Permalink
we can generate the Schema from the PRO JSON.
Browse files Browse the repository at this point in the history
wow, we barely have to write a line of code now.
  • Loading branch information
apotonick committed May 3, 2024
1 parent c9555b0 commit add1bb4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/trailblazer/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Workflow
require "trailblazer/workflow/introspect/state_table"
require "trailblazer/workflow/introspect/event_table"

require "trailblazer/workflow/generate/schema"
require "trailblazer/workflow/generate/state_table"
require "trailblazer/workflow/generate/state_guards"

Expand Down
8 changes: 5 additions & 3 deletions lib/trailblazer/workflow/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def call(json_filename:)

# TODO: add outputs

_schema = Trailblazer::Workflow::Collaboration(json_file: json_filename, lanes: stubbed_lanes) # FIXME: we're re-parsing JSON.
schema = Trailblazer::Workflow::Collaboration(json_file: json_filename, lanes: stubbed_lanes) # FIXME: we're re-parsing JSON.

return schema, ctx
end
end

Expand All @@ -58,7 +60,7 @@ def call(json_filename:, start_lane:, dsl_options_for_run_multiple_times: {})
# imply we start from a public resume and discover the path?
# we could save work on {run_multiple_times} with this.

collaboration = Stub.(json_filename: json_filename)
collaboration, parsed_structure = Stub.(json_filename: json_filename)

initial_lane_positions = Collaboration::Synchronous.initial_lane_positions(collaboration.to_h[:lanes])
initial_lane_positions = Collaboration::Positions.new(initial_lane_positions.collect { |activity, task| Trailblazer::Workflow::Collaboration::Position.new(activity, task) }) # FIXME: initial_lane_positions should return {Collaboration::Positions}
Expand Down Expand Up @@ -185,7 +187,7 @@ def call(json_filename:, start_lane:, dsl_options_for_run_multiple_times: {})
end
end

return discovered_states, collaboration
return discovered_states, collaboration, parsed_structure
end

module DSL
Expand Down
45 changes: 45 additions & 0 deletions lib/trailblazer/workflow/generate/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Trailblazer
module Workflow
class Generate
class Schema < Trailblazer::Activity::Railway
step :render

def render(ctx, parsed_structure:, namespace:, json_filename:, **options)
lanes_cfg = parsed_structure[:lane_hints]

lanes = parsed_structure[:intermediates].collect do |json_id, intermediate|
tasks = intermediate.wiring.find_all { |task_ref, _| task_ref.data[:type] == :task }

implementation = tasks.collect do |task_ref, _|
%( "#{task_ref.data[:label]}" => Trailblazer::Activity::Railway.Subprocess(#{namespace}::#{task_ref.data[:label]}),)
end.join("\n")

icon, label, _ = json_id.split(".") # FIXME: abstract.

# lane_task_2_wiring = intermediate.wiring.find_all { |task_ref, _| task_ref.data[:type] == :task }
%( "#{json_id}" => {
label: "#{label}",
icon: "#{icon}",
implementation: {
#{implementation}
}
},)
end.join("\n")

snippet = %(module #{namespace}
Schema = Trailblazer::Workflow.Collaboration(
json_file: "#{json_filename}",
lanes: {
#{lanes}
}, # :lanes
state_guards: #{namespace}::StateGuards::Decider,
)
end
)

ctx[:snippet] = snippet
end
end
end
end
end
27 changes: 24 additions & 3 deletions lib/trailblazer/workflow/task/discover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ module Discover
module_function

# DISCUSS: what about running this before we have a schema?
def call(namespace:, target_dir:, test_filename:, **discovery_options)
def call(namespace:, target_dir:, test_filename:, json_filename:, **discovery_options)

filepath = Filepath.new(target_dir)

schema_filename = filepath.("schema.rb")
state_guard_filename = filepath.("state_guards.rb")
state_table_filename = filepath.("generated/state_table.rb")
iteration_set_filename = filepath.("generated/iteration_set.json")

states, schema = Trailblazer::Workflow::Discovery.(
states, schema, parsed_structure = Trailblazer::Workflow::Discovery.(
json_filename: json_filename,
**discovery_options
# run_multiple_times: run_multiple_times,
)
Expand Down Expand Up @@ -42,6 +44,15 @@ def call(namespace:, target_dir:, test_filename:, **discovery_options)
state_table_filename: state_table_filename,
namespace: namespace,
)

Produce::Schema.(
parsed_structure: parsed_structure,
namespace: namespace,
filename: schema_filename,
json_filename: json_filename,
)

return states
end


Expand Down Expand Up @@ -118,7 +129,17 @@ def call(iteration_set, filename:, state_table_filename:, lanes_cfg:, namespace:
File.write(filename, ruby_output)
end
end
end

class Schema
def self.call(parsed_structure:, namespace:, filename:, json_filename:, **)
_, (ctx, _) = Trailblazer::Workflow::Generate::Schema.invoke([{parsed_structure: parsed_structure, namespace: namespace, json_filename: json_filename}, {}])
ruby_output = ctx[:snippet]

File.write(filename, ruby_output)
end
end

end # Produce

class Filepath
def initialize(base_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/workflow/test/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def assert_advance(event_label, test_plan:, schema:, ctx: {seq: []}, flow_option
# we're expecting an invalid transition.
if invalid
# assert_equal signal.to_h[:semantic], :failure # TODO: reuse endpoint/matcher
assert [:failure, :not_authorized].include?(signal.to_h[:semantic]) # TODO: reuse endpoint/matcher
assert [:failure, :not_authorized, :invalid_event].include?(signal.to_h[:semantic]) # TODO: reuse endpoint/matcher # FIXME: either invalid_event or not_authorized? depends on the level of endpoint.
return ctx # FIXME: test this!
end

Expand Down
55 changes: 55 additions & 0 deletions test/discover_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,61 @@ module Posting::Collaboration::Generated
"⏸︎ Update form♦Notify approver [110]" => {suspend_tuples: [["lifecycle", "suspend-Gateway_1wzosup"], ["UI", "suspend-Gateway_1g3fhu2"], ["editor", "suspend-gw-to-catch-before-Activity_05zip3u"]], catch_tuples: [["UI", "catch-before-Activity_1165bw9"], ["UI", "catch-before-Activity_1dt5di5"]]},
}
end
)

assert_equal File.read("#{TEST_ROOT}/app/concepts/posting/collaboration/schema.rb"), %(module Posting::Collaboration
Schema = Trailblazer::Workflow.Collaboration(
json_file: "../fixtures/v1/posting-v11.json",
lanes: {
"⛾.lifecycle.posting" => {
label: "lifecycle",
icon: "⛾",
implementation: {
"Update" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Update),
"Notify approver" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Notify approver),
"Approve" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Approve),
"Publish" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Publish),
"Archive" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Archive),
"Delete" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Delete),
"Revise" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Revise),
"Reject" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Reject),
"Create" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Create),
}
},
"☝.UI.blogger" => {
label: "UI",
icon: "☝",
implementation: {
"Update form" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Update form),
"Notify approver" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Notify approver),
"Publish" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Publish),
"Delete" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Delete),
"Delete? form" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Delete? form),
"Cancel" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Cancel),
"Revise" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Revise),
"Revise form" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Revise form),
"Update" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Update),
"Update form with errors" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Update form with errors),
"Revise form with errors" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Revise form with errors),
"Archive" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Archive),
"Create" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Create),
"Create form with errors" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Create form with errors),
"Create form" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Create form),
}
},
"☑.editor.reviewer" => {
label: "editor",
icon: "☑",
implementation: {
"Approve" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Approve),
"Reject" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Reject),
"Notify" => Trailblazer::Activity::Railway.Subprocess(Posting::Collaboration::Notify),
}
},
}, # :lanes
state_guards: Posting::Collaboration::StateGuards::Decider,
)
end
)

end
Expand Down
3 changes: 2 additions & 1 deletion test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def stub_task(lane_label, task_label)
end

it "Discovery.call" do
states, schema = Trailblazer::Workflow::Discovery.(
states, schema, parsed_structure = Trailblazer::Workflow::Discovery.(
json_filename: "test/fixtures/v1/posting-v11.json",
start_lane: "UI",

Expand All @@ -54,6 +54,7 @@ def stub_task(lane_label, task_label)
},
)

assert_equal parsed_structure[:intermediates].keys, ["⛾.lifecycle.posting", "☝.UI.blogger", "☑.editor.reviewer"]

# pp states
assert_equal states.size, 22
Expand Down

0 comments on commit add1bb4

Please sign in to comment.