Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a few specs, refactor a bit and improve README

  • Loading branch information...
commit 7757ffa4cf2949a53ebdd4298d5896d9214805ac 1 parent 0aeac52
@rymai rymai authored
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
11 .travis.yml
@@ -0,0 +1,11 @@
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
+ - ruby-head
+ - ree
+ #- jruby-18mode
+ #- jruby-19mode
+ #- jruby-head
+ - rbx-18mode
+ - rbx-19mode
View
8 Gemfile
@@ -2,3 +2,11 @@ source "http://rubygems.org"
# Specify your gem's dependencies in guard-sprockets.gemspec
gemspec
+
+gem 'rake'
+gem 'ruby_gntp'
+platforms :ruby do
+ gem 'rb-readline'
+end
+gem 'guard-rspec'
+gem 'uglifier'
View
5 Guardfile
@@ -0,0 +1,5 @@
+guard :rspec do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
View
57 README.markdown
@@ -1,57 +0,0 @@
-## What?
-Packages your javascript files together when you save your source file.
-
-## Sprockets support
-master branch supports Sprockets v ~> 2
-sprockets_1 branch supports v < 2
-
-## Use
-`gem install guard-sprockets`
-in your project's directory root
-`guard init`
-`guard init sprockets`
-
-which will add this to your Guardfile
-
- guard 'sprockets', :destination => "public/javascripts", :asset_paths => ['/app/assets/javascripts'] do
- watch (%r{app/assets/javascripts/application.js})
- end
-
-change the destination to your public facing javascript directory and the `watch` regex to point at where your source javascript file is.
-
-The base configuration would work for a rails 3 project with a directory structure like
-
- |-- app
- | |-- controllers
- | |-- assets
- | | |-- sass
- | | `-- javascripts
- | | `-- application.js # your source javascript file
- | |-- helpers
- | |-- mailers
- | |-- models
- | `-- views
- | `-- layouts
- |-- config
- | |-- environments
- | |-- initializers
- | `-- locales
- |-- db
- |-- doc
- |-- lib
- | `-- tasks
- ... etc.
-
-## TODO
-tests
-
-## License
-(The MIT License)
-
-Copyright (c) 2011 Aaron Cruz
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
78 README.md
@@ -0,0 +1,78 @@
+# Guard::Sprockets [![Build Status](https://secure.travis-ci.org/guard/guard-sprockets.png?branch=master)](http://travis-ci.org/guard/guard-sprockets)
+
+Sprockets guard automatically packages your JavaScript files together when your source files are saved.
+
+* Compatible with Sprockets 2.x (for Sprockets 1.x compatibility, please use Guard::Sprockets 0.1.4 or the `sprockets_1` branch).
+* Tested against Ruby 1.8.7, 1.9.2, 1.9.3, REE and the latest versions of JRuby & Rubinius.
+
+## Install
+
+Please be sure to have [Guard](https://github.com/guard/guard) installed before continue.
+
+Install the gem:
+
+```
+$ gem install guard-sprockets
+```
+
+Add it to your Gemfile (inside the `:tools` group for instance):
+
+```ruby
+group :tools do
+ gem 'guard-sprockets'
+end
+```
+
+Add guard definition to your Guardfile by running this command:
+
+```
+$ guard init sprockets
+```
+
+## Usage
+
+Please read [Guard usage doc](https://github.com/guard/guard#readme)
+
+## Guardfile
+
+Guard::Sprockets can be adapted to all kind of projects.
+
+### Typical Rails 3 app (default generated Guardfile)
+
+``` ruby
+guard 'sprockets', :destination => 'public/javascripts', :asset_paths => ['/app/assets/javascripts'] do
+ watch 'app/assets/javascripts/application.js'
+end
+```
+
+Please read [Guard doc](https://github.com/guard/guard#readme) for more information about the Guardfile DSL.
+
+## Options
+
+``` ruby
+:destination => 'public/js' # change the destination folder in which the compiled assets are saved, default: 'public/javascripts'
+:asset_paths => 'app/js' # add a directory (or on array of directories) to Sprockets' environment's load path, default: ['app/assets/javascripts']
+:asset_paths => ['app/js', 'lib/js'] # asset_paths can be a String or an Array
+:minify => true # minify the JavaScript files content using Uglifier, default: false
+ # be sure to add: "gem 'uglifier'" in your Gemfile
+:root_file => 'app/js/app.js' # if set, only this file will be compiled, default: nil
+```
+
+which will add this to your Guardfile
+
+ guard 'sprockets', :destination => "public/javascripts", :asset_paths => ['/app/assets/javascripts'] do
+ watch (%r{app/assets/javascripts/application.js})
+ end
+
+change the destination to your public facing javascript directory and the `watch` regex to point at where your source javascript file is.
+
+## License
+(The MIT License)
+
+Copyright (c) 2011-2012 Aaron Cruz
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
29 guard-sprockets.gemspec
@@ -1,21 +1,26 @@
# -*- encoding: utf-8 -*-
-$:.push File.expand_path("../lib", __FILE__)
+$:.push File.expand_path('../lib', __FILE__)
+require 'guard/sprockets/version'
Gem::Specification.new do |s|
- s.name = "guard-sprockets"
- s.version = "0.3.1"
+ s.name = 'guard-sprockets'
+ s.version = Guard::SprocketsVersion::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["Aaron Cruz", 'Kematzy']
- s.email = ["aaron@aaroncruz.com", "kematzy at gmail"]
- s.homepage = "http://aaroncruz.com"
- s.summary = %q{guard file for sprockets}
- s.description = %q{guard file for sprockets}
+ s.authors = ['Aaron Cruz', 'Kematzy']
+ s.email = ['aaron@aaroncruz.com', 'kematzy at gmail']
+ s.homepage = 'https://rubygems.org/gems/guard-sprockets'
+ s.summary = 'Guard gem for Sprockets'
+ s.description = 'Guard::Sprockets automatically packages your javascript files together.'
- s.rubyforge_project = "guard-sprockets"
+ s.required_rubygems_version = '>= 1.3.6'
+ s.rubyforge_project = 'guard-sprockets'
- s.add_dependency 'guard', '~> 0.2.2'
- s.add_dependency "sprockets", '~> 2'
+ s.add_dependency 'guard', '>= 0.10.0'
+ s.add_dependency 'sprockets', '~> 2.0'
- s.files = Dir.glob('{lib}/**/*') #+ %w[LICENSE README.rdoc]
+ s.add_development_dependency 'bundler', '~> 1.0'
+ s.add_development_dependency 'rspec', '~> 2.7'
+
+ s.files = Dir.glob('{lib}/**/*') + %w[README.md]
s.require_path = 'lib'
end
View
97 lib/guard/sprockets.rb
@@ -6,84 +6,85 @@
module Guard
class Sprockets < Guard
- def initialize(watchers=[], options={})
- super
- @sprockets = ::Sprockets::Environment.new
+ attr_reader :asset_paths, :destination, :root_file, :sprockets
- @asset_paths = options.delete(:asset_paths) || []
- @asset_paths.each do |p|
- @sprockets.append_path p
- end
+ def initialize(watchers = [], options = {})
+ super(watchers, options)
+
+ @options = options
+ @asset_paths = Array(@options[:asset_paths] || 'app/assets/javascripts')
+ @destination = @options[:destination] || 'public/javascripts'
+ @root_file = @options[:root_file]
+
+ @sprockets = ::Sprockets::Environment.new
+ @asset_paths.each { |p| @sprockets.append_path(p) }
+ @sprockets.append_path(@root_file) if @root_file
- if options.delete(:minify)
+ if @options.delete(:minify)
begin
require 'uglifier'
- @sprockets_env.js_compressor = ::Uglifier.new
- UI.info "Sprockets will compress output (minify)."
- rescue
- UI.error "minify: Uglifier cannot be loaded. No compression will be used.\nPlease include 'uglifier' in your Gemfile.\n#{$!}"
+ @sprockets.js_compressor = ::Uglifier.new
+ UI.info 'Sprockets will compress output.'
+ rescue LoadError => ex
+ UI.error "minify: Uglifier cannot be loaded. No compression will be used.\nPlease include 'uglifier' in your Gemfile."
+ UI.debug ex.message
end
end
- # store the output destination
- @destination = options.delete(:destination)
- @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 guard is ready and waiting for some file changes..."
+ UI.info 'Guard::Sprockets is ready and waiting for some file changes...'
+ UI.debug "Guard::Sprockets.asset_paths = #{@asset_paths.inspect}" unless @asset_paths.empty?
+ UI.debug "Guard::Sprockets.destination = #{@destination.inspect}"
run_all
end
def run_all
- run_on_change([ @root_file ]) if @root_file
-
- true
+ run_on_change([@root_file]) if @root_file
end
def run_on_change(paths)
- if @root_file
- sprocketize(@root_file)
- else
- paths.each do |file|
- sprocketize(file)
- end
- end
+ paths = [@root_file] if @root_file
- true
+ success = true
+ paths.each do |file|
+ success &= sprocketize(file)
+ end
+ success
end
private
def sprocketize(path)
- changed = Pathname.new(path)
+ path = Pathname.new(path)
- @sprockets.append_path changed.dirname
+ @sprockets.append_path(path.dirname) unless @sprockets.paths.include?(path.dirname)
- output_basename = changed.basename.to_s
- output_basename.gsub!(/^(.*\.(?:js|css))\.[^.]+$/, '\1')
+ output_filename = without_preprocessor_extension(path.basename.to_s)
+ output_path = Pathname.new(File.join(@destination, output_filename))
- output_file = Pathname.new(File.join(@destination, output_basename))
+ UI.info "Sprockets will compile #{output_filename}"
- 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[output_basename]
+ FileUtils.mkdir_p(output_path.parent) unless output_path.parent.exist?
+ output_path.open('w') do |f|
+ f.write @sprockets[output_filename]
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
+ UI.info "Sprockets compiled #{output_filename}"
+ Notifier.notify "Sprockets compiled #{output_filename}"
+ rescue ExecJS::ProgramError => ex
+ UI.error "Sprockets failed compiling #{output_filename}"
+ UI.error ex.message
+ Notifier.notify "Sprockets failed compiling #{output_filename}!", :priority => 2, :image => :failed
+
+ false
end
+
+ def without_preprocessor_extension(filename)
+ filename.gsub(/^(.*\.(?:js|css))\.[^.]+$/, '\1')
+ end
+
end
end
View
4 lib/guard/sprockets/templates/Guardfile
@@ -1,3 +1,3 @@
-guard 'sprockets', :destination => "public/javascripts", :asset_paths => ['app/assets/javascripts'] do
- watch (%r{app/assets/javascripts/application.js})
+guard 'sprockets', :destination => 'public/javascripts', :asset_paths => ['app/assets/javascripts'] do
+ watch 'app/assets/javascripts/application.js'
end
View
5 lib/guard/sprockets/version.rb
@@ -0,0 +1,5 @@
+module Guard
+ module SprocketsVersion
+ VERSION = "0.3.1"
+ end
+end
View
43 spec/guard/sprockets_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Guard::Sprockets do
+
+ describe '.initialize' do
+ describe 'options' do
+ describe 'asset_paths' do
+ it { described_class.new.asset_paths.should eq ['app/assets/javascripts'] }
+ it { described_class.new([], :asset_paths => 'foo/bar').asset_paths.should eq ['foo/bar'] }
+ it { described_class.new([], :asset_paths => ['foo/bar']).asset_paths.should eq ['foo/bar'] }
+ end
+
+ describe 'destination' do
+ it { described_class.new.destination.should eq 'public/javascripts' }
+ it { described_class.new([], :destination => 'foo/bar').destination.should eq 'foo/bar' }
+ end
+
+ describe 'minify' do
+ it { described_class.new.sprockets.js_compressor.should be_nil }
+ it { described_class.new([], :minify => true).sprockets.js_compressor.should_not be_nil }
+ end
+
+ describe 'root_file' do
+ it { described_class.new.root_file.should be_nil }
+ it { described_class.new([], :root_file => 'foo/bar').root_file.should eq 'foo/bar' }
+ end
+ end
+ end
+
+ describe 'without_preprocessor_extension' do
+ it { subject.send(:without_preprocessor_extension, 'foo.js.coffee').should eq 'foo.js' }
+ end
+
+ describe 'run_on_change' do
+ before do
+ subject.sprockets.should_receive(:[]).and_raise ExecJS::ProgramError
+ end
+ after { FileUtils.rm_r('public') }
+
+ it { subject.run_on_change(['foo']).should be_false }
+ end
+
+end
View
16 spec/spec_helper.rb
@@ -0,0 +1,16 @@
+require 'rspec'
+require 'guard/sprockets'
+ENV["GUARD_ENV"] = 'test'
+
+RSpec.configure do |config|
+ config.color_enabled = true
+ config.filter_run :focus => true
+ config.run_all_when_everything_filtered = true
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+
+ # config.before(:each) do
+ # @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
+ # @lib_path = Pathname.new(File.expand_path('../../lib/', __FILE__))
+ # end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.