css_asset_tagger 1.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,20 @@
1
+ Copyright (c) 2009 Redline Software Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # CssAssetTagger
2
+
3
+ Adds asset query strings to assets found in css files. This gem originated as a rails plugin well before the asset pipeline was part of rails. This plugin is still useful in Rails 3.1+ apps though if the asset pipeline is not enabled.
4
+
5
+ ## Install
6
+
7
+ In your Gemfile:
8
+ `gem 'css_asset_tagger', :git => 'git://github.com/redlinesoftware/css_asset_tagger.git'`
9
+
10
+ ## Example
11
+
12
+ Simply install the plugin and css files will be updated to include asset tags for any found assets.
13
+
14
+ before:
15
+ `background: url("/images/background.png") repeat-x scroll 0 0 #ffffff;`
16
+
17
+ after:
18
+ `background: url(/images/background.png?1296473764) repeat-x scroll 0 0 #ffffff;`
19
+
20
+ To modify the plugins behaviour, add an initializer file to config/initializers
21
+
22
+ * __perform_tagging__ - Flag to determine if the css files should be modified with asset tags.
23
+ Defaults to tag in production only.
24
+ * __css_paths__ - An array of css stylesheet paths. Defaults to the main stylesheets path.
25
+ Defaults to ["#{Rails.root}/public/stylesheets"]
26
+ * __asset_path__ - The main asset path to be used when looking for assets with absolute file references.
27
+ Defaults to Rails.root.join('public')
28
+ * __show_warnings__ - Set to true to see warnings for assets that can't be found on the filesystem or false to not show the warnings.
29
+ Defaults to showing warnings in dev mode only.
30
+
31
+ ex. config/initializers/css_tagger_options.rb
32
+
33
+ require 'css_asset_tagger_options'
34
+
35
+ CssAssetTaggerOptions.setup do |config|
36
+ config.perform_tagging = true
37
+ end
38
+
39
+ Copyright (c) 2009-2013 Redline Software Inc., released under the MIT license
@@ -0,0 +1,41 @@
1
+ class CssAssetTagger
2
+ def self.tag(paths, logger = nil)
3
+ logger ||= Logger.new($stderr)
4
+
5
+ paths.each do |path|
6
+ files = Dir.glob(File.join(path, '**/*.css'))
7
+ for file in files
8
+ css = File.read file
9
+ res = css.gsub!(/url\((?:"([^"]*)"|'([^']*)'|([^)]*))\)/mi) do |s|
10
+ # $1 is the double quoted string, $2 is single quoted, $3 is no quotes
11
+ uri = ($1 || $2 || $3).to_s.strip
12
+
13
+ # if the uri appears to begin with a protocol then the asset isn't on the local filesystem
14
+ # or if query string appears to exist already, the uri is returned as is
15
+ if uri =~ /[a-z]+:\/\//i || uri =~ /(\?|&)\d{10}/
16
+ "url(#{uri})"
17
+ else
18
+ # if the first char is a / then get the path of the file with respect to the absolute path of the asset files
19
+ # otherwise get the path relative to the current file
20
+ path = (uri.chars.first == '/' ? "#{CssAssetTaggerOptions.asset_path}#{uri}" : "#{File.dirname(file)}/#{uri}")
21
+
22
+ begin
23
+ # construct the uri with the associated asset query string
24
+ sep = (uri =~ /\?/).nil? ? '?' : '&'
25
+ "url(#{uri}#{sep}#{File.stat(path).mtime.to_i})"
26
+ rescue Errno::ENOENT
27
+ # the asset can't be found, so return the uri as is
28
+ logger.warn "CssAssetTagger: #{path} referenced from #{file} cannot be found." if CssAssetTaggerOptions.show_warnings
29
+ "url(#{uri})"
30
+ end
31
+ end
32
+ end
33
+
34
+ # write the new contents of the file out if asset tags were added
35
+ File.open(file, 'w'){|f| f.puts css} unless res.nil?
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ require 'css_asset_tagger/railtie' if defined?(Rails)
@@ -0,0 +1,13 @@
1
+ require 'rails/railtie'
2
+
3
+ module CssAssetTaggerOptions
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ Dir[File.expand_path('../../tasks/*.rake', __FILE__)].each { |f| load f }
7
+ end
8
+
9
+ config.to_prepare do
10
+ CssAssetTagger.tag(CssAssetTaggerOptions.css_paths) if CssAssetTaggerOptions.perform_tagging
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module CssAssetTaggerOptions
2
+ VERSION = '1.1'.freeze
3
+ end
@@ -0,0 +1,14 @@
1
+ module CssAssetTaggerOptions
2
+ mattr_accessor :perform_tagging, :css_paths, :asset_path, :show_warnings
3
+
4
+ if defined?(Rails)
5
+ @@perform_tagging = Rails.env.production?
6
+ @@css_paths = %W(#{Rails.root}/public/stylesheets)
7
+ @@asset_path = Rails.root.join('public')
8
+ @@show_warnings = Rails.env.development?
9
+ end
10
+
11
+ def self.setup
12
+ yield self
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require 'css_asset_tagger'
2
+ require 'css_asset_tagger_options'
3
+
4
+ namespace :css_asset_tagger do
5
+ desc "Add asset timestamps to assets found in the project stylesheets"
6
+ task :tag do
7
+ CssAssetTagger.tag CssAssetTaggerOptions.css_paths
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+
3
+ require 'test/unit'
4
+ require 'active_support/all'
5
+
6
+ require 'css_asset_tagger'
7
+ require 'css_asset_tagger_options'
8
+
9
+ class CssAssetTaggerTest < Test::Unit::TestCase
10
+ ASSET_PATH = File.dirname(__FILE__)+'/assets'
11
+ TEST_STYLESHEET = ASSET_PATH+'/stylesheets/test.css'
12
+
13
+ def setup
14
+ CssAssetTaggerOptions.perform_tagging = true
15
+ CssAssetTaggerOptions.asset_path = [ASSET_PATH]
16
+ FileUtils.cp TEST_STYLESHEET+'.template', TEST_STYLESHEET
17
+ end
18
+
19
+ def teardown
20
+ FileUtils.rm TEST_STYLESHEET
21
+ end
22
+
23
+ def test_this_plugin
24
+ # TODO capture the warnings and test for them
25
+ # how is this done?
26
+ l = Logger.new STDOUT
27
+ l.level = Logger::ERROR
28
+
29
+ CssAssetTagger.tag [ASSET_PATH+'/stylesheets'], l
30
+
31
+ # set the timestamps for the relative and absolute images
32
+ abs_time = File.stat(ASSET_PATH+'/images/test.png').mtime.to_i
33
+ rel_time = File.stat(ASSET_PATH+'/stylesheets/images/test.png').mtime.to_i
34
+
35
+ assert_equal ERB.new(File.read(TEST_STYLESHEET+'.expected')).result(self.send(:binding)).to_s, File.read(TEST_STYLESHEET).to_s
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_asset_tagger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ version: "1.1"
10
+ platform: ruby
11
+ authors:
12
+ - Andrew Kaspick
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-02-06 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rails
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 3
30
+ - 0
31
+ version: "3.0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Tags assets in Rails app css files with asset query strings.
35
+ email: contact@plataformatec.com.br
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - MIT-LICENSE
44
+ - README.md
45
+ - lib/tasks/css_asset_tagger_tasks.rake
46
+ - lib/css_asset_tagger_options.rb
47
+ - lib/css_asset_tagger.rb
48
+ - lib/css_asset_tagger/railtie.rb
49
+ - lib/css_asset_tagger/version.rb
50
+ - test/css_asset_tagger_test.rb
51
+ homepage: https://github.com/redlinesoftware/css_asset_tagger
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.25
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Tags assets in Rails app css files with asset query strings.
84
+ test_files:
85
+ - test/css_asset_tagger_test.rb