lib/linner.rb in linner-0.4.0 vs lib/linner.rb in linner-0.4.1

- old
+ new

@@ -1,10 +1,11 @@ require "nokogiri" require "linner/version" require "linner/command" require "linner/asset" +require "linner/cache" require "linner/helper" require "linner/reactor" require "linner/wrapper" require "linner/template" require "linner/notifier" @@ -21,28 +22,48 @@ def root @root ||= Pathname('.').expand_path end + def env + @env ||= Environment.new begin + linner_file = root.join("Linnerfile") + config_file = root.join("config.yml") + File.exist?(linner_file) ? linner_file : config_file + end + end + def cache - @cache ||= {} + @cache ||= Cache.new end - def env - @env ||= Environment.new root.join("config.yml") + def manifest + @manifest ||= begin + hash = {} + env.groups.each do |config| + config["concat"].to_h.each do |dest, pattern| + asset = Asset.new(File.join env.public_folder, dest) + hash[dest] = asset.relative_digest_path + asset.revision! + end + end + hash + end end def compile? @compile end def sass_engine_options - @options ||= Compass.configuration.to_sass_engine_options - env.paths.each do |load_path| - @options[:load_paths] << Sass::Importers::Filesystem.new(load_path) + @options ||= begin + options = Compass.configuration.to_sass_engine_options + env.paths.each do |load_path| + options[:load_paths] << Sass::Importers::Filesystem.new(load_path) + end + options end - @options end def perform(*asset) env.groups.each do |config| copy(config) if config["copy"] @@ -54,53 +75,54 @@ private def concat(config) config["concat"].each_with_index do |pair, index| dest, pattern, order = pair.first, pair.last, config["order"]||[] matches = Dir.glob(pattern).order_by(order) - next if matches.select {|p| cache_miss? p}.empty? - dest = Asset.new(File.join env.public_folder, dest) - definition = Wrapper.definition if dest.path == env.definition - dest.content = matches.inject(definition || "") {|s, m| s << cache[m].content} - dest.compress if compile? - dest.write + next if matches.select {|p| cache.miss? p}.empty? + write_asset(dest, matches) end end def copy(config) config["copy"].each do |dest, pattern| Dir.glob(pattern).each do |path| - next if not cache_miss?(path) + next if not cache.miss?(path) logical_path = Asset.new(path).logical_path dest_path = File.join(env.public_folder, dest, logical_path) FileUtils.mkdir_p File.dirname(dest_path) FileUtils.cp_r path, dest_path end end end def revision - revision = File.join env.public_folder, env.revision - doc = Nokogiri::HTML.parse(File.read revision) - doc.search("script").each do |x| - next unless src = x.attributes["src"] - asset = Asset.new(File.join env.public_folder, src) - x.set_attribute "src", asset.revision! + dump_manifest + return unless File.exist?(rev = File.join(env.public_folder, env.revision.to_s)) + doc = Nokogiri::HTML.parse(File.read rev) + replace_tag_with_manifest_value doc, "script", "src" + replace_tag_with_manifest_value doc, "link", "href" + File.open(rev, "w") {|f| f.write doc.to_html} + end + + private + + def write_asset(dest, child_assets) + asset = Asset.new(File.join env.public_folder, dest) + definition = (asset.path == env.definition ? Wrapper.definition : "") + asset.content = child_assets.inject(definition) {|s, m| s << cache[m].content} + asset.compress if compile? + asset.write + end + + def replace_tag_with_manifest_value doc, tag, attribute + doc.search(tag).each do |x| + next unless node = x.attributes[attribute] + x.set_attribute attribute, manifest[node.value] end - doc.search("link").each do |x| - next unless href = x.attributes["href"] - asset = Asset.new(File.join env.public_folder, href) - x.set_attribute "href", asset.revision! - end - File.open(revision, "w") do |f| - f.write doc.to_html - end end - def cache_miss?(path) - asset = Asset.new(path) - if cache[path] and cache[path].mtime == asset.mtime - false - else - cache[path] = asset + def dump_manifest + File.open(File.join(env.public_folder, env.manifest), "w") do |f| + YAML.dump(manifest, f) end end end