hulk 0.0.3

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use hulk
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hulk.gemspec
4
+ gemspec
@@ -0,0 +1,122 @@
1
+ Hulk: simple asset compression
2
+ ==============================
3
+
4
+ Hulk makes it easy to compress CSS and JavaScript strings into groups called _bundles._ It works with raw strings, not file systems. Compression is done with [YUI Compressor][YUI].
5
+
6
+ Why another asset compiler?
7
+ ---------------------------
8
+
9
+ Hulk processes raw strings. This means you can load your assets from wherever you want: a database, filesystem, remote URL, unicorn colony, etc.
10
+
11
+ Why use raw strings instead of a file system? Flexibility. For example, a hosted CMS might allows users to edit CSS & JS through a web-interface and save it to a database.
12
+
13
+ Hulk is _extremely simple_ -- it's basically just a light layer of sugar on top of [YUI Compressor][YUI].If you want support for all kinds of other fandangled preprocessors or are simply reading files from a filesystem, you'll probably want to use one of these instead:
14
+
15
+ * [Sprockets][SPROCKETS]
16
+ * [Jammit][JAMMIT]
17
+ * [Crush][CRUSH]
18
+
19
+ Overview
20
+ --------
21
+
22
+ _Bundles_ are the basic compilation unit of Hulk. A bundle is a collection of strings that compile into a single, named unit. Usually you'll just have two bundles: one for you CSS, and one for your JavaScripts.
23
+
24
+ Although you can work with bundles directly, it's much easier to do so indirectly through an instance of `Hulk::Assets`. This lets you build up your source strings and compile them all in one go.
25
+
26
+ Example Usage
27
+ -------------
28
+
29
+ First, create an instance of `Hulk::Assets` to hold your bundles:
30
+
31
+ assets = Hulk::Assets.new
32
+
33
+ Now add your CSS & JavaScript to the appropriate bundle:
34
+
35
+ assets.css[:all] << File.read('reset.css')
36
+ assets.css[:all] << File.read('screen.css')
37
+ assets.css[:all] << File.read('mobile.css')
38
+ assets.css[:all] << "body { background: #fff; }"
39
+
40
+ assets.js[:all] << File.read('jquery.js')
41
+ assets.js[:all] << File.read('backbone.js')
42
+ assets.js[:all] << File.read('application.js')
43
+
44
+ If you want to use something like [Sass](http://sass-lang.com/) or [CoffeeScript](http://jashkenas.github.com/coffee-script/) you can compile the sources yourself before adding to the bundle:
45
+
46
+ assets.css[:all] << Sass.compile(File.read('helpers.scss'))
47
+ assets.js[:all] << CoffeeScript.compile(File.read('app.coffee'))
48
+
49
+ And finally, compile everything:
50
+
51
+ assets.compile!
52
+
53
+ _Assets are compiled in the same order they're appended._
54
+
55
+ Use either `to_css` or `to_js` to get the contents of the bundle:
56
+
57
+ assets.css[:all].to_css # The compiled CSS
58
+ assets.js[:all].to_js # The compiled JS
59
+
60
+ _These methods will compile the bundle if it's not already compiled._
61
+
62
+ From here you can do whatever you want, like write the bundle to a file:
63
+
64
+ file = File.open("all.css", 'w')
65
+ file.write(assets.css[:base].to_css)
66
+ file.close()
67
+
68
+ Bundles have a `digest` based on the compiled contents:
69
+
70
+ assets.css[:all].digest # => "d41d8cd98f00b204e9800998ecf8427e"
71
+
72
+ They also have a `filename` helper method:
73
+
74
+ assets.css[:all].filename # => "d41d8cd98f00b204e9800998ecf8427e-all.css"
75
+
76
+ Once compiled, bundles are immutable:
77
+
78
+ assets.compile!
79
+ assets.css[:base] << "p { border: none; }" # Raises `Hulk::FrozenBundleError`
80
+
81
+ Bug Reports & Contributing
82
+ ----------------------------
83
+
84
+ You can report bugs using the [issue tracker][ISSUES] right here on GitHub! You get extra mojo if you include a fix.
85
+
86
+ License
87
+ --------
88
+
89
+ Hulk is distributed under a Simplified BSD License:
90
+
91
+ Copyright 2011 Carbonmade LLC. All rights reserved.
92
+
93
+ Redistribution and use in source and binary forms, with or without modification, are
94
+ permitted provided that the following conditions are met:
95
+
96
+ 1. Redistributions of source code must retain the above copyright notice, this list of
97
+ conditions and the following disclaimer.
98
+
99
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
100
+ of conditions and the following disclaimer in the documentation and/or other materials
101
+ provided with the distribution.
102
+
103
+ THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
104
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
105
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
106
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
107
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
108
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
109
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
110
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
111
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112
+
113
+ The views and conclusions contained in the software and documentation are those of the
114
+ authors and should not be interpreted as representing official policies, either expressed
115
+ or implied, of Carbonmade LLC.
116
+
117
+
118
+ [YUI]: https://github.com/sstephenson/ruby-yui-compressor
119
+ [SPROCKETS]: https://github.com/sstephenson/sprockets
120
+ [JAMMIT]: https://github.com/documentcloud/jammit
121
+ [CRUSH]: https://github.com/petebrowne/crush
122
+ [ISSUES]: https://github.com/Carbonmade/hulk/issues
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hulk/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hulk"
7
+ s.version = Hulk::VERSION
8
+ s.authors = ["Kyle Fox"]
9
+ s.email = ["kyle@carbonmade.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{An easy way to compress static assets.}
12
+ s.description = %q{Hulk allows you to group JavaScript & CSS into compressed bundles.}
13
+
14
+ s.rubyforge_project = "hulk"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "yui-compressor", "0.9.6"
24
+ s.add_runtime_dependency "uglifier", "1.1.0"
25
+ end
@@ -0,0 +1,8 @@
1
+ require "hulk/version"
2
+ require "hulk/css_bundle"
3
+ require "hulk/js_bundle"
4
+ require "hulk/assets"
5
+
6
+ module Hulk
7
+ class FrozenBundleError < StandardError; end
8
+ end
@@ -0,0 +1,17 @@
1
+ require "hulk/bundle_collection"
2
+
3
+ module Hulk
4
+ class Assets
5
+ attr_accessor :css, :js
6
+
7
+ def initialize
8
+ @css = Hulk::BundleCollection.new(:css)
9
+ @js = Hulk::BundleCollection.new(:js)
10
+ end
11
+
12
+ def compile!
13
+ @css.compile!
14
+ @js.compile!
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ require 'digest/md5'
2
+
3
+ module Hulk
4
+ class Bundle
5
+
6
+ attr_accessor :name
7
+
8
+ def initialize(name)
9
+ @name = name.to_s
10
+ @raw = []
11
+ @compiled = nil
12
+ end
13
+
14
+ def <<(val)
15
+ (@raw << val) and return unless compiled?
16
+ raise Hulk::FrozenBundleError.new("This bundle has already been compiled.")
17
+ end
18
+
19
+ def compiled?
20
+ !@compiled.nil?
21
+ end
22
+
23
+ def digest
24
+ return nil unless compiled?
25
+ @digest ||= Digest::MD5.hexdigest(compile!)
26
+ end
27
+
28
+ def compile!
29
+ @compiled ||= generate_compiled(@raw.join)
30
+ end
31
+
32
+ def filename
33
+ "#{digest}-#{name}.#{ext}"
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ require 'hulk/css_bundle'
2
+ require 'hulk/js_bundle'
3
+
4
+ module Hulk
5
+ class BundleCollection
6
+ def initialize(type)
7
+ @bundle_class = {
8
+ :css => Hulk::CssBundle,
9
+ :js => Hulk::JsBundle,
10
+ }[type]
11
+ @bundles = Hash.new {|h, k| h[k] = @bundle_class.new(k)}
12
+ end
13
+
14
+ def [](name)
15
+ @bundles[name]
16
+ end
17
+
18
+ def compile!
19
+ @bundles.each do |_, bundle|
20
+ bundle.compile!
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require 'yui/compressor'
2
+ require 'digest/md5'
3
+ require 'hulk/bundle'
4
+
5
+ module Hulk
6
+ class CssBundle < Hulk::Bundle
7
+
8
+ alias_method :to_css, :compile!
9
+
10
+ def ext
11
+ :css
12
+ end
13
+
14
+ def generate_compiled(source)
15
+ self.class.compressor.compress(source)
16
+ end
17
+
18
+ private
19
+
20
+ def self.compressor
21
+ @@compressor ||= YUI::CssCompressor.new
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require 'uglifier'
2
+ require 'hulk/bundle'
3
+
4
+ module Hulk
5
+ class JsBundle < Hulk::Bundle
6
+
7
+ alias_method :to_js, :compile!
8
+
9
+ def ext
10
+ :js
11
+ end
12
+
13
+ def generate_compiled(source)
14
+ self.class.compressor.compile(source)
15
+ end
16
+
17
+ private
18
+
19
+ def self.compressor
20
+ @@compressor ||= Uglifier.new
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Hulk
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'test/unit'
2
+ require 'hulk'
3
+
4
+ class TestCssBundle < MiniTest::Unit::TestCase
5
+
6
+ def test_compile_css
7
+ bundle = Hulk::CssBundle.new(:awesome)
8
+ bundle << <<-EOF
9
+ body {
10
+ /* This makes the background red. */
11
+ background: red;
12
+ }
13
+ EOF
14
+ bundle << "table { width: 100%; border: 1px solid red; }"
15
+ expected = "body{background:red}table{width:100%;border:1px solid red}"
16
+ assert_equal expected, bundle.compile!
17
+ assert_equal bundle.compile!, bundle.to_css
18
+ end
19
+
20
+ def test_bundle_digest
21
+ bundle = Hulk::CssBundle.new(:awesome)
22
+ bundle << "body { background: red; }"
23
+ assert_equal nil, bundle.digest
24
+ bundle.compile!
25
+ assert_equal "a9e1e9ad1b183e6166efd88af91e8761", bundle.digest
26
+ end
27
+
28
+ def test_bundle_filename
29
+ bundle = Hulk::CssBundle.new(:awesome)
30
+ bundle << "body { background: red; }"
31
+ bundle.compile!
32
+ assert_equal "a9e1e9ad1b183e6166efd88af91e8761-awesome.css", bundle.filename
33
+ end
34
+
35
+ def test_frozen_bundle
36
+ bundle = Hulk::CssBundle.new(:awesome)
37
+ bundle << "body { background: red; }"
38
+ bundle.compile!
39
+ assert bundle.compiled?
40
+ assert_raises Hulk::FrozenBundleError do
41
+ bundle << "p { font-size: 14px; }"
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+ require 'hulk'
3
+
4
+ class TestHulk < MiniTest::Unit::TestCase
5
+
6
+ def test_css_bundles
7
+ assets = Hulk::Assets.new
8
+ assets.css[:base] << "body { background: red; }"
9
+ assets.css[:base] << "a { text-decoration: none; }"
10
+ assets.compile!
11
+ assert_equal "body{background:red}a{text-decoration:none}", assets.css[:base].to_css
12
+ end
13
+
14
+ def test_multiple_css_bundles
15
+ assets = Hulk::Assets.new
16
+ assets.css[:base] << "body{background:#c00}"
17
+ assets.css[:base] << "a { text-decoration: none; }"
18
+ assets.css[:type] << "body { line-height: 1.5em; }"
19
+ assets.css[:type] << "p { margin-bottom: 1.5em; }"
20
+ assets.compile!
21
+ assert_equal "body{background:#c00}a{text-decoration:none}", assets.css[:base].to_css
22
+ assert_equal "body{line-height:1.5em}p{margin-bottom:1.5em}", assets.css[:type].to_css
23
+ end
24
+
25
+ def test_js_bundle
26
+ assets = Hulk::Assets.new
27
+ assets.js[:base] << 'function test() { return "Hello, world!"; };'
28
+ assets.js[:base] << 'var person = {name: "Bob"};'
29
+ assets.compile!
30
+ assert_equal "function test(){return\"Hello, world!\"}var person={name:\"Bob\"}", assets.js[:base].to_js
31
+ end
32
+
33
+ def test_multiple_js_bundle
34
+ assets = Hulk::Assets.new
35
+ assets.js[:base] << 'function test() { return "Hello, world!"; };'
36
+ assets.js[:base] << 'var person = {name: "Bob"};'
37
+ assets.js[:app] << 'function App(name) { this.name = name; };'
38
+ assets.js[:app] << 'var app = new App("test");'
39
+ assets.compile!
40
+ assert_equal "function test(){return\"Hello, world!\"}var person={name:\"Bob\"}", assets.js[:base].to_js
41
+ assert_equal "function App(a){this.name=a}var app=new App(\"test\")", assets.js[:app].to_js
42
+ end
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require 'hulk'
3
+
4
+ class TestJsBundle < MiniTest::Unit::TestCase
5
+
6
+ def test_compile_js
7
+ bundle = Hulk::JsBundle.new(:awesome)
8
+ bundle << "var foo = 10, person = {'name': 'Joe', age: 42};"
9
+ bundle << <<-EOF
10
+ function thing() {
11
+ alert('whoa!');
12
+ }
13
+ EOF
14
+ expected = "function thing(){alert(\"whoa!\")}var foo=10,person={name:\"Joe\",age:42}"
15
+ assert_equal expected, bundle.compile!
16
+ assert_equal bundle.compile!, bundle.to_js
17
+ end
18
+
19
+ def test_bundle_digest
20
+ bundle = Hulk::JsBundle.new(:awesome)
21
+ bundle << "var foo = 10, person = {'name': 'Joe', age: 42};"
22
+ bundle << <<-EOF
23
+ function thing() {
24
+ alert('whoa!');
25
+ alert('whoa!');
26
+ alert('whoa!');
27
+ }
28
+ EOF
29
+ bundle.compile!
30
+ assert_equal "baafd2a470f3514fe17e93d380d997af", bundle.digest
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hulk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Fox
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yui-compressor
16
+ requirement: &70123693257600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70123693257600
25
+ - !ruby/object:Gem::Dependency
26
+ name: uglifier
27
+ requirement: &70123693257100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70123693257100
36
+ description: Hulk allows you to group JavaScript & CSS into compressed bundles.
37
+ email:
38
+ - kyle@carbonmade.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - README.md
47
+ - Rakefile
48
+ - hulk.gemspec
49
+ - lib/hulk.rb
50
+ - lib/hulk/assets.rb
51
+ - lib/hulk/bundle.rb
52
+ - lib/hulk/bundle_collection.rb
53
+ - lib/hulk/css_bundle.rb
54
+ - lib/hulk/js_bundle.rb
55
+ - lib/hulk/version.rb
56
+ - test/test_css_bundle.rb
57
+ - test/test_hulk.rb
58
+ - test/test_js_bundle.rb
59
+ homepage: ''
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project: hulk
79
+ rubygems_version: 1.8.11
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: An easy way to compress static assets.
83
+ test_files:
84
+ - test/test_css_bundle.rb
85
+ - test/test_hulk.rb
86
+ - test/test_js_bundle.rb