#!/usr/bin/env ruby

require 'bundler/setup'
require 'haml'
require 'thor'
require 'benchmark/ips'
require_relative '../benchmark/utils/benchmark_ips_extension'

class Bench < Thor
  class_option :show_template, type: :boolean, aliases: ['-t']

  desc 'bench HAML', 'Benchmark haml template'
  option :compile, type: :boolean, aliases: ['-c']
  option :show_code, type: :boolean, aliases: ['-s']
  def bench(*files)
    files.each { |file| render(file) }
    files.each { |file| compile(file) if options[:compile] }
    files.each { |file| code(file) if options[:show_code] }
  end

  desc 'compile HAML', 'Benchmark compilation'
  def compile(file)
    puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
    haml = File.read(file)

    Benchmark.ips do |x|
      x.report("haml v#{Haml::VERSION}") { Haml::Engine.new.call(haml) }
      x.compare!
    end
  end

  desc 'render HAML', 'Benchmark rendering'
  def render(file)
    puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
    haml = File.read(file)
    puts haml + "\n" if options[:show_template]
    object = Object.new
    ruby_file = file.gsub(/\.haml\z/, '.rb')
    if File.exist?(ruby_file)
      object.instance_eval(File.read(ruby_file))
    end

    object.instance_eval "def haml; #{Haml::Engine.new.call(haml)}; end"

    Benchmark.ips do |x|
      x.report("haml v#{Haml::VERSION}") { object.haml }
      x.compare!
    end
  end

  desc 'code HAML', 'Show compiled code'
  def code(file)
    haml = File.read(file)
    puts "\n#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
    puts Haml::Engine.new.call(haml)
  end

  private

  def method_missing(*args)
    return super if args.length > 1
    render(args.first.to_s)
  end
end

Bench.start
