Sha256: 9b8e11fdf229a7469dd3dda3b5f67ae562f113a8636b11561584eb5c96c8d69f

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require "erb"
require "hanami/utils/files"
require "pathname"

module Snowpack
  class Generator
    attr_reader :templates_dir
    # attr_reader :templates
    attr_reader :files
    attr_reader :file_helper

    def initialize(templates_dir:)
      @templates_dir = templates_dir
      @files = Hanami::Utils::Files
    end

    def call(output_dir, **env)
      templates(**env).each do |template|
        output_file_path = render_file_path(template.relative_path_from(templates_dir).to_s, env)

        if File.extname(template) == ".tt"
          result = render(template.to_s, env)

          files.write \
            File.join(output_dir, output_file_path.sub(%r{#{File.extname(template)}$}, "")),
            result
        else
          files.cp template, File.join(output_dir, output_file_path)
        end
      end
    end

    private

    # TODO: when we go to multi-template dirs, we'll need to make it so that the
    # returned templates are bundled up with their root dirs
    def templates(**env)
      Dir[File.join(templates_dir, "**/{*,.*}")].select(&File.method(:file?)).map(&method(:Pathname))
    end

    def render(template, env)
      ERB.new(File.read(template), nil, _trim_mode = "-").result_with_hash(env)
    end

    FILE_PATH_VAR_DELIMITER = "__"
    FILE_PATH_VAR_REGEXP = /#{FILE_PATH_VAR_DELIMITER}\S+#{FILE_PATH_VAR_DELIMITER}/

    def render_file_path(path, env)
      path.gsub(FILE_PATH_VAR_REGEXP) { |match|
        var_name = match.gsub(FILE_PATH_VAR_DELIMITER, "")
        env.fetch(var_name.to_sym).to_s
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/generator.rb
snowpack-1.0.0.alpha6 lib/snowpack/generator.rb
snowpack-1.0.0.alpha5 lib/snowpack/generator.rb
snowpack-1.0.0.alpha4 lib/snowpack/generator.rb
snowpack-1.0.0.alpha3 lib/snowpack/generator.rb
snowpack-1.0.0.alpha2 lib/snowpack/generator.rb