barista 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Darcy Laycock
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # Barista
2
+
3
+ Barista is very, very similar to [bistro\_car](http://github.com/jnicklas/bistro_car) (infact, credit where credit is due - it shares similar
4
+ code / is almost a fork).
5
+
6
+ The main difference being, it lets you use coffee as you would javascript. Simply put, Write coffee
7
+ and place it in `app/scripts` and Barista will automatically serve it as if it was placed in `public/javascripts`
8
+
9
+ That is, `app/scripts/demo.coffee` will work for `/javascripts/demo.js`. Even better (and more importantly
10
+ for me), it provides `Barista.compile_all!` which takes all coffee files and compiles them into `public/javascripts`.
11
+
12
+ If you're using Jammit, this means you can simple run a rake task (`rake barista:brew` before running jammit) and
13
+ your coffeescripts will be automatically provided, ready for bundling.
14
+
15
+ Barista require rails 3+ (but patches for Rails 2 will be accepted.)
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'lib/barista/version'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "barista"
10
+ gem.summary = %Q{Transparent coffeescript support for rails 3}
11
+ gem.description = %Q{Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.}
12
+ gem.email = "sutto@sutto.net"
13
+ gem.homepage = "http://github.com/Sutto/barista"
14
+ gem.version = Barista::Version::STRING
15
+ gem.authors = ["Darcy Laycock"]
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
25
+
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = "barista #{version}"
28
+ rdoc.rdoc_files.include('README*')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
@@ -0,0 +1,24 @@
1
+ class BaristaController < ActionController::Base
2
+
3
+ caches_page :show if Rails.env.production?
4
+
5
+ def show
6
+ headers['Content-Type'] = "application/javascript"
7
+ path = normalize_path(params[:js_path])
8
+ p path
9
+ return head(:forbidden) unless can_render_path?(path)
10
+ compiled = Barista.render_path(path)
11
+ compiled.nil? ? head(:not_found) : render(:text => compiled.to_s)
12
+ end
13
+
14
+ protected
15
+
16
+ def normalize_path(path)
17
+ File.join(Array.wrap(path).flatten)
18
+ end
19
+
20
+ def can_render_path?(path)
21
+ !path.include?("..")
22
+ end
23
+
24
+ end
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{barista}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Darcy Laycock"]
12
+ s.date = %q{2010-04-24}
13
+ s.description = %q{Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.}
14
+ s.email = %q{sutto@sutto.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "app/controllers/barista_controller.rb",
26
+ "barista.gemspec",
27
+ "config/routes.rb",
28
+ "lib/barista.rb",
29
+ "lib/barista/compiler.rb",
30
+ "lib/barista/tasks/barista.rake",
31
+ "lib/barista/version.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/Sutto/barista}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{Transparent coffeescript support for rails 3}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
49
+
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ match 'javascripts/*js_path.js', :to => 'barista#show'
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_support'
2
+ require 'pathname'
3
+
4
+ module Barista
5
+
6
+ autoload :Compiler, 'barista/compiler'
7
+
8
+ class << self
9
+
10
+ def root
11
+ @root ||= Rails.root.join("app", "scripts")
12
+ end
13
+
14
+ def root=(value)
15
+ @root = Pathname(value.to_s)
16
+ end
17
+
18
+ def output_root
19
+ @output_root ||= Rails.root.join("public", "javascripts")
20
+ end
21
+
22
+ def output_root=(value)
23
+ @output_root = Pathname(value.to_s)
24
+ end
25
+
26
+ def render_path(path)
27
+ full_path = root.join("#{path.gsub(/(\A\/|\/\Z)/, '')}.coffee")
28
+ return unless full_path.exist? && full_path.readable?
29
+ Compiler.compile(full_path.read)
30
+ rescue SysCallError
31
+ nil
32
+ end
33
+
34
+ def compile_file!(file, force = false)
35
+ file = file.to_s
36
+ file = root.join(file).to_s unless file.include?(root.to_s)
37
+ destination_path = file.gsub(/\.(coffee|js)\Z/, '').gsub(root.to_s, output_root.to_s) + ".js"
38
+ return unless force || should_compile_file?(file, destination_path)
39
+ File.open(destination_path, "w+") do |f|
40
+ f.write Compiler.compile(File.read(file))
41
+ end
42
+ true
43
+ rescue SysCallError
44
+ false
45
+ end
46
+
47
+ def should_compile_file?(from, to)
48
+ File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
49
+ end
50
+
51
+ def compile_all!(force = false)
52
+ Dir[root.join("**", "*.coffee")].each {|file| compile_file! file, force }
53
+ true
54
+ end
55
+
56
+ end
57
+
58
+ if defined?(Rails::Engine)
59
+ class Engine < Rails::Engine
60
+ rake_tasks do
61
+ load File.expand_path('./barista/tasks/barista.rake', File.dirname(__FILE__))
62
+ end
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,55 @@
1
+ require 'digest/sha2'
2
+
3
+ module Barista
4
+ class Compiler
5
+
6
+ class << self; attr_accessor :bin_path; end
7
+ self.bin_path ||= "coffee"
8
+
9
+ def self.compile(content)
10
+ new(content).to_js
11
+ end
12
+
13
+ def initialize(content)
14
+ @compiled = false
15
+ @content = content
16
+ end
17
+
18
+ def compile!
19
+ # Compiler code thanks to bistro_car.
20
+ tf = temp_file_for_content
21
+ @compiled_content = invoke_coffee(temp_file_for_content.path)
22
+ @compiled = true
23
+ ensure
24
+ tf.unlink rescue nil
25
+ end
26
+
27
+ def to_js
28
+ compile! unless @compiled
29
+ @compiled_content.to_s
30
+ end
31
+
32
+ protected
33
+
34
+ def coffee_options
35
+ "-p"
36
+ end
37
+
38
+ def temp_file_for_content
39
+ tf = Tempfile.new("barista-#{content_hash}.coffee")
40
+ tf.write @content
41
+ tf.close
42
+ tf
43
+ end
44
+
45
+ def invoke_coffee(path)
46
+ command = "#{self.class.bin_path} #{coffee_options} '#{path}'"
47
+ %x(#{command})
48
+ end
49
+
50
+ def content_hash
51
+ @content_hash ||= Digest::SHA256.hexdigest(@content)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ namespace :barista do
2
+
3
+ desc "Compiles coffeescripts from app/scripts into public/javascripts"
4
+ task :brew => :environment do
5
+ Barista.compile_all!(true)
6
+ end
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ module Barista
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = [MAJOR, MINOR, PATCH].join(".")
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barista
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Darcy Laycock
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-24 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.
22
+ email: sutto@sutto.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.md
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - app/controllers/barista_controller.rb
37
+ - barista.gemspec
38
+ - config/routes.rb
39
+ - lib/barista.rb
40
+ - lib/barista/compiler.rb
41
+ - lib/barista/tasks/barista.rake
42
+ - lib/barista/version.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/Sutto/barista
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.6
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Transparent coffeescript support for rails 3
73
+ test_files: []
74
+