Sha256: 4dc2c3652cc61b15468f8f53daedd5c1db6148bc1fa5e6508ddf6ced88659874

Contents?: true

Size: 1.71 KB

Versions: 13

Compression:

Stored size: 1.71 KB

Contents

#!/usr/bin/env ruby

require 'xmlsimple'
require 'yaml'
require 'fileutils'

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])

require 'soaspec'

include Soaspec::ExeHelpers

default_output_file = 'output.yml'

# Create file if not present. If present but different give warning
def create_file(options)
  filename = options[:filename]
  raise 'Need to pass filename' unless filename
  content = options[:content]
  raise 'Need to pass contents to insert into file' unless content
  if File.exist? filename
    old_content = File.read(filename)
    if old_content != content
      warn "!! #{filename} already exists and differs from template"
    end
  else
    File.open(filename, 'w') do |f|
      f.puts content
    end
    puts 'Created: ' + filename
  end
end

# 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| [underscore_key(k), 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

13 entries across 13 versions & 1 rubygems

Version Path
soaspec-0.0.36 exe/xml_to_yaml_file
soaspec-0.0.35 exe/xml_to_yaml_file
soaspec-0.0.34 exe/xml_to_yaml_file
soaspec-0.0.33 exe/xml_to_yaml_file
soaspec-0.0.32 exe/xml_to_yaml_file
soaspec-0.0.31 exe/xml_to_yaml_file
soaspec-0.0.30 exe/xml_to_yaml_file
soaspec-0.0.29 exe/xml_to_yaml_file
soaspec-0.0.28 exe/xml_to_yaml_file
soaspec-0.0.27 exe/xml_to_yaml_file
soaspec-0.0.26 exe/xml_to_yaml_file
soaspec-0.0.25 exe/xml_to_yaml_file
soaspec-0.0.24 exe/xml_to_yaml_file