Skip to content

Commit

Permalink
add discover, which is a generator specific to workflow and should
Browse files Browse the repository at this point in the history
probably be somewhere else?
  • Loading branch information
apotonick committed Apr 23, 2024
1 parent a8883c8 commit 3da453b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/trailblazer/pro/rails/generator/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "trailblazer/workflow"

module Trailblazer
module Pro
# FIXME: test me!
class Discover < ::Rails::Generators::Base # FIXME: should be trailblazer:workflow:discover
# source_root File.expand_path("templates", __dir__)
argument :json_filename, type: :string, banner: "Filename of imported diagram.json"
class_option :namespace, type: :string, banner: "Collaboration namespace constant"
class_option :start_lane, type: :string, default: nil, banner: "Start activity"#, aliases: :start
# argument :filename, type: :string, default: nil, banner: "Filename where to serialize the iterations"
class_option :test, type: :string, default: nil, banner: "Filename for a spec"
class_option :failure, type: :string, default: nil, banner: "Instruct discovery to run a segment of path again, with a failing task", repeatable: true

def discover_from_schema_name
namespace = @options[:namespace]
start_lane = @options[:start_lane]
failure_options = @options[:failure]
test_filename = @options[:test]


# Posting::Collaboration::Schema => schema
# Posting::Collaboration => namespace
#
# posting/collaboration/generated/posting-v1.json
# posting/collaboration/generated/iteration_set.json
# posting/collaboration/generated/state_table.rb
# posting/collaboration/state_guards.rb # what you change

states = Trailblazer::Workflow::Task::Discover.(
json_filename: json_filename,
namespace: namespace,
target_dir: File.join("app/concepts", namespace.to_s.underscore),
start_lane: start_lane,
test_filename: test_filename,
dsl_options_for_run_multiple_times: Utils.translate_failure_option_for_run_multiple_times(failure_options),
)

puts "Discovered #{states.size} states."
end

module Utils
module_function

# --failure UI:Create,lifecycle:Create --failure blubb,yeah
def translate_failure_option_for_run_multiple_times(failure_args)
return {} unless failure_args

failure_args.collect do |lane_task|
lane_tuple, task_tuple = lane_task.split(",")

[
lane_tuple.split(":"),
{ctx_merge: {task_tuple.to_sym => Trailblazer::Activity::Left}, config_payload: {outcome: :failure}}
]
end.to_h
end
end
end # Discover
end
end
48 changes: 48 additions & 0 deletions test/dummies/uninitialized/test/discover_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require_relative "test_helper"

class DiscoverTest < Minitest::Spec
include ExecuteInRails

let(:api_key) { "tpka_909ae987_c834_43e4_9869_2eefd2aa9bcf" }
let(:trailblazer_pro_host) { "https://testbackend-pro.trb.to" }

after do
Dir.chdir("test/dummies/uninitialized") do
`rm app/concepts/posting/collaboration/state_guards.rb` #if File.exist?("tmp/trb-pro")
`rm -r app/concepts/posting/collaboration/generated` #if File.exist?("tmp/trb-pro")
end
end

it "retrieve document" do
Dir.chdir("test/dummies/uninitialized") do
cli = File.popen({"BUNDLE_GEMFILE" => "../Gemfile"}, "bin/rails.rb g trailblazer:pro:install", "r+")
cli.write "#{api_key}\n"
cli.close

json = File.read("tmp/trb-pro/session")
json = json.sub("https://pro.trailblazer.to", trailblazer_pro_host)
File.write("tmp/trb-pro/session", json) # FIXME: what do you mean? This is super clean :D

# Import.
cli = File.popen({"BUNDLE_GEMFILE" => "../Gemfile"}, "bin/rails.rb g trailblazer:pro:import 79d163 /tmp/79d163.json", "r+")
assert_equal read_from_cli(cli)[1], %(Diagram 79d163 successfully imported to /tmp/79d163.json.\n)

# TODO: now, we should infer/generate {app/concepts/posting/collaboration/schema.rb}.

cli = File.popen({"BUNDLE_GEMFILE" => "../Gemfile"}, %(bin/rails.rb g trailblazer:pro:discover \
/tmp/79d163.json \
--namespace Posting::Collaboration \
--test test/posting_collaboration_test.rb \
--start_lane "lifecycle" \
--failure lifecycle:Create,engine:Instantiate --failure lifecycle:Update,engine:Modify \
), "r+")

assert_equal read_from_cli(cli)[1], %(Discovered 6 states.\n)
cli.close

assert File.exist?("app/concepts/posting/collaboration/generated/iteration_set.json")
assert File.exist?("app/concepts/posting/collaboration/generated/state_table.rb")
assert File.exist?("app/concepts/posting/collaboration/state_guards.rb")
end
end
end

0 comments on commit 3da453b

Please sign in to comment.