Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 166 lines (131 sloc) 6.057 kb
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
1 $LOAD_PATH.push File.expand_path(File.dirname(__FILE__))
a55e9d6 Jeremy Ashkenas bits and snatches of tests
jashkenas authored
2
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
3 # @Jammit@ is the central namespace for all Jammit classes, and provides access
4 # to all of the configuration options.
8f30dbd Jeremy Ashkenas initial jammit commit
jashkenas authored
5 module Jammit
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
6
8629653 Jeremy Ashkenas Jammit 0.4.1, with combined JS/JST reverted
jashkenas authored
7 VERSION = "0.4.1"
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
8
8e264a3 Jeremy Ashkenas first draft of relative URL rewriting
jashkenas authored
9 ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
10
c1575b3 Jeremy Ashkenas removing all references to RAILS_ROOT in favor of Rails.root
jashkenas authored
11 ASSET_ROOT = File.expand_path(defined?(Rails) ? Rails.root : ".") unless defined?(ASSET_ROOT)
8e264a3 Jeremy Ashkenas first draft of relative URL rewriting
jashkenas authored
12
13 PUBLIC_ROOT = "#{ASSET_ROOT}/public"
c9dd3dd Jeremy Ashkenas Jammit 0.2.4
jashkenas authored
14
15 DEFAULT_CONFIG_PATH = "#{ASSET_ROOT}/config/assets.yml"
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
16
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
17 DEFAULT_PACKAGE_PATH = "assets"
14e66b9 Jeremy Ashkenas wrote a first draft of documentation into index -- got the configuration...
jashkenas authored
18
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
19 DEFAULT_JST_SCRIPT = "#{ROOT}/lib/jammit/jst.js"
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
20
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
21 DEFAULT_JST_COMPILER = "template"
97953ad Jeremy Ashkenas merging in agibralter's patch that adds template_namespace as an option,...
jashkenas authored
22
6ab2f65 Aaron Gibralter Added the ability to specify an alternative for window.JST in assets.yml...
agibralter authored
23 DEFAULT_JST_NAMESPACE = "window.JST"
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
24
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
25 AVAILABLE_COMPRESSORS = [:yui, :closure]
26
27 DEFAULT_COMPRESSOR = :yui
4a82cbb Jeremy Ashkenas initial support for Google's Closure Compiler as an option
jashkenas authored
28
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
29 # Jammit raises a @PackageNotFound@ exception when a non-existent package is
30 # requested by a browser -- rendering a 404.
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
31 class PackageNotFound < NameError; end
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
32
c9dd3dd Jeremy Ashkenas Jammit 0.2.4
jashkenas authored
33 # Jammit raises a ConfigurationNotFound exception when you try to load the
34 # configuration of an assets.yml file that doesn't exist.
35 class ConfigurationNotFound < NameError; end
36
6d6d154 Jeremy Ashkenas raising an OutputNotWritable exception when you try to cache packages to...
jashkenas authored
37 # Jammit raises an OutputNotWritable exception if the output directory for
38 # cached packages is locked.
39 class OutputNotWritable < StandardError; end
40
11f0d5e Jeremy Ashkenas asset versioning, and precompliation through the 'jammit' command
jashkenas authored
41 class << self
1dda206 Jeremy Ashkenas adding a gzip_assets option to assets.yml
jashkenas authored
42 attr_reader :configuration, :template_function, :template_namespace,
43 :embed_assets, :package_assets, :compress_assets, :gzip_assets,
44 :package_path, :mhtml_enabled, :include_jst_script,
af21b30 Jeremy Ashkenas Jammit 0.3.3, allowing linebreaks
jashkenas authored
45 :javascript_compressor, :compressor_options, :css_compressor_options
8f30dbd Jeremy Ashkenas initial jammit commit
jashkenas authored
46 end
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
47
14e66b9 Jeremy Ashkenas wrote a first draft of documentation into index -- got the configuration...
jashkenas authored
48 # The minimal required configuration.
49 @configuration = {}
50 @package_path = DEFAULT_PACKAGE_PATH
51
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
52 # Load the complete asset configuration from the specified @config_path@.
11f0d5e Jeremy Ashkenas asset versioning, and precompliation through the 'jammit' command
jashkenas authored
53 def self.load_configuration(config_path)
0543e41 Jeremy Ashkenas passing assets.yml through ERB before loading it, for environment-specif...
jashkenas authored
54 exists = config_path && File.exists?(config_path)
55 raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
56 conf = YAML.load(ERB.new(File.read(config_path)).result)
4a82cbb Jeremy Ashkenas initial support for Google's Closure Compiler as an option
jashkenas authored
57 @config_path = config_path
c9dd3dd Jeremy Ashkenas Jammit 0.2.4
jashkenas authored
58 @configuration = conf = conf.symbolize_keys
4a82cbb Jeremy Ashkenas initial support for Google's Closure Compiler as an option
jashkenas authored
59 @package_path = conf[:package_path] || DEFAULT_PACKAGE_PATH
c65ed0d Jeremy Ashkenas renaming embed_images to embed_assets in anticipation of font embedding
jashkenas authored
60 @embed_assets = conf[:embed_assets] || conf[:embed_images]
e525356 Jeremy Ashkenas fixing broken configuration test -- making 'compress_assets' a first-cla...
jashkenas authored
61 @compress_assets = !(conf[:compress_assets] == false)
1dda206 Jeremy Ashkenas adding a gzip_assets option to assets.yml
jashkenas authored
62 @gzip_assets = !(conf[:gzip_assets] == false)
c65ed0d Jeremy Ashkenas renaming embed_images to embed_assets in anticipation of font embedding
jashkenas authored
63 @mhtml_enabled = @embed_assets && @embed_assets != "datauri"
a8fb7d3 Jeremy Ashkenas adding a JavaNotFound exception, and raising it if Java isn't installed,...
jashkenas authored
64 @compressor_options = (conf[:compressor_options] || {}).symbolize_keys
af21b30 Jeremy Ashkenas Jammit 0.3.3, allowing linebreaks
jashkenas authored
65 @css_compressor_options = (conf[:css_compressor_options] || {}).symbolize_keys
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
66 set_javascript_compressor(conf[:javascript_compressor])
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
67 set_package_assets(conf[:package_assets])
68 set_template_function(conf[:template_function])
6ab2f65 Aaron Gibralter Added the ability to specify an alternative for window.JST in assets.yml...
agibralter authored
69 set_template_namespace(conf[:template_namespace])
a8fb7d3 Jeremy Ashkenas adding a JavaNotFound exception, and raising it if Java isn't installed,...
jashkenas authored
70 check_java_version
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
71 self
8f30dbd Jeremy Ashkenas initial jammit commit
jashkenas authored
72 end
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
73
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
74 # Force a reload by resetting the Packager and reloading the configuration.
29c624f Jeremy Ashkenas 0.1.3 is going to have a fix for this before_filter issue, one way or an...
jashkenas authored
75 # In development, this will be called as a before_filter before every request.
b577b64 Jeremy Ashkenas fixed image compression for data-uri-supporting browsers plus reloading ...
jashkenas authored
76 def self.reload!
77 Thread.current[:jammit_packager] = nil
78 load_configuration(@config_path)
79 end
80
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
81 # Keep a global (thread-local) reference to a @Jammit::Packager@, to avoid
82 # recomputing asset lists unnecessarily.
11f0d5e Jeremy Ashkenas asset versioning, and precompliation through the 'jammit' command
jashkenas authored
83 def self.packager
84 Thread.current[:jammit_packager] ||= Packager.new
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
85 end
86
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
87 # Generate the base filename for a version of a given package.
bc7073a Jeremy Ashkenas getting close to a first release -- commented and lightly refactored, mi...
jashkenas authored
88 def self.filename(package, extension, suffix=nil)
89 suffix_part = suffix ? "-#{suffix}" : ''
90 "#{package}#{suffix_part}.#{extension}"
8f30dbd Jeremy Ashkenas initial jammit commit
jashkenas authored
91 end
440d347 Jeremy Ashkenas lots of jammit goodies -- versioning for far-futures expires headers, sp...
jashkenas authored
92
17dfaa8 Jeremy Ashkenas serving datauri-embedded-images to IE8, never embedding images larger th...
jashkenas authored
93 # Generates the server-absolute URL to an asset package.
a393223 Jeremy Ashkenas got relatively bulletproof mhtml generation working
jashkenas authored
94 def self.asset_url(package, extension, suffix=nil, mtime=nil)
95 timestamp = mtime ? "?#{mtime.to_i}" : ''
14e66b9 Jeremy Ashkenas wrote a first draft of documentation into index -- got the configuration...
jashkenas authored
96 "/#{package_path}/#{filename(package, extension, suffix)}#{timestamp}"
2de7c26 Jeremy Ashkenas working pre-compiled mhtml
jashkenas authored
97 end
98
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
99
100 private
101
9697b78 Jeremy Ashkenas removing the clunky case statements for setting 'package_assets' and 'te...
jashkenas authored
102 # Ensure that the JavaScript compressor is a valid choice.
fd636c4 Jeremy Ashkenas 0.2.3, with more robust compressor setting
jashkenas authored
103 def self.set_javascript_compressor(value)
104 value = value && value.to_sym
105 @javascript_compressor = AVAILABLE_COMPRESSORS.include?(value) ? value : DEFAULT_COMPRESSOR
106 end
107
9697b78 Jeremy Ashkenas removing the clunky case statements for setting 'package_assets' and 'te...
jashkenas authored
108 # Turn asset packaging on or off, depending on configuration and environment.
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
109 def self.set_package_assets(value)
18419fe Jeremy Ashkenas removing all traces of RAILS_ENV
jashkenas authored
110 package_env = !defined?(Rails) || !Rails.env.development?
9697b78 Jeremy Ashkenas removing the clunky case statements for setting 'package_assets' and 'te...
jashkenas authored
111 @package_assets = value == true || value.nil? ? package_env :
112 value == 'always' ? true : false
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
113 end
114
9697b78 Jeremy Ashkenas removing the clunky case statements for setting 'package_assets' and 'te...
jashkenas authored
115 # Assign the JST template function, unless explicitly turned off.
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
116 def self.set_template_function(value)
9697b78 Jeremy Ashkenas removing the clunky case statements for setting 'package_assets' and 'te...
jashkenas authored
117 @template_function = value == true || value.nil? ? DEFAULT_JST_COMPILER :
118 value == false ? '' : value
fdf1e64 Jeremy Ashkenas with diagram image and opt-out of the template_function
jashkenas authored
119 @include_jst_script = @template_function == DEFAULT_JST_COMPILER
120 end
121
97953ad Jeremy Ashkenas merging in agibralter's patch that adds template_namespace as an option,...
jashkenas authored
122 # Set the root JS object in which to stash all compiled JST.
6ab2f65 Aaron Gibralter Added the ability to specify an alternative for window.JST in assets.yml...
agibralter authored
123 def self.set_template_namespace(value)
97953ad Jeremy Ashkenas merging in agibralter's patch that adds template_namespace as an option,...
jashkenas authored
124 @template_namespace = value == true || value.nil? ? DEFAULT_JST_NAMESPACE : value.to_s
6ab2f65 Aaron Gibralter Added the ability to specify an alternative for window.JST in assets.yml...
agibralter authored
125 end
126
a8fb7d3 Jeremy Ashkenas adding a JavaNotFound exception, and raising it if Java isn't installed,...
jashkenas authored
127 # The YUI Compressor requires Java > 1.4, and Closure requires Java > 1.6.
128 def self.check_java_version
129 java = @compressor_options[:java] || 'java'
af21b30 Jeremy Ashkenas Jammit 0.3.3, allowing linebreaks
jashkenas authored
130 @css_compressor_options[:java] ||= java if @compressor_options[:java]
a58a8d4 Jeremy Ashkenas adding a test for broken java configuration
jashkenas authored
131 version = (`#{java} -version 2>&1`)[/\d+\.\d+/]
c5f9882 Jeremy Ashkenas removed the JavaNotFound exception in favor of running in degraded mode ...
jashkenas authored
132 disable_compression if !version ||
133 (@javascript_compressor == :closure && version < '1.6') ||
134 (@javascript_compressor == :yui && version < '1.4')
135 end
136
137 # If we don't have a working Java VM, then disable asset compression and
138 # complain loudly.
139 def self.disable_compression
e525356 Jeremy Ashkenas fixing broken configuration test -- making 'compress_assets' a first-cla...
jashkenas authored
140 @compress_assets = false
b119b57 Chris Eppstein Support for legacy rails logging and when no default logger has been set...
chriseppstein authored
141 logger.warn("Warning: Jammit asset compression disabled -- Java unavailable.")
142 end
143
144 def self.logger
145 @logger ||= if defined?(Rails)
146 Rails.logger
147 elsif defined?(RAILS_DEFAULT_LOGGER)
148 RAILS_DEFAULT_LOGGER
149 end
150 @logger || std_error_logger
151 end
152
153 def self.std_error_logger
154 @std_error_logger ||= begin
155 err_logger = Object.new
156 def err_logger.warn(message)
157 STDERR.puts(message)
158 end
159 err_logger
160 end
a8fb7d3 Jeremy Ashkenas adding a JavaNotFound exception, and raising it if Java isn't installed,...
jashkenas authored
161 end
162
8f30dbd Jeremy Ashkenas initial jammit commit
jashkenas authored
163 end
164
2310339 Jeremy Ashkenas pull out all external dependencies into a standalone file
jashkenas authored
165 require 'jammit/dependencies'
Something went wrong with that request. Please try again.