rake-pipeline-web-filters-fork 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. data/.gitignore +19 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +11 -0
  4. data/.yardopts +2 -0
  5. data/Gemfile +6 -0
  6. data/README.markdown +58 -0
  7. data/README.yard +25 -0
  8. data/Rakefile +10 -0
  9. data/lib/rake-pipeline-web-filters.rb +31 -0
  10. data/lib/rake-pipeline-web-filters/cache_buster_filter.rb +42 -0
  11. data/lib/rake-pipeline-web-filters/chained_filter.rb +116 -0
  12. data/lib/rake-pipeline-web-filters/coffee_script_filter.rb +41 -0
  13. data/lib/rake-pipeline-web-filters/filter_with_dependencies.rb +27 -0
  14. data/lib/rake-pipeline-web-filters/gzip_filter.rb +59 -0
  15. data/lib/rake-pipeline-web-filters/handlebars_filter.rb +62 -0
  16. data/lib/rake-pipeline-web-filters/helpers.rb +138 -0
  17. data/lib/rake-pipeline-web-filters/iife_filter.rb +38 -0
  18. data/lib/rake-pipeline-web-filters/jade_filter.rb +61 -0
  19. data/lib/rake-pipeline-web-filters/less_filter.rb +55 -0
  20. data/lib/rake-pipeline-web-filters/markdown_filter.rb +70 -0
  21. data/lib/rake-pipeline-web-filters/minispade_filter.rb +63 -0
  22. data/lib/rake-pipeline-web-filters/neuter_filter.rb +110 -0
  23. data/lib/rake-pipeline-web-filters/sass_filter.rb +87 -0
  24. data/lib/rake-pipeline-web-filters/stylus_filter.rb +59 -0
  25. data/lib/rake-pipeline-web-filters/tilt_filter.rb +70 -0
  26. data/lib/rake-pipeline-web-filters/uglify_filter.rb +82 -0
  27. data/lib/rake-pipeline-web-filters/version.rb +9 -0
  28. data/lib/rake-pipeline-web-filters/yui_css_filter.rb +70 -0
  29. data/lib/rake-pipeline-web-filters/yui_javascript_filter.rb +59 -0
  30. data/rake-pipeline-web-filters.gemspec +33 -0
  31. data/spec/cache_buster_filter_spec.rb +108 -0
  32. data/spec/chained_filter_spec.rb +79 -0
  33. data/spec/coffee_script_filter_spec.rb +113 -0
  34. data/spec/gzip_filter_spec.rb +52 -0
  35. data/spec/handlebars_filter_spec.rb +73 -0
  36. data/spec/helpers_spec.rb +146 -0
  37. data/spec/iife_filter_spec.rb +58 -0
  38. data/spec/jade_filter_spec.rb +92 -0
  39. data/spec/less_filter_spec.rb +62 -0
  40. data/spec/markdown_filter_spec.rb +89 -0
  41. data/spec/minispade_filter_spec.rb +82 -0
  42. data/spec/neuter_filter_spec.rb +216 -0
  43. data/spec/sass_filter_spec.rb +152 -0
  44. data/spec/spec_helper.rb +28 -0
  45. data/spec/stylus_filter_spec.rb +72 -0
  46. data/spec/support/spec_helpers/file_utils.rb +33 -0
  47. data/spec/support/spec_helpers/filters.rb +37 -0
  48. data/spec/support/spec_helpers/input_helpers.rb +23 -0
  49. data/spec/support/spec_helpers/memory_file_wrapper.rb +38 -0
  50. data/spec/support/spec_helpers/memory_manifest.rb +19 -0
  51. data/spec/tilt_filter_spec.rb +82 -0
  52. data/spec/uglify_filter_spec.rb +128 -0
  53. data/spec/yui_css_filter_spec.rb +91 -0
  54. data/spec/yui_javascript_filter_spec.rb +71 -0
  55. metadata +308 -0
