Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing the RoyFilter #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.gem
*.rbc
*.swp
.bundle
.config
.yardoc
Expand Down
1 change: 1 addition & 0 deletions lib/rake-pipeline-web-filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ module Filters
require "rake-pipeline-web-filters/chained_filter"
require "rake-pipeline-web-filters/less_filter"
require "rake-pipeline-web-filters/handlebars_filter"
require "rake-pipeline-web-filters/roy_filter"
require "rake-pipeline-web-filters/helpers"
6 changes: 6 additions & 0 deletions lib/rake-pipeline-web-filters/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def less(*args, &block)
def handlebars(*args, &block)
filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block)
end

# Add a new {RoyFilter} to the pipeline.
# @see RoyFilter#initialize
def roy(*args, &block)
filter(Rake::Pipeline::Web::Filters::RoyFilter, *args, &block)
end
end

module ProjectHelpers
Expand Down
56 changes: 56 additions & 0 deletions lib/rake-pipeline-web-filters/roy_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'rake-pipeline-web-filters/filter_with_dependencies'

module Rake::Pipeline::Web::Filters
# Public: The RoyFilter compiles scripts written in Roy to JavaScript.
#
# This filter depends on the roy-lang gem.
#
# Examples
#
# The following is an example Assetfile usage.
#
# output "public"
#
# input "assets" do
# match "*.roy" do
# roy
# end
# end
class RoyFilter < Rake::Pipeline::Filter
include Rake::Pipeline::Web::Filters::FilterWithDependencies

# Public: Returns the hash of options used when compiling.
attr_reader :options

# Public: Initializes a RoyFilter.
#
# options - The Hash of options to use when compiling (default: {}):
# See the roy-lang gem for available options.
# block - A block to map input names to ouput names (defaults to
# mapping .roy files to .js).
def initialize(options = {}, &block)
super &(block || ->input {input.sub(/\.roy\z/, ".js")})
@options = options
end

protected
# Internal: Generates a JavaScript output file containing each
# compiled input.
#
# inputs - Array of FileWrappers of Roy files.
# output - A FileWrapper of a JavaScript file.
def generate_output(inputs, output)
inputs.each do |input|
output.write Roy.compile(input.read, @options)
end
end

private
# Internal: Defines the external dependencies of this filter.
#
# Returns an Array of the dependencies.
def external_dependencies
["roy-lang"]
end
end
end
2 changes: 2 additions & 0 deletions rake-pipeline-web-filters.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "yui-compressor"
gem.add_development_dependency "uglifier"
gem.add_development_dependency "less"
gem.add_development_dependency "therubyracer"
gem.add_development_dependency "json"
gem.add_development_dependency "roy-lang"
end
2 changes: 1 addition & 1 deletion spec/coffee_script_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def setup_filter(filter)
tasks = filter.generate_rake_tasks
lambda {
tasks.each(&:invoke)
}.should raise_error(ExecJS::RuntimeError, /Error compiling input.coffee. reserved word "function" on line 1/i)
}.should raise_error(ExecJS::RuntimeError, /Error compiling input.coffee\..*reserved word "function" on line 1/i)
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def filter
filter.should be_kind_of(Rake::Pipeline::Web::Filters::HandlebarsFilter)
end
end

describe "#roy" do
it "creates a RoyFilter" do
dsl.roy
filter.should be_kind_of(Rake::Pipeline::Web::Filters::RoyFilter)
end
end
end

describe "ProjectHelpers" do
Expand Down
45 changes: 45 additions & 0 deletions spec/roy_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
describe "RoyFilter" do
let(:file_wrapper_class){Rake::Pipeline::SpecHelpers::MemoryFileWrapper}
let(:rake_app){Rake::Application.new}

let(:encoding){"UTF-8"}
let(:input_path){"/in"}
let(:output_path){"/out"}
let(:input_name){"a.roy"}
let(:output_name){"a.js"}

let(:input_file){file_wrapper_class.new(input_path, input_name, encoding, input)}
let(:output_file){file_wrapper_class.new(output_path, output_name, encoding)}

subject {
filter = Rake::Pipeline::Web::Filters::RoyFilter.new(options)
filter.file_wrapper_class = file_wrapper_class
filter.input_files = [input_file]
filter.output_root = output_path
filter.rake_application = rake_app
filter
}

describe "#generate_output" do
context "when default options are used" do
let(:options){{}}
let(:output){<<-JS.gsub(/^\s*/,"")
(function() {
var x = 10;
})();
JS
}
context "when the input is valid" do
let(:input){"let x = 10"}
before{subject.generate_rake_tasks.each(&:invoke)}
specify{subject.output_files.should == [output_file]}
specify{file_wrapper_class.files["#{output_path}/#{output_name}"].body.should == output}
specify{file_wrapper_class.files["#{output_path}/#{output_name}"].encoding.should == encoding}
end
context "when the input is invalid" do
let(:input){"var = x, 10"}
specify{->{subject.generate_rake_tasks.each(&:invoke)}.should raise_error}
end
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ def tmp
config.after do
Dir.chdir(original)
end

config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
end