module PMLCode::CLI

Constants

DEFAULTS
DEFAULT_PATTERN
REQUIRED_PATTERN_CAPTURES
USAGE

Public Class Methods

run(args) click to toggle source
# File lib/pmlcode/cli.rb, line 79
def self.run(args)
  options = OpenStruct.new(DEFAULTS)
  # Parse arguments
  parser = build_parser(options)
  parser.parse!(args)

  sources = prepare(options, parser, args)

  sources.each do |source|
    options.source = source
    options.output = File.dirname(source.path)
    update!(options)
  end

end

Private Class Methods

build_parser(options) click to toggle source
# File lib/pmlcode/cli.rb, line 134
def self.build_parser(options)
  OptionParser.new do |opts|
    opts.banner = USAGE

    opts.on("-t [TYPE]", "--type", [:sparse, :full], "Export type (sparse, full; default: #{options.type})") do |value|
      options.type = value
    end

    opts.on("-a [PATH]", "--application-directory", "Application directory (default: #{options.app || "NONE"})") do |value|
      options.app = value
    end

    opts.on("-p [PATTERN]", "--pattern", "Pattern (default: \"#{options.pattern.source}\")") do |value|
      options.pattern = Regexp.new(value)
    end

    opts.on('-c', '--content', "Show content") do
      options.content = true
    end

    opts.on("-V", "--verbose", "Verbose output (Only useful with --content)") do
      options.verbose = true
    end

    opts.on('-x', '--dry-run', "Dry run (do not write files)") do
      options.dry_run = true
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

  end
end
prepare(options, parser, args) click to toggle source
# File lib/pmlcode/cli.rb, line 107
def self.prepare(options, parser, args)
  if args.empty?
    $stderr.puts "No PML files given."
    $stderr.puts parser
    exit
  end
  unless options.app
    $stderr.puts "No --application-directory given."
    $stderr.puts parser
    exit
  end
  unless REQUIRED_PATTERN_CAPTURES.all? { |cap| options.pattern.named_captures.key?(cap) }
    $stderr.puts "Pattern does not define one or more required named captures: #{REQUIRED_NAMED_CAPTURES}"
    $stderr.puts "Check your use of --pattern or the PMLCODE_PATTERN environment variable"
    $stderr.puts parser
    exit
  end
  sources = args.map { |pml| PMLCode::Source.parse(pml) }
  missing = sources.select(&:missing?)
  unless missing.empty?
    $stderr.puts "PML sources #{missing} not found"
    puts parser
    exit
  end
  sources
end
update!(options) click to toggle source
# File lib/pmlcode/cli.rb, line 97
def self.update!(options)
  updater = PMLCode::Updater.find(options)
  unless updater
    $stderr.puts "No updater found. --type '#{options.type}' may not be supported."
    $stderr.puts parser
    exit
  end
  updater.run(options)
end