Skip to content

Commit

Permalink
Added error reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jan 26, 2012
1 parent 003daa7 commit ba75743
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
7 changes: 4 additions & 3 deletions guard-sprockets.gemspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require 'guard/sprockets/version'

Gem::Specification.new do |s|
s.name = "guard-sprockets"
s.version = '0.2.0'
s.version = Guard::SprocketsVersion::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Aaron Cruz", 'Kematzy']
s.email = ["aaron@aaroncruz.com", "kematzy at gmail"]
Expand All @@ -12,10 +13,10 @@ Gem::Specification.new do |s|
s.description = %q{guard file for sprockets}

s.rubyforge_project = "guard-sprockets"

s.add_dependency 'guard', '>= 0.2.2'
s.add_dependency "sprockets", '~> 2'

s.files = Dir.glob('{lib}/**/*') #+ %w[LICENSE README.rdoc]
s.require_path = 'lib'
end
62 changes: 36 additions & 26 deletions lib/guard/sprockets.rb
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
5 changes: 5 additions & 0 deletions lib/guard/sprockets/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Guard
module SprocketsVersion
VERSION = "0.2.0"
end
end

0 comments on commit ba75743

Please sign in to comment.