Sha256: 14da6648647224c3680266e3a731a0915aa48b1b752c400e582ce5b8717e75cf

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require "linner/version"
require "linner/command"
require "linner/asset"
require "linner/helper"
require "linner/wrapper"
require "linner/template"
require "linner/notifier"
require "linner/compressor"
require "linner/environment"

module Linner
  extend self

  def root
    @root ||= Pathname('.').expand_path
  end

  def cache
    @cache ||= {}
  end

  def environment
    @env ||= Environment.new root.join("config.yml")
  end

  def perform(compile: false)
    environment.files.each do |config|
      concat(config, compile)
      copy(config)
    end
  end

  private
  def concat(config, compile)
    config["concat"].map do |dest, regex|
      matches = Dir.glob(regex).uniq.order_by(before:config["order"]["before"], after:config["order"]["after"])
      dest = Asset.new(File.join environment.public_folder, dest)
      dest.content = ""
      cached = matches.select do |path|
        mtime = File.mtime(path).to_i
        mtime == cache[path] ? false : cache[path] = mtime
      end
      next if cached.empty?
      matches.each do |m|
        asset = Asset.new(m)
        content = asset.content
        if asset.wrappable?
          content = asset.wrap
        end
        dest.content << content
      end
      dest.compress if compile
      dest.write
    end
  end

  def copy(config)
    config["copy"].each do |dest, regex|
      Dir.glob(regex).each do |path|
        mtime = File.mtime(path).to_i
        next if cache[path] == mtime
        cache[path] = mtime
        logical_path = Asset.new(path).logical_path
        dest_path = File.join(environment.public_folder, dest, logical_path)
        FileUtils.mkdir_p File.dirname(dest_path)
        FileUtils.cp_r path, dest_path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linner-0.2.0 lib/linner.rb