Skip to content

Commit a9c3cb0

Browse files
committed
Initial commit
0 parents  commit a9c3cb0

7 files changed

+127
-0
lines changed

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2010 Jeremy Ashkenas
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Ruby CoffeeScript
2+
=================
3+
4+
Ruby CoffeeScript is a thin wrapper around the `coffee` binary.
5+
6+
CoffeeScript.compile File.open("script.coffee")
7+
8+
Dependencies
9+
------------
10+
11+
This is **not** the CoffeeScript parser. This means you need to install `node` and `coffee`.
12+
13+
If your `coffee` binary is in a weird location, you can specify the path by hand.
14+
15+
CoffeeScript.coffee_bin = "/usr/local/bin/coffee"

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'rake/testtask'
2+
3+
task :default => :test
4+
5+
Rake::TestTask.new do |t|
6+
t.warning = true
7+
end

coffee-script.gemspec

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Gem::Specification.new do |s|
2+
s.name = 'coffee-script'
3+
s.version = '0.9.0'
4+
s.date = '2010-9-10'
5+
6+
s.homepage = "http://github.com/josh/ruby-coffee-script"
7+
s.summary = "The CoffeeScript Compiler"
8+
s.description = <<-EOS
9+
CoffeeScript is a little language that compiles into JavaScript. Think
10+
of it as JavaScript's less ostentatious kid brother -- the same genes,
11+
roughly the same height, but a different sense of style. Apart from a
12+
handful of bonus goodies, statements in CoffeeScript correspond
13+
one-to-one with their equivalent in JavaScript, it's just another
14+
way of saying it.
15+
EOS
16+
17+
s.files = [
18+
'lib/coffee-script.rb',
19+
'lib/coffee_script.rb',
20+
'LICENSE',
21+
'README.md4'
22+
]
23+
24+
s.requirements << 'node'
25+
s.requirements << 'coffee-script'
26+
27+
s.authors = ['Jeremy Ashkenas']
28+
s.email = '[email protected]'
29+
s.rubyforge_project = 'coffee-script'
30+
end

lib/coffee-script.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'coffee_script'

lib/coffee_script.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module CoffeeScript
2+
def self.locate_coffee_bin
3+
out = `which coffee`
4+
if $?.success?
5+
out.chomp
6+
else
7+
raise LoadError, "could not find `coffee` in PATH"
8+
end
9+
end
10+
11+
def self.coffee_bin
12+
@@coffee_bin ||= locate_coffee_bin
13+
end
14+
15+
def self.coffee_bin=(path)
16+
@@coffee_bin = path
17+
end
18+
19+
# Compile a script (String or IO) to JavaScript.
20+
def self.compile(script, options = {})
21+
script = script.read if script.respond_to?(:read)
22+
command = "#{coffee_bin} -sp"
23+
command += " --no-wrap" if options[:no_wrap]
24+
25+
IO.popen(command, "w+") do |f|
26+
f << script
27+
f.close_write
28+
f.read
29+
end
30+
end
31+
end

test/test_coffee_script.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'coffee_script'
2+
require 'test/unit'
3+
require 'stringio'
4+
5+
class TestCoffeeScript < Test::Unit::TestCase
6+
def test_compile
7+
assert_equal "(function() {\n puts('Hello, World!');\n})();\n",
8+
CoffeeScript.compile("puts 'Hello, World!'\n")
9+
end
10+
11+
def test_compile_with_io
12+
io = StringIO.new("puts 'Hello, World!'\n")
13+
assert_equal "(function() {\n puts('Hello, World!');\n})();\n",
14+
CoffeeScript.compile(io)
15+
end
16+
17+
def test_compile_with_no_wrap
18+
assert_equal "puts('Hello, World!');",
19+
CoffeeScript.compile("puts 'Hello, World!'\n", :no_wrap => true)
20+
end
21+
end

0 commit comments

Comments
 (0)