barista 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --debug
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use "ree-1.8.7-2010.02@barista"
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem 'jeweler'
5
+ end
6
+
7
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ barista (0.5.1)
5
+ open4
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ gemcutter (0.6.1)
11
+ git (1.2.5)
12
+ jeweler (1.4.0)
13
+ gemcutter (>= 0.1.0)
14
+ git (>= 1.2.5)
15
+ rubyforge (>= 2.0.0)
16
+ json_pure (1.4.6)
17
+ open4 (1.0.1)
18
+ rubyforge (2.0.4)
19
+ json_pure (>= 1.1.7)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ barista!
26
+ jeweler
27
+ open4
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Barista #
2
2
 
3
+ Barista is a rails plugin that transparently compiles CoffeeScript to JavaScript. When a `.coffee` file is changed and the page is refreshed, it delays sending the `.js` file until it's been regenerated from the newly modified CoffeeScript. This way, you can refresh immediately after saving the `.coffee` file, and not worry about an old `.js` file being sent to the browser (as often happens when using `coffee --watch`).
4
+
5
+ Barista supports using `therubyracer` when installed or, by default, the node.js version of CoffeeScript via the `coffee` executable.
6
+
3
7
  Barista is very, very similar to [bistro\_car](http://github.com/jnicklas/bistro_car) (infact, credit where credit is due - it shares similar
4
8
  code / is almost a fork).
5
9
 
@@ -27,9 +31,9 @@ Barista require rails 3+ (but patches for Rails 2 will be accepted.)
27
31
 
28
32
  ## Frameworks ##
29
33
 
30
- One of the other main features Barista adds (over bistro\_car) is frameworks similar
31
- to Compass. The idea being, you add coffeescripts at runtime from gems etc. To do this,
32
- in your gem just have a coffeescript directory and then in you gem add the following code:
34
+ One of the other main features Barista adds (over other tools) is frameworks similar
35
+ to Compass. The idea being, you add coffee scripts at runtime from gems etc. To do this,
36
+ in your gem just have a `coffeescript` directory and then in you gem add the following code:
33
37
 
34
38
  Barista::Framework.register 'name', 'full-path-to-directory' if defined?(Barista::Framework)
35
39
 
@@ -83,16 +87,38 @@ on compilation.
83
87
  Please note that barista lets you configure several options. To do this,
84
88
  it's as simple as setting up an initializer with:
85
89
 
86
- rails generate barista_install
90
+ rails generate barista:install
87
91
 
88
92
  Then editing `config/initializers/barista_config.rb`.
89
93
 
90
94
  Currently available options are:
91
95
 
