Sha256: 63ebd46fdbbb514a6872f569922442745329ebf8e5653fc33e367ede2ef71e30

Contents?: true

Size: 992 Bytes

Versions: 7

Compression:

Stored size: 992 Bytes

Contents

#!/usr/bin/env ruby

require 'pathname'
require 'json'
require 'yaml'

root = Pathname.new(__FILE__).dirname.join('..')

document_dirs = [root.join('documents'), root.join('test/documents')]
documents = document_dirs.map { |d| Pathname.glob(d.join('**/*')) }.inject([], &:+)
json_documents = documents.select { |p| p.file? && !['.yml', '.yaml'].include?(p.extname) }

json_documents.each do |file|
  begin
    json_contents = JSON.parse(file.read)
    yaml = YAML.dump(json_contents, line_width: -1)
    yaml_filepath = Pathname.new(file.to_path.chomp('.json') + '.yml')
    if yaml_filepath.exist?
      contents = yaml_filepath.read
      if contents == yaml
        puts "#{yaml_filepath.to_path} unchanged"
      else
        yaml_filepath.write(yaml)
        puts "#{yaml_filepath.to_path} changed; updated"
      end
    else
      yaml_filepath.write(yaml)
      puts "#{yaml_filepath.to_path} created"
    end
  rescue JSON::ParserError
    puts "#{file.to_path}: not json"
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
scorpio-0.5.0 bin/documents_to_yml.rb
scorpio-0.4.6 bin/documents_to_yml.rb
scorpio-0.4.5 bin/documents_to_yml.rb
scorpio-0.4.4 bin/documents_to_yml.rb
scorpio-0.4.3 bin/documents_to_yml.rb
scorpio-0.4.2 bin/documents_to_yml.rb
scorpio-0.4.1 bin/documents_to_yml.rb