guard-coffeescript 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = Guard::CoffeeScript
2
2
 
3
- CoffeeScript guard allows to automatically & intelligently compile your CoffeeScripts when files are modified.
3
+ Guard::CoffeeScript compiles you CoffeeScripts automatically when files are modified.
4
4
 
5
5
  - Tested on Ruby 1.8.7 & 1.9.2.
6
6
 
@@ -32,7 +32,7 @@ Please read {guard usage doc}[http://github.com/guard/guard#readme]
32
32
 
33
33
  == Guardfile
34
34
 
35
- CoffeeScript guard can be really be adapated to all kind of projects.
35
+ CoffeeScript guard can be adapated to all kind of projects.
36
36
  Please read {guard doc}[http://github.com/guard/guard#readme] for more info about Guardfile DSL.
37
37
 
38
38
  === Standard ruby gems
@@ -47,6 +47,37 @@ Please read {guard doc}[http://github.com/guard/guard#readme] for more info abou
47
47
  watch('^app/coffeescripts/(.*)\.coffee')
48
48
  end
49
49
 
50
+ == Options
51
+
52
+ There following options can be passed to the CoffeeSCript Guard:
53
+
54
+ :output => 'javascripts' # Relative path to the output directory
55
+ :wrap => false # Compile without the top-level function wrapper
56
+ :shallow => true # Do not create nested output directories
57
+
58
+ === Nested directories
59
+
60
+ 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:
61
+
62
+ A file
63
+
64
+ /app/coffeescripts/ui/buttons/toggle_button.coffee
65
+
66
+ that has been detected by the watch
67
+
68
+ watch('^app/coffeescripts/(.*)\.coffee')
69
+
70
+ with an output directory of
71
+
72
+ :output => 'public/javascripts/compiled'
73
+
74
+ will be compiled to
75
+
76
+ public/javascripts/compiled/ui/buttons/toggle_button.js
77
+
78
+ This behaviour can be switched off by passing the option ':shallow => false' to the guard, so that all JavaScripts will
79
+ be compiled directly to the output directory.
80
+
50
81
  == Development
51
82
 
52
83
  - Source hosted at {GitHub}[http://github.com/netzpirat/guard-coffeescript]
@@ -58,7 +89,7 @@ Pull requests are very welcome! Make sure your patches are well tested.
58
89
 
59
90
  {Michael Kessler}[http://github.com/netzpirat]
60
91
 
61
- == Kudo
92
+ == Acknowledgment
93
+
94
+ Many thanks to the {Guard Team}[https://github.com/guard/guard/contributors] and all the authors of the numerous {Guards}[http://github.com/guard]
62
95
 
63
- Many thanks to {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg] for creating the excellent {guard}[http://github.com/guard/guard]
64
- gem.
@@ -5,23 +5,24 @@ require 'guard/watcher'
5
5
  module Guard
6
6
  class CoffeeScript < Guard
7
7
 
8
- autoload :Runner, 'guard/coffeescript/runner'
9
8
  autoload :Inspector, 'guard/coffeescript/inspector'
9
+ autoload :Runner, 'guard/coffeescript/runner'
10
+ autoload :Compiler, 'guard/coffeescript/compiler'
10
11
 
11
12
  def initialize(watchers = [], options = {})
12
- @watchers, @options = watchers, options
13
- @options[:output] ||= 'javascripts'
14
- @options[:nowrap] ||= false
13
+ super(watchers, {
14
+ :output => 'javascripts',
15
+ :wrap => true,
16
+ :shallow => false
17
+ }.merge(options))
15
18
  end
16
19
 
17
20
  def run_all
18
- paths = Inspector.clean(Watcher.match_files(self, Dir.glob(File.join('**', '*.coffee'))))
19
- Runner.run(paths, options.merge(:message => 'Compile all CoffeeScripts')) unless paths.empty?
21
+ run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.coffee'))))
20
22
  end
21
23
 
22
24
  def run_on_change(paths)
23
- paths = Inspector.clean(paths)
24
- Runner.run(paths, options) unless paths.empty?
25
+ Runner.run(Inspector.clean(paths), watchers, options)
25
26
  end
26
27
 
27
28
  end
@@ -0,0 +1,75 @@
1
+ module Guard
2
+ class CoffeeScript
3
+
4
+ # This has been kindly borrowed from https://github.com/josh/ruby-coffee-script/raw/master/lib/coffee_script.rb
5
+ # due to namespace conflicts with the guards own CoffeeScript class
6
+ #
7
+ # The only change to the original file is to redirect stderr to stdout when executing the compiler,
8
+ # so that the error messages can easily be parsed and sent to the Growl notifier.
9
+ #
10
+ module Compiler
11
+ class << self
12
+ def locate_coffee_bin
13
+ out = `which coffee`
14
+ if $?.success?
15
+ out.chomp
16
+ else
17
+ raise LoadError, "could not find `coffee` in PATH"
18
+ end
19
+ end
20
+
21
+ def coffee_bin
22
+ @@coffee_bin ||= locate_coffee_bin
23
+ end
24
+
25
+ def coffee_bin=(path)
26
+ @@coffee_bin = path
27
+ end
28
+
29
+ def version
30
+ `#{coffee_bin} --version`[/(\d|\.)+/]
31
+ end
32
+
33
+ # Compile a script (String or IO) to JavaScript.
34
+ def compile(script, options = {})
35
+ args = "-sp"
36
+
37
+ if options[:wrap] == false ||
38
+ options.key?(:bare) ||
39
+ options.key?(:no_wrap)
40
+ args += " --#{no_wrap_flag}"
41
+ end
42
+
43
+ execute_coffee(script, args)
44
+ end
45
+
46
+ # Evaluate a script (String or IO) and return the stdout.
47
+ # Note: the first argument will be process.argv[3], the second
48
+ # process.argv[4], etc.
49
+ def evaluate(script, *args)
50
+ execute_coffee(script, "-s #{args.join(' ')}")
51
+ end
52
+
53
+ private
54
+ def execute_coffee(script, args)
55
+ command = "#{coffee_bin} #{args} 2>&1"
56
+ script = script.read if script.respond_to?(:read)
57
+
58
+ IO.popen(command, "w+") do |f|
59
+ f << script
60
+ f.close_write
61
+ f.read
62
+ end
63
+ end
64
+
65
+ def no_wrap_flag
66
+ if `#{coffee_bin} --help`.lines.grep(/--no-wrap/).any?
67
+ 'no-wrap'
68
+ else
69
+ 'bare'
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -3,46 +3,82 @@ module Guard
3
3
  module Runner
4
4
  class << self
5
5
 
6
- def run(paths, options = {})
6
+ def run(files, watchers, options = {})
7
+ message = notify_start(files, options)
8
+ errors = compile_files(files, options, watchers)
9
+ notify_result(errors, message)
10
+
11
+ rescue LoadError
12
+ ::Guard::UI.error "Command 'coffee' not found. Please install CoffeeScript."
13
+ end
7
14
 
8
- if coffee_executable_exists?
9
- message = options[:message] || "Compile #{paths.join(' ')}"
10
- ::Guard::UI.info message, :reset => true
15
+ private
11
16
 
12
- output = `#{ coffee_script_command(paths, options) } 2>&1`
17
+ def notify_start(files, options)
18
+ message = options[:message] || "Compile #{ files.join(', ') }"
19
+ ::Guard::UI.info message, :reset => true
20
+ message
21
+ end
13
22
 
14
- puts output
23
+ def compile_files(files, options, watchers)
24
+ errors = []
25
+ directories = detect_nested_directories(watchers, files, options)
15
26
 
16
- if $?.to_i == 0
17
- message = message.gsub(/^Compile/, 'Successfully compiled')
18
- ::Guard::Notifier.notify(message, :title => 'CoffeeScript results')
19
- else
20
- message = output.split("\n").select { |line| line =~ /^Error:/ }.join("\n")
21
- ::Guard::Notifier.notify(message, :title => 'CoffeeScript results', :image => :failed)
27
+ directories.each do |directory, scripts|
28
+ directory = File.expand_path(directory)
29
+ scripts.each do |file|
30
+ content, success = compile(file, options)
31
+ process_compile_result(content, file, directory, errors, success)
22
32
  end
33
+ end
34
+
35
+ errors
36
+ end
37
+
38
+ def compile(file, options)
39
+ content = Compiler.compile(File.open(file), options)
40
+ [content, $?.success?]
41
+ end
23
42
 
43
+ def process_compile_result(content, file, directory, errors, success)
44
+ if success
45
+ FileUtils.mkdir_p(directory) if !File.directory?(directory)
46
+ File.open(File.join(directory, File.basename(file)), 'w') { |f| f.write(content) }
24
47
  else
25
- ::Guard::UI.error "Command 'coffee' not found. Please install CoffeeScript."
48
+ errors << file + ': ' + content.split("\n").select { |line| line =~ /^Error:/ }.join("\n")
49
+ ::Guard::UI.error(content)
26
50
  end
27
51
  end
28
52
 
29
- private
53
+ def detect_nested_directories(watchers, files, options)
54
+ return { options[:output] => files } if options[:shallow]
55
+
56
+ directories = {}
30
57
 
31
- def coffee_script_command(paths, options)
32
- cmd_parts = []
33
- cmd_parts << 'coffee'
34
- cmd_parts << '-c'
35
- cmd_parts << '--no-wrap' if options[:nowrap]
36
- cmd_parts << "-o #{ options[:output] }"
37
- cmd_parts << paths.join(' ')
38
- cmd_parts.join(' ')
58
+ watchers.product(files).each do |watcher, file|
59
+ if matches = file.match(watcher.pattern)
60
+ target = File.join(options[:output], File.dirname(matches[1]))
61
+ if directories[target]
62
+ directories[target] << file
63
+ else
64
+ directories[target] = [file]
65
+ end
66
+ end
67
+ end
68
+
69
+ directories
39
70
  end
40
71
 
41
- def coffee_executable_exists?
42
- system('which coffee > /dev/null 2>/dev/null')
72
+ def notify_result(errors, message)
73
+ if errors.empty?
74
+ message = message.gsub(/^Compile/, 'Successfully compiled')
75
+ ::Guard::Notifier.notify(message, :title => 'CoffeeScript results')
76
+ else
77
+ ::Guard::Notifier.notify(errors.join("\n"), :title => 'CoffeeScript results', :image => :failed)
78
+ end
43
79
  end
44
80
 
45
81
  end
46
82
  end
47
83
  end
48
- end
84
+ end
@@ -1,3 +1,3 @@
1
- guard 'coffeescript', :output => 'public/javascripts/compiled', :nowrap => false do
1
+ guard 'coffeescript', :output => 'public/javascripts/compiled' do
2
2
  watch('^app/coffeescripts/(.*)\.coffee')
3
3
  end
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module CoffeeScriptVersion
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-coffeescript
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Kessler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-29 00:00:00 +02:00
18
+ date: 2010-11-11 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -92,6 +92,7 @@ extensions: []
92
92
  extra_rdoc_files: []
93
93
 
94
94
  files:
95
+ - lib/guard/coffeescript/compiler.rb
95
96
  - lib/guard/coffeescript/inspector.rb
96
97
  - lib/guard/coffeescript/runner.rb
97
98
  - lib/guard/coffeescript/templates/Guardfile