Module: Wisco::Commands::Fixtures

Defined in:
lib/wisco/commands/fixtures.rb

Constant Summary collapse

SENTINEL =
'# Remove this comment before updating. Files that include this line will be overwritten.'

Class Method Summary collapse

Class Method Details

.call_exec(path:, connector:, connection:, output:, debug: false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wisco/commands/fixtures.rb', line 123

def call_exec(path:, connector:, connection:, output:, debug: false)
  options = { connector: connector, output: output }
  options[:connection] = connection if connection

  if debug
    warn "[fixtures] path:       #{path}"
    warn "[fixtures] connector:  #{connector}"
    warn "[fixtures] connection: #{connection.inspect}"
    warn "[fixtures] output:     #{output}"
  end

  cmd = Workato::CLI::ExecCommand.new(path: path, options: options)
  begin
    cmd.call
  rescue StandardError => e
    warn "  Warning: #{path} failed — #{e.message}"
    return
  end

  return unless File.exist?(output)

  pretty = JSON.pretty_generate(JSON.parse(File.read(output)))
  File.write(output, pretty + "\n")
end

.generate_execute_input(input_fields_file, fixtures_dir, overwrite: false, debug: false) ⇒ Object

Reads input_fields.json, builds a template hash, writes execute_input.json. The file is prefixed with SENTINEL so it is identifiable as an unedited template. Overwrite rules:

- File absent                  -> write
- File present, sentinel L1    -> overwrite (still a template)
- File present, no sentinel    -> skip (user-edited); force with --overwrite


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wisco/commands/fixtures.rb', line 73

def generate_execute_input(input_fields_file, fixtures_dir, overwrite: false, debug: false)
  return unless File.exist?(input_fields_file)

  fields = JSON.parse(File.read(input_fields_file))
  return if fields.empty?

  output_file = File.join(fixtures_dir, 'execute_input.json')

  if File.exist?(output_file)
    first_line = begin
      File.open(output_file, &:readline).chomp
    rescue StandardError
      ''
    end
    has_sentinel = (first_line == SENTINEL)

    unless has_sentinel || overwrite
      puts "  Skipped (user-edited): #{output_file}" if debug
      return
    end
  end

  template = schema_to_template(fields)
  content  = "#{SENTINEL}\n#{JSON.pretty_generate(template)}\n"
  File.write(output_file, content)
  puts "  Written: #{output_file}"
end

.run(path_arg, target_dir, overwrite: false, debug: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wisco/commands/fixtures.rb', line 15

def run(path_arg, target_dir, overwrite: false, debug: false)
  target_dir = File.expand_path(target_dir)
  config_path = Wisco.config_path(target_dir)

  unless File.exist?(config_path)
    warn "Error: No #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} found in #{target_dir}."
    warn "       Run '#{Wisco::CLI_NAME} init' first."
    exit 1
  end

  config = Wisco::Config.load_config(config_path)
  connector_path = config.dig('connector', 'path')
  connector_file = config.dig('connector', 'file')

  if connector_path.nil? || connector_file.nil?
    warn "Error: #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} is missing connector path/file. Run '#{Wisco::CLI_NAME} init' again."
    exit 1
  end

  connector_full_path = File.join(connector_path, connector_file)
  connection = config['connection']

  connector = Wisco::Connector.load_connector_from_config(target_dir)
  pairs = Wisco::PathUtils.parse_path(path_arg, connector)

  pairs.each do |section, key|
    puts "Processing #{section}.#{key}"
    fixtures_dir = File.join(target_dir, 'fixtures', section, key)
    FileUtils.mkdir_p(fixtures_dir)

    input_fields_file = File.join(fixtures_dir, 'input_fields.json')
    call_exec(
      path: "#{section}.#{key}.input_fields",
      connector: connector_full_path,
      connection: connection,
      output: input_fields_file,
      debug: debug
    )

    generate_execute_input(input_fields_file, fixtures_dir, overwrite: overwrite, debug: debug)

    output_fields_file = File.join(fixtures_dir, 'output_fields.json')
    call_exec(
      path: "#{section}.#{key}.output_fields",
      connector: connector_full_path,
      connection: connection,
      output: output_fields_file,
      debug: debug
    )
  end
end

.schema_to_template(fields) ⇒ Object

Recursively converts a Workato schema array into a template hash. Scalars become “<type_value_required|optional>” placeholder strings. Objects expand into a nested hash via their properties. Arrays expand into a single-element array via their properties.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wisco/commands/fixtures.rb', line 105

def schema_to_template(fields)
  fields.each_with_object({}) do |field, hash|
    name     = field['name']
    type     = field['type'] || 'string'
    optional = field.fetch('optional', true)
    req_str  = optional ? 'optional' : 'required'

    hash[name] = case type
                 when 'object'
                   schema_to_template(field['properties'] || [])
                 when 'array'
                   [schema_to_template(field['properties'] || [])]
                 else
                   "<#{type}_value_#{req_str}>"
                 end
  end
end