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



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/wisco/commands/fixtures.rb', line 203

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


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wisco/commands/fixtures.rb', line 79

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

.process_method(key, connector, fixtures_dir, overwrite: false) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/wisco/commands/fixtures.rb', line 129

def process_method(key, connector, fixtures_dir, overwrite: false)
  method_fn = connector[:methods]&.[](key.to_sym)

  unless method_fn.respond_to?(:parameters)
    warn "  Warning: method '#{key}' is not callable — skipping."
    return
  end

  params = method_fn.parameters   # all params are real inputs (no connection to drop)

  if params.empty?
    puts "  No input required: #{fixtures_dir}"
    return
  end

  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
    unless first_line == SENTINEL || overwrite
      puts "  Skipped (user-edited): #{output_file}"
      return
    end
  end

  # Positional array — each element is the param name as a placeholder string
  template = params.map { |(_, name)| name.to_s }
  content  = "#{SENTINEL}\n#{JSON.pretty_generate(template)}\n"
  File.write(output_file, content)
  puts "  Written: #{output_file}"
end

.process_pick_list(key, connector, fixtures_dir, overwrite: false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/wisco/commands/fixtures.rb', line 165

def process_pick_list(key, connector, fixtures_dir, overwrite: false)
  pick_list_fn = connector[:pick_lists]&.[](key.to_sym)

  unless pick_list_fn.respond_to?(:parameters)
    warn "  Warning: pick_list '#{key}' is not callable — skipping."
    return
  end

  # Drop the first parameter (connection); remaining params become input fields
  input_params = pick_list_fn.parameters.drop(1)

  if input_params.empty?
    puts "  No input required: #{fixtures_dir}"
    return
  end

  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
    unless first_line == SENTINEL || overwrite
      puts "  Skipped (user-edited): #{output_file}"
      return
    end
  end

  template = input_params.each_with_object({}) do |(_, name), hash|
    hash[name.to_s] = '<string_value_required>'
  end
  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
66
67
68
69
70
71
# 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)

    if section == 'pick_lists'
      process_pick_list(key, connector, fixtures_dir, overwrite: overwrite)
    elsif section == 'methods'
      process_method(key, connector, fixtures_dir, overwrite: overwrite)
    else
      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
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.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wisco/commands/fixtures.rb', line 111

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