Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

stage assets and let Jekyll copy them to site destination

  • Loading branch information...
commit 25bad861797c1ed736cbe252951c4b05036b62cf 1 parent c34c429
Matt Hodan authored
21 lib/jekyll_asset_pipeline.rb
View
@@ -10,8 +10,6 @@
require 'liquid'
# Jekyll extensions
-require 'jekyll_asset_pipeline/extensions/jekyll/static_file_extensions'
-require 'jekyll_asset_pipeline/extensions/jekyll/static_asset_file'
require 'jekyll_asset_pipeline/extensions/jekyll/site_extensions'
require 'jekyll_asset_pipeline/extensions/jekyll/site'
@@ -37,11 +35,20 @@
module JekyllAssetPipeline
# Default configuration settings for Jekyll Asset Pipeline
# Strings used for keys to play nice when merging with _config.yml
+ #
+ # 'output_path' Destination for bundle file (within the '_site' directory)
+ # 'display_path' Optional. Override path to assets for output HTML refs
+ # 'staging_path' Destination for staged assets (within project root directory)
+ # 'bundle' true = Bundle assets, false = Leave assets unbundled
+ # 'compress' true = Minify assets, false = Leave assets unminified
+ # 'gzip' true = Create gzip versions, false = Do not create gzip versions
+ #
DEFAULTS = {
- 'output_path' => 'assets', # Destination for bundle file (within the '_site' directory)
- 'display_path' => nil, # Optional. Override path to assets for output HTML refs
- 'bundle' => true, # true = Bundle assets, false = Leave assets unbundled
- 'compress' => true, # true = Minify assets, false = Leave assets unminified
- 'gzip' => false # true = Create gzip versions, false = Do not create gzip versions
+ 'output_path' => 'assets',
+ 'display_path' => nil,
+ 'staging_path' => '.asset_pipeline',
+ 'bundle' => true,
+ 'compress' => true,
+ 'gzip' => false
}
end
15 lib/jekyll_asset_pipeline/extensions/jekyll/site_extensions.rb
View
@@ -15,6 +15,21 @@ def self.included(base)
original_return_val
end
+
+ # Store the original Jekyll::Site#write method
+ old_write_method = instance_method(:write)
+
+ # Override Jekyll::Site#write
+ define_method(:write) do
+ # Run the Jekyll::Site#write method
+ original_return_value = old_write_method.bind(self).call()
+
+ # Clear Jekyll Asset Pipeline staged assets
+ config = self.config['asset_pipeline'] || {}
+ Pipeline.remove_staged_assets(self.source, config)
+
+ original_return_value
+ end
end
end
end
5 lib/jekyll_asset_pipeline/extensions/jekyll/static_asset_file.rb
View
@@ -1,5 +0,0 @@
-module JekyllAssetPipeline
- class StaticAssetFile < ::Jekyll::StaticFile
- include JekyllAssetPipeline::JekyllStaticFileExtensions
- end
-end
8 lib/jekyll_asset_pipeline/extensions/jekyll/static_file_extensions.rb
View
@@ -1,8 +0,0 @@
-module JekyllAssetPipeline
- module JekyllStaticFileExtensions
- # Override #write method since the asset file is dynamically created
- def write(dest)
- true
- end
- end
-end
3  lib/jekyll_asset_pipeline/extensions/liquid/liquid_block_extensions.rb
View
@@ -21,7 +21,8 @@ def render(context)
if pipeline.is_a?(Pipeline)
# Prevent Jekyll from cleaning up saved assets if new pipeline
pipeline.assets.each do |asset|
- site.static_files << StaticAssetFile.new(site, site.dest,
+ config = JekyllAssetPipeline::DEFAULTS.merge(config)
+ site.static_files << Jekyll::StaticFile.new(site, config['staging_path'],
asset.output_path, asset.filename)
end unless cached
18 lib/jekyll_asset_pipeline/pipeline.rb
View
@@ -58,6 +58,20 @@ def clear_cache
@cache = {}
end
+ # Remove staged assets
+ def remove_staged_assets(source, config)
+ begin
+ config = DEFAULTS.merge(config)
+ staging_path = File.join(source, config['staging_path'])
+ FileUtils.rm_rf(staging_path)
+ rescue Exception => e
+ puts "Failed to remove staged assets."
+
+ # Re-raise the exception
+ raise e
+ end
+ end
+
# Add prefix to output
def puts(message)
$stdout.puts("Asset Pipeline: #{message}")
@@ -179,10 +193,10 @@ def gzip
# Save assets to file
def save
output_path = @options['output_path']
+ staging_path = @options['staging_path']
@assets.each do |asset|
- # Create directories if necessary
- directory = File.join(@destination, output_path)
+ directory = File.join(@source, staging_path, output_path)
FileUtils::mkpath(directory) unless File.directory?(directory)
begin
4 spec/extensions/jekyll/site_extensions_spec.rb
View
@@ -13,6 +13,10 @@ def cleanup
'old_return_value'
end
+ def write
+ 'old_write_return_value'
+ end
+
include JekyllSiteExtensions
end
11 spec/extensions/jekyll/static_asset_file_extensions_spec.rb
View
@@ -1,11 +0,0 @@
-require './spec/helper'
-
-describe JekyllStaticFileExtensions do
- subject do
- object = Object.new
- object.extend(JekyllStaticFileExtensions)
- object.write('foobar')
- end
-
- specify { subject.must_equal(true) }
-end
5 spec/extensions/jekyll/static_asset_file_spec.rb
View
@@ -1,5 +0,0 @@
-require './spec/helper'
-
-describe StaticAssetFile do
- specify { StaticAssetFile.include?(JekyllStaticFileExtensions).must_equal(true) }
-end
2  spec/extensions/liquid/liquid_block_extensions_spec.rb
View
@@ -108,7 +108,7 @@ def self.output_type
it "creates new pipeline and processes it" do
$stdout.stub(:puts, nil) do
Pipeline.stub(:run, [pipeline, false]) do
- JekyllAssetPipeline::StaticAssetFile.stub(:new, 'foobar') do
+ Jekyll::StaticFile.stub(:new, 'foobar') do
subject.must_equal('foobaz_html')
end
end
4 spec/integration_spec.rb
View
@@ -13,13 +13,13 @@
clear_temp_path
end
- it "saves assets to output path" do
+ it "saves assets to staging path" do
$stdout.stub(:puts, nil) do
config['output_path'] = 'foobar_assets'
pipeline, cached = Pipeline.run(manifest, prefix, source_path, temp_path,
tag_name, extension, config)
pipeline.assets.each do |asset|
- file_path = File.join(temp_path, config['output_path'], asset.filename)
+ file_path = File.join(source_path, DEFAULTS['staging_path'], config['output_path'], asset.filename)
File.open(file_path) do |file|
file.read.must_equal(asset.content)
end
12 spec/pipeline_spec.rb
View
@@ -98,8 +98,10 @@
subject.last.filename.must_equal("#{prefix}-#{hash}#{type}")
end
- it "saves asset to disk at the output path" do
- File.exist?(File.join(temp_path, subject.last.output_path, subject.last.filename)).must_equal(true)
+ it "saves asset to disk at the staging path" do
+ asset = subject.last
+ staging_path = File.join(source_path, DEFAULTS['staging_path'], asset.output_path, asset.filename)
+ File.exist?(staging_path).must_equal(true)
end
end #context "bundle => true"
@@ -120,9 +122,11 @@
end
end
- it "saves assets to disk at the output path" do
+ it "saves assets to disk at the staging path" do
subject.each do |a|
- File.exist?(File.join(temp_path, a.output_path, a.filename)).must_equal(true)
+ asset = subject.last
+ staging_path = File.join(source_path, DEFAULTS['staging_path'], a.output_path, a.filename)
+ File.exist?(staging_path).must_equal(true)
end
end
end #context "bundle => false"
Please sign in to comment.
Something went wrong with that request. Please try again.