roger_yuicompressor 1.0.1 → 1.1.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTU3ODZlMWM0ODgzMGVmYzQ0ZWM1NGMzYTYxODBiNDRkMmRlMDZiMA==
4
+ MjhlYzQ2NjU5MGU1NTI4MGI5YzE3NzdjMzFiZmY3OTgzMGE2NmViOQ==
5
5
  data.tar.gz: !binary |-
6
- OTJmOGM2YWM5MTUyNzUzZmJiNDQ3NmEzN2NjOTAwOWZjNzNhZDhmOA==
6
+ NzJiZWFjZTJhOWVmNTQwNDg1OTFkYTA5YWUxNDhlOWI2YjEwMDgwNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjlkNWRlOGJmYTdmNDkyZGNlN2ExNzRmOGYyZDMwZDQ1YmI4NzZlNTM2NDRj
10
- ZWNlNDQzNWMxOTM0MGYxNzI1NmRhYTlkOTcwNjIwZDc2NmJjNDM5OThmNjA4
11
- MjE4YzI3NGJlZWRjMTNkZTA4YjViZDVmOWJhZWI1MDk5ZDZmYTM=
9
+ MTUxNDcxOTI0NDQ2OWE3MzcyYTkyMmUzMWFmNTcxNjk1OGYzZTQ4NTY3MTc2
10
+ N2M5N2IyYTdhMjIwZDU0NDcxOWJjZTQ1NGY2MmMyYjRjODAxZWUzM2RiODhh
11
+ MGZjMjY5MWY3ODFmZWY4NTQ3M2Q4NDRhYTljMDJjODI4MzY2MmI=
12
12
  data.tar.gz: !binary |-
13
- YmQyMWVlYWQ5N2QyOTM3MGU1ZTYwYzhhNWIxYzI4YTc0OGQ4YmUxMGQ5Y2Mx
14
- NTNhMTc4Mjc4NWRlYTZiMzE1Y2M5NmE0NDk0MzEwZTQ3NGJlYWExYThhNTI2
15
- MThhMDI2OTdlMzVjN2EwYThlMjE1ZDY0ZDMzMTg3Yzg3ZGQ4MWI=
13
+ YjE2NzFhM2I3YTVkMmY2ZjRlNjg2ZTkxZTZhMTJhMjMwOTk1OTUxODRjYThl
14
+ YWJkNDhjNzI4ZmQyNWYxMWUzYjYzYTJmNmM1YTBhMWNkNzA0ZjcyYTcwNTI5
15
+ MGY0YTc4MzE1ZmY4ZjI4ZWJmMGU0OGMyMzkwNTFmNjk1OTU2OGY=
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - rbx-2
7
+ - jruby
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: rbx-2
11
+ - rvm: jruby
12
+ fast_finish: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.1.0
4
+ * Add support for minifying files with a suffix without overwriting the original.
5
+
3
6
  ## Version 1.0.1
4
7
  * Be more permissive in Roger versions
