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

@[email protected] 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
105
106
107
108
109
110
# File 'lib/jammit-core.rb', line 99

def asset_root
  unless @asset_root      
    if (options && options[:asset_root])
      @asset_root = options[:asset_root]
    else
      @asset_root = ASSET_ROOT
    end
  end
  
  #@asset_root ||= (options && options[:asset_root]) ? options[:asset_root] : ASSET_ROOT   
  @asset_root
end

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

Generates the server-absolute URL to an asset package.


212
213
214
215
# File 'lib/jammit-core.rb', line 212

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.


155
156
157
158
159
160
161
162
# File 'lib/jammit-core.rb', line 155

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 @[email protected]


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jammit-core.rb', line 126

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


150
151
152
# File 'lib/jammit-core.rb', line 150

def config=(config)
  @config = config
end

.config_pathObject


116
117
118
# File 'lib/jammit-core.rb', line 116

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.


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

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.


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

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.


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

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.


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

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.


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

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.


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

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

.package_pathObject


120
121
122
# File 'lib/jammit-core.rb', line 120

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::[email protected], to avoid recomputing asset lists unnecessarily.


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

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

.public_rootObject


112
113
114
# File 'lib/jammit-core.rb', line 112

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.


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

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