Sha256: 535ca07534b847bdc45d33512ac4873ba1874f9770653e6094b52009c8be59a2

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'rake/tasklib'

module Rake
  module Funnel
    module Tasks
      class QuickTemplate < Rake::TaskLib
        include Rake::Funnel::Support

        attr_accessor :name, :search_pattern, :context

        def initialize(*args, &task_block)
          setup_ivars(args)

          define(args, &task_block)
        end

        private

        def setup_ivars(args)
          @name = args.shift || :template

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

        def define(args, &task_block) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
          desc 'Generate templates' unless Rake.application.last_description
          task(name, *args) do |_, task_args|
            yield(*[self, task_args].slice(0, task_block.arity)) if task_block

            templates.all_or_default.each do |template|
              target = result_filename(template)
              Rake.rake_output_message "Creating file #{target}"

              content = TemplateEngine.render(File.read(template), template, context)
              File.write(target, content)
            end
          end

          self
        end

        def templates
          Finder.new(search_pattern, self, 'No templates found.')
        end

        def result_filename(template)
          template.ext
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rake-funnel-0.22.1 lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.22.0 lib/rake/funnel/tasks/quick_template.rb
rake-funnel-0.21.2 lib/rake/funnel/tasks/quick_template.rb