5
8
 
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rake"
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ roger_yuicompressor (1.0.1)
5
+ roger (>= 0.11.0)
6
+ yui-compressor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ hpricot (0.8.6)
12
+ mime-types (2.3)
13
+ rack (1.5.2)
14
+ rake (10.3.2)
15
+ redcarpet (3.1.2)
16
+ roger (0.12.2)
17
+ hpricot (>= 0.6.4)
18
+ mime-types (~> 2.2)
19
+ rack (>= 1.0.0)
20
+ redcarpet (>= 3.1.1)
21
+ thor (~> 0.16.0)
22
+ tilt (~> 2.0.1)
23
+ thor (0.16.0)
24
+ tilt (2.0.1)
25
+ yui-compressor (0.12.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ rake
32
+ roger_yuicompressor!
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/unit/**/*_test.rb']
8
+ t.verbose = true
9
+ end
@@ -1,5 +1,23 @@
1
+ require 'roger/release'
2
+ require 'yui/compressor'
3
+
1
4
  module RogerYuicompressor
2
- class Yuicompressor < Roger::Release::Processors::Base
5
+ class Yuicompressor < ::Roger::Release::Processors::Base
6
+
7
+ attr_reader :options
8
+
9
+ def initialize(options={})
10
+ @options = {
11
+ :match => ["**/*.{css,js}"],
12
+ :skip => [/javascripts\/vendor\/.*\.js\Z/, /_doc\/.*/],
13
+ :delimiter => Regexp.escape("/* -------------------------------------------------------------------------------- */"),
14
+ :suffix => nil
15
+ }.update(options)
16
+
17
+ compressor_options = {:line_break => 80}
18
+ @css_compressor = YUI::CssCompressor.new(compressor_options)
19
+ @js_compressor = YUI::JavaScriptCompressor.new(compressor_options)
20
+ end
3
21
 
4
22
  # Compresses all JS and CSS files, it will keep all lines before
5
23
  #
@@ -10,48 +28,69 @@ module RogerYuicompressor
10
28
  # @options options [Array] match Files to match, default to ["**/*.{css,js}"]
11
29
  # @options options [Regexp] :delimiter An array of header delimiters. Defaults to the one above. The delimiter will be removed from the output.
12
30
  # @options options [Array[Regexp]] :skip An array of file regular expressions to specifiy which files to skip. Defaults to [/javascripts\/vendor\/.\*.js\Z/, /_doc\/.*/]
31
+ # @options options [Nil|string] :suffix A string that will be added to the filename of the minified file, if set to nil will overwrite the existing file. Defaults to nil.
32
+ # (example: test.js => test.min.js where :suffix => '.min').
13
33
  def call(release, options={})
14
- options = {
15
- :match => ["**/*.{css,js}"],
16
- :skip => [/javascripts\/vendor\/.*\.js\Z/, /_doc\/.*/],
17
- :delimiter => Regexp.escape("/* -------------------------------------------------------------------------------- */")
18
- }.update(options)
19
-
20
- compressor_options = {:line_break => 80}
21
- css_compressor = YUI::CssCompressor.new(compressor_options)
22
- js_compressor = YUI::JavaScriptCompressor.new(compressor_options)
23
-
34
+ @options.update(options)
35
+
24
36
  release.log self, "Minifying #{options[:match].inspect}"
25
-
37
+
38
+ raise(RuntimeError, "The given suffix #{@options[:suffix]} is invalid.") unless valid_suffix?(@options[:sufix])
39
+
26
40
  # Add version numbers and minify the files
27
- release.get_files(options[:match], options[:skip]).each do |f|
28
- type = f[/\.([^.]+)\Z/,1]
41
+ release.get_files(options[:match], options[:skip]).each do |filename|
42
+ minify_file(filename, release)
43
+ end
44
+ end
45
+
46
+ # Retrieve the type of the file (based on extension)
47
+ #
48
+ # @param [String] The name of the file incl. extension
49
+ def get_file_type(filename)
50
+ filename[/\.([^.]+)\Z/, 1]
51
+ end
52
+
53
+ # Check if a valid suffix is given
54
+ def valid_suffix?(suffix = nil)
55
+ suffix === nil || suffix.kind_of?(String)
56
+ end
57
+
58
+ # Read and minfiy the given file
59
+ def minify_file(file, release)
60
+ data = File.read(file);
61
+ type = get_file_type(file)
62
+
63
+ if @options[:suffix] != nil
64
+ output_file = file.sub(".#{type}", "") + @options[:suffix] + ".#{type}"
65
+ else
66
+ output_file = file
67
+ end
68
+
69
+ File.open(output_file, "w") do |fh|
29
70
 
30
- data = File.read(f);
31
- File.open(f,"w") do |fh|
32
-
33
- # Extract header and store for later use
34
- header = data[/\A(.+?)\n#{options[:delimiter]}\s*\n/m,1]
35
- minified = [header]
36
-
37
- # Actual minification
38
- release.debug self, "Minifying #{f}"
39
- case type
40
- when "css"
41
- minified << css_compressor.compress(data)
42
- when "js"
43
- minified << js_compressor.compress(data)
44
- else
45
- release.warn self, "Error minifying: encountered unknown type \"#{type}\""
46
- minified << data
47
- end
48
-
49
- fh.write minified.join("\n")
71
+ # Extract header and store for later use
72
+ header = data[/\A(.+?)\n#{options[:delimiter]}\s*\n/m,1]
73
+ minified = [header]
74
+
75
+ # Actual minification
76
+ # release.debug self, "Minifying #{file}"
77
+ case type
78
+ when "css"
79
+ minified << @css_compressor.compress(data)
80
+ when "js"
81
+ minified << @js_compressor.compress(data)
82
+ else
83
+ # release.warn self, "Error minifying: encountered unknown type \"#{type}\""
84
+ minified << data
50
85
  end
51
- end
52
-
86
+
87
+ fh.write minified.join("\n")
88
+ end
89
+
90
+ output_file
53
91
  end
92
+
54
93
  end
55
94
  end
56
95
 
57
- Roger::Release::Processors.register(:yuicompressor, RogerYuicompressor::Yuicompressor)
96
+ ::Roger::Release::Processors.register(:yuicompressor, RogerYuicompressor::Yuicompressor)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "roger_yuicompressor"
5
- s.version = "1.0.1"
5
+ s.version = "1.1.0"
6
6
 
7
7
  s.authors = ["Flurin Egger"]
8
8
  s.email = ["info@digitpaint.nl", "flurin@digitpaint.nl"]
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + "/../../lib/roger_yuicompressor/processor"
2
+ require "test/unit"
3
+
4
+ class ProcessorTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @processor = RogerYuicompressor::Yuicompressor.new
8
+ @tmp_file = 'file.css'
9
+
10
+ # TEMP file creation
11
+ File.open(@tmp_file, 'w') do |tmp_file|
12
+ tmp_file.write('.selector { background: green; }')
13
+ end
14
+ end
15
+
16
+ # Just make sure we're dealing with the correct class
17
+ def test_setup
18
+ assert_kind_of(RogerYuicompressor::Yuicompressor, @processor, "Not initialized the correct class.")
19
+ end
20
+
21
+ # When no suffix is given, there is no suffix and
22
+ # this default behaviour should be valid
23
+ def test_default_valid_suffix
24
+ assert(@processor.valid_suffix?, "The given suffix (#{@processor.options[:suffix]}) is not valid.")
25
+ end
26
+
27
+ # When the suffix '.min' is given it should be valid
28
+ def test_valid_suffix
29
+ assert(@processor.valid_suffix?(".min"), "Suffix '.min' should be valid.")
30
+ assert(@processor.valid_suffix?("-test"), "Suffix '-test' should be valid.")
31
+ end
32
+
33
+ # Test that extension of file get correct extracted
34
+ def test_extracting_extension
35
+ assert_equal('rb', @processor.get_file_type('file.rb'))
36
+ assert_equal('js', @processor.get_file_type('file.min.js'))
37
+ assert_equal('css', @processor.get_file_type('file.css'))
38
+ assert_not_equal('min.js', @processor.get_file_type('file.min.js'))
39
+ end
40
+
41
+ # Test default behaviour that minfied data is put in the original file
42
+ def test_default_behaviour_processor_minify
43
+ assert_equal('file.css', @processor.minify_file(@tmp_file, nil))
44
+ end
45
+
46
+ def test_behaviour_processor_minify
47
+ processor = RogerYuicompressor::Yuicompressor.new({:suffix => '.min'})
48
+ assert_equal('file.min.css', processor.minify_file(@tmp_file, nil))
49
+
50
+ assert(File.exists?('file.min.css'))
51
+ assert(File.exists?('file.css'))
52
+ end
53
+
54
+ def teardown
55
+ File.delete('file.css') if File.exists?('file.css')
56
+ File.delete('file.min.css') if File.exists?('file.min.css')
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roger_yuicompressor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flurin Egger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-09 00:00:00.000000000 Z
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roger
@@ -46,12 +46,17 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - .travis.yml
49
50
  - CHANGELOG.md
51
+ - Gemfile
52
+ - Gemfile.lock
50
53
  - LICENSE
51
54
  - README.md
55
+ - Rakefile
52
56
  - lib/roger_yuicompressor.rb
53
57
  - lib/roger_yuicompressor/processor.rb
54
58
  - roger_yuicompressor.gemspec
59
+ - test/unit/processor_test.rb
55
60
  homepage: http://github.com/digitpaint/roger_yuicompressor
56
61
  licenses:
57
62
  - MIT
@@ -76,4 +81,5 @@ rubygems_version: 2.1.5
76
81
  signing_key:
77
82
  specification_version: 4
78
83
  summary: Processor for using yuicompressor to minify CSS/JS
79
- test_files: []
84
+ test_files:
85
+ - test/unit/processor_test.rb