Sha256: 406f48953b937fb899313bc8c06b0206bcf051e9ef1a4f79fc9022c5c2f93fed

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 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("--[no-]debug", "Enable debug mode") do |value|
    $DEBUG = value
  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 |arg|
    warn "==> Formatting #{arg}..."
    system __FILE__, arg, *[
      ('--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

3 entries across 3 versions & 1 rubygems

Version Path
erb-formatter-0.2.2 exe/erb-format
erb-formatter-0.2.1 exe/erb-format
erb-formatter-0.2.0 exe/erb-format