Module: Jammit

Defined in:
lib/jammit-core.rb,
lib/jammit-core/ui.rb,
lib/jammit-core/cli.rb,
lib/jammit-core/config.rb,
lib/jammit-core/helper.rb,
lib/jammit-core/routes.rb,
lib/jammit-core/packager.rb,
lib/jammit-core/compressor.rb,
lib/jammit-core/command_line.rb

Overview

@Jammit@ is the central namespace for all Jammit classes, and provides access to all of the configuration options.

Defined Under Namespace

Modules: Config, Helper, Routes Classes: AlreadyConfigured, AppNotFound, CLI, CommandLine, Compressor, ConfigurationNotFound, Error, OutputNotWritable, PackageNotFound, Packager, UI

Constant Summary collapse

VERSION =
"0.4.3"
ROOT =
File.expand_path(File.dirname(__FILE__))
ASSET_ROOT =
File.expand_path(".")
PUBLIC_ROOT =
File.join(ASSET_ROOT, 'public')
DEFAULT_CONFIG_PATH =
File.join(ASSET_ROOT, 'config')
DEFAULT_CONFIG_FILENAME =
'assets.yml'
DEFAULT_PACKAGE_PATH =
"assets"
DEFAULT_JST_SCRIPT =
File.join(ROOT, 'lib/jammit/jst.js')
DEFAULT_JST_COMPILER =
"template"
DEFAULT_JST_NAMESPACE =
"window.JST"
AVAILABLE_COMPRESSORS =
[:yui, :closure]
DEFAULT_COMPRESSOR =
:yui
SUFFIX =
'all'
DEBUG_SUFFIX =
'all-debug'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



67
68
69
# File 'lib/jammit-core.rb', line 67

def configuration
  @configuration
end

.optionsObject



95
96
97
# File 'lib/jammit-core.rb', line 95

def options
  @options
end

.uiObject



91
92
93
# File 'lib/jammit-core.rb', line 91

def ui
  @ui ||= UI.new
end

Class Method Details

.asset_rootObject



99
100
101
102
103
104
# File 'lib/jammit-core.rb', line 99

def asset_root
  unless @asset_root      
    @asset_root = (options && options[:asset_root]) ? options[:asset_root] : ASSET_ROOT
  end
  @asset_root
end

.asset_url(package, extension, suffix = nil, mtime = nil) ⇒ Object

Generates the server-absolute URL to an asset package.



206
207
208
209
# File 'lib/jammit-core.rb', line 206

def asset_url(package, extension, suffix=nil, mtime=nil)
  timestamp = mtime ? "?#{mtime.to_i}" : ''
  "/#{package_path}/#{filename(package, extension, suffix)}#{timestamp}"
end

.check_java_versionObject

The YUI Compressor requires Java > 1.4, and Closure requires Java > 1.6.



149
150
151
152
153
154
155
156
# File 'lib/jammit-core.rb', line 149

def check_java_version
  java = @config[:compressor_options][:java] || 'java'
  @config[:css_compressor_options][:java] ||= java if @config[:compressor_options][:java]
  version = (`#{java} -version 2>&1`)[/\d+\.\d+/]
  disable_compression if !version ||
    (@config[:javascript_compressor] == :closure && version < '1.6') ||
    (@config[:javascript_compressor] == :yui && version < '1.4')
end

.configObject

Load the complete asset configuration from the specified @config_path@.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jammit-core.rb', line 120

def config
  unless @config
    # Normalize config
    @config = Jammit::Config.load.to_mash
                  
    @config[:output_folder]         = (@config[:output_folder]) ? File.join(asset_root, @config[:output_folder]) : public_root
    @config[:package_path]          = @config[:package_path] || Jammit::DEFAULT_PACKAGE_PATH
    @config[:embed_assets]          = @config[:embed_assets] || @config[:embed_images]
    @config[:compress_assets]       = !(@config[:compress_assets] == false)
    @config[:gzip_assets]           = !(@config[:gzip_assets] == false)  
    @config[:mhtml_enabled]         = @config[:embed_assets] && @config[:embed_assets] != "datauri"
    @config[:compressor_options]    = (@config[:compressor_options] || {}).to_mash
    @config[:css_compressor_options]  = (@config[:css_compressor_options] || {}).to_mash
    @config[:javascript_compressor] = get_javascript_compressor(@config[:javascript_compressor].to_sym)
    @config[:package_assets]        = get_package_assets(@config[:package_assets])
    @config[:template_function]     = get_template_function(@config[:template_function])
    @config[:include_jst_script]    = @config[:template_function] == Jammit::DEFAULT_JST_COMPILER
    @config[:template_namespace]    = get_template_namespace(@config[:template_namespace])
    @config[:suffix]                = SUFFIX unless @config[:suffix]
    @config[:debug_suffix]          = DEBUG_SUFFIX unless @config[:debug_suffix]
  end
  @config
