Skip to content

Commit

Permalink
Add a shortcut notation for the Guard configuration. (Fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Apr 4, 2011
1 parent 54ad3a1 commit eeb4b2b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,31 @@ Please read the [Guard usage documentation](http://github.com/guard/guard#readme
Guard::CoffeeScript can be adapted to all kind of projects. Please read the
[Guard documentation](http://github.com/guard/guard#readme) for more information about the Guardfile DSL.

### Standard ruby gems
In addition to the standard configuration, this Guard has a short notation for configure projects with a single input a output
directory. This notation creates a watcher from the `:input` parameter that matches all CoffeeScript files under the given directory
and you don't have to specify a watch regular expression.

guard 'coffeescript' do
watch(%r{coffeescripts/(.+\.coffee)})
end
### Standard ruby gem

### Rails app
guard 'coffeescript', :input => 'coffeescripts', :output => 'javascripts'

guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch(%r{app/coffeescripts/(.+\.coffee)})
end
### Rails app

guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'

## Options

There following options can be passed to Guard::CoffeeScript:

:output => 'javascripts' # Relative path to the output directory
:bare => true # Compile without the top-level function wrapper
:shallow => true # Do not create nested output directories
:input => 'coffeescripts' # Relative path to the input directory, default: nil
:output => 'javascripts' # Relative path to the output directory, default: nil
:bare => true # Compile without the top-level function wrapper, default: false
:shallow => true # Do not create nested output directories, default: false

### Nested directories

The guard detects by default nested directories and creates these within the output directory. The detection is based on the match of the watch regular expression:
The guard detects by default nested directories and creates these within the output directory. The detection is based on the match
of the watch regular expression:

A file

Expand All @@ -110,10 +111,37 @@ will be compiled to

public/javascripts/compiled/ui/buttons/toggle_button.js

Note the parenthesis around the `.+\.coffee`. This enables Guard::CoffeeScript to place the full path that was matched inside the
parenthesis into the proper output directory.

Note the parenthesis around the `.+\.coffee`. This enables guard-coffeescript to place the full path that was matched inside the parenthesis into the proper output directory.
This behaviour can be switched off by passing the option `:shallow => true` to the guard, so that all JavaScripts will be compiled
directly to the output directory.

This behaviour can be switched off by passing the option `:shallow => true` to the guard, so that all JavaScripts will be compiled directly to the output directory.
### Multiple source directories

The Guard short notation

guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'

will be internally converted into the standard notation by adding `(.+\.coffee)` to the `input` option string and create a Watcher
that is equivalent to:

guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch(%r{app/coffeescripts/(.+\.coffee)})
end

To add a second source directory that will be compiled to the same output directory, just add another watcher:

guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled' do
watch(%r{lib/coffeescripts/(.+\.coffee)})
end

which is equivalent to:

guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch(%r{app/coffeescripts/(.+\.coffee)})
watch(%r{lib/coffeescripts/(.+\.coffee)})
end

## Development

Expand Down
10 changes: 6 additions & 4 deletions lib/guard/coffeescript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class CoffeeScript < Guard
autoload :Compiler, 'guard/coffeescript/compiler'

def initialize(watchers = [], options = {})
watchers = [] if !watchers
watchers << ::Guard::Watcher.new(%r{#{ options.delete(:input) }/(.+\.coffee)}) if options[:input]

super(watchers, {
:output => 'javascripts',
:bare => false,
:shallow => false
:bare => false,
:shallow => false
}.merge(options))
end

Expand All @@ -34,6 +36,6 @@ def notify changed_files
guard.run_on_change paths unless paths.empty?
end
end

end
end
4 changes: 1 addition & 3 deletions lib/guard/coffeescript/templates/Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch(%r{app/coffeescripts/(.+\.coffee)})
end
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'
21 changes: 12 additions & 9 deletions spec/guard/coffeescript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
context 'when no options are provided' do
let(:guard) { Guard::CoffeeScript.new }

it 'sets a default :output option' do
guard.options[:output].should eql 'javascripts'
end

it 'sets a default :wrap option' do
guard.options[:bare].should be_false
end
Expand All @@ -27,10 +23,6 @@
context 'with other options than the default ones' do
let(:guard) { Guard::CoffeeScript.new(nil, { :output => 'output_folder', :bare => true, :shallow => true }) }

it 'sets the provided :output option' do
guard.options[:output].should eql 'output_folder'
end

it 'sets the provided :bare option' do
guard.options[:bare].should be_true
end
Expand All @@ -39,6 +31,18 @@
guard.options[:shallow].should be_true
end
end

context 'with a input option' do
let(:guard) { Guard::CoffeeScript.new(nil, { :input => 'app/coffeescripts' }) }

it 'creates a watcher' do
guard.should have(1).watchers
end

it 'watches all *.coffee files' do
guard.watchers.first.pattern.should eql %r{app/coffeescripts/(.+\.coffee)}
end
end
end

describe '.run_all' do
Expand Down Expand Up @@ -69,7 +73,6 @@
it 'starts the Runner with the cleaned files' do
Guard::CoffeeScript::Inspector.should_receive(:clean).with(['a.coffee', 'b.coffee']).and_return ['a.coffee']
Guard::CoffeeScript::Runner.should_receive(:run).with(['a.coffee'], [], {
:output => 'javascripts',
:bare => false,
:shallow => false }).and_return ['a.js']
guard.run_on_change(['a.coffee', 'b.coffee'])
Expand Down

1 comment on commit eeb4b2b

@jacquescrocker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet, this looks awesome. testing it out now

Please sign in to comment.