padrino-assets 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
1
+ *.gem
2
+ archive/*
3
+ doc/*
4
+ .yardoc
5
+ .bundle
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ lang: ruby
2
+ script: rspec
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby
7
+ - ruby-head
data/.yardopts ADDED
@@ -0,0 +1,10 @@
1
+ --no-save
2
+ --no-private
3
+ --title Padrino Assets
4
+ --markup-provider redcarpet
5
+ --markup markdown
6
+ lib/**/*.rb
7
+ -
8
+ README.md
9
+ CHANGELOG.md
10
+ LICENSE
data/CHANGELOG.md CHANGED
@@ -1,9 +1,12 @@
1
- # Version History
2
-
3
- ### 0.2.0 - (January 17, 2012)
4
- * The current `request` is now passed to **:assets_host** when a `Proc` is used
5
- * Added the rake task `assets:compress` for [deflating](http://en.wikipedia.org/wiki/Gzip) assets
6
- * Basic support for asset minification
7
-
8
- ### 0.1.0 - (January 12, 2012)
1
+ # Version History
2
+
3
+ ### 0.3.0 - IN DEVELOPMENT
4
+ * Now uses [Sprockets](https://github.com/sstephenson/sprockets) `context_class` in-order to make helpers available to all assets
5
+
6
+ ### 0.2.0 - (January 17, 2012)
7
+ * The current `request` is now passed to **:assets_host** when a `Proc` is used
8
+ * Added the rake task `assets:compress` for [deflating](http://en.wikipedia.org/wiki/Gzip) assets
9
+ * Basic support for asset minification
10
+
11
+ ### 0.1.0 - (January 12, 2012)
9
12
  * Initial release
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source :rubygems
2
2
  gemspec
data/LICENSE CHANGED
@@ -1,19 +1,19 @@
1
- Copyright (c) 2012 Benjamin Bloch
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
11
- all 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
1
+ Copyright (c) 2012 Benjamin Bloch
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
11
+ all 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
19
19
  THE SOFTWARE.
data/README.md CHANGED
@@ -1,64 +1,126 @@
1
- # Padrino Assets
2
-
3
- ### Overview
4
-
5
- Padrino assets is a plugin for the [Padrino](https://github.com/padrino/padrino-framework) web framework which makes use of the [Rack](https://github.com/rack/rack) plugin [Sprockets](https://github.com/sstephenson/sprockets) to manage and compile web assets.
6
-
7
- ### Setup & Installation
8
-
9
- Include it in your project's `Gemfile` with Bundler:
10
-
11
- ``` ruby
12
- gem 'padrino-assets'
13
- ```
14
-
15
- Modify your `app/app.rb` file to register the plugin:
16
-
17
- ``` ruby
18
- class ExampleApplication < Padrino::Application
19
- register Padrino::Assets
20
- end
21
- ```
22
-
23
- Modify your `config.ru` file to mount the environment:
24
-
25
- ``` ruby
26
- map '/assets' do
27
- run Padrino::Assets.environment
28
- end
29
- ```
30
-
31
- By default, Sprockets is configured to load assets from your project's `app/assets` and `lib/assets` directories. Any files stored in these directories are readily available to the included helpers and will be served by the Sprockets middleware.
32
-
33
- Because of this the following directories are no longer used and will instead be served statically:
34
-
35
- * `public/images`
36
- * `public/stylesheets`
37
- * `public/javascripts`
38
-
39
- You should now be storing your assets in the following directories:
40
-
41
- * `app/assets/images`
42
- * `app/assets/stylesheets`
43
- * `app/assets/javascripts`
44
-
45
- Should your project need to add additional paths you can do so by adding the following line:
46
-
47
- ``` ruby
48
- Padrino::Assets.load_paths << 'path/to/my/assets'
49
- ```
50
-
51
- ### Dependencies
52
-
53
- * [Padrino-Core](https://github.com/padrino/padrino-framework) and [Padrino-Helpers](https://github.com/padrino/padrino-framework)
54
- * [Ruby](http://www.ruby-lang.org/en) >= 1.9.2
55
- * [Sprockets](https://github.com/sstephenson/sprockets)
56
-
57
- ### TODO
58
-
59
- * Additional documentation
60
- * Tests
61
-
62
- ### Copyright
63
-
64
- Copyright © 2012 Benjamin Bloch (Cirex). See LICENSE for details.
1
+ # Padrino Assets
2
+
3
+ Overview
4
+ --------
5
+
6
+ Padrino assets is a plugin for the [Padrino](https://github.com/padrino/padrino-framework) web framework which uses [Sprockets](https://github.com/sstephenson/sprockets) to manage and compile web assets.
7
+
8
+ Setup & Installation
9
+ --------------------
10
+
11
+ Include it in your project's `Gemfile`
12
+
13
+ ``` ruby
14
+ gem 'padrino-assets'
15
+ ```
16
+
17
+ Modify your `app/app.rb` file to register the plugin:
18
+
19
+ ``` ruby
20
+ class ExampleApplication < Padrino::Application
21
+ register Padrino::Assets
22
+ end
23
+ ```
24
+ * __Note:__ Because `padrino-assets` overrides some existing helpers the above must be placed below `Padrino::Helpers`
25
+
26
+ Modify your `config.ru` file to mount the environment:
27
+
28
+ ``` ruby
29
+ map '/assets' do
30
+ run Padrino::Assets.environment
31
+ end
32
+ ```
33
+
34
+ By default, Sprockets is configured to load assets from your project's `app/assets` and `lib/assets` directories. Any files stored in these directories are readily available to the included helpers, and will be served by the Sprockets middleware.
35
+
36
+ Because of this the following directories are no longer used, and will be served statically:
37
+
38
+ * public/images
39
+ * public/stylesheets
40
+ * public/javascripts
41
+
42
+ You should now be storing your assets in the following directories:
43
+
44
+ * app/assets/images
45
+ * app/assets/stylesheets
46
+ * app/assets/javascripts
47
+
48
+ Should your project need to add additional paths you can easily do so by adding the following line:
49
+
50
+ ``` ruby
51
+ Padrino::Assets.load_paths << 'path/to/my/assets'
52
+ ```
53
+
54
+ Configuration
55
+ -------------
56
+
57
+ Although the defaults are enough to get you started. There are several configuration options available to further customize this plugin to your project's personal needs. The following options can be changed at anytime with `set`, `enable`, or `disable`.
58
+
59
+ #### :assets_host
60
+ Is the URL to the external server currently hosting your assets. Generally, you only set this option if you want to host your assets on a separate server or service such as Amazon S3.
61
+
62
+ When a `Proc` is used, the current asset, and request will be passed to it as arguments:
63
+
64
+ ``` ruby
65
+ set :assets_hosts, ->(asset, request) do
66
+ if request.ssl?
67
+ 'https://secure.assets.com'
68
+ else
69
+ 'http://assets.com'
70
+ end
71
+ end
72
+ ```
73
+
74
+ Of course if you don't need anything flashy a string will suffice:
75
+
76
+ ``` ruby
77
+ set :assets_host, 'http://assets.com'
78
+ ```
79
+
80
+ #### :compress_assets
81
+ If enabled, will automatically compress, and gzip your assets to save server bandwidth.
82
+ Due to the Java dependencies, and the lack of a suitable pure Ruby alternative, a default compressor is not __currently__ provided.
83
+
84
+ #### :precompile_assets
85
+ Is an array of assets that will be compiled when the task `assets:precompile` is run.
86
+ Once compiled, an MD5 checksum is calculated and inserted into the file name.
87
+ This checksum serves as a way to version your assets so that they can be properly cached by web browsers.
88
+
89
+ To filter which assets are compiled you have a few options. As an example, using a regular expression:
90
+
91
+ ``` ruby
92
+ set :precompile_assets, [/^\w\.(?!(?:css|js)$)/i]
93
+ ```
94
+
95
+ The above would compile all assets that do not have the extension `.css` or `.js`.
96
+
97
+ Or if you prefer, a collection of files would work too:
98
+
99
+ ``` ruby
100
+ set :precompile_assets, [
101
+ 'jquery.js',
102
+ 'jquery.unobtrusive.js',
103
+ 'application.css',
104
+ 'pony.jpg'
105
+ ]
106
+ ```
107
+
108
+ If you are deploying to a production environment it is recommended that you precompile your assets so that they can be statically served by your web server. In a development environment, your assets will be compiled with every request.
109
+
110
+ Dependencies
111
+ ------------
112
+
113
+ * [Padrino-Core](https://github.com/padrino/padrino-framework)
114
+ * [Padrino-Helpers](https://github.com/padrino/padrino-framework)
115
+ * [Ruby](http://www.ruby-lang.org/en) >= 1.9.2
116
+ * [Sprockets](https://github.com/sstephenson/sprockets)
117
+
118
+ TODO
119
+ ----
120
+
121
+ * Additional documentation
122
+
123
+ Copyright
124
+ ---------
125
+
126
+ Copyright &copy; 2012 Benjamin Bloch (Cirex). See LICENSE for details.
data/Rakefile CHANGED
@@ -1,7 +1,30 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
-
4
- Rake::TestTask.new do |test|
5
- test.test_files = FileList['test/**/test_*.rb']
6
- test.verbose = true
7
- end
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'padrino-assets/version'
3
+
4
+ require 'rake'
5
+ require 'yard'
6
+ require 'rspec'
7
+ require 'rspec/core/rake_task'
8
+
9
+ RSpec::Core::RakeTask.new do |task|
10
+ task.pattern = 'spec/**/*_spec.rb'
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new
14
+
15
+ task :build do
16
+ `gem build padrino-assets.gemspec`
17
+ end
18
+
19
+ task :install => :build do
20
+ `gem install padrino-assets-#{Padrino::Assets::VERSION}.gem`
21
+ end
22
+
23
+ desc 'Releases the current version into the wild'
24
+ task :release => :build do
25
+ `git tag -a v#{Padrino::Assets::VERSION} -m "Version #{Padrino::Assets::VERSION}"`
26
+ `gem push padrino-assets-#{Padrino::Assets::VERSION}.gem`
27
+ `git push --tags`
28
+ end
29
+
30
+ task :default => :spec
@@ -1,91 +1,154 @@
1
- require 'padrino-core'
2
- require 'padrino-helpers'
3
-
4
- FileSet.glob_require('padrino-assets/**/*.rb', __FILE__)
5
-
6
- module Padrino
7
- module Assets
8
- class << self
9
- ##
10
- # Returns a list of paths Sprockets will use in order to find assets used by the project
11
- #
12
- # @return [Array]
13
- # List of assets paths
14
- #
15
- # @example
16
- # Padrino::Assets.load_paths << Padrino.root('vendor', '**', 'assets')
17
- #
18
- # @since 0.1.0
19
- # @api public
20
- def load_paths
21
- @_load_paths ||= ['app/assets/**', 'lib/assets/**'].map do |file|
22
- Dir[Padrino.root(file)]
23
- end.flatten
24
- end
25
-
26
- ##
27
- # Returns the configured Sprockets environment
28
- #
29
- # @return [Sprockets::Environment]
30
- # Sprockets environment
31
- #
32
- # @since 0.1.0
33
- # @api public
34
- def environment
35
- @_environment
36
- end
37
-
38
- ##
39
- # Returns a compiled manifest of our assets
40
- #
41
- # @return [Sprockets::Manifest]
42
- # Sprockets manifest
43
- #
44
- # @since 0.1.0
45
- # @api public
46
- def manifest
47
- @_manifest
48
- end
49
-
50
- # @private
51
- def registered(app)
52
- app.helpers Helpers
53
- app.set :assets_prefix, '/assets'
54
- app.set :assets_version, 1.0
55
- app.set :assets_host, nil
56
- app.set :compress_assets, true
57
- app.set :js_compressor, nil
58
- app.set :css_compressor, nil
59
- app.set :index_assets, -> { app.environment == :production }
60
- app.set :manifest_file, -> { File.join(app.public_folder, app.assets_prefix, 'manifest.json') }
61
- app.set :precompile_assets, [ /^.+\.(?!js|css).+$/i, /^application\.(js|css)$/i ]
62
-
63
- require 'sprockets'
64
-
65
- Padrino.after_load do
66
- @_environment = Sprockets::Environment.new(app.root) do |environment|
67
- environment.logger = app.logger
68
- environment.version = app.assets_version
69
- if defined?(Padrino::Cache)
70
- if app.respond_to?(:caching) && app.caching?
71
- environment.cache = app.cache
72
- end
73
- end
74
-
75
- if app.compress_assets?
76
- environment.js_compressor = app.js_compressor
77
- environment.css_compressor = app.css_compressor
78
- end
79
- end
80
-
81
- load_paths.each { |path| environment.append_path(path) }
82
-
83
- @_environment = environment.index if app.index_assets?
84
- @_manifest = Sprockets::Manifest.new(environment, app.manifest_file)
85
- end
86
-
87
- Padrino::Tasks.files << Dir[File.dirname(__FILE__) + '/tasks/**/*.{rake,rb}']
88
- end
89
- end
90
- end
91
- end
1
+ # encoding: utf-8
2
+ require 'padrino-core'
3
+ require 'padrino-helpers'
4
+
5
+ FileSet.glob_require('padrino-assets/**/*.rb', __FILE__)
6
+
7
+ module Padrino
8
+ module Assets
9
+ class << self
10
+ ###
11
+ # Returns a list of paths Sprockets will use in order to find assets used by the project
12
+ #
13
+ # @return [Array]
14
+ # List of assets paths
15
+ #
16
+ # @example
17
+ # Padrino::Assets.load_paths << Dir[Padrino.root('vendor', '**', 'assets')]
18
+ #
19
+ # @since 0.1.0
20
+ # @api public
21
+ def load_paths
22
+ @_load_paths ||= ['app/assets/**', 'lib/assets/**'].map do |directory|
23
+ Dir[Padrino.root(directory)]
24
+ end.flatten
25
+ end
26
+
27
+ ###
28
+ # Returns the configured Sprockets environment
29
+ #
30
+ # @return [Sprockets::Environment]
31
+ # Sprockets environment
32
+ #
33
+ # @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb
34
+ #
35
+ # @since 0.1.0
36
+ # @api public
37
+ def environment
38
+ @_environment
39
+ end
40
+
41
+ ###
42
+ # Returns a compiled manifest of our assets
43
+ #
44
+ # @return [Sprockets::Manifest]
45
+ # Sprockets manifest
46
+ #
47
+ # @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/manifest.rb
48
+ #
49
+ # @since 0.1.0
50
+ # @api public
51
+ def manifest
52
+ @_manifest
53
+ end
54
+
55
+ ###
56
+ # Returns a list of available asset compressors
57
+ #
58
+ # @return [Hash]
59
+ # List of available asset compressors
60
+ #
61
+ # @since 0.3.0
62
+ # @api public
63
+ def compressors
64
+ @_compressors ||= Hash.new { |k, v| k[v] = Hash.new }
65
+ end
66
+
67
+ ###
68
+ # Registers an asset compressor for use with Sprockets
69
+ #
70
+ # @param [Symbol] type
71
+ # The type of compressor you are registering (:js, :css)
72
+ #
73
+ # @example
74
+ # Padrino::Assets.register_compressor :js, :simple => 'SimpleCompressor'
75
+ # Padrino::Assets.register_compressor :css, :simple => 'SimpleCompressor'
76
+ #
77
+ # @since 0.3.0
78
+ # @api public
79
+ def register_compressor(type, compressor)
80
+ compressors[type].merge!(compressor)
81
+ end
82
+
83
+ # @since 0.3.0
84
+ # @api private
85
+ def find_registered_compressor(type, compressor)
86
+ return compressor unless compressor.is_a?(Symbol)
87
+
88
+ if compressor = compressors[type][compressor]
89
+ compressor = compressor.safe_constantize
90
+ end
91
+
92
+ compressor.respond_to?(:new) ? compressor.new : compressor
93
+ end
94
+
95
+ # @private
96
+ def registered(app)
97
+ app.helpers Helpers
98
+ app.set :assets_prefix, '/assets'
99
+ app.set :assets_version, 1.0
100
+ app.set :assets_host, nil
101
+ app.set :compress_assets, true
102
+ app.set :js_compressor, nil
103
+ app.set :css_compressor, nil
104
+ app.set :index_assets, -> { app.environment == :production }
105
+ app.set :manifest_file, -> { File.join(app.public_folder, app.assets_prefix, 'manifest.json') }
106
+ app.set :precompile_assets, [ /^\w+\.(?!(?:js|css)$)/i, /^application\.(js|css)$/i ]
107
+
108
+ # FIXME: Temporary fix for `padrino start`
109
+ app.get '/assets/*' do
110
+ env['PATH_INFO'].gsub!('/assets', '')
111
+ Padrino::Assets.environment.call(env)
112
+ end
113
+
114
+ Padrino.after_load do
115
+ require 'sprockets'
116
+
117
+ environment = Sprockets::Environment.new(Padrino.root)
118
+
119
+ environment.logger = app.logger
120
+ environment.version = app.assets_version
121
+
122
+ if defined?(Padrino::Cache)
123
+ if app.respond_to?(:caching) && app.caching?
124
+ environment.cache = app.cache
125
+ end
126
+ end
127
+
128
+ if app.compress_assets?
129
+ environment.js_compressor = find_registered_compressor(:js, app.js_compressor)
130
+ environment.css_compressor = find_registered_compressor(:css, app.css_compressor)
131
+ end
132
+
133
+ load_paths.flatten.each do |path|
134
+ environment.append_path(path)
135
+ end
136
+
137
+ environment.context_class.class_eval do
138
+ include Helpers
139
+ end
140
+
141
+ @_environment = app.index_assets ? environment.index : environment
142
+ @_manifest = Sprockets::Manifest.new(@_environment, app.manifest_file)
143
+ end
144
+
145
+ Padrino::Tasks.files << Dir[File.dirname(__FILE__) + '/tasks/**/*.rake']
146
+ end
147
+ end
148
+
149
+ register_compressor :css, :yui => 'YUI::CssCompressor'
150
+ register_compressor :js, :yui => 'YUI::JavaScriptCompressor'
151
+ register_compressor :js, :closure => 'Closure::Compiler'
152
+ register_compressor :js, :uglifier => 'Uglifier'
153
+ end # Assets
154
+ end # Padrino