unified-assets 0.0.1

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 @@
1
+ unified-assets-*.gem
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Jake Gordon and contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,99 @@
1
+ Unified Assets (v0.0.1)
2
+ =======================
3
+
4
+ A small utility for unifying (and minifying) static javascript and css asset
5
+ files, either at build time or at run-time.
6
+
7
+ You declare a list of source files, and the gem will unify and minify them.
8
+
9
+ The gem also provides a simple rack-based web server that will serve up your
10
+ static website files and automatically recreate the unified files if the underlying
11
+ source files have been modified.
12
+
13
+ **NOTE:**
14
+
15
+ This gem is really for my own personal use when building some of my HTML-only games. They
16
+ consist of pure client-side html, javascript and css, with no dynamic server back end, but I
17
+ still want to auto-generate unified and minified scripts and styles.
18
+
19
+ **You will almost certainly be better off with** more robust tools such as:
20
+
21
+ * [sprockets](https://github.com/sstephenson/sprockets) - if you have a ruby/rails back end
22
+ * [gears](https://github.com/gears/gears) - if you have a python back end
23
+ * [mincer](https://github.com/nodeca/mincer) - if you have a node back end
24
+ * [requirejs](http://requirejs.org/) - if you want to do it the AMD way
25
+
26
+ This is just a tiny little gem that works well for **my** specific purposes and might not be the
27
+ best tool for others. Use your own judgement.
28
+
29
+ Installation
30
+ ============
31
+
32
+ $ gem install unified-assets
33
+
34
+ Usage
35
+ =====
36
+
37
+ To generate a unified/minified `scripts.js` and `styles.css` add tasks to your Rakefile
38
+
39
+ require 'unified_assets/tasks'
40
+
41
+ UnifiedAssets::Tasks.new do |t|
42
+ t.minify = true
43
+ t.assets = {
44
+
45
+ "scripts.js" => [
46
+ 'js/game/vendor/stats.js', # https://github.com/mrdoob/stats.js
47
+ 'js/game/vendor/sizzle.js', # http://sizzlejs.com/
48
+ 'js/game/vendor/animator.js', # http://berniesumption.com/software/animator/
49
+ 'js/game/vendor/audio-fx.js', # https://github.com/jakesgordon/javascript-audio-fx
50
+ 'js/game/vendor/state-machine.js', # https://github.com/jakesgordon/javascript-state-machine
51
+ 'js/game/base.js',
52
+ 'js/game/game.js',
53
+ 'js/game/dom.js',
54
+ 'js/game/menu.js',
55
+ 'js/game/key.js',
56
+ 'js/game/math.js',
57
+ 'js/game/vector.js',
58
+ 'js/snakes.js'
59
+ ],
60
+
61
+ "styles.css" => [
62
+ 'css/vendor/normalize.css',
63
+ 'css/snakes.css'
64
+ ]
65
+
66
+ }
67
+ end
68
+
69
+ Will make 3 rake tasks available to you:
70
+
71
+ > rake -T
72
+
73
+ rake assets:clear # clear unified asset files
74
+ rake assets:create # create unified asset files
75
+ rake assets:server # simple webserver that auto-regenerates assets if they are out of date
76
+
77
+ Use `rake assets:create` whenever your source files have changed to recreate the unified/minified versions.
78
+
79
+ Use `rake assets:server` to run a simple rack-based web server that can serve up your static website
80
+ and will automatically regenerate your unified assets if the source files have been changed.
81
+
82
+ TODO
83
+ ====
84
+
85
+ * command line scripts
86
+ * support templating (e.g. sass, coffeescript, erb)
87
+
88
+ License
89
+ =======
90
+
91
+ [MIT](http://en.wikipedia.org/wiki/MIT_License) license.
92
+
93
+ Contact
94
+ =======
95
+
96
+ If you have any ideas, feedback, requests or bug reports, you can reach me at
97
+ [jake@codeincomplete.com](mailto:jake@codeincomplete.com), or via
98
+ my website: [Code inComplete](http://codeincomplete.com).
99
+
@@ -0,0 +1,4 @@
1
+ February 9th 2013 - v0.0.1
2
+ --------------------------
3
+ * MVP - first rough release
4
+
@@ -0,0 +1,34 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/lib"
2
+ require "rake/testtask"
3
+ require "unified_assets/tasks"
4
+
5
+ task :default => :test
6
+
7
+ #------------------------------------------------------------------------------
8
+
9
+ Rake::TestTask.new do |t|
10
+ t.libs << "test"
11
+ t.test_files = FileList["test/**/*_test.rb"]
12
+ t.verbose = true
13
+ end
14
+
15
+ #------------------------------------------------------------------------------
16
+
17
+ UnifiedAssets::Tasks.new do |t|
18
+ t.minify = true
19
+ t.assets = {
20
+ "test/reference/unified.js" => [
21
+ 'test/js/a.js',
22
+ 'test/js/b.js',
23
+ 'test/js/c.js'
24
+ ],
25
+ "test/reference/unified.css" => [
26
+ 'test/css/a.css',
27
+ 'test/css/b.css',
28
+ 'test/css/c.css'
29
+ ]
30
+ }
31
+ end
32
+
33
+ #------------------------------------------------------------------------------
34
+
@@ -0,0 +1,22 @@
1
+ require 'unified_assets/builder'
2
+ require 'unified_assets/minifier'
3
+
4
+ module UnifiedAssets
5
+
6
+ #----------------------------------------------------------------------------
7
+
8
+ VERSION = "0.0.1"
9
+ SUMMARY = "Unify and minify JS and CSS website assets"
10
+ DESCRIPTION = "Simple tool to automatically unify and minify javascript and css website assets"
11
+ LIB = File.dirname(__FILE__)
12
+
13
+ #----------------------------------------------------------------------------
14
+
15
+ def self.build(files, unified_file_name, options = {})
16
+ Builder.new(files, unified_file_name, options).build
17
+ end
18
+
19
+ #----------------------------------------------------------------------------
20
+
21
+ end # module UnifiedAssets
22
+
@@ -0,0 +1,72 @@
1
+ module UnifiedAssets
2
+ class Builder
3
+
4
+ attr :files
5
+ attr :unified_file_name
6
+ attr :unified_file_mtime
7
+ attr :minified_file_name
8
+ attr :minified_file_mtime
9
+ attr :options
10
+
11
+ def initialize(files, unified_file_name, options = {})
12
+ @options = options
13
+ @files = Array(files)
14
+ @unified_file_name = unified_file_name
15
+ @unified_file_mtime = File.stat(unified_file_name).mtime if File.exists?(unified_file_name)
16
+ @minified_file_name = Minifier.minified_name(unified_file_name) if minifying?
17
+ @minified_file_mtime = File.stat(minified_file_name).mtime if minifying? && File.exists?(minified_file_name)
18
+ end
19
+
20
+ def unified_file_missing?
21
+ unified_file_mtime.nil?
22
+ end
23
+
24
+ def minified_file_missing?
25
+ minifying? && minified_file_mtime.nil?
26
+ end
27
+
28
+ def minifying?
29
+ !!options[:minify] && Minifier.enabled?(unified_file_name)
30
+ end
31
+
32
+ def rebuild?
33
+ if @rebuild_because.nil?
34
+ @rebuild_because = []
35
+ @rebuild_because << "UNIFIED FILE IS MISSING" if unified_file_missing?
36
+ @rebuild_because << "MINIFIED FILE IS MISSING" if minified_file_missing?
37
+ @rebuild_because << "MINIFIED FILE IS OUT OF DATE" if minifying? && !minified_file_missing? && !unified_file_missing? && (minified_file_mtime < unified_file_mtime)
38
+ unless unified_file_missing?
39
+ files.each do |name|
40
+ file_mtime = File.stat(name).mtime
41
+ if (file_mtime > unified_file_mtime)
42
+ @rebuild_because << "#{name} IS OUT OF DATE"
43
+ end
44
+ end
45
+ end
46
+ end
47
+ !@rebuild_because.empty?
48
+ end
49
+
50
+ def rebuild_reason
51
+ @rebuild_because.join(', ') if rebuild?
52
+ end
53
+
54
+ def build
55
+ if rebuild?
56
+ debug("**** REGENERATING #{unified_file_name} BECAUSE #{rebuild_reason} ****", options)
57
+ File.open(unified_file_name, 'w') do |uf|
58
+ files.each do |filename|
59
+ debug("**** RENDERING #{filename}")
60
+ uf << IO.read(filename)
61
+ end
62
+ end
63
+ Minifier.minify(unified_file_name) if minifying?
64
+ end
65
+ end
66
+
67
+ def debug(msg, options = {})
68
+ puts msg if options[:debug]
69
+ end
70
+
71
+ end # class Builder
72
+ end # module UnifiedAssets
@@ -0,0 +1,38 @@
1
+ module UnifiedAssets
2
+ module Minifier
3
+
4
+ class << self
5
+ attr_accessor :enabled
6
+ attr_accessor :extensions
7
+ attr_accessor :jarfile
8
+ end
9
+ self.enabled = true
10
+ self.extensions = ['.js', '.css']
11
+ self.jarfile = "#{File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "vendor", "yuicompressor"))}/yuicompressor-2.4.6.jar"
12
+
13
+ def self.available?
14
+ @available ||= !`which java`.empty? # warning: linux only way of checking if java is available
15
+ end
16
+
17
+ def self.enabled?(name = nil)
18
+ enabled && available? && (name.nil? || extensions.include?(File.extname(name)))
19
+ end
20
+
21
+ def self.minified_name(name)
22
+ if enabled?(name)
23
+ ext = File.extname(name)
24
+ name.sub(ext, ".min#{ext}")
25
+ else
26
+ name
27
+ end
28
+ end
29
+
30
+ def self.minify(name)
31
+ if name && enabled?(name) && File.exists?(name)
32
+ minified_name = minified_name(name)
33
+ `java -jar "#{jarfile}" "#{name}" -o "#{minified_name}"`
34
+ end
35
+ end
36
+
37
+ end # module Minifier
38
+ end # module UnifiedAssets
@@ -0,0 +1,47 @@
1
+ module UnifiedAssets
2
+ class Server
3
+
4
+ def initialize(app, options)
5
+ @app = app
6
+ @root = options[:root] or raise ArgumentError, ":root option is required"
7
+ @assets = options[:assets]
8
+ @minify = options[:minify]
9
+ @debug = options[:debug]
10
+ end
11
+
12
+ def call(env)
13
+
14
+ path_info = Rack::Utils.unescape(env["PATH_INFO"])
15
+ path = File.join(@root, path_info)
16
+
17
+ # redirect with trailing slash if necessary
18
+ if File.directory?(path) && path_info !~ /\/$/
19
+ new_path_info = path_info + "/"
20
+ return [
21
+ 302,
22
+ { 'Location' => new_path_info, 'Content-Type' => 'text/html' },
23
+ [ "Redirecting you to #{new_path_info}&hellip;" ]
24
+ ]
25
+ end
26
+
27
+ # add index.html if necessary
28
+ new_env = env.dup
29
+ if File.directory?(path)
30
+ path_to_index = File.join(path, "index.html")
31
+ if File.file?(path_to_index)
32
+ new_env["PATH_INFO"] = File.join(path_info, "index.html")
33
+ end
34
+ end
35
+
36
+ # regenerate assets if necessary
37
+ asset = path_info[1..-1] # trim leading slash
38
+ if @assets && @assets.keys.include?(asset)
39
+ UnifiedAssets.build(@assets[asset], asset, :minify => @minify, :debug => @debug)
40
+ end
41
+
42
+ @app.call(new_env)
43
+
44
+ end
45
+
46
+ end # class Server
47
+ end # module UnifiedAssets
@@ -0,0 +1,74 @@
1
+ require 'rake/tasklib'
2
+ require 'unified_assets'
3
+
4
+ module UnifiedAssets
5
+ class Tasks < Rake::TaskLib
6
+
7
+ attr_accessor :assets
8
+ attr_accessor :minify
9
+ attr_accessor :server
10
+
11
+ def initialize(options = {})
12
+ @assets = options[:assets]
13
+ @minify = options[:minify]
14
+ @server = options[:server] || {}
15
+ yield(self) if block_given?
16
+ define
17
+ end
18
+
19
+ def define
20
+ namespace :assets do
21
+ define_clear_task
22
+ define_create_task
23
+ define_server_task
24
+ end
25
+ end
26
+
27
+ def define_clear_task
28
+ desc "clear unified asset files"
29
+ task :clear do
30
+ assets.keys.each do |asset|
31
+ Dir[asset, Minifier.minified_name(asset)].each {|f| File.delete(f) if File.exists?(f) }
32
+ end
33
+ end
34
+ end
35
+
36
+ def define_create_task
37
+ desc "create unified asset files"
38
+ task :create do
39
+ Rake::Task["assets:clear"].invoke
40
+ assets.keys.each do |asset|
41
+ UnifiedAssets.build(assets[asset], asset, :minify => minify)
42
+ end
43
+ end
44
+ end
45
+
46
+ def define_server_task
47
+ desc "simple webserver that auto-regenerates assets if they are out of date"
48
+ task :server do |t, args|
49
+ require 'rack'
50
+ require 'unified_assets/server'
51
+ options = {
52
+ :root => ENV['ROOT'] || server[:root] || '.',
53
+ :port => ENV['PORT'] || server[:port] || 3000,
54
+ :debug => ENV['DEBUG'] || server[:debug] || true,
55
+ :assets => assets
56
+ }
57
+ app = Rack::Builder.new do
58
+ use Rack::CommonLogger
59
+ use Rack::ShowExceptions
60
+ use Rack::Lint
61
+ use UnifiedAssets::Server, options
62
+ run Rack::File.new(options[:root])
63
+ end.to_app
64
+ handler = Rack::Handler.get(ENV['HANDLER'] || server[:handler])
65
+ handler ||= begin; Rack::Handler::Thin; rescue LoadError; end
66
+ handler ||= begin; Rack::Handler::Mongrel; rescue LoadError; end
67
+ handler ||= Rack::Handler::WEBrick
68
+ handler.run(app, :Port => options[:port])
69
+ end
70
+ end
71
+
72
+ end # class Tasks
73
+ end # module UnifiedAsset
74
+
@@ -0,0 +1,151 @@
1
+ require "test_helper"
2
+
3
+ class BuilderTest < Test::Unit::TestCase
4
+
5
+ #----------------------------------------------------------------------------
6
+
7
+ def test_unified_javascript_file
8
+ UnifiedAssets.build(SOURCE_JS, UNIFIED_JS)
9
+ assert_equal(REF_UNIFIED_JS, IO.read(UNIFIED_JS))
10
+ end
11
+
12
+ def test_unified_css_file
13
+ UnifiedAssets.build(SOURCE_CSS, UNIFIED_CSS)
14
+ assert_equal(REF_UNIFIED_CSS, IO.read(UNIFIED_CSS))
15
+ end
16
+
17
+ #----------------------------------------------------------------------------
18
+
19
+ def test_minified_javascript_file
20
+ UnifiedAssets.build(SOURCE_JS, UNIFIED_JS, :minify => true)
21
+ assert_equal(REF_UNIFIED_JS, IO.read(UNIFIED_JS))
22
+ assert_equal(REF_MINIFIED_JS, IO.read(MINIFIED_JS))
23
+ end
24
+
25
+ def test_minified_css_file
26
+ UnifiedAssets.build(SOURCE_CSS, UNIFIED_CSS, :minify => true)
27
+ assert_equal(REF_UNIFIED_CSS, IO.read(UNIFIED_CSS))
28
+ assert_equal(REF_MINIFIED_CSS, IO.read(MINIFIED_CSS))
29
+ end
30
+
31
+ #----------------------------------------------------------------------------
32
+
33
+ def test_rebuild_required_because_unified_file_is_missing
34
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS)
35
+ assert_equal(SOURCE_JS, ua.files)
36
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
37
+ assert_equal(nil, ua.unified_file_mtime)
38
+ assert_equal(nil, ua.minified_file_name)
39
+ assert_equal(nil, ua.minified_file_mtime)
40
+ assert_equal(true, ua.unified_file_missing?)
41
+ assert_equal(false, ua.minified_file_missing?)
42
+ assert_equal(false, ua.minifying?)
43
+ assert_equal(true, ua.rebuild?)
44
+ assert_equal("UNIFIED FILE IS MISSING", ua.rebuild_reason)
45
+ end
46
+
47
+ def test_rebuild_required_because_unified_file_is_out_of_date
48
+ with_unified_js do
49
+ tickle SOURCE_JS[0]
50
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS)
51
+ assert_equal(SOURCE_JS, ua.files)
52
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
53
+ assert_equal(File.stat(UNIFIED_JS).mtime, ua.unified_file_mtime)
54
+ assert_equal(nil, ua.minified_file_name)
55
+ assert_equal(nil, ua.minified_file_mtime)
56
+ assert_equal(false, ua.unified_file_missing?)
57
+ assert_equal(false, ua.minified_file_missing?)
58
+ assert_equal(false, ua.minifying?)
59
+ assert_equal(true, ua.rebuild?)
60
+ assert_equal("#{SOURCE_JS[0]} IS OUT OF DATE", ua.rebuild_reason)
61
+ end
62
+ end
63
+
64
+ def test_rebuild_not_required
65
+ with_unified_js do
66
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS)
67
+ assert_equal(SOURCE_JS, ua.files)
68
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
69
+ assert_equal(File.stat(UNIFIED_JS).mtime, ua.unified_file_mtime)
70
+ assert_equal(nil, ua.minified_file_name)
71
+ assert_equal(nil, ua.minified_file_mtime)
72
+ assert_equal(false, ua.unified_file_missing?)
73
+ assert_equal(false, ua.minified_file_missing?)
74
+ assert_equal(false, ua.minifying?)
75
+ assert_equal(false, ua.rebuild?)
76
+ assert_equal(nil, ua.rebuild_reason)
77
+ end
78
+ end
79
+
80
+ #----------------------------------------------------------------------------
81
+
82
+ def test_rebuild_required_because_minified_file_is_missing
83
+ with_unified_js do
84
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS, :minify => true)
85
+ assert_equal(SOURCE_JS, ua.files)
86
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
87
+ assert_equal(File.stat(UNIFIED_JS).mtime, ua.unified_file_mtime)
88
+ assert_equal(MINIFIED_JS, ua.minified_file_name)
89
+ assert_equal(nil, ua.minified_file_mtime)
90
+ assert_equal(false, ua.unified_file_missing?)
91
+ assert_equal(true, ua.minified_file_missing?)
92
+ assert_equal(true, ua.minifying?)
93
+ assert_equal(true, ua.rebuild?)
94
+ assert_equal("MINIFIED FILE IS MISSING", ua.rebuild_reason)
95
+ end
96
+ end
97
+
98
+ def test_rebuild_required_because_minified_file_is_out_of_date
99
+ with_minified_js do
100
+ tickle UNIFIED_JS
101
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS, :minify => true)
102
+ assert_equal(SOURCE_JS, ua.files)
103
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
104
+ assert_equal(File.stat(UNIFIED_JS).mtime, ua.unified_file_mtime)
105
+ assert_equal(MINIFIED_JS, ua.minified_file_name)
106
+ assert_equal(File.stat(MINIFIED_JS).mtime, ua.minified_file_mtime)
107
+ assert_equal(false, ua.unified_file_missing?)
108
+ assert_equal(false, ua.minified_file_missing?)
109
+ assert_equal(true, ua.minifying?)
110
+ assert_equal(true, ua.rebuild?)
111
+ assert_equal("MINIFIED FILE IS OUT OF DATE", ua.rebuild_reason)
112
+ end
113
+ end
114
+
115
+ def test_rebuild_not_required_when_minifying
116
+ with_minified_js do
117
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS, :minify => true)
118
+ assert_equal(SOURCE_JS, ua.files)
119
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
120
+ assert_equal(File.stat(UNIFIED_JS).mtime, ua.unified_file_mtime)
121
+ assert_equal(MINIFIED_JS, ua.minified_file_name)
122
+ assert_equal(File.stat(MINIFIED_JS).mtime, ua.minified_file_mtime)
123
+ assert_equal(false, ua.unified_file_missing?)
124
+ assert_equal(false, ua.minified_file_missing?)
125
+ assert_equal(true, ua.minifying?)
126
+ assert_equal(false, ua.rebuild?)
127
+ assert_equal(nil, ua.rebuild_reason)
128
+ end
129
+ end
130
+
131
+ #----------------------------------------------------------------------------
132
+
133
+ def test_minify_is_ignored_if_minifier_is_not_enabled
134
+ UnifiedAssets::Minifier.enabled = false
135
+ ua = UnifiedAssets::Builder.new(SOURCE_JS, UNIFIED_JS, :minify => true)
136
+ assert_equal(SOURCE_JS, ua.files)
137
+ assert_equal(UNIFIED_JS, ua.unified_file_name)
138
+ assert_equal(nil, ua.unified_file_mtime)
139
+ assert_equal(nil, ua.minified_file_name)
140
+ assert_equal(nil, ua.minified_file_mtime)
141
+ assert_equal(true, ua.unified_file_missing?)
142
+ assert_equal(false, ua.minified_file_missing?)
143
+ assert_equal(false, ua.minifying?)
144
+ assert_equal(true, ua.rebuild?)
145
+ assert_equal("UNIFIED FILE IS MISSING", ua.rebuild_reason)
146
+ end
147
+
148
+ #----------------------------------------------------------------------------
149
+
150
+ end
151
+
@@ -0,0 +1 @@
1
+ .a { border: 1px solid aqua; }
@@ -0,0 +1 @@
1
+ .b { border: 1px solid blue; }
@@ -0,0 +1 @@
1
+ .c { border: 1px solid coral; }
@@ -0,0 +1 @@
1
+ A = "Antelope";
@@ -0,0 +1 @@
1
+ B = "Bear";
@@ -0,0 +1 @@
1
+ C = "Centipede";
@@ -0,0 +1,54 @@
1
+ require "test_helper"
2
+
3
+ class MinifierTest < Test::Unit::TestCase
4
+
5
+ def test_minifier_defaults
6
+ assert_equal(true, UnifiedAssets::Minifier.enabled)
7
+ assert_equal(['.js', '.css'], UnifiedAssets::Minifier.extensions)
8
+ assert_equal(true, UnifiedAssets::Minifier.available?)
9
+ end
10
+
11
+ def test_minifier_enabled
12
+ UnifiedAssets::Minifier.enabled = true
13
+ assert_equal(true, UnifiedAssets::Minifier.enabled?("foo.js"))
14
+ assert_equal(true, UnifiedAssets::Minifier.enabled?("foo.css"))
15
+ assert_equal(false, UnifiedAssets::Minifier.enabled?("foo.png"), ".png is not appopriate for minifying")
16
+ assert_equal("foo.min.js", UnifiedAssets::Minifier.minified_name("foo.js"))
17
+ assert_equal("foo.min.css", UnifiedAssets::Minifier.minified_name("foo.css"))
18
+ assert_equal("foo.png", UnifiedAssets::Minifier.minified_name("foo.png"), ".png is not appropriate for minifying")
19
+ end
20
+
21
+ def test_minifier_disabled
22
+ UnifiedAssets::Minifier.enabled = false
23
+ assert_equal(false, UnifiedAssets::Minifier.enabled?("foo.js"))
24
+ assert_equal(false, UnifiedAssets::Minifier.enabled?("foo.css"))
25
+ assert_equal(false, UnifiedAssets::Minifier.enabled?("foo.png"))
26
+ assert_equal("foo.js", UnifiedAssets::Minifier.minified_name("foo.js"))
27
+ assert_equal("foo.css", UnifiedAssets::Minifier.minified_name("foo.css"))
28
+ assert_equal("foo.png", UnifiedAssets::Minifier.minified_name("foo.png"))
29
+ end
30
+
31
+ def test_minify_javascript
32
+ with_unified_js do
33
+ assert_equal(true, File.exists?(UNIFIED_JS), "preconditions")
34
+ assert_equal(false, File.exists?(MINIFIED_JS), "preconditions")
35
+ UnifiedAssets::Minifier.minify(UNIFIED_JS)
36
+ assert_equal(true, File.exists?(UNIFIED_JS))
37
+ assert_equal(true, File.exists?(MINIFIED_JS))
38
+ assert_equal(REF_MINIFIED_JS, IO.read(MINIFIED_JS))
39
+ end
40
+ end
41
+
42
+ def test_minify_css
43
+ with_unified_css do
44
+ assert_equal(true, File.exists?(UNIFIED_CSS), "preconditions")
45
+ assert_equal(false, File.exists?(MINIFIED_CSS), "preconditions")
46
+ UnifiedAssets::Minifier.minify(UNIFIED_CSS)
47
+ assert_equal(true, File.exists?(UNIFIED_CSS))
48
+ assert_equal(true, File.exists?(MINIFIED_CSS))
49
+ assert_equal(REF_MINIFIED_CSS, IO.read(MINIFIED_CSS))
50
+ end
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,3 @@
1
+ .a { border: 1px solid aqua; }
2
+ .b { border: 1px solid blue; }
3
+ .c { border: 1px solid coral; }
@@ -0,0 +1,3 @@
1
+ A = "Antelope";
2
+ B = "Bear";
3
+ C = "Centipede";
@@ -0,0 +1 @@
1
+ .a{border:1px solid aqua}.b{border:1px solid blue}.c{border:1px solid coral}
@@ -0,0 +1 @@
1
+ A="Antelope";B="Bear";C="Centipede";
@@ -0,0 +1,72 @@
1
+ require "test/unit"
2
+ require 'fileutils'
3
+ require "unified_assets"
4
+
5
+ class Test::Unit::TestCase
6
+
7
+ #----------------------------------------------------------------------------
8
+
9
+ SOURCE_JS = ["test/js/a.js", "test/js/b.js", "test/js/c.js"]
10
+ SOURCE_CSS = ["test/css/a.css", "test/css/b.css", "test/css/c.css"]
11
+
12
+ UNIFIED_JS = "test/unified.js"
13
+ UNIFIED_CSS = "test/unified.css"
14
+ MINIFIED_JS = "test/unified.min.js"
15
+ MINIFIED_CSS = "test/unified.min.css"
16
+
17
+ REF_UNIFIED_JS = IO.read("test/reference/unified.js")
18
+ REF_UNIFIED_CSS = IO.read("test/reference/unified.css")
19
+ REF_MINIFIED_JS = IO.read("test/reference/unified.min.js")
20
+ REF_MINIFIED_CSS = IO.read("test/reference/unified.min.css")
21
+
22
+ #----------------------------------------------------------------------------
23
+
24
+ def setup
25
+ [UNIFIED_JS, UNIFIED_CSS, MINIFIED_JS, MINIFIED_CSS].each{|name| assert(!File.exists?(name), "preconditions") }
26
+ assert(UnifiedAssets::Minifier.enabled, "preconditions")
27
+ end
28
+
29
+ def teardown
30
+ [UNIFIED_JS, UNIFIED_CSS, MINIFIED_JS, MINIFIED_CSS].each{|name| File.delete(name) if File.exists?(name) }
31
+ UnifiedAssets::Minifier.enabled = true
32
+ end
33
+
34
+ #----------------------------------------------------------------------------
35
+
36
+ def with_ref(source, target)
37
+ FileUtils.cp(source, target)
38
+ yield
39
+ File.delete(target)
40
+ end
41
+
42
+ def with_unified_js(&block)
43
+ with_ref("test/reference/unified.js", UNIFIED_JS, &block)
44
+ end
45
+
46
+ def with_unified_css(&block)
47
+ with_ref("test/reference/unified.css", UNIFIED_CSS, &block)
48
+ end
49
+
50
+ def with_minified_js(&block)
51
+ with_ref("test/reference/unified.js", UNIFIED_JS) do
52
+ with_ref("test/reference/unified.min.js", MINIFIED_JS, &block)
53
+ end
54
+ end
55
+
56
+ def with_minified_css(&block)
57
+ with_ref("test/reference/unified.css", UNIFIED_CSS) do
58
+ with_ref("test/reference/unified.min.css", MINIFIED_CSS, &block)
59
+ end
60
+ end
61
+
62
+ #----------------------------------------------------------------------------
63
+
64
+ def tickle(file)
65
+ sleep(0.1)
66
+ `touch #{file}`
67
+ end
68
+
69
+ #----------------------------------------------------------------------------
70
+
71
+ end
72
+
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path("lib", File.dirname(__FILE__))
3
+ require 'unified_assets'
4
+
5
+ Gem::Specification.new do |s|
6
+
7
+ s.name = "unified-assets"
8
+ s.version = UnifiedAssets::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Jake Gordon"]
11
+ s.email = ["jake@codeincomplete.com"]
12
+ s.homepage = "https://github.com/jakesgordon/unified-assets"
13
+ s.summary = UnifiedAssets::SUMMARY
14
+ s.description = UnifiedAssets::DESCRIPTION
15
+
16
+ s.add_dependency('rack') # for rake assets:server
17
+
18
+ s.has_rdoc = false
19
+ s.extra_rdoc_files = ["README.md"]
20
+ s.rdoc_options = ["--charset=UTF-8"]
21
+ s.files = `git ls-files `.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+
26
+ end
27
+
28
+
@@ -0,0 +1,54 @@
1
+ YUI Compressor Copyright License Agreement (BSD License)
2
+
3
+ Copyright (c) 2010, Yahoo! Inc.
4
+ All rights reserved.
5
+
6
+ Redistribution and use of this software in source and binary forms,
7
+ with or without modification, are permitted provided that the following
8
+ conditions are met:
9
+
10
+ * Redistributions of source code must retain the above
11
+ copyright notice, this list of conditions and the
12
+ following disclaimer.
13
+
14
+ * Redistributions in binary form must reproduce the above
15
+ copyright notice, this list of conditions and the
16
+ following disclaimer in the documentation and/or other
17
+ materials provided with the distribution.
18
+
19
+ * Neither the name of Yahoo! Inc. nor the names of its
20
+ contributors may be used to endorse or promote products
21
+ derived from this software without specific prior
22
+ written permission of Yahoo! Inc.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ This software also requires access to software from the following sources:
36
+
37
+ The Jarg Library v 1.0 ( http://jargs.sourceforge.net/ ) is available
38
+ under a BSD License � Copyright (c) 2001-2003 Steve Purcell,
39
+ Copyright (c) 2002 Vidar Holen, Copyright (c) 2002 Michal Ceresna and
40
+ Copyright (c) 2005 Ewan Mellor.
41
+
42
+ The Rhino Library ( http://www.mozilla.org/rhino/ ) is dually available
43
+ under an MPL 1.1/GPL 2.0 license, with portions subject to a BSD license.
44
+
45
+ Additionally, this software contains modified versions of the following
46
+ component files from the Rhino Library:
47
+
48
+ [org/mozilla/javascript/Decompiler.java]
49
+ [org/mozilla/javascript/Parser.java]
50
+ [org/mozilla/javascript/Token.java]
51
+ [org/mozilla/javascript/TokenStream.java]
52
+
53
+ The modified versions of these files are distributed under the MPL v 1.1
54
+ ( http://www.mozilla.org/MPL/MPL-1.1.html )
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unified-assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jake Gordon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Simple tool to automatically unify and minify javascript and css website
31
+ assets
32
+ email:
33
+ - jake@codeincomplete.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files:
37
+ - README.md
38
+ files:
39
+ - .gitignore
40
+ - LICENSE
41
+ - README.md
42
+ - RELEASE_NOTES.md
43
+ - Rakefile
44
+ - lib/unified_assets.rb
45
+ - lib/unified_assets/builder.rb
46
+ - lib/unified_assets/minifier.rb
47
+ - lib/unified_assets/server.rb
48
+ - lib/unified_assets/tasks.rb
49
+ - test/builder_test.rb
50
+ - test/css/a.css
51
+ - test/css/b.css
52
+ - test/css/c.css
53
+ - test/js/a.js
54
+ - test/js/b.js
55
+ - test/js/c.js
56
+ - test/minifier_test.rb
57
+ - test/reference/unified.css
58
+ - test/reference/unified.js
59
+ - test/reference/unified.min.css
60
+ - test/reference/unified.min.js
61
+ - test/test_helper.rb
62
+ - unified_assets.gemspec
63
+ - vendor/yuicompressor/LICENSE.TXT
64
+ - vendor/yuicompressor/yuicompressor-2.4.6.jar
65
+ homepage: https://github.com/jakesgordon/unified-assets
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Unify and minify JS and CSS website assets
90
+ test_files: []