Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Major reworking of functionality with Sprockets2 support

* Added support for customized external asset paths.

   i.e.:  $HOME/code/js/

* Added "debug" output on start to show external asset paths and output path.

*  Incorporated therabidbanana's "Allow multiple files, and setting options" commit.

* Incorporated cyu's "support for sprockets 2.0" commit.
  • Loading branch information...
commit 510d2abac4362caf92ab2e0bb8a463e795843e75 1 parent b8bda4f
Kematzy kematzy authored
Showing with 32 additions and 20 deletions.
  1. +32 −20 lib/guard/sprockets.rb
52 lib/guard/sprockets.rb
View
@@ -8,11 +8,24 @@ module Guard
class Sprockets < Guard
def initialize(watchers=[], options={})
super
- @destination = options[:destination]
+
+ # init Sprocket env for use later
+ @sprockets_env = ::Sprockets::Environment.new
+
+ @asset_paths = options.delete(:asset_paths) || []
+ # add the asset_paths to the Sprockets env
+ @asset_paths.each do |p|
+ @sprockets_env.append_path p
+ end
+ # store the output destination
+ @destination = options.delete(:destination)
+ @opts = options
end
def start
- UI.info "Sprockets waiting for js file changes..."
+ UI.info "Sprockets waiting for asset file changes..."
+ UI.info " -- external asset paths = [#{@asset_paths.inspect}]" unless @asset_paths.empty?
+ UI.info " -- destination path = [#{@destination.inspect}]"
end
def run_all
@@ -20,29 +33,28 @@ def run_all
end
def run_on_change(paths)
- sprocketize paths.first
+ paths.each{ |js| sprocketize(js) }
+ true
end
private
def sprocketize(path)
- parts = path.split('/')
- file = parts.pop
- source_dir = "#{parts[0...-1].join('/')}/*"
- destination = parts[1..-1].join('/')
- @destination ||= destination
- secretary = ::Sprockets::Secretary.new(
- :asset_root => "#{parts.first}",
- :source_files => ["#{path}"],
- :interpolate_constants => false
- )
- # Generate a Sprockets::Concatenation object from the source files
- concatenation = secretary.concatenation
- # Write the concatenation to disk
- concatenation.save_to("#{@destination}/#{file}")
- # Install provided assets into the asset root
- secretary.install_assets
- UI.info "Sprockets creating file #{@destination}/#{file}"
+ changed = Pathname.new(path)
+
+ @sprockets_env.append_path changed.dirname
+
+ output_basename = changed.basename.to_s
+ if m = output_basename.match(/^(.*\.(?:js|css))\.[^.]+$/)
+ output_basename = m[1]
+ end
+
+ output_file = Pathname.new(File.join(@destination, output_basename))
+ UI.info "Sprockets creating file #{output_file}"
+ FileUtils.mkdir_p(output_file.parent) unless output_file.parent.exist?
+ output_file.open('w') do |f|
+ f.write @sprockets_env[output_basename]
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.