#!/usr/bin/env ruby require 'optparse' require(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd'))) options = {} commands = { combine: OptionParser.new do |opts| opts.banner = "prmd combine [options] " opts.on("-m", "--meta meta.json", "Set defaults for schemata") do |m| options[:meta] = m end end, doc: OptionParser.new do |opts| opts.banner = "prmd doc [options] " opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p| options[:prepend] = p end end, init: OptionParser.new do |opts| opts.banner = "prmd init [options] " opts.on("-y", "--yaml", "Generate YAML") do |y| options[:yaml] = y end end, render: OptionParser.new do |opts| opts.banner = "prmd doc [options] " opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p| options[:prepend] = p end opts.on("-t", "--template schemata.erb", String, "Use alternate template") do |t| options[:template] = t end end, verify: OptionParser.new do |opts| opts.banner = "prmd verify [options] " end } help_text = commands.values.map do |command| " #{command.banner}" end.join("\n") global = OptionParser.new do |opts| opts.banner = "Usage: prmd [options] [command [options]]" opts.separator "\nAvailable options:" opts.on("--version", "Return version") do |opts| puts "prmd #{Prmd::VERSION}" exit(0) end opts.separator "\nAvailable commands:" opts.separator help_text end if ARGV.empty? puts global exit(1) end global.order! command = ARGV.shift.to_sym option = commands[command] if option.nil? puts global exit(1) end if ARGV.empty? && $stdin.tty? puts option exit(1) end option.order! case command when :combine puts Prmd.combine(ARGV[0], options).to_s when :doc data = unless $stdin.tty? $stdin.read else File.read(ARGV[0]) end schema = Prmd::Schema.new(JSON.parse(data)) options[:template] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'prmd', 'templates', 'schema.erb')) puts Prmd.render(schema, options) when :init puts Prmd.init(ARGV[0], options) when :render data = unless $stdin.tty? $stdin.read else File.read(ARGV[0]) end schema = Prmd::Schema.new(JSON.parse(data)) puts Prmd.render(schema, options) when :verify data, errors = '', [] unless $stdin.tty? data = $stdin.read errors.concat(Prmd.verify(JSON.parse(data))) else data = JSON.parse(File.read(ARGV[0])) Prmd.verify(data).each do |error| errors << "#{ARGV[0]}: #{error}" end end errors.each do |error| $stderr.puts(error) end exit(1) unless errors.empty? puts(data) unless $stdout.tty? end