Sha256: 87a8e1c6036780e89afb58a992dc65670e97e948e3b5f6cfacefdc1daa46a111

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'rake/clean'
require 'rake/tasklib'

module Rake::Funnel::Tasks
  class QuickTemplate < Rake::TaskLib
    attr_accessor :name, :search_pattern, :context

    def initialize(name = :template)
      @name = name

      @search_pattern = %w(**/*.erb)
      @context = binding

      yield self if block_given?
      define
    end

    private
    def define
      results = templates.all_or_default.map { |t| result_filename(t) }
      CLEAN.include(*results)

      desc "Generate #{templates.all_or_default.join(', ')}"
      task name do
        templates.all_or_default.each do |template|
          target = result_filename(template)
          Rake.rake_output_message "Creating file #{target}"

          content = Rake::Funnel::Support::TemplateEngine.render(File.read(template), template, context)
          File.write(target, content)
        end
      end

      self
    end

    def templates
      Rake::Funnel::Support::Finder.new(search_pattern, self, 'No templates found.')
    end

    def result_filename(template)
      template.ext
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rake-funnel-0.0.6.pre lib/rake/funnel/tasks/quick_template.rb