-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
003daa7
commit ba75743
Showing
3 changed files
with
45 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,79 @@ | ||
require 'guard' | ||
require 'guard/guard' | ||
require 'erb' | ||
|
||
require 'sprockets' | ||
require 'execjs' | ||
|
||
module Guard | ||
class Sprockets < Guard | ||
def initialize(watchers=[], options={}) | ||
super | ||
|
||
# init Sprocket env for use later | ||
@sprockets_env = ::Sprockets::Environment.new | ||
|
||
super | ||
|
||
@sprockets = ::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 | ||
@sprockets.append_path p | ||
end | ||
# store the output destination | ||
|
||
@destination = options.delete(:destination) | ||
# the only file that should be built | ||
@root_file = options.delete(:root_file) | ||
@root_file = options.delete(:root_file) | ||
|
||
@opts = options | ||
end | ||
|
||
def start | ||
UI.info "Sprockets activated..." | ||
UI.info " -- external asset paths = [#{@asset_paths.inspect}]" unless @asset_paths.empty? | ||
UI.info " -- destination path = [#{@destination.inspect}]" | ||
UI.info "Sprockets is ready and waiting for some file changes..." | ||
UI.info "Sprockets activated." | ||
UI.info " - external asset paths = #{@asset_paths.inspect}" unless @asset_paths.empty? | ||
UI.info " - destination path = #{@destination.inspect}" | ||
UI.info "Sprockets guard is ready and waiting for some file changes..." | ||
|
||
run_all | ||
end | ||
|
||
def run_all | ||
run_on_change([@root_file]) if @root_file | ||
run_on_change([ @root_file ]) if @root_file | ||
|
||
true | ||
end | ||
|
||
def run_on_change(paths) | ||
if @root_file | ||
sprocketize(@root_file) | ||
else | ||
paths.each{ |js| sprocketize(js) } | ||
paths.each do |file| | ||
sprocketize(file) | ||
end | ||
end | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
def sprocketize(path) | ||
changed = Pathname.new(path) | ||
|
||
@sprockets_env.append_path changed.dirname | ||
@sprockets.append_path changed.dirname | ||
|
||
output_basename = changed.basename.to_s | ||
if match = output_basename.match(/^(.*\.(?:js|css))\.[^.]+$/) | ||
output_basename = match[1] | ||
end | ||
output_basename.gsub!(/^(.*\.(?:js|css))\.[^.]+$/, '\1') | ||
|
||
output_file = Pathname.new(File.join(@destination, output_basename)) | ||
UI.info "Sprockets started compiling #{output_file}" | ||
|
||
UI.info "Sprockets: compiling #{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] | ||
f.write @sprockets[output_basename] | ||
end | ||
|
||
UI.info "Sprockets finished compiling #{output_file}" | ||
Notifier.notify "Compiled #{output_basename}" | ||
rescue ExecJS::ProgramError => e | ||
UI.error "Sprockets failed to compile #{output_file}" | ||
UI.error e.message | ||
Notifier.notify "Syntax error in #{output_basename}: #{e.message}", :priority => 2, :image => :failed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Guard | ||
module SprocketsVersion | ||
VERSION = "0.2.0" | ||
end | ||
end |