Skip to content

Commit

Permalink
Add more barista stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sutto committed Apr 24, 2010
1 parent 017bc18 commit 02b7a2d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 14 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
# Barista

Barista is very, very similar to [bistro\_car](http://github.com/jnicklas/bistro_car) (infact, credit where credit is due - it shares similar
code / is almost a fork).

The main difference being, it lets you use coffee as you would javascript. Simply put, Write coffee
and place it in `app/scripts` and Barista will automatically serve it as if it was placed in `public/javascripts`

That is, `app/scripts/demo.coffee` will work for `/javascripts/demo.js`. Even better (and more importantly
for me), it provides `Barista.compile_all!` which takes all coffee files and compiles them into `public/javascripts`.

If you're using Jammit, this means you can simple run a rake task (`rake barista:brew` before running jammit) and
your coffeescripts will be automatically provided, ready for bundling.

Barista require rails 3+ (but patches for Rails 2 will be accepted.)
15 changes: 9 additions & 6 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,18 @@
require 'rubygems' require 'rubygems'
require 'rake' require 'rake'


require 'lib/barista/version'

begin begin
require 'jeweler' require 'jeweler'
Jeweler::Tasks.new do |gem| Jeweler::Tasks.new do |gem|
gem.name = "barista" gem.name = "barista"
gem.summary = %Q{TODO: one-line summary of your gem} gem.summary = %Q{Transparent coffeescript support for rails 3}
gem.description = %Q{TODO: longer description of your gem} gem.description = %Q{Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.}
gem.email = "sutto@sutto.net" gem.email = "sutto@sutto.net"
gem.homepage = "http://github.com/Sutto/barista" gem.homepage = "http://github.com/Sutto/barista"
gem.authors = ["Darcy Laycock"] gem.version = Barista::Version::STRING
gem.authors = ["Darcy Laycock"]
end end
Jeweler::GemcutterTasks.new Jeweler::GemcutterTasks.new
rescue LoadError rescue LoadError
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/barista_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,14 @@
class BaristaController < ActionController::Base class BaristaController < ActionController::Base


caches_page :show caches_page :show if Rails.env.production?


def show def show
headers['Content-Type'] = "application/javascript" headers['Content-Type'] = "application/javascript"
path = normalize_path(params[:js_path]) path = normalize_path(params[:js_path])
p path
return head(:forbidden) unless can_render_path?(path) return head(:forbidden) unless can_render_path?(path)
compiled = Barista.render_path(path) compiled = Barista.render_path(path)
compiled.nil? ? head(:not_found) : render(:text => compiled.to_s) compiled.nil? ? head(:not_found) : render(:text => compiled.to_s)
end
end end


protected protected
Expand Down
46 changes: 46 additions & 0 deletions barista.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{barista}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Darcy Laycock"]
s.date = %q{2010-04-24}
s.description = %q{TODO: longer description of your gem}
s.email = %q{sutto@sutto.net}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.rdoc",
"Rakefile",
"app/controllers/barista_controller.rb",
"config/routes.rb",
"lib/barista.rb",
"lib/barista/compiler.rb"
]
s.homepage = %q{http://github.com/Sutto/barista}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{TODO: one-line summary of your gem}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
end

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
Rails.application.routes.draw do Rails.application.routes.draw do
match 'javascripts/*path.js', :to => 'barista#show' match 'javascripts/*js_path.js', :to => 'barista#show'
end end
11 changes: 6 additions & 5 deletions lib/barista.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def render_path(path)
nil nil
end end


def compile_file!(file) def compile_file!(file, force = false)
file = file.to_s file = file.to_s
file = root.join(file).to_s unless file.include?(root.to_s) file = root.join(file).to_s unless file.include?(root.to_s)
destination_path = file.gsub(/\.(coffee|js)\Z/, '') + ".js" destination_path = file.gsub(/\.(coffee|js)\Z/, '').gsub(root.to_s, output_root.to_s) + ".js"
return unless should_compile_file?(file, destination_path) return unless force || should_compile_file?(file, destination_path)
File.open(destination_path, "w+") do |f| File.open(destination_path, "w+") do |f|
f.write Compiler.compile(File.read(file)) f.write Compiler.compile(File.read(file))
end end
Expand All @@ -48,8 +48,9 @@ def should_compile_file?(from, to)
File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from)) File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
end end


def compile_all! def compile_all!(force = false)
Dir[root.join("**", "*.coffee")].each {|file| compile_file! file } Dir[root.join("**", "*.coffee")].each {|file| compile_file! file, force }
true
end end


end end
Expand Down
8 changes: 8 additions & 0 deletions lib/barista/tasks/barista.rake
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace :barista do

desc "Compiles coffeescripts from app/scripts into public/javascripts"
task :brew => :environment do
Barista.compile_all!(true)
end

end
8 changes: 8 additions & 0 deletions lib/barista/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
module Barista
module Version
MAJOR = 0
MINOR = 1
PATCH = 0
STRING = [MAJOR, MINOR, PATCH].join(".")
end
end

0 comments on commit 02b7a2d

Please sign in to comment.