@@ -0,0 +1,28 @@
1
+ require "rake-pipeline"
2
+ require "rake-pipeline-web-filters"
3
+
4
+ require "support/spec_helpers/file_utils"
5
+ require "support/spec_helpers/filters"
6
+ require "support/spec_helpers/input_helpers"
7
+ require "support/spec_helpers/memory_file_wrapper"
8
+ require "support/spec_helpers/memory_manifest"
9
+
10
+ RSpec.configure do |config|
11
+ original = Dir.pwd
12
+
13
+ config.include Rake::Pipeline::SpecHelpers::FileUtils
14
+
15
+ def tmp
16
+ File.expand_path("../tmp", __FILE__)
17
+ end
18
+
19
+ config.before do
20
+ rm_rf(tmp)
21
+ mkdir_p(tmp)
22
+ Dir.chdir(tmp)
23
+ end
24
+
25
+ config.after do
26
+ Dir.chdir(original)
27
+ end
28
+ end
@@ -0,0 +1,72 @@
1
+ describe "StylusFilter" do
2
+ StylusFilter ||= Rake::Pipeline::Web::Filters::StylusFilter
3
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
4
+ MemoryManifest ||= Rake::Pipeline::SpecHelpers::MemoryManifest
5
+
6
+ let(:styl_input) { <<-STYLUS }
7
+ border-radius()
8
+ -webkit-border-radius arguments
9
+ -moz-border-radius arguments
10
+ border-radius arguments
11
+
12
+ body
13
+ font 12px Helvetica, Arial, sans-serif
14
+
15
+ a.button
16
+ border-radius 5px
17
+ STYLUS
18
+
19
+ let(:expected_css_output) { <<-CSS }
20
+ body {
21
+ font: 12px Helvetica, Arial, sans-serif;
22
+ }
23
+ a.button {
24
+ -webkit-border-radius: 5px;
25
+ -moz-border-radius: 5px;
26
+ border-radius: 5px;
27
+ }
28
+ CSS
29
+
30
+ def input_file(name, content)
31
+ MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
32
+ end
33
+
34
+ def output_file(name)
35
+ MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
36
+ end
37
+
38
+ def setup_filter(filter, input=styl_input)
39
+ filter.file_wrapper_class = MemoryFileWrapper
40
+ filter.manifest = MemoryManifest.new
41
+ filter.last_manifest = MemoryManifest.new
42
+ filter.input_files = [input_file("border.styl", input)]
43
+ filter.output_root = "/path/to/output"
44
+ filter.rake_application = Rake::Application.new
45
+ filter
46
+ end
47
+
48
+ it "generates output" do
49
+ filter = setup_filter StylusFilter.new
50
+
51
+ filter.output_files.should == [output_file("border.css")]
52
+
53
+ tasks = filter.generate_rake_tasks
54
+ tasks.each(&:invoke)
55
+
56
+ file = MemoryFileWrapper.files["/path/to/output/border.css"]
57
+ file.body.should == expected_css_output
58
+ file.encoding.should == "UTF-8"
59
+ end
60
+
61
+ describe "naming output files" do
62
+ it "translates .styl extensions to .css by default" do
63
+ filter = setup_filter StylusFilter.new
64
+ filter.output_files.first.path.should == "border.css"
65
+ end
66
+
67
+ it "accepts a block to customize output file names" do
68
+ filter = setup_filter(StylusFilter.new { |input| "octopus" })
69
+ filter.output_files.first.path.should == "octopus"
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,33 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+
4
+ # TODO: OS agnostic modules
5
+ module FileUtils
6
+ def mkdir_p(dir)
7
+ system "mkdir", "-p", dir
8
+ end
9
+
10
+ def touch(file)
11
+ system "touch", file
12
+ end
13
+
14
+ def rm_rf(dir)
15
+ system "rm", "-rf", dir
16
+ end
17
+
18
+ def touch_p(file)
19
+ dir = File.dirname(file)
20
+ mkdir_p dir
21
+ touch file
22
+ end
23
+
24
+ def age_existing_files
25
+ old_time = Time.now - 10
26
+ Dir[File.join(tmp, "**/*.js")].each do |file|
27
+ File.utime(old_time, old_time, file)
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+
4
+ module Filters
5
+ ConcatFilter = Rake::Pipeline::ConcatFilter
6
+
7
+ class StripAssertsFilter < Rake::Pipeline::Filter
8
+ def generate_output(inputs, output)
9
+ inputs.each do |input|
10
+ output.write input.read.gsub(%r{^\s*assert\(.*\)\s*;?\s*$}m, '')
11
+ end
12
+ end
13
+ end
14
+
15
+ class DynamicImportFilter < Rake::Pipeline::Filter
16
+ def additional_dependencies(input)
17
+ includes(input)
18
+ end
19
+
20
+ def includes(input)
21
+ input.read.scan(/^@import\(\"(.*)\"\)$/).map(&:first).map do |inc|
22
+ File.join(input.root, "#{inc}.import")
23
+ end
24
+ end
25
+
26
+ def generate_output(inputs, output)
27
+ inputs.each do |input|
28
+ output.write input.read
29
+ includes(input).each do |inc|
30
+ output.write File.read(inc)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+ module InputHelpers
4
+ def input_file(path, root=File.join(tmp, "app/assets"))
5
+ Rake::Pipeline::FileWrapper.new root, path
6
+ end
7
+
8
+ def output_file(path, root=File.join(tmp, "public"))
9
+ input_file(path, root)
10
+ end
11
+
12
+ def create_files(files)
13
+ Array(files).each do |file|
14
+ mkdir_p File.dirname(file.fullpath)
15
+
16
+ File.open(file.fullpath, "w") do |file|
17
+ file.write "// This is #{file.path}\n"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+ class MemoryFileWrapper < Struct.new(:root, :path, :encoding, :body)
4
+ @@files = {}
5
+ @@data = {}
6
+
7
+ def self.files
8
+ @@files
9
+ end
10
+
11
+ def self.data
12
+ @@data
13
+ end
14
+
15
+ def with_encoding(new_encoding)
16
+ self.class.new(root, path, new_encoding, body)
17
+ end
18
+
19
+ def fullpath
20
+ File.join(root, path)
21
+ end
22
+
23
+ def create
24
+ @@files[fullpath] = self
25
+ self.body = ""
26
+ yield
27
+ end
28
+
29
+ def read
30
+ body || @@data[fullpath] || ""
31
+ end
32
+
33
+ def write(contents)
34
+ self.body << contents
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+ class MemoryManifest
4
+ def initialize
5
+ @entries = {}
6
+ end
7
+
8
+ # Look up an entry by filename.
9
+ def [](key)
10
+ @entries[key]
11
+ end
12
+
13
+ # Set an entry
14
+ def []=(key, value)
15
+ @entries[key] = value
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,82 @@
1
+ describe "TiltFilter" do
2
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
3
+ MemoryManifest ||= Rake::Pipeline::SpecHelpers::MemoryManifest
4
+
5
+ let(:input_files) {
6
+ [
7
+ MemoryFileWrapper.new("/path/to/input", "foo.erb", "UTF-8", "<%= 'foo' %>\n"),
8
+ MemoryFileWrapper.new("/path/to/input", "bar.str", "UTF-8", '#{ "bar" }')
9
+ ]
10
+ }
11
+
12
+ let(:output_files) {
13
+ [
14
+ MemoryFileWrapper.new("/path/to/output", "foo.txt", "UTF-8"),
15
+ MemoryFileWrapper.new("/path/to/output", "bar.txt", "UTF-8")
16
+ ]
17
+ }
18
+
19
+ def make_filter(*args)
20
+ filter = Rake::Pipeline::Web::Filters::TiltFilter.new(*args) do |input|
21
+ input.sub(/\.(erb|str)$/, '.txt')
22
+ end
23
+ filter.file_wrapper_class = MemoryFileWrapper
24
+ filter.manifest = MemoryManifest.new
25
+ filter.last_manifest = MemoryManifest.new
26
+ filter.input_files = input_files
27
+ filter.output_root = "/path/to/output"
28
+ filter.rake_application = Rake::Application.new
29
+ filter
30
+ end
31
+
32
+ it "generates output" do
33
+ filter = make_filter
34
+
35
+ filter.output_files.should == output_files
36
+
37
+ tasks = filter.generate_rake_tasks
38
+ tasks.each(&:invoke)
39
+
40
+ file = MemoryFileWrapper.files["/path/to/output/foo.txt"]
41
+ file.body.should == "foo"
42
+ file.encoding.should == "UTF-8"
43
+
44
+ file = MemoryFileWrapper.files["/path/to/output/bar.txt"]
45
+ file.body.should == "bar"
46
+ file.encoding.should == "UTF-8"
47
+ end
48
+
49
+ it "accepts options to pass to the template class" do
50
+ # :trim => '' should tell ERB not to trim newlines
51
+ filter = make_filter(:trim => '')
52
+
53
+ tasks = filter.generate_rake_tasks
54
+ tasks.each(&:invoke)
55
+ file = MemoryFileWrapper.files["/path/to/output/foo.txt"]
56
+ file.body.should == "foo\n"
57
+ end
58
+
59
+ describe 'with a rendering context' do
60
+
61
+ let(:input_files) do
62
+ [
63
+ MemoryFileWrapper.new("/path/to/input", "foo.erb", "UTF-8", "<%= foo %>"),
64
+ ]
65
+ end
66
+
67
+ let(:context) do
68
+ context = Class.new do
69
+ def foo; 'foo'; end
70
+ end.new
71
+ end
72
+
73
+ it 'uses the context' do
74
+ filter = make_filter({}, context)
75
+
76
+ tasks = filter.generate_rake_tasks
77
+ tasks.each(&:invoke)
78
+ file = MemoryFileWrapper.files["/path/to/output/foo.txt"]
79
+ file.body.should == "foo"
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,128 @@
1
+ describe "UglifyFilter" do
2
+ UglifyFilter ||= Rake::Pipeline::Web::Filters::UglifyFilter
3
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
4
+ MemoryManifest ||= Rake::Pipeline::SpecHelpers::MemoryManifest
5
+
6
+ let(:js_input) { <<-HERE }
7
+ var name = "Truckasaurus Gates";
8
+ console.log(name);
9
+ HERE
10
+
11
+ let(:expected_js_output) {
12
+ 'var name="Truckasaurus Gates";console.log(name);'
13
+ }
14
+
15
+ let(:expected_beautiful_js_output) {
16
+ %[var name = "Truckasaurus Gates";\n\nconsole.log(name);;]
17
+ }
18
+
19
+ let(:filter_args) { [] }
20
+ let(:filter_block) { nil }
21
+
22
+ let(:filter) {
23
+ filter = UglifyFilter.new(*filter_args, &filter_block)
24
+ filter.file_wrapper_class = MemoryFileWrapper
25
+ filter.manifest = MemoryManifest.new
26
+ filter.last_manifest = MemoryManifest.new
27
+ filter.input_files = [input_file("name.js", js_input)]
28
+ filter.output_root = "/path/to/output"
29
+ filter.rake_application = Rake::Application.new
30
+ filter
31
+ }
32
+
33
+ def input_file(name, content)
34
+ MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
35
+ end
36
+
37
+ def output_file(name)
38
+ MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
39
+ end
40
+
41
+ it "generates output" do
42
+ filter.output_files.should == [output_file("name.min.js")]
43
+
44
+ tasks = filter.generate_rake_tasks
45
+ tasks.each(&:invoke)
46
+
47
+ file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
48
+ file.body.should == expected_js_output
49
+ file.encoding.should == "UTF-8"
50
+ end
51
+
52
+ describe "Skipping" do
53
+ it "skips files ending in .min.js" do
54
+ filter.input_files = [input_file("name.min.js", 'fake-js')]
55
+
56
+ filter.output_files.should == [output_file("name.min.js")]
57
+
58
+ tasks = filter.generate_rake_tasks
59
+ tasks.each(&:invoke)
60
+
61
+ file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
62
+ file.body.should == 'fake-js'
63
+ file.encoding.should == "UTF-8"
64
+ end
65
+
66
+ it "does not count files ending in min.js as preminified" do
67
+ filter.input_files = [input_file("min.js", js_input)]
68
+
69
+ filter.output_files.should == [output_file("min.min.js")]
70
+
71
+ tasks = filter.generate_rake_tasks
72
+ tasks.each(&:invoke)
73
+
74
+ file = MemoryFileWrapper.files["/path/to/output/min.min.js"]
75
+ file.body.should == expected_js_output
76
+ file.encoding.should == "UTF-8"
77
+ end
78
+ end
79
+
80
+ it "translates .js extensions to .min.js by default" do
81
+ filter.output_files.first.path.should == "name.min.js"
82
+ end
83
+
84
+ context "with preserve_input option" do
85
+ let(:filter_args) do
86
+ [{ :preserve_input => true }]
87
+ end
88
+
89
+ it "should output both the unminified and the minified files" do
90
+ filter.output_files.should == [output_file("name.js"), output_file("name.min.js")]
91
+
92
+ tasks = filter.generate_rake_tasks
93
+ tasks.each(&:invoke)
94
+
95
+ file = MemoryFileWrapper.files["/path/to/output/name.js"]
96
+ file.body.should == js_input
97
+ file.encoding.should == "UTF-8"
98
+
99
+ file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
100
+ file.body.should == expected_js_output
101
+ file.encoding.should == "UTF-8"
102
+ end
103
+ end
104
+
105
+ context "with output name block" do
106
+ let(:filter_block) do
107
+ Proc.new { |input| "octopus" }
108
+ end
109
+
110
+ it "customizes output file names" do
111
+ filter.output_files.first.path.should == "octopus"
112
+ end
113
+ end
114
+
115
+ context "with Uglify options" do
116
+ let(:filter_args) do
117
+ [{ :beautify => true }]
118
+ end
119
+
120
+ it "passes options to the Uglify compressor" do
121
+ filter.input_files = [input_file("name.js", js_input)]
122
+ tasks = filter.generate_rake_tasks
123
+ tasks.each(&:invoke)
124
+ file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
125
+ file.body.should == expected_beautiful_js_output
126
+ end
127
+ end
128
+ end