rapper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -29,7 +29,7 @@ Rapper is configured using a YAML file that defines the settings to be used in v
29
29
  <<: *base
30
30
  bundle: false # optional, default: true
31
31
  compress: false # optional, default: true
32
- versions: false # optional, default: true
32
+ version: false # optional, default: true
33
33
  log: stdout # optional, ["stdout", file path], default: off
34
34
  log_verbose: true # optional, default: off
35
35
 
@@ -37,7 +37,7 @@ Rapper is configured using a YAML file that defines the settings to be used in v
37
37
  <<: *base
38
38
  bundle: true
39
39
  compress: true
40
- versions: true
40
+ version: true
41
41
  # optional, passed to Google Closure Compiler
42
42
  closure_compiler:
43
43
  # default: SIMPLE_OPTIMIZATIONS
@@ -55,7 +55,7 @@ The following defaults are applied if not defined in your configuration:
55
55
  bundle: true
56
56
  compress: true
57
57
  tag_style: html5
58
- versions: true
58
+ version: true
59
59
  closure_compiler:
60
60
  compilation_level: SIMPLE_OPTIMIZATIONS
61
61
 
@@ -123,6 +123,7 @@ Browsers will automatically re-download and cache the new asset.
123
123
 
124
124
  ## Version history
125
125
 
126
+ * **0.0.2** - Compression now works and is specced.
126
127
  * **0.0.1** - Initial release. Functioning bundler, minus the view helpers.
127
128
 
128
129
  ## Contributing to rapper
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/rapper.rb CHANGED
@@ -55,6 +55,11 @@ module Rapper
55
55
 
56
56
  log :verbose, "Joining #{definition["assets"].count} files to #{name}"
57
57
  join_files( source_files, destination_file )
58
+
59
+ if get_config( "compress" )
60
+ log :verbose, "Compressing #{name}"
61
+ compress( destination_file )
62
+ end
58
63
  end
59
64
  end
60
65
 
@@ -1,46 +1,105 @@
1
1
  require File.expand_path( File.dirname( __FILE__ ) + "/../yui/css_compressor.rb" )
2
2
  require "closure-compiler"
3
+ require 'fileutils'
3
4
 
4
5
  module Rapper
5
- # Compression methods for various types of assets.
6
+ # Compression handlers for various types of assets. And by "various" I mean
7
+ # JavaScript and CSS.
6
8
  module Compressors
7
9
 
8
- # Use Richard Hulse's port of the YUI CSS Compressor to compress the
9
- # contents of a source file to a destination file.
10
+ # Compress a file in-place. Relies on the file's suffix to determine type.
10
11
  #
11
- # @param [String] source Path to source CSS file.
12
- #
13
- # @param [String] destination Path to destination CSS file.
14
- # written to.
15
- def compress_css( source, destination )
16
- log :verbose, "Compressing #{source}"
17
-
18
- source = readable_file( source )
19
- destination = writable_file( source )
20
-
21
- destination.write( YUI::CSS.compress( source.read ) )
22
- destination.write "\n"
23
-
24
- source.close && destination.close
12
+ # @param [String] file Path to the file to compress.
13
+ def compress( file )
14
+ opts = {}
15
+ # TODO: Someday this goes away.
16
+ opts = get_config( "closure_compiler" ) if file =~ /\.js/
17
+ Rapper::Compressors::Compressor.compress( file, opts )
25
18
  end
26
19
 