end

.config=(config) ⇒ Object



144
145
146
# File 'lib/jammit-core.rb', line 144

def config=(config)
  @config = config
end

.config_pathObject



110
111
112
# File 'lib/jammit-core.rb', line 110

def config_path
  @config_path ||= File.join(asset_root, 'config', DEFAULT_CONFIG_FILENAME)
end

.configureObject



72
73
74
75
76
77
78
79
80
# File 'lib/jammit-core.rb', line 72

def configure
  @configured ||= begin
    ##
    # perform some config here?
    #
    #configure_gem_home_and_path
    true
  end
end

.disable_compressionObject

If we don’t have a working Java VM, then disable asset compression and complain loudly.



160
161
162
163
# File 'lib/jammit-core.rb', line 160

def disable_compression
  @config[:compress_assets] = false
  Jammit.ui.warn("Asset compression disabled -- Java unavailable.")
end

.ensure_in_appObject

Ensure we’re running within a rails app unless generating a new app ($ xmvc generate app foo)



85
86
87
88
89
# File 'lib/jammit-core.rb', line 85

def ensure_in_app
  unless ARGV.empty? || ARGV.first == 'help' || (File.exists?('app') && File.exists?('config') && File.exists?('config/environment.json'))
    raise Jammit::AppNotFound.new("This command must be executed from the root of an extjs-mvc application")
  end
end

.filename(package, extension, suffix = nil) ⇒ Object

Generate the base filename for a version of a given package.



200
201
202
203
# File 'lib/jammit-core.rb', line 200

def filename(package, extension, suffix=nil)
  suffix_part  = suffix ? "-#{suffix}" : ''
  "#{package}#{suffix_part}.#{extension}"
end

.get_javascript_compressor(value) ⇒ Object

Ensure that the JavaScript compressor is a valid choice.



177
178
179
# File 'lib/jammit-core.rb', line 177

def get_javascript_compressor(value)
  Jammit::AVAILABLE_COMPRESSORS.include?(value) ? value : Jammit::DEFAULT_COMPRESSOR
end

.get_package_assets(value) ⇒ Object

Turn asset packaging on or off, depending on configuration and environment.



166
167
168
169
# File 'lib/jammit-core.rb', line 166

def get_package_assets(value)
  package_env = !defined?(Rails) || !Rails.env.development?
  (value == true || value.nil?) ? package_env : (value == 'always') ? true : false
end

.get_template_function(value) ⇒ Object

Assign the JST template function, unless explicitly turned off.



172
173
174
# File 'lib/jammit-core.rb', line 172

def get_template_function(value)
  (value == true || value.nil?) ? DEFAULT_JST_COMPILER : value == false ? '' : value
end

.get_template_namespace(value) ⇒ Object

Set the root JS object in which to stash all compiled JST.



182
183
184
# File 'lib/jammit-core.rb', line 182

def get_template_namespace(value)
  (value == true || value.nil?) ? Jammit::DEFAULT_JST_NAMESPACE : value.to_s
end

.package_pathObject



114
115
116
# File 'lib/jammit-core.rb', line 114

def package_path
  @package_path ||= (@options && @options[:package_path]) ? @options[:package_path] : config[:package_path]
end

.packagerObject

Keep a global (thread-local) reference to a @Jammit::Packager@, to avoid recomputing asset lists unnecessarily.



195
196
197
# File 'lib/jammit-core.rb', line 195

def packager    
  Thread.current[:jammit_packager] ||= Packager.new
end

.public_rootObject



106
107
108
# File 'lib/jammit-core.rb', line 106

def public_root
  @public_root ||= File.join(asset_root, 'public')
end

.reload!Object

Force a reload by resetting the Packager and reloading the configuration. In development, this will be called as a before_filter before every request.



188
189
190
191
# File 'lib/jammit-core.rb', line 188

def reload!
  Thread.current[:jammit_packager] = nil
  @config = Config.load(config[:config_path], true)
end