Module: Browserctl::Workflow::FlowWrapper

Defined in:
lib/browserctl/workflow/flow_wrapper.rb

Overview

Renders a ‘Browserctl.flow` definition that wraps a promoted workflow. The flow becomes a globally-registered, parameterised handle that runs the underlying workflow via `Runner#run_workflow`. Params are inferred from the workflow’s ‘param_defs` so callers see the same surface area they would on the workflow itself.

Wrapping (rather than translating step-by-step) keeps the workflow as the single source of truth: edits to the workflow file flow through to the wrapper without regeneration.

Class Method Summary collapse

Class Method Details

.render(defn) ⇒ String

Returns Ruby source for a flow file.

Parameters:

Returns:

  • (String)

    Ruby source for a flow file



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/browserctl/workflow/flow_wrapper.rb', line 30

def render(defn)
  params = defn.param_defs.values.map { |p| render_param(p) }.join("\n")
  desc   = defn.description || "Promoted from workflow '#{defn.name}'"
  <<~RUBY
    # frozen_string_literal: true

    require "browserctl/flow"
    require "browserctl/runner"

    # Auto-generated flow wrapper for workflow '#{defn.name}'.
    # Edit the underlying workflow file rather than this wrapper.
    Browserctl.flow(#{defn.name.inspect}) do
      version "1.0.0"
      requires_browserctl "0.11.0"
      desc #{desc.inspect}

    #{params.gsub(/^/, '  ') unless params.empty?}

      step("run workflow #{defn.name}") do
        Browserctl::Runner.new.run_workflow(#{defn.name.inspect}, **params)
      end
    end
  RUBY
end

.render_param(param) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/browserctl/workflow/flow_wrapper.rb', line 70

def render_param(param)
  opts = []
  opts << "required: true" if param.required
  opts << "secret: true"   if param.secret && !param.secret_ref
  opts << "secret_ref: #{param.secret_ref.inspect}" if param.secret_ref
  opts << "default: #{param.default.inspect}" unless param.default.nil?
  suffix = opts.empty? ? "" : ", #{opts.join(', ')}"
  "param :#{param.name}#{suffix}"
end

.target_dirObject



20
21
22
# File 'lib/browserctl/workflow/flow_wrapper.rb', line 20

def target_dir
  File.join(Browserctl::BROWSERCTL_DIR, "flows")
end

.target_path(name) ⇒ Object



24
25
26
# File 'lib/browserctl/workflow/flow_wrapper.rb', line 24

def target_path(name)
  File.join(target_dir, "#{name}.rb")
end

.write(defn, overwrite: true, dir: nil) ⇒ String

Returns path written.

Parameters:

  • defn (Browserctl::WorkflowDefinition)
  • overwrite (Boolean) (defaults to: true)
  • dir (String, nil) (defaults to: nil)

    override target dir (testing)

Returns:

  • (String)

    path written



59
60
61
62
63
64
65
66
67
68
# File 'lib/browserctl/workflow/flow_wrapper.rb', line 59

def write(defn, overwrite: true, dir: nil)
  path = dir ? File.join(dir, "#{defn.name}.rb") : target_path(defn.name)
  if File.exist?(path) && !overwrite
    raise Browserctl::WorkflowError, "flow wrapper already exists at #{path} (pass overwrite: true to replace)"
  end

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, render(defn))
  path
end