Sha256: 171c3eeed91ee358ddbcf63cd47a882e3059803b7a05f20c684c68985cc137a3

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby

require 'optparse'

write, filename, read_stdin, code = nil

OptionParser.new do |parser|
  parser.banner = "Usage: #{$0} FILENAME... --write"

  parser.on("-w", "--[no-]write", "Write file") do |value|
    write = value
  end

  parser.on("--stdin-filename FILEPATH", "Set the stdin filename (implies --stdin)") do |value|
    filename = value
    read_stdin = true
  end

  parser.on("--[no-]stdin", "Read the file from stdin") do |value|
    if read_stdin == true && value == false
      abort "Can't set stdin filename and not use stdin at the same time"
    end

    read_stdin = value
    filename ||= '-'
  end

  parser.on("-h", "--help", "Prints this help") do
    puts parser
    exit
  end
end.parse!(ARGV)

abort "Can't read both stdin and a list of files" if read_stdin && !ARGV.empty?

# If multiple files are provided assume `--write` and
# execute on each of them.
if ARGV.size > 1
  ARGV.each do
    warn "==> Formatting #{_1}..."
    system __FILE__, _1, *[
      ('--write' if write)
    ].compact or exit(1)
  end
  exit
end

require 'erb/formatter'

filename ||= ARGV.first
code = read_stdin ? $stdin.read : File.read(filename)
ignore = ERB::Formatter::IgnoreList.new

if ignore.should_ignore_file? filename
  print code unless write
else
  html = ERB::Formatter.format(code, filename: filename)

  if write
    File.write(filename, html)
  else
    puts html
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
erb-formatter-0.1.1 exe/erb-format
erb-formatter-0.1.0 exe/erb-format