27
- # Use Google's Closure Compiler to compress the JavaScript from a source
28
- # file to a destination file.
29
- #
30
- # @param [String] source Path to source JavaScript file.
31
- #
32
- # @param [String] destination Path to destination JavaScript file.
33
- def compress_js( source, destination, opts={} )
34
- log :verbose, "Compressing #{source}"
35
-
36
- source = readable_file( source )
37
- destination = writable_file( source )
38
- closure = Closure::Compiler.new( opts )
20
+ protected
21
+
22
+ # Base class for a compression handler.
23
+ class Compressor
24
+ class << self
25
+
26
+ # Get the compressor for a given file.
27
+ #
28
+ # @param [String] file Path to the file to fetch the Compressor for
29
+ # @return [Types] Description
30
+ def compress( file_path, opts={} )
31
+ unless compressor = @extensions[File.extname( file_path )]
32
+ raise Rapper::Errors::UnknownFileExtension,
33
+ "Rapper doesn't know how to compress #{file_path}"
34
+ end
35
+
36
+ compressor.do_compress( file_path, opts )
37
+ end
38
+
39
+ protected
40
+
41
+ attr_accessor :extensions
42
+
43
+ # Register `self` as a file compressor.
44
+ #
45
+ # @param [String] extension The file extension `self` handles.
46
+ def register( extension )
47
+ superclass.extensions ||= {}
48
+ superclass.extensions[extension] = self
49
+ end
50
+
51
+ # Compress a file.
52
+ def do_compress( file_path )
53
+ raise NotImplementedError
54
+ end
55
+
56
+ # @param [String] path Path to a file.
57
+ #
58
+ # @return [String] The contents of the file.
59
+ def read_file( path )
60
+ File.new( path, 'r' ).read
61
+ end
62
+
63
+ # @param [String] path Path to the desired file.
64
+ #
65
+ # @return [File] Writable file instance with 0644 permissions.
66
+ def writable_file( path )
67
+ File.new( path, 'w', 0644 )
68
+ end
69
+ end
70
+ end
71
+
72
+ # Use Richard Hulse's Ruby port of the YUI CSS Compressor to compress the
73
+ # contents of a CSS file.
74
+ class CSSCompressor < Compressor
75
+ register ".css"
39
76
 
40
- destination.write( closure.compile( destination ) )
41
- destination.write "\n"
77
+ def self.do_compress( file_path, opts={} )
78
+ css = read_file( file_path )
79
+ css = YUI::CSS.compress( css )
80
+ destination = writable_file( file_path )
81
+
82
+ destination.write( css )
83
+ destination.write "\n"
84
+ destination.close
85
+ end
86
+ end
87
+
88
+ # Uses Google's Closure Compiler (via DocumentCloud's closure-compiler gem)
89
+ # to compress JavaScrpt.
90
+ class JSCompressor < Compressor
91
+ register ".js"
42
92
 
43
- source.close && destination.close
93
+ def self.do_compress( file_path, opts={} )
94
+ closure = Closure::Compiler.new( opts )
95
+
96
+ js = read_file( file_path )
97
+ destination = writable_file( file_path )
98
+
99
+ destination.write( closure.compile( js ) )
100
+ destination.write "\n"
101
+ destination.close
102
+ end
44
103
  end
45
104
 
46
105
  end
data/lib/rapper/config.rb CHANGED
@@ -45,7 +45,7 @@ module Rapper
45
45
  if default_config[key].is_a?( Hash )
46
46
  default_config[key].merge( env_config[key] || {} )
47
47
  else
48
- env_config[key] || default_config[key]
48
+ env_config.key?( key ) ? env_config[key] : default_config[key]
49
49
  end
50
50
  end
51
51
 
@@ -74,7 +74,7 @@ module Rapper
74
74
  "bundle" => true,
75
75
  "compress" => true,
76
76
  "tag_style" => "html5",
77
- "versions" => true,
77
+ "version" => true,
78
78
  "closure_compiler" => {
79
79
  "compilation_level" => "SIMPLE_OPTIMIZATIONS"
80
80
  }
data/lib/rapper/errors.rb CHANGED
@@ -19,5 +19,8 @@ module Rapper
19
19
  # is one not defined in a given definition file.
20
20
  class InvalidAssetName < StandardError; end
21
21
 
22
+ # Raised when attempting to compress a file with an extension that Rapper
23
+ # doesn't recognize.
24
+ class UnknownFileExtension < StandardError; end
22
25
  end
23
26
  end
data/lib/rapper/utils.rb CHANGED
@@ -12,7 +12,7 @@ module Rapper
12
12
  #
13
13
  # @return [String] Path to the definition file for the given asset type.
14
14
  def definition_path( type )
15
- File.join( env_config["definition_root"], "#{type}.yml")
15
+ File.join( get_config( "definition_root" ), "#{type}.yml")
16
16
  end
