Sha256: 1fef5f449a4a654cf0765b1af1018b77148a1cb6b8c88c5f3498ee7484144c89

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require "optparse"
require_relative "../lib/template-ruby"

options = { timeout: 0, profile: false, profiler: "text" }

OptionParser
  .new do |opts|
    opts.banner = "Usage: template [options]"

    opts.on("-v", "--version", "Version of template") do |_input|
      puts Template::Version
      exit
    end

    opts.on(
      "-i INPUT",
      "--input=INPUT",
      "Input in the template language (String or File)"
    ) do |input|
      input = File.read(input) if File.exist?(input)

      options[:input] = input
    end

    opts.on(
      "-c CONTEXT",
      "--context=CONTEXT",
      "Context in the code language (String or File)"
    ) do |context|
      context = File.read(context) if File.exist?(context)

      options[:context] = context
    end

    opts.on("-p", "--parse", "Get parser results for input") do |parse|
      options[:parse] = parse
    end

    opts.on(
      "-t TIMEOUT",
      "--timeout=TIMEOUT",
      "Set timeout in seconds"
    ) { |timeout| options[:timeout] = timeout.to_f }

    opts.on("--profile", "Profile Ruby code") do |_timeout|
      require "ruby-prof"
      options[:profile] = true
    end

    opts.on(
      "--profiler TYPE",
      "Profiler output type (text (default) or html)"
    ) do |profiler|
      require "ruby-prof"
      options[:profile] = true
      options[:profiler] = profiler
    end
  end
  .parse!

input = options.fetch(:input, "")
context = options.fetch(:context, "")

RubyProf.start if options[:profile]

if options[:parse]
  pp ::Template::Parser.parse(input).to_raw
else
  Template.evaluate(input, context, io: $stdout, timeout: options[:timeout])
end

if options[:profile]
  result = RubyProf.stop
  if options[:profiler] == "text"
    printer = RubyProf::FlatPrinter.new(result)
    printer.print($stdout)
  elsif options[:profiler] == "html"
    printer = RubyProf::GraphHtmlPrinter.new(result)
    printer.print($stdout)
  else
    abort "#{options[:profiler]} not recognized"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
template-ruby-0.6.6 bin/template
template-ruby-0.6.5 bin/template
template-ruby-0.6.4 bin/template