Skip to content

Commit b6ff4f0

Browse files
committed
Adding command-line and config_file option for overriding PUBLIC_ROOT.
1 parent 3dedc9d commit b6ff4f0

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

lib/jammit.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Jammit
1010

1111
ASSET_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ENV['RAILS_ROOT'] || ".") unless defined?(ASSET_ROOT)
1212

13-
PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
13+
DEFAULT_PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
1414

1515
DEFAULT_CONFIG_PATH = File.join(ASSET_ROOT, 'config', 'assets.yml')
1616

@@ -51,12 +51,14 @@ class << self
5151
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
5252
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
5353
:javascript_compressor, :compressor_options, :css_compressor_options,
54-
:template_extension, :template_extension_matcher, :allow_debugging
54+
:template_extension, :template_extension_matcher, :allow_debugging,
55+
:public_root
5556
attr_accessor :compressors
5657
end
5758

5859
# The minimal required configuration.
5960
@configuration = {}
61+
@public_root = DEFAULT_PUBLIC_ROOT
6062
@package_path = DEFAULT_PACKAGE_PATH
6163
@compressors = COMPRESSORS
6264

@@ -87,6 +89,7 @@ def self.load_configuration(config_path, soft=false)
8789
set_template_function(conf[:template_function])
8890
set_template_namespace(conf[:template_namespace])
8991
set_template_extension(conf[:template_extension])
92+
set_public_root(conf[:public_root]) if conf[:public_root]
9093
symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
9194
symbolize_keys(conf[:javascripts]) if conf[:javascripts]
9295
check_for_deprecations
@@ -124,16 +127,24 @@ def self.package!(options={})
124127
:config_path => Jammit::DEFAULT_CONFIG_PATH,
125128
:output_folder => nil,
126129
:base_url => nil,
130+
:public_root => nil,
127131
:force => false
128132
}.merge(options)
129133
load_configuration(options[:config_path])
134+
set_public_root(options[:public_root]) if options[:public_root]
130135
packager.force = options[:force]
131136
packager.package_names = options[:package_names]
132137
packager.precache_all(options[:output_folder], options[:base_url])
133138
end
134139

135140
private
136141

142+
# Allows command-line definition of `PUBLIC_ROOT`, for those using Jammit
143+
# outside of Rails.
144+
def self.set_public_root(public_root=nil)
145+
@public_root = public_root if public_root
146+
end
147+
137148
# Ensure that the JavaScript compressor is a valid choice.
138149
def self.set_javascript_compressor(value)
139150
value = value && value.to_sym

lib/jammit/command_line.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def parse_options
6666
opts.on('-p', '--packages LIST', 'list of packages to build (ex: "core,ui", default: all)') do |package_names|
6767
@options[:package_names] = package_names.split(/,\s*/).map {|n| n.to_sym }
6868
end
69+
opts.on('-P', '--public-root PATH', 'path to public assets (default: "public")') do |public_root|
70+
puts "Option for PUBLIC_ROOT"
71+
@options[:public_root] = public_root
72+
end
6973
opts.on_tail('-v', '--version', 'display Jammit version') do
7074
puts "Jammit version #{Jammit::VERSION}"
7175
exit

lib/jammit/compressor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ def construct_asset_path(asset_path, css_path, variant)
187187
# not be relative, given the path of the stylesheet that contains it.
188188
def absolute_path(asset_pathname, css_pathname)
189189
(asset_pathname.absolute? ?
190-
Pathname.new(File.join(PUBLIC_ROOT, asset_pathname)) :
190+
Pathname.new(File.join(Jammit.public_root, asset_pathname)) :
191191
css_pathname.dirname + asset_pathname).cleanpath
192192
end
193193

194194
# CSS assets that are referenced by relative paths, and are *not* being
195195
# embedded, must be rewritten relative to the newly-merged stylesheet path.
196196
def relative_path(absolute_path)
197-
File.join('../', absolute_path.sub(PUBLIC_ROOT, ''))
197+
File.join('../', absolute_path.sub(Jammit.public_root, ''))
198198
end
199199

200200
# Similar to the AssetTagHelper's method of the same name, this will

lib/jammit/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Controller < ActionController::Base
1111

1212
SUFFIX_STRIPPER = /-(datauri|mhtml)\Z/
1313

14-
NOT_FOUND_PATH = "#{PUBLIC_ROOT}/404.html"
14+
NOT_FOUND_PATH = "#{Jammit.public_root}/404.html"
1515

1616
# The "package" action receives all requests for asset packages that haven't
1717
# yet been cached. The package will be built, cached, and gzipped.

lib/jammit/packager.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ module Jammit
66
# with the correct timestamps.
77
class Packager
88

9-
# In Rails, the difference between a path and an asset URL is "public".
10-
PATH_DIFF = PUBLIC_ROOT.sub(ASSET_ROOT, '')
11-
PATH_TO_URL = /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(PATH_DIFF)})?/
129

1310
# Set force to false to allow packages to only be rebuilt when their source
1411
# files have changed since the last time their package was built.
@@ -18,6 +15,10 @@ class Packager
1815
# Jammit.configuration. When assets.yml is being changed on the fly,
1916
# create a new Packager.
2017
def initialize
18+
# In Rails, the difference between a path and an asset URL is "public".
19+
@path_diff = Jammit.public_root.sub(ASSET_ROOT, '')
20+
@path_to_url = /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(@path_diff)})?/
21+
2122
@compressor = Compressor.new
2223
@force = false
2324
@package_names = nil
@@ -37,7 +38,7 @@ def initialize
3738
# Unless forced, will only rebuild assets whose source files have been
3839
# changed since their last package build.
3940
def precache_all(output_dir=nil, base_url=nil)
40-
output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path)
41+
output_dir ||= File.join(Jammit.public_root, Jammit.package_path)
4142
cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) }
4243
cacheable(:css, output_dir).each do |p|
4344
cache(p, 'css', pack_stylesheets(p), output_dir)
@@ -153,10 +154,10 @@ def create_packages(config)
153154
paths = globs.flatten.uniq.map {|glob| glob_files(glob) }.flatten.uniq
154155
packages[name][:paths] = paths
155156
if !paths.grep(Jammit.template_extension_matcher).empty?
156-
packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(PATH_TO_URL, '') }
157+
packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(@path_to_url, '') }
157158
packages[name][:urls] += [Jammit.asset_url(name, Jammit.template_extension)]
158159
else
159-
packages[name][:urls] = paths.map {|path| path.sub(PATH_TO_URL, '') }
160+
packages[name][:urls] = paths.map {|path| path.sub(@path_to_url, '') }
160161
end
161162
end
162163
packages

0 commit comments

Comments
 (0)