92
- * root - the folder path to read coffeescripts from, defaults to app/coffeescripts
93
- * output\_root - the folder to write them into, defautls to public/javascripts.
94
- * no\_wrap - stop coffee from automatically wrapping JS in a closure.
95
- * change\_output\_prefix! - method to change the output prefix for a framework.
96
+ * `root` - the folder path to read coffeescripts from, defaults to app/coffeescripts
97
+ * `output_root` - the folder to write them into, defaults to public/javascripts.
98
+ * `no_wrap` and `bare` - stop coffee from automatically wrapping JS in a closure.
99
+ * `change_output_prefix!` - method to change the output prefix for a framework.
100
+ * All of the hooks mentioned above.
101
+ * `verbose` - whether or not barista will add a preamble to files.
102
+ * `compiler` - One of `:native` or `:node`, to force the compiler version.
103
+ * `compiler_klass` - A custom compiler class.
104
+ * `js_path` - Path to the pure-javascript compiler
105
+
106
+ # Contributors / Credits
107
+
108
+ The following people have all contributed to Barista:
109
+
110
+ * [Xavier Shay](https://github.com/xaviershay) - Added preamble text to generated text in verbose mode.
111
+ * [einarmagnus](https://github.com/einarmagnus) - Fixed jruby support.
112
+ * [Matt Dean](https://github.com/trabian) - Added `before_full_compilation` and `on_compilation_complete` hooks.
113
+ * [Trevor Burnham](https://github.com/TrevorBurnham) - Misc. documentation tweaks and hooks idea.
114
+ * [Sean McCullough](https://github.com/mcculloughsean) - Initial switch to support bare (vs. no\_wrap)
115
+ * [Ben Atkin](https://github.com/benatkin) - Docs work.
116
+
117
+ Barista was originally heavily inspired by [Bistro Car](https://github.com/jnicklas/bistro_car), but taking a fundamentally
118
+ different approach in a few areas.
119
+
120
+ The Native JavaScript compiler was heavily inspired by and based on [Sam Stephenson's](https://github.com/sstephenson) fork of
121
+ the ruby-coffee-script gem. All credit for the idea goes to him and the code is based on his simple approach.
96
122
 
97
123
  ## Note on Patches/Pull Requests ##
98
124
 
@@ -104,4 +130,4 @@ Currently available options are:
104
130
 
105
131
  ## Copyright ##
106
132
 
107
- Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
133
+ Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
data/Rakefile CHANGED
@@ -14,6 +14,12 @@ begin
14
14
  gem.version = Barista::Version::STRING
15
15
  gem.authors = ["Darcy Laycock"]
16
16
  gem.add_dependency 'open4'
17
+ gem.add_dependency 'railties', '~> 3.0'
18
+
19
+ gem.add_development_dependency 'rspec', '~> 2.0.0.beta.22'
20
+ gem.add_development_dependency 'rr', '~> 1.0'
21
+ gem.add_development_dependency 'therubyracer'
22
+
17
23
  end
18
24
  Jeweler::GemcutterTasks.new
19
25
  rescue LoadError
data/barista.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{barista}
8
- s.version = "0.5.1"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Darcy Laycock"]
12
- s.date = %q{2010-09-19}
12
+ s.date = %q{2010-11-15}
13
13
  s.description = %q{Automatically compiles app/coffeescripts/*.coffee to javascript for rails awesomesauce.}
14
14
  s.email = %q{sutto@sutto.net}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ ".rspec",
23
+ ".rvmrc",
24
+ "Gemfile",
25
+ "Gemfile.lock",
22
26
  "LICENSE",
23
27
  "README.md",
24
28
  "Rakefile",
@@ -27,20 +31,32 @@ Gem::Specification.new do |s|
27
31
  "config/routes.rb",
28
32
  "lib/barista.rb",
29
33
  "lib/barista/compiler.rb",
34
+ "lib/barista/compilers.rb",
35
+ "lib/barista/compilers/base.rb",
36
+ "lib/barista/compilers/native.rb",
37
+ "lib/barista/compilers/node.rb",
30
38
  "lib/barista/filter.rb",
31
39
  "lib/barista/framework.rb",
32
40
  "lib/barista/hooks.rb",
33
41
  "lib/barista/tasks/barista.rake",
34
42
  "lib/barista/version.rb",
35
- "lib/generators/barista_install/USAGE",
36
- "lib/generators/barista_install/barista_install_generator.rb",
37
- "lib/generators/barista_install/templates/initializer.rb"
43
+ "lib/coffee-script/coffee-script-0.9.4.js",
44
+ "lib/generators/barista/install/USAGE",
45
+ "lib/generators/barista/install/install_generator.rb",
46
+ "lib/generators/barista/install/templates/initializer.rb",
47
+ "lib/generators/barista_install_generator.rb",
48
+ "spec/barista_spec.rb",
49
+ "spec/spec_helper.rb"
38
50
  ]
39
51
  s.homepage = %q{http://github.com/Sutto/barista}
40
52
  s.rdoc_options = ["--charset=UTF-8"]
41
53
  s.require_paths = ["lib"]
42
54
  s.rubygems_version = %q{1.3.7}
43
55
  s.summary = %q{Transparent coffeescript support for rails 3.}
56
+ s.test_files = [
57
+ "spec/barista_spec.rb",
58
+ "spec/spec_helper.rb"
59
+ ]
44
60
 
45
61
  if s.respond_to? :specification_version then
46
62
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -48,11 +64,23 @@ Gem::Specification.new do |s|
48
64
 
49
65
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
66
  s.add_runtime_dependency(%q<open4>, [">= 0"])
67
+ s.add_runtime_dependency(%q<railties>, ["~> 3.0"])
68
+ s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
69
+ s.add_development_dependency(%q<rr>, ["~> 1.0"])
70
+ s.add_development_dependency(%q<therubyracer>, [">= 0"])
51
71
  else
52
72
  s.add_dependency(%q<open4>, [">= 0"])
73
+ s.add_dependency(%q<railties>, ["~> 3.0"])
74
+ s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
75
+ s.add_dependency(%q<rr>, ["~> 1.0"])
76
+ s.add_dependency(%q<therubyracer>, [">= 0"])
53
77
  end
54
78
  else
55
79
  s.add_dependency(%q<open4>, [">= 0"])
80
+ s.add_dependency(%q<railties>, ["~> 3.0"])
81
+ s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
82
+ s.add_dependency(%q<rr>, ["~> 1.0"])
83
+ s.add_dependency(%q<therubyracer>, [">= 0"])
56
84
  end
57
85
  end
58
86
 
data/lib/barista.rb CHANGED
@@ -8,6 +8,7 @@ module Barista
8
8
  CompilerUnavailableError = Class.new(Error)
9
9
 
10
10
  autoload :Compiler, 'barista/compiler'
11
+ autoload :Compilers, 'barista/compilers'
11
12
  autoload :Filter, 'barista/filter'
12
13
  autoload :Framework, 'barista/framework'
13
14
  autoload :Hooks, 'barista/hooks'
@@ -135,22 +136,38 @@ module Barista
135
136
  Rails.logger.debug "[Barista] #{message}" if defined?(Rails.logger) && Rails.logger
136
137
  end
137
138
 
138
- # By default, only add it in dev / test
139
- def add_filter?
140
- Rails.env.test? || Rails.env.development?
139
+ def verbose?
140
+ @verbose ||= (defined?(Rails) && (Rails.env.test? || Rails.env.development?))
141
141
  end
142
+
143
+ def verbose!
144
+ self.verbose = true
145
+ end
146
+
147
+ def verbose=(value)
148
+ @verbose = !!value
149
+ end
150
+
151
+ alias add_filter? verbose?
152
+ alias add_preamble? verbose?
142
153
 
143
154
  def no_wrap?
144
155
  defined?(@no_wrap) && @no_wrap
145
156
  end
157
+ alias bare? no_wrap?
146
158
 
147
159
  def no_wrap!
148
160
  self.no_wrap = true
149
161
  end
162
+ alias bare! no_wrap!
150
163
 
151
164
  def no_wrap=(value)
152
165
  @no_wrap = !!value
153
166
  end
167
+ alias bare= no_wrap=
168
+
169
+ delegate :bin_path, :bin_path=, :js_path, :js_path=, :compiler, :compiler=,
170
+ :compiler_klass, :compiler_klass=, :to => Compiler
154
171
 
155
172
  end
156
173
 
@@ -1,86 +1,51 @@
1
1
  require 'digest/sha2'
2
- require 'open4'
3
2
 
4
3
  module Barista
5
4
  class Compiler
6
5
 
7
- class << self; attr_accessor :bin_path; end
8
- self.bin_path ||= "coffee"
9
-
10
- def self.available?
11
- @coffee_available ||= system("which '#{self.bin_path}' >/dev/null 2>&1")
12
- end
13
-
14
- def self.check_availability!(silence = false)
15
- available?.tap do |available|
16
- if !available && Barista.exception_on_error? && !silence
17
- raise CompilerUnavailableError, "The coffeescript compiler '#{self.bin_path}' could not be found."
18
- end
19
- end
6
+ class << self
7
+ attr_accessor :bin_path, :js_path
20
8
  end
9
+ self.bin_path ||= "coffee"
10
+ self.js_path ||= File.expand_path('../coffee-script/coffee-script-0.9.4.js', File.dirname(__FILE__))
21
11
 
22
- def self.compile(path, options = {})
23
- new(path, options).to_js
12
+ def self.compilers
13
+ [Compilers::Native, Compilers::Node]
24
14
  end
25
-
26
- def initialize(path, options = {})
27
- @compiled = false
28
- @options = {}
29
- @path = path
15
+
16
+ def self.compiler=(value)
17
+ name = "Barista::Compilers::#{value.to_s.classify}".constantize
18
+ self.compiler_klass = name
19
+ rescue
20
+ self.compiler_klass = nil
30
21
  end
31
-
32
- def compile!
33
- # Compiler code thanks to bistro_car.
34
- @compiled_content = invoke_coffee(@path)
35
- @compiled = true
22
+
23
+ def self.compiler
24
+ compiler_klass.name.underscore.gsub("barista/compilers/", '').to_sym
36
25
  end
37
-
38
- def to_js
39
- compile! unless @compiled
40
- @compiled_content
26
+
27
+ def self.compiler_klass
28
+ @compiler_klass ||= compilers.detect(&:available?)
41
29
  end
42
-
43
- def self.dirty?(from, to)
44
- File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
30
+
31
+ def self.compiler_klass=(value)
32
+ @compiler_klass = value.present? ? value : nil
45
33
  end
46
34
 
47
- protected
48
-
49
- def coffee_options
50
- ["-p"].tap do |options|
51
- options << "--no-wrap" if Barista.no_wrap?
52
- end.join(" ")
35
+ def self.available?
36
+ compiler_klass.present?
53
37
  end
54
38
 
55
- def invoke_coffee(path)
56
- command = "#{self.class.bin_path} #{coffee_options} '#{path}'".squeeze(' ')
57
- Barista.invoke_hook :before_compilation, path
58
-
59
- #jruby cannot use open4 because it uses fork.
60
- #This should hopefully work for both jruby and ruby
61
- popen_class = IO.respond_to?(:popen4) ? IO : Open4
62
-
63
- pid, stdin, stdout, stderr = popen_class.popen4(command)
64
- stdin.close
65
- _, status = Process.waitpid2(pid)
66
- out = stdout.read.strip
67
- err = stderr.read.strip
68
- if status.success?
69
- if err.blank?
70
- Barista.invoke_hook :compiled, path
71
- else
72
- Barista.invoke_hook :compiled_with_warning, path, err
73
- end
74
- else
75
- Barista.invoke_hook :compilation_failed, path, err
76
- if Barista.exception_on_error? && !@options[:silence]
77
- raise CompilationError, "\"#{command}\" exited with a non-zero status."
78
- else
79
- out = nil
39
+ def self.check_availability!(silence = false)
40
+ available?.tap do |available|
41
+ if !available && Barista.exception_on_error? && !silence
42
+ raise CompilerUnavailableError, "A coffee script compiler is currently unavailable. Please install therubyracer or coffee-script via node"
80
43
  end
81
44
  end
82
- out
83
45
  end
84
46
 
47
+ def self.compile(path, options = {})
48
+ compiler_klass.new(path, options).to_js
49
+ end
85
50
  end
86
51
  end
@@ -0,0 +1,9 @@
1
+ module Barista
2
+ module Compilers
3
+
4
+ autoload :Base, 'barista/compilers/base'
5
+ autoload :Native, 'barista/compilers/native'
6
+ autoload :Node, 'barista/compilers/node'
7
+
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module Barista
2
+ module Compilers
3
+ class Base
4
+
5
+ def self.available?
6
+ false
7
+ end
8
+
9
+ def initialize(path, options = {})
10
+ @compiled = false
11
+ @options = {}
12
+ @path = path
13
+ end
14
+
15
+ def compile!
16
+ @compiled_content = invoke_coffee(@path)
17
+ @compiled_content = preamble + @compiled_content if Barista.add_preamble?
18
+ @compiled = true
19
+ end
20
+
21
+ def to_js
22
+ compile! unless @compiled
23
+ @compiled_content
24
+ end
25
+
26
+ def self.dirty?(from, to)
27
+ File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
28
+ end
29
+
30
+ protected
31
+
32
+ def preamble
33
+ "/* DO NOT MODIFY. This file was compiled from\n * #{@path}\n */\n\n"
34
+ end
35
+
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ # Most of the code in here is from sstephenson's fantastic
2
+ # fork of the ruby-coffee-script gem (since merged in to master).
3
+ #
4
+ # All credit hence belongs with him - I simply ported it to
5
+ # work with Barista.
6
+ module Barista
7
+ module Compilers
8
+ class Native < Base
9
+
10
+ def self.available?
11
+ @native_compiler_available ||= begin
12
+ require 'v8'
13
+ true
14
+ rescue LoadError
15
+ false
16
+ end
17
+ end
18
+
19
+ def self.coffee_script_source
20
+ File.read(Compiler.js_path)
21
+ end
22
+
23
+ def self.no_wrap_option
24
+ @no_wrap_option ||= coffee_script_source.include?('noWrap') ? 'noWrap' : 'bare'
25
+ end
26
+
27
+ def self.coffee_script
28
+ @coffee_script ||= V8::Context.new.tap do |c|
29
+ c.eval(coffee_script_source)
30
+ end["CoffeeScript"]
31
+ end
32
+
33
+ protected
34
+
35
+ def coffee_script
36
+ self.class.coffee_script
37
+ end
38
+
39
+ def invoke_coffee(path)
40
+ script = File.read(path)
41
+ Barista.invoke_hook :before_compilation, path
42
+ out = coffee_script.compile(script, self.class.no_wrap_option => Barista.no_wrap?)
43
+ Barista.invoke_hook :compiled, path
44
+ out
45
+ rescue V8::JSError => e
46
+ Barista.invoke_hook :compilation_failed, path, e.message
47
+ if Barista.exception_on_error? && !@options[:silence]
48
+ raise CompilationError, "CoffeeScript via V8 raised an exception"
49
+ end
50
+ nil
51
+ end
52
+
53
+ end
54
+ end
55
+ end