guard-coffeescript 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +90 -119
- data/lib/guard/coffeescript.rb +33 -4
- data/lib/guard/coffeescript/formatter.rb +47 -6
- data/lib/guard/coffeescript/inspector.rb +24 -1
- data/lib/guard/coffeescript/runner.rb +77 -10
- data/lib/guard/coffeescript/version.rb +2 -1
- metadata +36 -9
- data/lib/guard/coffeescript.rbc +0 -1150
- data/lib/guard/coffeescript/formatter.rbc +0 -1031
- data/lib/guard/coffeescript/inspector.rbc +0 -750
- data/lib/guard/coffeescript/runner.rbc +0 -2609
- data/lib/guard/coffeescript/version.rbc +0 -203
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
# Guard::CoffeeScript
|
2
|
-
|
3
|
-
![travis-ci](http://travis-ci.org/netzpirat/guard-coffeescript.png)
|
1
|
+
# Guard::CoffeeScript [![Build Status](https://secure.travis-ci.org/netzpirat/guard-coffeescript.png)](http://travis-ci.org/netzpirat/guard-coffeescript)
|
4
2
|
|
5
3
|
Guard::CoffeeScript compiles or validates your CoffeeScripts automatically when files are modified.
|
6
4
|
|
7
|
-
Tested on MRI Ruby 1.8.7, 1.9.2 and the latest versions of JRuby & Rubinius.
|
5
|
+
Tested on MRI Ruby 1.8.7, 1.9.2, REE and the latest versions of JRuby & Rubinius.
|
8
6
|
|
9
7
|
If you have any questions please join us on our [Google group](http://groups.google.com/group/guard-dev) or on `#guard`
|
10
8
|
(irc.freenode.net).
|
@@ -15,21 +13,15 @@ Please be sure to have [Guard](https://github.com/guard/guard) installed.
|
|
15
13
|
|
16
14
|
Install the gem:
|
17
15
|
|
18
|
-
|
19
|
-
$ gem install guard-coffeescript
|
20
|
-
```
|
16
|
+
$ gem install guard-coffeescript
|
21
17
|
|
22
18
|
Add it to your `Gemfile`, preferably inside the development group:
|
23
19
|
|
24
|
-
|
25
|
-
gem 'guard-coffeescript'
|
26
|
-
```
|
20
|
+
gem 'guard-coffeescript'
|
27
21
|
|
28
22
|
Add guard definition to your `Guardfile` by running this command:
|
29
23
|
|
30
|
-
|
31
|
-
$ guard init coffeescript
|
32
|
-
```
|
24
|
+
$ guard init coffeescript
|
33
25
|
|
34
26
|
## JSON
|
35
27
|
|
@@ -41,59 +33,61 @@ to install the `json` or `json_pure` gem. On Ruby 1.9, JSON is included in the s
|
|
41
33
|
Guard::CoffeeScript uses [Ruby CoffeeScript](https://github.com/josh/ruby-coffee-script/) to compile the CoffeeScripts,
|
42
34
|
that in turn uses [ExecJS](https://github.com/sstephenson/execjs) to pick the best runtime to evaluate the JavaScript.
|
43
35
|
|
44
|
-
|
36
|
+
* With CRuby you want to use a V8 JavaScript Engine or Mozilla SpiderMonkey.
|
37
|
+
* With JRuby you want to use the Mozilla Rhino.
|
38
|
+
* On Mac OS X you want to use Apple JavaScriptCore.
|
39
|
+
* On Linux or as a node.js developer you want to use Node.js (V8).
|
40
|
+
* On Windows you want to use Microsoft Windows Script Host.
|
41
|
+
|
42
|
+
## JavaScript runtimes
|
43
|
+
|
44
|
+
The following sections gives you a short overview of the available JavaScript runtimes and how to install it.
|
45
45
|
|
46
|
-
###
|
46
|
+
### Node.js (V8)
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
You can install [node.js](http://nodejs.org/) and use its V8 engine. On OS X you may want to install it with
|
49
|
+
[Homebrew](http://mxcl.github.com/homebrew/), on Linux with your package manager and on Windows you have to download and
|
50
|
+
install the [executable](http://www.nodejs.org/#download).
|
50
51
|
|
51
52
|
### V8 JavaScript Engine
|
52
53
|
|
53
54
|
To use the [V8 JavaScript Engine](http://code.google.com/p/v8/), simple add `therubyracer` to your `Gemfile`.
|
54
55
|
The Ruby Racer acts as a bridge between Ruby and the V8 engine, that will be automatically installed by the Ruby Racer.
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
```
|
57
|
+
group :development do
|
58
|
+
gem 'therubyracer'
|
59
|
+
end
|
61
60
|
|
62
61
|
Another alternative is [Mustang](https://github.com/nu7hatch/mustang), a Ruby proxy library for the awesome Google V8
|
63
62
|
JavaScript engine. Just add `mustang` to your `Gemfile`:
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
69
|
-
```
|
64
|
+
group :development do
|
65
|
+
gem 'mustang'
|
66
|
+
end
|
70
67
|
|
71
68
|
### Mozilla SpiderMonkey
|
72
69
|
|
73
70
|
To use [Mozilla SpiderMonkey](https://developer.mozilla.org/en/SpiderMonkey), simple add `johnson` to your `Gemfile`.
|
74
71
|
Johnson embeds the Mozilla SpiderMonkey JavaScript runtime as a C extension.
|
75
72
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
80
|
-
```
|
73
|
+
group :development do
|
74
|
+
gem 'johnson'
|
75
|
+
end
|
81
76
|
|
82
77
|
### Mozilla Rhino
|
83
78
|
|
84
79
|
If you're using JRuby, you can embed the [Mozilla Rhino](http://www.mozilla.org/rhino/) runtime by adding `therubyrhino`
|
85
80
|
to your `Gemfile`:
|
86
81
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
end
|
91
|
-
```
|
82
|
+
group :development do
|
83
|
+
gem 'therubyrhino'
|
84
|
+
end
|
92
85
|
|
93
86
|
### Apple JavaScriptCore
|
94
87
|
|
95
|
-
JavaScriptCore
|
96
|
-
|
88
|
+
[JavaScriptCore](http://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/index.html)
|
89
|
+
is Safari's Nitro JavaScript Engine and only usable on Mac OS X. You don't have to install anything, because
|
90
|
+
JavaScriptCore is already packaged with Mac OS X.
|
97
91
|
|
98
92
|
### Microsoft Windows Script Host
|
99
93
|
|
@@ -109,71 +103,61 @@ Please read the [Guard usage documentation](https://github.com/guard/guard#readm
|
|
109
103
|
Guard::CoffeeScript can be adapted to all kind of projects. Please read the
|
110
104
|
[Guard documentation](https://github.com/guard/guard#readme) for more information about the Guardfile DSL.
|
111
105
|
|
112
|
-
###
|
106
|
+
### Ruby project
|
113
107
|
|
114
|
-
In a
|
108
|
+
In a Ruby project you want to configure your input and output directories.
|
115
109
|
|
116
|
-
|
117
|
-
guard 'coffeescript', :input => 'coffeescripts', :output => 'javascripts'
|
118
|
-
```
|
110
|
+
guard 'coffeescript', :input => 'coffeescripts', :output => 'javascripts'
|
119
111
|
|
120
112
|
If your output directory is the same as the input directory, you can simply skip it:
|
121
113
|
|
122
|
-
|
123
|
-
guard 'coffeescript', :input => 'javascripts'
|
124
|
-
```
|
114
|
+
guard 'coffeescript', :input => 'javascripts'
|
125
115
|
|
126
116
|
### Rails app with the asset pipeline
|
127
117
|
|
128
118
|
With the introduction of the [asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html) in Rails 3.1 there is
|
129
|
-
no need to compile your CoffeeScripts with this Guard.
|
130
|
-
(preferably with a Growl notification) directly after you save a change, then you may want still use this Guard and just
|
131
|
-
skip the generation of the output file:
|
119
|
+
no need to compile your CoffeeScripts with this Guard.
|
132
120
|
|
133
|
-
|
134
|
-
|
135
|
-
|
121
|
+
However, if you would still like to have feedback on the validation of your CoffeeScripts
|
122
|
+
(preferably with a Growl notification) directly after you save a change, then you can still
|
123
|
+
use this Guard and simply skip generation of the output file:
|
124
|
+
|
125
|
+
guard 'coffeescript', :input => 'app/assets/javascripts', :noop => true
|
136
126
|
|
137
127
|
This give you a faster compilation feedback compared to making a subsequent request to your Rails application. If you
|
138
128
|
just want to be notified when an error occurs you can hide the success compilation message:
|
139
129
|
|
140
|
-
|
141
|
-
guard 'coffeescript', :input => 'app/assets/javascripts', :noop => true, :hide_success => true
|
142
|
-
```
|
130
|
+
guard 'coffeescript', :input => 'app/assets/javascripts', :noop => true, :hide_success => true
|
143
131
|
|
144
132
|
### Rails app without the asset pipeline
|
145
133
|
|
146
134
|
Without the asset pipeline you just define an input and output directory like within a normal Ruby project:
|
147
135
|
|
148
|
-
|
149
|
-
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts',
|
150
|
-
```
|
136
|
+
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts'
|
151
137
|
|
152
138
|
## Options
|
153
139
|
|
154
140
|
There following options can be passed to Guard::CoffeeScript:
|
155
141
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
# default: nil
|
142
|
+
:input => 'coffeescripts' # Relative path to the input directory.
|
143
|
+
# A suffix `/(.+\.coffee)` will be added to this option.
|
144
|
+
# default: nil
|
160
145
|
|
161
|
-
:output => 'javascripts' # Relative path to the output directory.
|
162
|
-
|
146
|
+
:output => 'javascripts' # Relative path to the output directory.
|
147
|
+
# default: the path given with the :input option
|
163
148
|
|
164
|
-
:noop => true # No operation: do not write an output file.
|
165
|
-
|
149
|
+
:noop => true # No operation: do not write an output file.
|
150
|
+
# default: false
|
166
151
|
|
167
|
-
:bare => true # Compile without the top-level function wrapper.
|
168
|
-
|
169
|
-
|
152
|
+
:bare => true # Compile without the top-level function wrapper.
|
153
|
+
# Provide either a boolean value or an Array of filenames.
|
154
|
+
# default: false
|
170
155
|
|
171
|
-
:shallow => true # Do not create nested output directories.
|
172
|
-
|
156
|
+
:shallow => true # Do not create nested output directories.
|
157
|
+
# default: false
|
173
158
|
|
174
|
-
:hide_success => true # Disable successful compilation messages.
|
175
|
-
|
176
|
-
```
|
159
|
+
:hide_success => true # Disable successful compilation messages.
|
160
|
+
# default: false
|
177
161
|
|
178
162
|
### Output short notation
|
179
163
|
|
@@ -181,25 +165,19 @@ In addition to the standard configuration, this Guard has a short notation for c
|
|
181
165
|
and output directory. This notation creates a watcher from the `:input` parameter that matches all CoffeeScript files
|
182
166
|
under the given directory and you don't have to specify a watch regular expression.
|
183
167
|
|
184
|
-
|
185
|
-
guard 'coffeescript', :input => 'javascripts'
|
186
|
-
```
|
168
|
+
guard 'coffeescript', :input => 'javascripts'
|
187
169
|
|
188
170
|
### Selective bare option
|
189
171
|
|
190
172
|
The `:bare` option can take a boolean value that indicates if all scripts should be compiled without the top-level
|
191
173
|
function wrapper.
|
192
174
|
|
193
|
-
|
194
|
-
:bare => true
|
195
|
-
```
|
175
|
+
:bare => true
|
196
176
|
|
197
177
|
But you can also pass an Array of filenames that should be compiled without the top-level function wrapper. The path of
|
198
178
|
the file to compile is ignored, so the list of filenames should not contain any path information:
|
199
179
|
|
200
|
-
|
201
|
-
:bare => %w{ a.coffee b.coffee }
|
202
|
-
```
|
180
|
+
:bare => %w{ a.coffee b.coffee }
|
203
181
|
|
204
182
|
In the above example, all `a.coffee` and `b.coffee` files will be compiled with option `:bare => true` and all other
|
205
183
|
files with option `:bare => false`.
|
@@ -211,27 +189,19 @@ the match of the watch regular expression:
|
|
211
189
|
|
212
190
|
A file
|
213
191
|
|
214
|
-
|
215
|
-
/app/coffeescripts/ui/buttons/toggle_button.coffee
|
216
|
-
```
|
192
|
+
/app/coffeescripts/ui/buttons/toggle_button.coffee
|
217
193
|
|
218
194
|
that has been detected by the watch
|
219
195
|
|
220
|
-
|
221
|
-
watch(%r{^app/coffeescripts/(.+\.coffee)$})
|
222
|
-
```
|
196
|
+
watch(%r{^app/coffeescripts/(.+\.coffee)$})
|
223
197
|
|
224
198
|
with an output directory of
|
225
199
|
|
226
|
-
|
227
|
-
:output => 'public/javascripts/compiled'
|
228
|
-
```
|
200
|
+
:output => 'public/javascripts/compiled'
|
229
201
|
|
230
202
|
will be compiled to
|
231
203
|
|
232
|
-
|
233
|
-
public/javascripts/compiled/ui/buttons/toggle_button.js
|
234
|
-
```
|
204
|
+
public/javascripts/compiled/ui/buttons/toggle_button.js
|
235
205
|
|
236
206
|
Note the parenthesis around the `.+\.coffee`. This enables Guard::CoffeeScript to place the full path that was matched
|
237
207
|
inside the parenthesis into the proper output directory.
|
@@ -243,42 +213,40 @@ compiled directly to the output directory.
|
|
243
213
|
|
244
214
|
The Guard short notation
|
245
215
|
|
246
|
-
|
247
|
-
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'
|
248
|
-
```
|
216
|
+
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'
|
249
217
|
|
250
218
|
will be internally converted into the standard notation by adding `/(.+\.coffee)` to the `input` option string and
|
251
219
|
create a Watcher that is equivalent to:
|
252
220
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
end
|
257
|
-
```
|
221
|
+
guard 'coffeescript', :output => 'public/javascripts/compiled' do
|
222
|
+
watch(%r{^app/coffeescripts/(.+\.coffee)$})
|
223
|
+
end
|
258
224
|
|
259
225
|
To add a second source directory that will be compiled to the same output directory, just add another watcher:
|
260
226
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
end
|
265
|
-
```
|
227
|
+
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled' do
|
228
|
+
watch(%r{lib/coffeescripts/(.+\.coffee)})
|
229
|
+
end
|
266
230
|
|
267
231
|
which is equivalent to:
|
268
232
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
end
|
274
|
-
```
|
233
|
+
guard 'coffeescript', :output => 'public/javascripts/compiled' do
|
234
|
+
watch(%r{app/coffeescripts/(.+\.coffee)})
|
235
|
+
watch(%r{lib/coffeescripts/(.+\.coffee)})
|
236
|
+
end
|
275
237
|
|
276
238
|
## Development
|
277
239
|
|
278
|
-
-
|
279
|
-
-
|
240
|
+
- Documentation hosted at [RubyDoc](http://rubydoc.info/gems/guard-coffeescript/frames).
|
241
|
+
- Source hosted at [GitHub](https://github.com/netzpirat/guard-coffeescript).
|
242
|
+
- Report issues and feature requests to [GitHub Issues](https://github.com/netzpirat/guard-coffeescript/issues).
|
243
|
+
|
244
|
+
Pull requests are very welcome! Please try to follow these simple "rules", though:
|
280
245
|
|
281
|
-
|
246
|
+
- Please create a topic branch for every separate change you make.
|
247
|
+
- Make sure your patches are well tested.
|
248
|
+
- Update the README (if applicable).
|
249
|
+
- Please **do not change** the version number.
|
282
250
|
|
283
251
|
For questions please join us on our [Google group](http://groups.google.com/group/guard-dev) or on `#guard`
|
284
252
|
(irc.freenode.net).
|
@@ -292,7 +260,10 @@ For questions please join us on our [Google group](http://groups.google.com/grou
|
|
292
260
|
|
293
261
|
## Acknowledgment
|
294
262
|
|
295
|
-
|
263
|
+
[Jeremy Ashkenas][] for [CoffeeScript][], that little language that compiles into JavaScript and makes me enjoy the
|
264
|
+
frontend.
|
265
|
+
|
266
|
+
The [Guard Team](https://github.com/guard/guard/contributors) for giving us such a nice piece of software
|
296
267
|
that is so easy to extend, one *has* to make a plugin for it!
|
297
268
|
|
298
269
|
All the authors of the numerous [Guards](https://github.com/guard) available for making the Guard ecosystem
|
data/lib/guard/coffeescript.rb
CHANGED
@@ -3,19 +3,34 @@ require 'guard/guard'
|
|
3
3
|
require 'guard/watcher'
|
4
4
|
|
5
5
|
module Guard
|
6
|
+
|
7
|
+
# The CoffeeScript guard that gets notifications about the following
|
8
|
+
# Guard events: `start`, `stop`, `reload`, `run_all` and `run_on_change`.
|
9
|
+
#
|
6
10
|
class CoffeeScript < Guard
|
7
11
|
|
8
12
|
autoload :Formatter, 'guard/coffeescript/formatter'
|
9
13
|
autoload :Inspector, 'guard/coffeescript/inspector'
|
10
14
|
autoload :Runner, 'guard/coffeescript/runner'
|
11
15
|
|
12
|
-
|
16
|
+
# Initialize Guard::CoffeeScript.
|
17
|
+
#
|
18
|
+
# @param [Array<Guard::Watcher>] watchers the watchers in the Guard block
|
19
|
+
# @param [Hash] options the options for the Guard
|
20
|
+
# @option options [String] :input the input directory
|
21
|
+
# @option options [String] :output the output directory
|
22
|
+
# @option options [Boolean] :bare do not wrap the output in a top level function
|
23
|
+
# @option options [Boolean] :shallow do not create nested directories
|
24
|
+
# @option options [Boolean] :hide_success hide success message notification
|
25
|
+
# @option options [Boolean] :noop do not generate an output file
|
26
|
+
#
|
27
|
+
def initialize(watchers = [], options = { })
|
13
28
|
watchers = [] if !watchers
|
14
29
|
defaults = {
|
15
|
-
:bare
|
16
|
-
:shallow
|
30
|
+
:bare => false,
|
31
|
+
:shallow => false,
|
17
32
|
:hide_success => false,
|
18
|
-
:noop
|
33
|
+
:noop => false
|
19
34
|
}
|
20
35
|
|
21
36
|
if options[:input]
|
@@ -26,10 +41,19 @@ module Guard
|
|
26
41
|
super(watchers, defaults.merge(options))
|
27
42
|
end
|
28
43
|
|
44
|
+
# Gets called when all specs should be run.
|
45
|
+
#
|
46
|
+
# @return [Boolean] when running all specs was successful
|
47
|
+
#
|
29
48
|
def run_all
|
30
49
|
run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.coffee'))))
|
31
50
|
end
|
32
51
|
|
52
|
+
# Gets called when watched paths and files have changes.
|
53
|
+
#
|
54
|
+
# @param [Array<String>] paths the changed paths and files
|
55
|
+
# @return [Boolean] when running the changed specs was successful
|
56
|
+
#
|
33
57
|
def run_on_change(paths)
|
34
58
|
changed_files, success = Runner.run(Inspector.clean(paths), watchers, options)
|
35
59
|
notify changed_files
|
@@ -39,6 +63,11 @@ module Guard
|
|
39
63
|
|
40
64
|
private
|
41
65
|
|
66
|
+
# Notify changed files back to Guard, so that other Guards can continue
|
67
|
+
# to work with the generated files.
|
68
|
+
#
|
69
|
+
# @param [Array<String>] changed_files the files that have been changed
|
70
|
+
#
|
42
71
|
def notify(changed_files)
|
43
72
|
::Guard.guards.each do |guard|
|
44
73
|
paths = Watcher.match_files(guard, changed_files)
|
@@ -1,32 +1,73 @@
|
|
1
1
|
module Guard
|
2
2
|
class CoffeeScript
|
3
|
+
|
4
|
+
# The Guard::CoffeeScript formatter collects console and
|
5
|
+
# system notification methods and enhances them with
|
6
|
+
# some color information.
|
7
|
+
#
|
3
8
|
module Formatter
|
4
9
|
class << self
|
5
10
|
|
6
|
-
|
11
|
+
# Print an info message to the console.
|
12
|
+
#
|
13
|
+
# @param [String] message the message to print
|
14
|
+
# @param [Hash] options the output options
|
15
|
+
# @option options [Boolean] :reset reset the UI
|
16
|
+
#
|
17
|
+
def info(message, options = { })
|
7
18
|
::Guard::UI.info(message, options)
|
8
19
|
end
|
9
20
|
|
10
|
-
|
21
|
+
# Print a debug message to the console.
|
22
|
+
#
|
23
|
+
# @param [String] message the message to print
|
24
|
+
# @param [Hash] options the output options
|
25
|
+
# @option options [Boolean] :reset reset the UI
|
26
|
+
#
|
27
|
+
def debug(message, options = { })
|
11
28
|
::Guard::UI.debug(message, options)
|
12
29
|
end
|
13
30
|
|
14
|
-
|
31
|
+
# Print a red error message to the console.
|
32
|
+
#
|
33
|
+
# @param [String] message the message to print
|
34
|
+
# @param [Hash] options the output options
|
35
|
+
# @option options [Boolean] :reset reset the UI
|
36
|
+
#
|
37
|
+
def error(message, options = { })
|
15
38
|
::Guard::UI.error(color(message, ';31'), options)
|
16
39
|
end
|
17
40
|
|
18
|
-
|
41
|
+
# Print a green success message to the console.
|
42
|
+
#
|
43
|
+
# @param [String] message the message to print
|
44
|
+
# @param [Hash] options the output options
|
45
|
+
# @option options [Boolean] :reset reset the UI
|
46
|
+
#
|
47
|
+
def success(message, options = { })
|
19
48
|
::Guard::UI.info(color(message, ';32'), options)
|
20
49
|
end
|
21
50
|
|
22
|
-
|
51
|
+
# Outputs a system notification.
|
52
|
+
#
|
53
|
+
# @param [String] message the message to print
|
54
|
+
# @param [Hash] options the output options
|
55
|
+
# @option options [Symbol, String] :image the image to use, either :failed, :pending or :success, or an image path
|
56
|
+
# @option options [String] :title the title of the system notification
|
57
|
+
#
|
58
|
+
def notify(message, options = { })
|
23
59
|
::Guard::Notifier.notify(message, options)
|
24
60
|
end
|
25
61
|
|
26
62
|
private
|
27
63
|
|
64
|
+
# Print a info message to the console.
|
65
|
+
#
|
66
|
+
# @param [String] test the text to colorize
|
67
|
+
# @param [String] color_code the color code
|
68
|
+
#
|
28
69
|
def color(text, color_code)
|
29
|
-
::Guard::UI.send(:color_enabled?) ? "\e[0#{color_code}m#{text}\e[0m" : text
|
70
|
+
::Guard::UI.send(:color_enabled?) ? "\e[0#{ color_code }m#{ text }\e[0m" : text
|
30
71
|
end
|
31
72
|
|
32
73
|
end
|