Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding command-line and config_file option for overriding PUBLIC_ROOT.

  • Loading branch information...
commit b6ff4f0d63ddf615874dc9ad023924e94a8e2da1 1 parent 3dedc9d
@samuelclay samuelclay authored
View
15 lib/jammit.rb
@@ -10,7 +10,7 @@ module Jammit
ASSET_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ENV['RAILS_ROOT'] || ".") unless defined?(ASSET_ROOT)
- PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
+ DEFAULT_PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
DEFAULT_CONFIG_PATH = File.join(ASSET_ROOT, 'config', 'assets.yml')
@@ -51,12 +51,14 @@ class << self
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
:javascript_compressor, :compressor_options, :css_compressor_options,
- :template_extension, :template_extension_matcher, :allow_debugging
+ :template_extension, :template_extension_matcher, :allow_debugging,
+ :public_root
attr_accessor :compressors
end
# The minimal required configuration.
@configuration = {}
+ @public_root = DEFAULT_PUBLIC_ROOT
@package_path = DEFAULT_PACKAGE_PATH
@compressors = COMPRESSORS
@@ -87,6 +89,7 @@ def self.load_configuration(config_path, soft=false)
set_template_function(conf[:template_function])
set_template_namespace(conf[:template_namespace])
set_template_extension(conf[:template_extension])
+ set_public_root(conf[:public_root]) if conf[:public_root]
symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
symbolize_keys(conf[:javascripts]) if conf[:javascripts]
check_for_deprecations
@@ -124,9 +127,11 @@ def self.package!(options={})
:config_path => Jammit::DEFAULT_CONFIG_PATH,
:output_folder => nil,
:base_url => nil,
+ :public_root => nil,
:force => false
}.merge(options)
load_configuration(options[:config_path])
+ set_public_root(options[:public_root]) if options[:public_root]
packager.force = options[:force]
packager.package_names = options[:package_names]
packager.precache_all(options[:output_folder], options[:base_url])
@@ -134,6 +139,12 @@ def self.package!(options={})
private
+ # Allows command-line definition of `PUBLIC_ROOT`, for those using Jammit
+ # outside of Rails.
+ def self.set_public_root(public_root=nil)
+ @public_root = public_root if public_root
+ end
+
# Ensure that the JavaScript compressor is a valid choice.
def self.set_javascript_compressor(value)
value = value && value.to_sym
View
4 lib/jammit/command_line.rb
@@ -66,6 +66,10 @@ def parse_options
opts.on('-p', '--packages LIST', 'list of packages to build (ex: "core,ui", default: all)') do |package_names|
@options[:package_names] = package_names.split(/,\s*/).map {|n| n.to_sym }
end
+ opts.on('-P', '--public-root PATH', 'path to public assets (default: "public")') do |public_root|
+ puts "Option for PUBLIC_ROOT"
+ @options[:public_root] = public_root
+ end
opts.on_tail('-v', '--version', 'display Jammit version') do
puts "Jammit version #{Jammit::VERSION}"
exit
View
4 lib/jammit/compressor.rb
@@ -187,14 +187,14 @@ def construct_asset_path(asset_path, css_path, variant)
# not be relative, given the path of the stylesheet that contains it.
def absolute_path(asset_pathname, css_pathname)
(asset_pathname.absolute? ?
- Pathname.new(File.join(PUBLIC_ROOT, asset_pathname)) :
+ Pathname.new(File.join(Jammit.public_root, asset_pathname)) :
css_pathname.dirname + asset_pathname).cleanpath
end
# CSS assets that are referenced by relative paths, and are *not* being
# embedded, must be rewritten relative to the newly-merged stylesheet path.
def relative_path(absolute_path)
- File.join('../', absolute_path.sub(PUBLIC_ROOT, ''))
+ File.join('../', absolute_path.sub(Jammit.public_root, ''))
end
# Similar to the AssetTagHelper's method of the same name, this will
View
2  lib/jammit/controller.rb
@@ -11,7 +11,7 @@ class Controller < ActionController::Base
SUFFIX_STRIPPER = /-(datauri|mhtml)\Z/
- NOT_FOUND_PATH = "#{PUBLIC_ROOT}/404.html"
+ NOT_FOUND_PATH = "#{Jammit.public_root}/404.html"
# The "package" action receives all requests for asset packages that haven't
# yet been cached. The package will be built, cached, and gzipped.
View
13 lib/jammit/packager.rb
@@ -6,9 +6,6 @@ module Jammit
# with the correct timestamps.
class Packager
- # In Rails, the difference between a path and an asset URL is "public".
- PATH_DIFF = PUBLIC_ROOT.sub(ASSET_ROOT, '')
- PATH_TO_URL = /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(PATH_DIFF)})?/
# Set force to false to allow packages to only be rebuilt when their source
# files have changed since the last time their package was built.
@@ -18,6 +15,10 @@ class Packager
# Jammit.configuration. When assets.yml is being changed on the fly,
# create a new Packager.
def initialize
+ # In Rails, the difference between a path and an asset URL is "public".
+ @path_diff = Jammit.public_root.sub(ASSET_ROOT, '')
+ @path_to_url = /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(@path_diff)})?/
+
@compressor = Compressor.new
@force = false
@package_names = nil
@@ -37,7 +38,7 @@ def initialize
# Unless forced, will only rebuild assets whose source files have been
# changed since their last package build.
def precache_all(output_dir=nil, base_url=nil)
- output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path)
+ output_dir ||= File.join(Jammit.public_root, Jammit.package_path)
cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) }
cacheable(:css, output_dir).each do |p|
cache(p, 'css', pack_stylesheets(p), output_dir)
@@ -153,10 +154,10 @@ def create_packages(config)
paths = globs.flatten.uniq.map {|glob| glob_files(glob) }.flatten.uniq
packages[name][:paths] = paths
if !paths.grep(Jammit.template_extension_matcher).empty?
- packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(PATH_TO_URL, '') }
+ packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(@path_to_url, '') }
packages[name][:urls] += [Jammit.asset_url(name, Jammit.template_extension)]
else
- packages[name][:urls] = paths.map {|path| path.sub(PATH_TO_URL, '') }
+ packages[name][:urls] = paths.map {|path| path.sub(@path_to_url, '') }
end
end
packages

1 comment on commit b6ff4f0

@thisivan

Is there a release date for this change?

Please sign in to comment.
Something went wrong with that request. Please try again.