Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 240 lines (160 sloc) 13.8 kB
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
1 # Barista
02b7a2d @Sutto Add more barista stuff
authored
2
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
3 Barista is a set of tools to make using [CoffeeScript](http://jashkenas.github.com/coffee-script/) in Rails 3, Rails 2 and Rack applications
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
4 easier. You can think of it as similar to [Compass](http://compass-style.org/), but for CoffeeScript instead of [Sass](http://sass-lang.com/).
af37599 @Sutto Let users switch the compilers in a simpler manner, change version nu…
authored
5
e47952a @Sutto Add a rewritten getting started section to the readme
authored
6 As an added bonus, Barista also gives:
7
8 * Automatic support for a `:coffeescript` filter in [Haml](http://haml-lang.com/) (when Haml is loaded before Barista) — automatically converting inline CoffeeScript to JavaScript for you.
9 * Where possible, support for `coffeescript_include_tag` and `coffeescript_tag`.
10 * When possible, instead of pre-compiling in development and test modes, Barista will embed CoffeeScript in the page for you.
11 * Support for Heroku via [therubyracer-heroku](https://github.com/aler/therubyracer-heroku) and either pre-compiled JS or, optionally, a lightweight Rack app that generates on request.
12
13 ## Getting Started
14
15 Out of the box, Barista has semi-automatic support for Rails 3.0, Rails 2 (currently untested) and Sinatra. With a minimal amount of effort, you can also make it work in any Rack-based framework.
16
17 ### Rails 3
a96e3b9 @benatkin added an intro with the main reason I use barista
benatkin authored
18
e47952a @Sutto Add a rewritten getting started section to the readme
authored
19 Adding Barista to your Rails 3 application should as simple as adding two gems to your `Gemfile`, and running two commands. To get started, open up your `Gemfile` and add the following:
20
21 gem "json" # Only needed if on Ruby 1.8 / a platform that ships without JSON
22 gem "barista"
23
24 Next, you'll need to run the the following:
25
26 bundle install
27 rails g barista:install
28
29 This will install the gem into your application and will generate a file in `config/initializers/barista_config.rb` that contains a set of options to configure Barista options.
02b7a2d @Sutto Add more barista stuff
authored
30
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
31 Place your CoffeeScripts in `app/coffeescripts` and Barista will automatically compile them on change into `public/javascripts`.
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
32
e47952a @Sutto Add a rewritten getting started section to the readme
authored
33 ### Rails 2
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
34
e47952a @Sutto Add a rewritten getting started section to the readme
authored
35 Much like on Rails 3, Barista supports deep integration into Rails 2. The only thing missing (that is currently supported in the Rails 3 version) is built in support for generating a config file. If you're using bundler in your application, all you need to do is add:
36
37 gem "json" # Only needed if on Ruby 1.8 / a platform that ships without JSON
38 gem "barista"
39
40 To your `Gemfile`. If you're not using bundler, doing `gem install json barista` and requiring barista both in your application should be enough to get you started.
41
42 If you wish to change the barista configuration, take a look at the [Rails 3 initializer](https://github.com/Sutto/barista/blob/master/lib/generators/barista/install/templates/initializer.rb) and modify it to suite your application as needed.
43
44 ### Sinatra
45
46 Adding Barista to a Sinatra application is a relatively straight forward affair. Like in Rails 2 and Rails 3, you first need to add and require the barista gem and (optionally, the json gem). Unlike Rails 2 and 3 (which set it up automatically), you must also register the extension in your application. So, in the scope of your app (either the top level scope or the `Sinatra::Application` subclass you're using), you then need to simple add:
47
48 register Barista::Integration::Sinatra
49
50 Which will automatically set up the Barista environment and other similar details (e.g. the automatic compilation filter). Since you don't have initializers like you do in Rails, you
51 can then simply run your `Barista.configure` call and block anywhere before your application starts serving requests.
52
53 ### Other Rack-based Frameworks
54
55 Lastly, even though it is built out of the box to support Rails and Sinatra, Barista can also be used with any Rack-based framework. For proper integration, several things must be done. Namely, wherever you declare your middleware (e.g. in a `config.ru` file), you should register the two pieces of middleware barista uses. `Barista::Filter` should only be registered when
56 Barista performs compilation (e.g. in development mode) and `Barista::Server::Proxy` should be registered if you want it to support automatic serving of a `coffeescript.js` file and / or
57 on the fly (versus pre-request compilation) of CoffeeScripts.
58
59 For example, your `config.ru` may look like:
60
61 # Setup goes here...
62 use Barista::Filter if Barista.add_filter?
63 use Barista::Server::Proxy
64 run MyRackApplication
65
66 Next, you need to configure barista anywhere before your the above code is run. e.g by adding the following immediatly preceeding it:
67
68 # Barista (for CoffeeScript Support)
69 Barista.app_root = root
70 Barista.root = File.join(root, 'coffeescripts')
71 Barista.setup_defaults
72 barista_config = root + '/barista_config.rb'
73 require barista_config if File.exist?(barista_config)
74
75 Hence, if you'e using, for example, [serve](https://github.com/jlong/serve) users should have a `config.ru` that looks similar to [this example](https://github.com/YouthTree/site-design/blob/master/config.ru).
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
76
c30119c @Sutto Add more notes to the readme
authored
77 ### A Quick Note on the JSON Gem
78
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
79 Barista indirectly requires the json gem via the coffee-script gem, but it isn't listed as a dependency for very
c30119c @Sutto Add more notes to the readme
authored
80 good reasons. If you encounter errors relating to `require 'json'`, Then you'll need to add either `gem 'json'`
81 or `gem 'json_pure'` to your Gemfile.
82
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
83 If you're already running Ruby 1.9, this will be unnecessary as JSON is shipped as part of the standard library.
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
84
85 ## General Information
86
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
87 Barista transparently compiles CoffeeScript to JavaScript. When a `.coffee` file is changed and the page is refreshed, Barista first regenerates all `.js` files whose `.coffee` sources have been recently changed. 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`).
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
88
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
89 Barista supports using `therubyracer` when installed or, by default, using either the `node` executable or `jsc` (on OS X) to compile your scripts. There is
90 no need for you to install the coffee-script executable in Node as having Node itself, or any of the alternatives available, is enough.
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
91
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
92 When you want to deploy, you can simple run `rake barista:brew` to force the compilation of all JavaScripts for the current application.
75b217f @Sutto Prepare to bump the gem version, start updating the docs
authored
93
1fff5db @Sutto More docs
authored
94 ## In Practice
02b7a2d @Sutto Add more barista stuff
authored
95
1fff5db @Sutto More docs
authored
96 Barista not only supports compiling all JavaScripts on demand (via `rake barista:brew` as above, or `Barista.compile_all!`) but it
97 also ships with a simple Rack server app that will compile on demand for platforms such as Heroku, meaning you don't need write access
98 (although it is helpful).
02b7a2d @Sutto Add more barista stuff
authored
99
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
100 If you're using [Jammit](http://documentcloud.github.com/jammit/), the precompilation phase (e.g. `rake barista:brew` before running Jammit) will make it possible for your application
1fff5db @Sutto More docs
authored
101 to automatically bundle not only normal JavaScripts but also your CoffeeScripts.
02b7a2d @Sutto Add more barista stuff
authored
102
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
103 To add Barista to your project, simply add `gem 'barista', '~> 1.0'` to your Gemfile and run `bundle install`.
762266f @Sutto More docs
authored
104
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
105 Please note that for Jammit compatibility, in test and development mode (by default) it will
1fff5db @Sutto More docs
authored
106 automatically compile all CoffeeScripts that have changed before rendering the page.
f6d9273 @Sutto More docs
authored
107
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
108 Barista works out of the box with Rails 3 (and theoretically, Rails 2) — with support for Rack if
1fff5db @Sutto More docs
authored
109 you're willing to set it up manually. More docs on how to set it up for other platforms
110 will be posted in the near future.
6497165 @Sutto Add Barista.configure, misc doc changes
authored
111
60d87fb @Sutto Updated docs on the barista config
authored
112 ## Sinatra
113
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
114 To use Barista with [Sinatra](http://www.sinatrarb.com/), you'll need to first require the Barista gem in your application
60d87fb @Sutto Updated docs on the barista config
authored
115 and then add the following to your application scope (e.g. if you're using a custom class, there):
116
117 register Barista::Integration::Sinatra
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
118
60d87fb @Sutto Updated docs on the barista config
authored
119 This will automatically setup the filter as needed, setup a server proxy for the `coffee-script.js`
120 file and setup the defaults based on your applications environment
121
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
122 ## Configuration
db73b47 @Sutto More docs!
authored
123
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
124 Please note that Barista lets you configure several options. To do this,
db73b47 @Sutto More docs!
authored
125 it's as simple as setting up an initializer with:
126
127 rails generate barista:install
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
128
db73b47 @Sutto More docs!
authored
129 Then editing `config/initializers/barista_config.rb`. The options available are:
130
131 ### Boolean Options
132
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
133 All of these come in the form of `#option?` (to check its status), `#option=(value)` (to set it)
db73b47 @Sutto More docs!
authored
134 and `#option!` (to set the value to true):
135
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
136 * `verbose` – Output debugging error messages. (Defaults to true in test / dev)
137 * `bare` – Don't wrap the compiled JS in a Closure.
138 * `add_filter` – Automatically add an around filter for processing changes. (Defaults to true in test / dev)
139 * `add_preamble` – Add a time + path preamble to compiled JS. (Defaults to true in test / dev)
140 * `exception_on_error` – Raise an exception on compilation errors (defaults to true)
141 * `embedded_interpreter` – Embeds coffeescript + link to coffee file instead of compiling for include tags and haml filters. (Defaults to true in test / dev)
142 * `auto_compile` – Automatically compile CoffeeScript to JS when CoffeeScript is newer than the generated JS file. After you turn it off, your server will use the generated JS file directly and won't depend on any CoffeeScript compilers. (Defaults is true)
db73b47 @Sutto More docs!
authored
143
144 ### Path options
145
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
146 * `root` – The folder path to read CoffeeScripts from. (Defaults to `app/coffeescripts`.)
147 * `output_root` – The folder to write compiled JS files to. (Defaults to `public/javascripts`.)
148 * `change_output_prefix!` – Method to change the output prefix for a framework.
149 * `verbose` – Whether or not Barista will add a preamble to files.
150 * `js_path` – Path to the pure-JavaScript compiler.
151 * `env` – The application environment. (Defaults to `Rails.env`.)
152 * `app_root` – The application's root path.
153 * `bin_path` – The path to the `node` executable if non-standard and not using `therubyracer`.
db73b47 @Sutto More docs!
authored
154 * All of the hook methods mentioned below.
155
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
156 ## Frameworks
55afa0c @Sutto Add framework docs
authored
157
af37599 @Sutto Let users switch the compilers in a simpler manner, change version nu…
authored
158 One of the other main features Barista adds (over other tools) is frameworks similar
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
159 to Compass. The idea being, you add CoffeeScripts at runtime from gems etc. To do this,
160 in your gem just have a `coffeescript` directory and then in your gem add the following code:
55afa0c @Sutto Add framework docs
authored
161
162 Barista::Framework.register 'name', 'full-path-to-directory' if defined?(Barista::Framework)
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
163
55afa0c @Sutto Add framework docs
authored
164 For an example of this in practice, check out [bhm-google-maps](http://github.com/YouthTree/bhm-google-maps)
83f23b6 @Sutto Readme info
authored
165 or, the currently-in-development, [shuriken](http://github.com/Sutto/shuriken). The biggest advantage of this
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
166 is you can then manage JS dependencies using existing tools like Bundler.
55afa0c @Sutto Add framework docs
authored
167
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
168 In your `Barista.configure` block, you can also configure on a per-application basis the output directory
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
169 for individual frameworks (e.g. put shuriken into `vendor/shuriken`, bhm-google-maps into `vendor/bhm-google-maps`):
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
170
91a269d @Sutto WIP implementation of hooks
authored
171 Barista.configure do |c|
172 c.change_output_prefix! 'shuriken', 'vendor/shuriken'
173 c.change_output_prefix! 'bhm-google-maps', 'vendor/bhm-google-maps'
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
174 end
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
175
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
176 Alternatively, to prefix all, you can use `Barista.each_framework` (if you pass true, it includes the 'default' framework
177 which is your application root).
178
91a269d @Sutto WIP implementation of hooks
authored
179 Barista.configure do |c|
180 c.each_framework do |framework|
181 c.change_output_prefix! framework.name, "vendor/#{framework.name}"
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
182 end
183 end
91a269d @Sutto WIP implementation of hooks
authored
184
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
185 ## Hooks
186
187 Barista lets you hook into the compilation at several stages, namely:
91a269d @Sutto WIP implementation of hooks
authored
188
189 * before compilation
190 * after compilation
191 * after compilation fails
8aeb5be Updated documentation
Matt Dean authored
192 * after compilation complete
91a269d @Sutto WIP implementation of hooks
authored
193
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
194 To hook into these hooks, you can do the following:
91a269d @Sutto WIP implementation of hooks
authored
195
196 * `Barista.before_compilation { |path| puts "Barista: Compiling #{path}" }`
197 * `Barista.on_compilation { |path| puts "Barista: Successfully compiled #{path}" }`
fd82e0f @Sutto Update to support hooks
authored
198 * `Barista.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" }`
91a269d @Sutto WIP implementation of hooks
authored
199 * `Barista.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" }`
8aeb5be Updated documentation
Matt Dean authored
200 * `Barista.on_compilation_complete { puts "Barista: Successfully compiled all files" }`
91a269d @Sutto WIP implementation of hooks
authored
201
202 These allow you to do things such as notify on compilation, automatically
203 perform compression post compilation and a variety of other cool things.
85060b1 @Sutto Bump version, allow you to change prefixes for frameworks
authored
204
4bc4b7c @Sutto Add barista_growl to the README
authored
205 An excellent example of these hooks in use is [barista\_growl](http://github.com/TrevorBurnham/barista_growl),
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
206 by Trevor Burnham — a gem perfect for development purposes that automatically shows Growl messages
4bc4b7c @Sutto Add barista_growl to the README
authored
207 on compilation.
208
a3f160e @Sutto Add documentation updates
authored
209 # Contributors / Credits
210
211 The following people have all contributed to Barista:
212
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
213 * [Xavier Shay](https://github.com/xaviershay) – Added preamble text to generated text in verbose mode.
214 * [einarmagnus](https://github.com/einarmagnus) – Fixed jruby support.
215 * [Matt Dean](https://github.com/trabian) – Added `before_full_compilation` and `on_compilation_complete` hooks.
216 * [Trevor Burnham](https://github.com/TrevorBurnham) – Misc. documentation tweaks and hooks idea.
217 * [Sean McCullough](https://github.com/mcculloughsean) – Initial switch to support bare (vs. no\_wrap)
218 * [Ben Atkin](https://github.com/benatkin) – Docs work.
219 * [Ben Hoskings](https://github.com/benhoskings) – Misc. fixes, added preamble support.
220 * [Kim Joar Bekkelund](https://github.com/kjbekkelund) – Docs work.
a3f160e @Sutto Add documentation updates
authored
221
db73b47 @Sutto More docs!
authored
222 Barista was originally heavily inspired by [Bistro Car](https://github.com/jnicklas/bistro_car), but has taken a few fundamentally
a3f160e @Sutto Add documentation updates
authored
223 different approach in a few areas.
224
db73b47 @Sutto More docs!
authored
225 Barista builds upon the awesome [coffee-script](https://github.com/josh/ruby-coffee-script) gem.
226
227 It's all possible thanks to [CoffeeScript](https://github.com/jashkenas/coffee-script) by Jeremy Ashkenas.
6497165 @Sutto Add Barista.configure, misc doc changes
authored
228
870add6 @Sutto Contributor details for Barista
authored
229 ## Note on Patches/Pull Requests ##
ef28ac5 @kjbekkelund Cleaned up the docs to make them more readable.
kjbekkelund authored
230
870add6 @Sutto Contributor details for Barista
authored
231 1. Fork the project.
232 2. Make your feature addition or bug fix.
233 3. Add tests for it. This is important so I don't break it in a future version unintentionally.
234 4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
235 5. Send me a pull request. Bonus points for topic branches.
236
237 ## Copyright ##
238
a96e3b9 @benatkin added an intro with the main reason I use barista
benatkin authored
239 Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
Something went wrong with that request. Please try again.