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.'- RB_SENTINEL =
'# WISCO_SKIP'- RB_TEMPLATE =
<<~RUBY # WISCO_SKIP # Remove the WISCO_SKIP line above once this script is ready to run. # # The last expression in this file becomes the input passed to the connector item. # Helper methods available inside the script: # call_method(:name, *args) — invoke a methods: entry on the connector # call_pick_list(:name, *args) — invoke a pick_lists: entry on the connector # connection — Hash of decrypted settings.yaml(.enc) # # Example: # require 'securerandom' # { # order_number: "ORD-\#{SecureRandom.hex(4).upcase}", # created_at: Time.now.iso8601 # } { # TODO: fill in fields } RUBY
Class Method Summary collapse
- .build_config_fields_comments(fields) ⇒ Object
- .call_exec(path:, connector:, connection:, output:, config_fields: nil, debug: false) ⇒ Object
- .config_fields_ready?(path) ⇒ Boolean
-
.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.
-
.generate_execute_input_rb(fixtures_dir, overwrite: false) ⇒ Object
Writes an ‘execute_input.rb` template into `fixtures_dir`.
- .process_method(key, connector, fixtures_dir, overwrite: false) ⇒ Object
- .process_pick_list(key, connector, fixtures_dir, overwrite: false) ⇒ Object
- .read_json_without_sentinel(path) ⇒ Object
- .run(path_arg, target_dir, overwrite: false, ruby: false, debug: false) ⇒ Object
-
.schema_to_template(fields) ⇒ Object
Recursively converts a Workato schema array into a template hash.
- .stringify_keys_deep(obj) ⇒ Object
- .write_config_fields_template(config_fields_array, output_file) ⇒ Object
Class Method Details
.build_config_fields_comments(fields) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/wisco/commands/fixtures.rb', line 314 def build_config_fields_comments(fields) return '' if fields.empty? lines = ['# Config fields:'] fields.each do |field| parts = [] parts << "label: #{field['label'].inspect}" if field['label'] parts << "hint: #{field['hint'].inspect}" if field['hint'] parts << "type: #{field['type'] || 'string'}" parts << (field.fetch('optional', true) ? 'optional' : 'required') parts << "control_type: #{field['control_type']}" if field['control_type'] parts << "pick_list: #{field['pick_list']}" if field['pick_list'] lines << "# #{field['name']}: #{parts.join(', ')}" end lines << '#' lines.map { |l| "#{l}\n" }.join end |
.call_exec(path:, connector:, connection:, output:, config_fields: nil, debug: false) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/wisco/commands/fixtures.rb', line 340 def call_exec(path:, connector:, connection:, output:, config_fields: nil, debug: false) = { connector: connector, output: output } [:connection] = connection if connection [:config_fields] = config_fields if config_fields 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: ) begin cmd.call rescue StandardError => e warn " Warning: #{path} failed — #{e.}" return end return unless File.exist?(output) pretty = JSON.pretty_generate(JSON.parse(File.read(output))) File.write(output, pretty + "\n") end |
.config_fields_ready?(path) ⇒ Boolean
295 296 297 298 299 300 301 302 303 304 |
# File 'lib/wisco/commands/fixtures.rb', line 295 def config_fields_ready?(path) return false unless File.exist?(path) first_line = begin File.open(path, &:readline).chomp rescue StandardError '' end first_line != SENTINEL 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
126 127 128 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 |
# File 'lib/wisco/commands/fixtures.rb', line 126 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) # Merge config_fields.json if present — its keys are prepended to the template cf_file = File.join(fixtures_dir, 'config_fields.json') if File.exist?(cf_file) cf_template = read_json_without_sentinel(cf_file) template = cf_template.merge(template) end content = "#{SENTINEL}\n#{JSON.pretty_generate(template)}\n" File.write(output_file, content) puts " Written: #{output_file}" end |
.generate_execute_input_rb(fixtures_dir, overwrite: false) ⇒ Object
Writes an ‘execute_input.rb` template into `fixtures_dir`. The template is prefixed with `# WISCO_SKIP` so it is identifiable as unedited and will be skipped by `wisco exec` until the user removes that line.
Overwrite rules mirror generate_execute_input:
- File absent -> write
- File present, # WISCO_SKIP on L1 -> overwrite (still a template)
- File present, no sentinel -> skip (user-edited); force with --overwrite
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/wisco/commands/fixtures.rb', line 170 def generate_execute_input_rb(fixtures_dir, overwrite: false) output_file = File.join(fixtures_dir, 'execute_input.rb') if File.exist?(output_file) first_line = begin File.open(output_file, &:readline).chomp rescue StandardError '' end has_sentinel = first_line.strip == RB_SENTINEL unless has_sentinel || overwrite puts " Skipped (user-edited): #{output_file}" return end end File.write(output_file, RB_TEMPLATE) puts " Written: #{output_file}" end |
.process_method(key, connector, fixtures_dir, overwrite: false) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/wisco/commands/fixtures.rb', line 213 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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/wisco/commands/fixtures.rb', line 249 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 |
.read_json_without_sentinel(path) ⇒ Object
287 288 289 290 291 292 293 |
# File 'lib/wisco/commands/fixtures.rb', line 287 def read_json_without_sentinel(path) lines = File.readlines(path) lines.shift while lines.first&.lstrip&.start_with?('#') JSON.parse(lines.join) rescue StandardError {} end |
.run(path_arg, target_dir, overwrite: false, ruby: false, debug: false) ⇒ Object
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 72 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/wisco/commands/fixtures.rb', line 38 def run(path_arg, target_dir, overwrite: false, ruby: false, debug: false) target_dir = File.(target_dir) config_path = Wisco.config_path(target_dir) unless File.exist?(config_path) Wisco::TerminalOutput.emit_error "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? Wisco::TerminalOutput.emit_error "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) generate_execute_input_rb(fixtures_dir, overwrite: overwrite) if ruby elsif section == 'methods' process_method(key, connector, fixtures_dir, overwrite: overwrite) generate_execute_input_rb(fixtures_dir, overwrite: overwrite) if ruby else # ── config_fields pre-check ────────────────────────────────────── item = connector[section.to_sym]&.[](key.to_sym) raw_cf = item&.[](:config_fields) cf_file = File.join(fixtures_dir, 'config_fields.json') cf_opt = nil # set to cf_file once the user has filled it in if raw_cf.present? if config_fields_ready?(cf_file) cf_opt = cf_file else write_config_fields_template(raw_cf, cf_file) warn " Written: #{cf_file}" Wisco::TerminalOutput.emit_warning(" Action required: fill in config_fields.json, then re-run fixtures.") next end end # ── input_fields ───────────────────────────────────────────────── 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, config_fields: cf_opt, debug: debug ) generate_execute_input(input_fields_file, fixtures_dir, overwrite: overwrite, debug: debug) generate_execute_input_rb(fixtures_dir, overwrite: overwrite) if ruby # ── output_fields ──────────────────────────────────────────────── 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, config_fields: cf_opt, 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.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/wisco/commands/fixtures.rb', line 195 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 |
.stringify_keys_deep(obj) ⇒ Object
332 333 334 335 336 337 338 |
# File 'lib/wisco/commands/fixtures.rb', line 332 def stringify_keys_deep(obj) case obj when Hash then obj.transform_keys(&:to_s).transform_values { |v| stringify_keys_deep(v) } when Array then obj.map { |e| stringify_keys_deep(e) } else obj end end |
.write_config_fields_template(config_fields_array, output_file) ⇒ Object
306 307 308 309 310 311 312 |
# File 'lib/wisco/commands/fixtures.rb', line 306 def write_config_fields_template(config_fields_array, output_file) stringified = stringify_keys_deep(config_fields_array) template = schema_to_template(stringified) comments = build_config_fields_comments(stringified) content = "#{SENTINEL}\n#{comments}#{JSON.pretty_generate(template)}\n" File.write(output_file, content) end |