Sha256: b19b1f5c48dab76217dc501eabaa3e13ab03ccca0816e1349d98019cce33f5c3
Contents?: true
Size: 1.19 KB
Versions: 12
Compression:
Stored size: 1.19 KB
Contents
#!/usr/bin/env ruby # frozen_string_literal: true require 'xmlsimple' require 'yaml' require 'fileutils' $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'soaspec' include Soaspec::ExeHelpers default_output_file = 'output.yml' # For all keys in a Hash, convert Camelcase to underscore separated def convert_hash_keys(value) case value when Array value.map { |v| convert_hash_keys(v) } when Hash Hash[value.map { |k, v| [k.snakecase, convert_hash_keys(v)] }] else value end end # Remove arrays created as another string def clean_up_yaml(yaml_string) yaml_string = yaml_string.gsub(/\R+(\s*)-/, '').gsub(/{}/, "''") # Remove arrays, {} -> '' # Insert new line where there are 2 ':' on 1 line. Issue from first gsub yaml_string.gsub(/:(\s)(\w*):/) { |s| s.insert(1, "\n") } end if ARGV[0] warn "Using '#{default_output_file}' as default output file since no 2nd argument passed" unless ARGV[1] hash = XmlSimple.xml_in(ARGV[0]) converted = convert_hash_keys hash yaml_file = clean_up_yaml(converted.to_yaml) create_file(filename: ARGV[1] || default_output_file, content: yaml_file) else puts 'usage: xml_to_yaml_file [input.xml] [output.yml] ' end
Version data entries
12 entries across 12 versions & 1 rubygems