Skip to content

Commit

Permalink
Add the CoffeeScript compiler and clean up some specs
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Dec 19, 2011
1 parent fadf900 commit 0ce7ebf
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/rake-pipeline-web-filters.rb
Expand Up @@ -17,5 +17,6 @@ module Filters
require "rake-pipeline-web-filters/ordering_concat_filter"
require "rake-pipeline-web-filters/sass_compiler"
require "rake-pipeline-web-filters/tilt_filter"
require "rake-pipeline-web-filters/coffee_script_compiler"
require "rake-pipeline-web-filters/helpers"

26 changes: 26 additions & 0 deletions lib/rake-pipeline-web-filters/coffee_script_compiler.rb
@@ -0,0 +1,26 @@
module Rake::Pipeline::Web::Filters
# A filter that compiles CoffeeScript to JavaScript.
class CoffeeScriptCompiler < Rake::Pipeline::Filter
# By default, the CoffeeScriptCompiler converts inputs
# with the extension +.coffee+ to +.js+.
#
# @param [Proc] block the output name generator block
def initialize(&block)
block ||= proc { |input| input.sub(/\.coffee$/, '.js') }
super(&block)
end

# The body of the filter. Compile each input file into
# a CoffeeScript compiled output file.
#
# @param [Array] inputs an Array of FileWrapper objects.
# @param [FileWrapper] output a FileWrapper object
def generate_output(inputs, output)
require "coffee-script"

inputs.each do |input|
output.write CoffeeScript.compile(input)
end
end
end
end
1 change: 1 addition & 0 deletions rake-pipeline-web-filters.gemspec
Expand Up @@ -21,5 +21,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "tilt"
gem.add_development_dependency "sass"
gem.add_development_dependency "compass"
gem.add_development_dependency "coffee-script"
gem.add_development_dependency "redcarpet", '~> 2.0'
end
62 changes: 62 additions & 0 deletions spec/coffee_filter_spec.rb
@@ -0,0 +1,62 @@
describe "CoffeeScriptCompiler" do
CoffeeScriptCompiler = Rake::Pipeline::Web::Filters::CoffeeScriptCompiler

COFFEE_INPUT = <<-COFFEE
x = 1;
y = ->
x += 1
COFFEE

EXPECTED_COFFEE_OUTPUT = <<-HTML
(function() {
var x, y;
x = 1;
y = function() {
return x += 1;
};
}).call(this);
HTML

def input_file(name, content)
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
end

def output_file(name)
MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
end

def setup_filter(filter)
filter.file_wrapper_class = MemoryFileWrapper
filter.input_files = [input_file("input.coffee", COFFEE_INPUT)]
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
filter
end

it "generates output" do
filter = setup_filter CoffeeScriptCompiler.new

filter.output_files.should == [output_file("input.js")]

tasks = filter.generate_rake_tasks
tasks.each(&:invoke)

file = MemoryFileWrapper.files["/path/to/output/input.js"]
file.body.should == EXPECTED_COFFEE_OUTPUT
file.encoding.should == "UTF-8"
end

describe "naming output files" do
it "translates .coffee extensions to .js by default" do
filter = setup_filter CoffeeScriptCompiler.new
filter.output_files.first.path.should == "input.js"
end

it "accepts a block to customize output file names" do
filter = setup_filter(CoffeeScriptCompiler.new { |input| "octopus" })
filter.output_files.first.path.should == "octopus"
end
end
end

1 change: 0 additions & 1 deletion spec/markdown_compiler_spec.rb
@@ -1,5 +1,4 @@
describe "MarkdownCompiler" do
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper
MarkdownCompiler = Rake::Pipeline::Web::Filters::MarkdownCompiler

MARKDOWN_INPUT = <<-MARKDOWN
Expand Down
2 changes: 0 additions & 2 deletions spec/minispade_filter_spec.rb
@@ -1,6 +1,4 @@
describe "MinispadeFilter" do
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper

let(:input_files) {
[
MemoryFileWrapper.new("/path/to/input", "foo.js", "UTF-8", "var foo = 'bar';")
Expand Down
2 changes: 0 additions & 2 deletions spec/ordering_concat_filter_spec.rb
@@ -1,6 +1,4 @@
describe "OrderingConcatFilter" do
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper

let(:input_files) {
[
MemoryFileWrapper.new("/path/to/input", "first.txt", "UTF-8", "FIRST"),
Expand Down
1 change: 0 additions & 1 deletion spec/sass_compiler_spec.rb
@@ -1,5 +1,4 @@
describe "SassCompiler" do
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper
SassCompiler = Rake::Pipeline::Web::Filters::SassCompiler

SCSS_INPUT = <<-SCSS
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -62,6 +62,7 @@ def age_existing_files

module Filters
ConcatFilter = Rake::Pipeline::ConcatFilter
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper

class StripAssertsFilter < Rake::Pipeline::Filter
def generate_output(inputs, output)
Expand Down
2 changes: 0 additions & 2 deletions spec/tilt_filter_spec.rb
@@ -1,6 +1,4 @@
describe "TiltFilter" do
MemoryFileWrapper = Rake::Pipeline::SpecHelpers::MemoryFileWrapper

let(:input_files) {
[
MemoryFileWrapper.new("/path/to/input", "foo.erb", "UTF-8", "<%= 'foo' %>\n"),
Expand Down

0 comments on commit 0ce7ebf

Please sign in to comment.