Sha256: 8c35822b59281ba9879e04f2f10a969a3b9961dc09ae07700e77f3f9d2024d71

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 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(**/*.template)
      @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

5 entries across 5 versions & 1 rubygems

Version Path
rake-funnel-0.0.5.pre lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.0.4.pre lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.0.3.pre lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.0.2.pre lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.0.1.pre lib/rake/funnel/tasks/quick_template.rb