Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make #match work.

  • Loading branch information...
commit a22cc487c9e1598f5e19150bdf2913d604567a6c 1 parent e81402e
@wycats wycats authored
View
2  .gitignore
@@ -1,5 +1,5 @@
*.gem
-*.rbc
+*.rbx
.bundle
.config
.yardoc
View
2  Gemfile
@@ -2,10 +2,10 @@ source 'http://rubygems.org'
# Specify your gem's dependencies in rake-pipeline.gemspec
gemspec
-gem "ruby-debug19"
gem "flay"
gem "flog"
gem "simplecov", :require => false
gem "yard"
gem "rdiscount"
+gem "rake"
View
8 Rakefile
@@ -1,2 +1,10 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+
+task :docs => Dir["lib/**"] do
+ sh "devbin/yard doc --readme README.yard --hide-void-return"
+end
+
+task :graph => :docs do
+ sh "devbin/yard graph --dependencies | dot -Tpng -o arch.png"
+end
View
20 lib/rake-pipeline.rb
@@ -21,6 +21,12 @@ def recursively_reenable(app)
end
end
+ class FileTask
+ def <=>(other)
+ [name, prerequisites] <=> [other.name, other.prerequisites]
+ end
+ end
+
# A Pipeline is responsible for taking a directory of input
# files, applying a number of filters to the inputs, and
# outputting them into an output directory.
@@ -130,7 +136,7 @@ def initialize
#
# @return [Rake::Pipeline] the newly configured pipeline
def self.build(&block)
- pipeline = Pipeline.new
+ pipeline = new
DSL.evaluate(pipeline, &block) if block
pipeline
end
@@ -139,14 +145,20 @@ def self.build(&block)
# @return [void]
def build(&block)
- pipeline = self.class.build(&block)
- pipeline.input_root = input_root
+ pipeline = copy(&block)
pipeline.output_root = File.expand_path(output_root)
- pipeline.tmpdir = tmpdir
@pipelines << pipeline
pipeline
end
+ def copy(target_class=self.class, &block)
+ pipeline = target_class.build(&block)
+ pipeline.input_root = input_root
+ pipeline.tmpdir = tmpdir
+ pipeline.rake_application = rake_application
+ pipeline
+ end
+
def input_root=(root)
@input_root = File.expand_path(root)
@pipelines.each { |pipeline| pipeline.input_root = root }
View
7 lib/rake-pipeline/dsl.rb
@@ -87,6 +87,13 @@ def filter(filter_class, string=nil, &block)
pipeline.add_filter(filter)
end
+ def match(pattern, &block)
+ matcher = pipeline.copy(Matcher, &block)
+ matcher.glob = pattern
+ pipeline.add_filter matcher
+ matcher
+ end
+
# Specify the output directory for the pipeline.
#
# @param [String] root the output directory.
View
19 lib/rake-pipeline/file_wrapper.rb
@@ -60,6 +60,10 @@ def fullpath
File.join(root, path)
end
+ def <=>(other)
+ [root, path, encoding] <=> [other.root, other.path, other.encoding]
+ end
+
# Does the file represented by the {FileWrapper} exist in the file system?
#
# @return [true,false]
@@ -76,9 +80,13 @@ def exists?
# @raise [EncodingError] when the contents of the file are not valid in the
# expected encoding specified in {#encoding}.
def read
- contents = File.read(fullpath, :encoding => encoding)
+ contents = if "".respond_to?(:encode)
+ File.read(fullpath, :encoding => encoding)
+ else
+ File.read(fullpath)
+ end
- unless contents.valid_encoding?
+ if "".respond_to?(:encode) && !contents.valid_encoding?
raise EncodingError, "The file at the path #{fullpath} is not valid UTF-8. Please save it again as UTF-8."
end
@@ -93,7 +101,12 @@ def read
# @return [File] if a block was not given
def create
FileUtils.mkdir_p(File.dirname(fullpath))
- @created_file = File.open(fullpath, "w:#{encoding}")
+
+ @created_file = if "".respond_to?(:encode)
+ File.open(fullpath, "w:#{encoding}")
+ else
+ File.open(fullpath, "w")
+ end
if block_given?
yield @created_file
View
144 spec/encoding_spec.rb
@@ -1,104 +1,106 @@
# encoding: UTF-8
-class String
- def strip_heredoc
- indent = scan(/^[ \t]*(?=\S)/).min
- indent = indent ? indent.size : 0
- gsub(/^[ \t]{#{indent}}/, '')
+if "".respond_to?(:encode)
+ class String
+ def strip_heredoc
+ indent = scan(/^[ \t]*(?=\S)/).min
+ indent = indent ? indent.size : 0
+ gsub(/^[ \t]{#{indent}}/, '')
+ end
end
-end
-inputs = {
- "app/javascripts/jquery.js" => <<-HERE.strip_heredoc,
- var jQuery = { japanese: "こんにちは" };
- HERE
+ inputs = {
+ "app/javascripts/jquery.js" => <<-HERE.strip_heredoc,
+ var jQuery = { japanese: "こんにちは" };
+ HERE
- "app/javascripts/sproutcore.js" => <<-HERE.strip_heredoc,
+ "app/javascripts/sproutcore.js" => <<-HERE.strip_heredoc,
+ var SC = {};
+ assert(SC);
+ SC.hi = function() { console.log("こんにちは"); };
+ HERE
+ }
+
+ expected_output = <<-HERE.strip_heredoc
+ var jQuery = { japanese: "こんにちは" };
var SC = {};
- assert(SC);
+
SC.hi = function() { console.log("こんにちは"); };
HERE
-}
-expected_output = <<-HERE.strip_heredoc
- var jQuery = { japanese: "こんにちは" };
- var SC = {};
+ describe "the pipeline's encoding handling" do
+ Filters = Rake::Pipeline::SpecHelpers::Filters
- SC.hi = function() { console.log("こんにちは"); };
-HERE
+ let(:inputs) { inputs }
-describe "the pipeline's encoding handling" do
- Filters = Rake::Pipeline::SpecHelpers::Filters
+ def output_should_exist(expected, encoding="UTF-8")
+ output = File.join(tmp, "public/javascripts/application.js")
- let(:inputs) { inputs }
-
- def output_should_exist(expected, encoding="UTF-8")
- output = File.join(tmp, "public/javascripts/application.js")
-
- File.exists?(output).should be_true
- output = File.read(output, :encoding => encoding)
- output.should == expected
- output.should be_valid_encoding
- end
+ File.exists?(output).should be_true
+ output = File.read(output, :encoding => encoding)
+ output.should == expected
+ output.should be_valid_encoding
+ end
- def create_files
- inputs.each do |name, contents|
- filename = File.join(tmp, name)
- mkdir_p File.dirname(filename)
+ def create_files
+ inputs.each do |name, contents|
+ filename = File.join(tmp, name)
+ mkdir_p File.dirname(filename)
- File.open(filename, "w:#{encoding}") do |file|
- file.write contents.encode(encoding)
+ File.open(filename, "w:#{encoding}") do |file|
+ file.write contents.encode(encoding)
+ end
end
end
- end
- before do
- create_files
+ before do
+ create_files
- @pipeline = Rake::Pipeline.build do
- tmpdir "temporary"
- input tmp, "app/javascripts/*.js"
- filter Filters::ConcatFilter, "javascripts/application.js"
- filter Filters::StripAssertsFilter
- output "public"
+ @pipeline = Rake::Pipeline.build do
+ tmpdir "temporary"
+ input tmp, "app/javascripts/*.js"
+ filter Filters::ConcatFilter, "javascripts/application.js"
+ filter Filters::StripAssertsFilter
+ output "public"
+ end
end
- end
- describe "when the input is UTF-8" do
- let(:encoding) { "UTF-8" }
+ describe "when the input is UTF-8" do
+ let(:encoding) { "UTF-8" }
- it "creates the correct file" do
- @pipeline.invoke
- output_should_exist(expected_output)
+ it "creates the correct file" do
+ @pipeline.invoke
+ output_should_exist(expected_output)
+ end
end
- end
- describe "when the input is not UTF-8" do
- let(:encoding) { "EUC-JP" }
+ describe "when the input is not UTF-8" do
+ let(:encoding) { "EUC-JP" }
- it "raises an exception" do
- lambda { @pipeline.invoke }.should raise_error(Rake::Pipeline::EncodingError, /not valid UTF-8/)
+ it "raises an exception" do
+ lambda { @pipeline.invoke }.should raise_error(Rake::Pipeline::EncodingError, /not valid UTF-8/)
+ end
end
- end
- describe "when dealing with only BINARY-type filters" do
- let(:encoding) { "EUC-JP" }
+ describe "when dealing with only BINARY-type filters" do
+ let(:encoding) { "EUC-JP" }
- it "does not raise an exception" do
- pipeline = Rake::Pipeline.build do
- tmpdir "temporary"
- input tmp, "app/javascripts/*.js"
- filter Filters::ConcatFilter, "javascripts/application.js"
- output "public"
- end
+ it "does not raise an exception" do
+ pipeline = Rake::Pipeline.build do
+ tmpdir "temporary"
+ input tmp, "app/javascripts/*.js"
+ filter Filters::ConcatFilter, "javascripts/application.js"
+ output "public"
+ end
- pipeline.invoke
+ pipeline.invoke
- expected = inputs.map do |filename, contents|
- contents.encode("EUC-JP")
- end.join
+ expected = inputs.map do |filename, contents|
+ contents.encode("EUC-JP")
+ end.join
- output_should_exist(expected.force_encoding("BINARY"), "BINARY")
+ output_should_exist(expected.force_encoding("BINARY"), "BINARY")
+ end
end
end
end
View
21 spec/filter_spec.rb
@@ -63,8 +63,8 @@ def output_file(path, file_root=output_root)
filter.output_name_generator = output_name_generator
outputs = filter.outputs
- outputs.keys.should == input_files.map { |f| output_file(f.path) }
- outputs.values.should == input_files.map { |f| [f] }
+ outputs.keys.sort.should == input_files.map { |f| output_file(f.path) }.sort
+ outputs.values.flatten.sort.should == input_files.sort
filter.output_files.should == input_files.map { |file| output_file(file.path) }
end
@@ -154,17 +154,18 @@ def input_task(path)
filter.output_name_generator = proc { |input| input }
filter.generate_output_block = proc do |inputs, output|
- inputs.should == [input_files[filter_runs]]
- output.should == output_file(input_files[filter_runs].path)
+ inputs.should == [input_file(output.path)]
filter_runs += 1
end
filter.generate_rake_tasks
tasks = filter.rake_tasks
- tasks.should == input_files.map { |file| output_task(file.path) }
- tasks.each_with_index do |task, index|
- task.prerequisites.should == [input_files[index].fullpath]
+ tasks.sort.should == input_files.map { |file| output_task(file.path) }.sort
+ tasks.each do |task|
+ name = task.name.sub(/^#{Regexp.escape(output_root)}/, '')
+ prereq = File.join(input_root, name)
+ task.prerequisites.should == [prereq]
end
tasks.each(&:invoke)
@@ -192,14 +193,14 @@ def input_task(path)
filter.generate_rake_tasks
tasks = filter.rake_tasks
- tasks.should == [output_task("javascripts/jquery.js"), output_task("javascripts/sproutcore.js")]
+ tasks.sort.should == [output_task("javascripts/jquery.js"), output_task("javascripts/sproutcore.js")].sort
- tasks[0].prerequisites.should == [
+ tasks.sort[0].prerequisites.should == [
File.join(input_root, "javascripts/jquery.js"),
File.join(input_root, "javascripts/jquery-ui.js")
]
- tasks[1].prerequisites.should == [File.join(input_root, "javascripts/sproutcore.js")]
+ tasks.sort[1].prerequisites.should == [File.join(input_root, "javascripts/sproutcore.js")]
tasks.each(&:invoke)
View
34 spec/rake_acceptance_spec.rb
@@ -200,7 +200,7 @@ def output_should_exist(expected = EXPECTED_JS_OUTPUT)
def output_should_exist(expected=EXPECTED_JS_OUTPUT)
super
- css = File.join(tmp, "public/stylesheets/application.css")
+ css = File.join(tmp, "public/stylesheets/application.css")
File.exists?(css).should be_true
File.read(css).should == EXPECTED_CSS_OUTPUT
@@ -223,5 +223,37 @@ def output_should_exist(expected=EXPECTED_JS_OUTPUT)
end
end
end
+
+ describe "using the matcher spec" do
+ it_behaves_like "the pipeline DSL"
+
+ def output_should_exist(expected=EXPECTED_JS_OUTPUT)
+ super
+
+ css = File.join(tmp, "public/stylesheets/application.css")
+
+ File.exists?(css).should be_true
+ File.read(css).should == EXPECTED_CSS_OUTPUT
+ end
+
+ before do
+ @pipeline = Rake::Pipeline.build do
+ tmpdir "temporary"
+ input File.join(tmp, "app"), "**/*.{js,css}"
+ output "public"
+
+ match "*.js" do
+ filter strip_asserts_filter
+ end
+
+ filter(concat_filter) do |input|
+ location = File.dirname(input).split(File::SEPARATOR).last
+ extension = File.extname(input)
+
+ File.join(location, "application#{extension}")
+ end
+ end
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.