17
17
 
18
18
  # @param [String] type Asset type.
@@ -64,20 +64,6 @@ module Rapper
64
64
  # = Files =
65
65
  # =========
66
66
 
67
- # @param [String] path Path to the desired file.
68
- #
69
- # @return [File] Readable File instance.
70
- def readable_file( path )
71
- File.new( path, 'r' )
72
- end
73
-
74
- # @param [String] path Path to the desired file.
75
- #
76
- # @return [File] Writable file instance with 0644 permissions.
77
- def writable_file( path )
78
- File.new( path, 'w', 0644 )
79
- end
80
-
81
67
  # Contatenate one or more files. Uses <code>cat</code>.
82
68
  #
83
69
  # @param [Array<String>,String] source_files A path or array of paths to
data/rapper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rapper}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyson Tate"]
12
- s.date = %q{2011-02-04}
12
+ s.date = %q{2011-02-05}
13
13
  s.description = %q{Static asset packager and compressor with versioning and built-in view helpers. Easy to configure, easy to use, and easy to ignore when you want to. No crazy JavaScript comment DSLs, either.}
14
14
  s.email = %q{tyson@tysontate.com}
15
15
  s.extra_rdoc_files = [
@@ -42,6 +42,13 @@ Gem::Specification.new do |s|
42
42
  "spec/fixtures/javascripts/simple_2.js",
43
43
  "spec/fixtures/stylesheets/simple_1.css",
44
44
  "spec/fixtures/stylesheets/simple_2.css",
45
+ "spec/fixtures/test_cases/compression/assets.yml",
46
+ "spec/fixtures/test_cases/compression/definitions/css.yml",
47
+ "spec/fixtures/test_cases/compression/definitions/js.yml",
48
+ "spec/fixtures/test_cases/compression/expected/base.css",
49
+ "spec/fixtures/test_cases/compression/expected/base.js",
50
+ "spec/fixtures/test_cases/compression/expected/base_reversed.css",
51
+ "spec/fixtures/test_cases/compression/expected/base_reversed.js",
45
52
  "spec/fixtures/test_cases/concatenation/assets.yml",
46
53
  "spec/fixtures/test_cases/concatenation/definitions/css.yml",
47
54
  "spec/fixtures/test_cases/concatenation/definitions/js.yml",
@@ -5,7 +5,7 @@ test:
5
5
  bundle: true
6
6
  compress: true
7
7
  tag_style: html
8
- versions: false
8
+ version: false
9
9
  closure_compiler:
10
10
  compilation_level: ADVANCED_OPTIMIZATIONS
11
11
  formatting: pretty_print
@@ -1,5 +1,5 @@
1
1
  var x = 1;
2
2
  var y = 2;
3
- function() {
3
+ function a() {
4
4
  return true;
5
5
  }
@@ -1,5 +1,5 @@
1
1
  a = 1;
2
2
  b = 2;
3
- function() {
3
+ function b() {
4
4
  return false;
5
5
  }
@@ -0,0 +1,5 @@
1
+ test:
2
+ definition_root: spec/fixtures/test_cases/concatenation/definitions
3
+ bundle: true
4
+ compress: true
5
+ version: false
@@ -0,0 +1,15 @@
1
+ --- !omap
2
+ - source_root: spec/fixtures/stylesheets
3
+ - destination_root: tmp
4
+ - suffix: css
5
+ - assets:
6
+ - base:
7
+ - files:
8
+ - simple_1
9
+ - simple_2
10
+ - version: 683e
11
+ - base_reversed:
12
+ - files:
13
+ - simple_2
14
+ - simple_1
15
+ - version: 4a04
@@ -0,0 +1,15 @@
1
+ --- !omap
2
+ - source_root: spec/fixtures/javascripts
3
+ - destination_root: tmp
4
+ - suffix: js
5
+ - assets:
6
+ - base:
7
+ - files:
8
+ - simple_1
9
+ - simple_2
10
+ - version: 7b06
11
+ - base_reversed:
12
+ - files:
13
+ - simple_2
14
+ - simple_1
15
+ - version: db62
@@ -0,0 +1 @@
1
+ body{margin:0;color:#333}*{margin:0;color:#fff}
@@ -0,0 +1,2 @@
1
+ var x=1,y=2;function a(){return true}a=1;b=2;function b(){return false};
2
+
@@ -0,0 +1 @@
1
+ *{margin:0;color:#fff}body{margin:0;color:#333}
@@ -0,0 +1,2 @@
1
+ a=1;b=2;function b(){return false}var x=1,y=2;function a(){return true};
2
+
@@ -2,4 +2,4 @@ test:
2
2
  definition_root: spec/fixtures/test_cases/concatenation/definitions
3
3
  bundle: true
4
4
  compress: false
5
- versions: false
5
+ version: false
@@ -7,9 +7,9 @@
7
7
  - files:
8
8
  - simple_1
9
9
  - simple_2
10
- - version: 7b06
10
+ - version: f3d9
11
11
  - base_reversed:
12
12
  - files:
13
13
  - simple_2
14
14
  - simple_1
15
- - version: db62
15
+ - version: fcfe
@@ -1,10 +1,10 @@
1
1
  var x = 1;
2
2
  var y = 2;
3
- function() {
3
+ function a() {
4
4
  return true;
5
5
  }
6
6
  a = 1;
7
7
  b = 2;
8
- function() {
8
+ function b() {
9
9
  return false;
10
10
  }
@@ -1,10 +1,10 @@
1
1
  a = 1;
2
2
  b = 2;
3
- function() {
3
+ function b() {
4
4
  return false;
5
5
  }
6
6
  var x = 1;
7
7
  var y = 2;
8
- function() {
8
+ function a() {
9
9
  return true;
10
10
  }
data/spec/rapper_spec.rb CHANGED
@@ -40,7 +40,7 @@ describe Rapper do
40
40
  # private
41
41
  rapper.send( :get_config, "bundle" ).should be_true
42
42
  rapper.send( :get_config, "compress" ).should be_true
43
- rapper.send( :get_config, "versions" ).should be_true
43
+ rapper.send( :get_config, "version" ).should be_true
44
44
  rapper.send( :get_config, "closure_compiler" ).should == {
45
45
  "compilation_level" => "SIMPLE_OPTIMIZATIONS"
46
46
  }
@@ -111,7 +111,6 @@ describe Rapper do
111
111
 
112
112
  results.each_index do |i|
113
113
  unless File.read( results[i] ) == File.read( expecteds[i] )
114
- # p File.read( results[i] )
115
114
  raise "#{results[i]} did not match #{expecteds[i]}"
116
115
  end
117
116
  end
@@ -123,10 +122,6 @@ describe Rapper do
123
122
  it "raises an error if a file doesn't exist"
124
123
  end
125
124
 
126
- describe "compressing" do
127
- it "can be turned off"
128
- end
129
-
130
125
  describe "view helpers" do
131
126
  it "returns tags for component files when bundling is off"
132
127
  it "returns tabs for asset when bundling is on"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyson Tate
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-04 00:00:00 -08:00
18
+ date: 2011-02-05 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -245,6 +245,13 @@ files:
245
245
  - spec/fixtures/javascripts/simple_2.js
246
246
  - spec/fixtures/stylesheets/simple_1.css
247
247
  - spec/fixtures/stylesheets/simple_2.css
248
+ - spec/fixtures/test_cases/compression/assets.yml
249
+ - spec/fixtures/test_cases/compression/definitions/css.yml
250
+ - spec/fixtures/test_cases/compression/definitions/js.yml
251
+ - spec/fixtures/test_cases/compression/expected/base.css
252
+ - spec/fixtures/test_cases/compression/expected/base.js
253
+ - spec/fixtures/test_cases/compression/expected/base_reversed.css
254
+ - spec/fixtures/test_cases/compression/expected/base_reversed.js
248
255
  - spec/fixtures/test_cases/concatenation/assets.yml
249
256
  - spec/fixtures/test_cases/concatenation/definitions/css.yml
250
257
  - spec/fixtures/test_cases/concatenation/definitions/js.yml