Upgraded asset_packager
This commit is contained in:
205
vendor/plugins/asset_packager/lib/jsmin.rb
vendored
205
vendor/plugins/asset_packager/lib/jsmin.rb
vendored
@@ -1,205 +0,0 @@
|
||||
#!/usr/bin/ruby
|
||||
# jsmin.rb 2007-07-20
|
||||
# Author: Uladzislau Latynski
|
||||
# This work is a translation from C to Ruby of jsmin.c published by
|
||||
# Douglas Crockford. Permission is hereby granted to use the Ruby
|
||||
# version under the same conditions as the jsmin.c on which it is
|
||||
# based.
|
||||
#
|
||||
# /* jsmin.c
|
||||
# 2003-04-21
|
||||
#
|
||||
# Copyright (c) 2002 Douglas Crockford (www.crockford.com)
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
# this software and associated documentation files (the "Software"), to deal in
|
||||
# the Software without restriction, including without limitation the rights to
|
||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
# of the Software, and to permit persons to whom the Software is furnished to do
|
||||
# so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# The Software shall be used for Good, not Evil.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
EOF = -1
|
||||
$theA = ""
|
||||
$theB = ""
|
||||
|
||||
# isAlphanum -- return true if the character is a letter, digit, underscore,
|
||||
# dollar sign, or non-ASCII character
|
||||
def isAlphanum(c)
|
||||
return false if !c || c == EOF
|
||||
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
|
||||
c == '\\' || c[0] > 126)
|
||||
end
|
||||
|
||||
# get -- return the next character from stdin. Watch out for lookahead. If
|
||||
# the character is a control character, translate it to a space or linefeed.
|
||||
def get()
|
||||
c = $stdin.getc
|
||||
return EOF if(!c)
|
||||
c = c.chr
|
||||
return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
|
||||
return "\n" if (c == "\r")
|
||||
return " "
|
||||
end
|
||||
|
||||
# Get the next character without getting it.
|
||||
def peek()
|
||||
lookaheadChar = $stdin.getc
|
||||
$stdin.ungetc(lookaheadChar)
|
||||
return lookaheadChar.chr
|
||||
end
|
||||
|
||||
# mynext -- get the next character, excluding comments.
|
||||
# peek() is used to see if a '/' is followed by a '/' or '*'.
|
||||
def mynext()
|
||||
c = get
|
||||
if (c == "/")
|
||||
if(peek == "/")
|
||||
while(true)
|
||||
c = get
|
||||
if (c <= "\n")
|
||||
return c
|
||||
end
|
||||
end
|
||||
end
|
||||
if(peek == "*")
|
||||
get
|
||||
while(true)
|
||||
case get
|
||||
when "*"
|
||||
if (peek == "/")
|
||||
get
|
||||
return " "
|
||||
end
|
||||
when EOF
|
||||
raise "Unterminated comment"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
|
||||
# action -- do something! What you do is determined by the argument: 1
|
||||
# Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B.
|
||||
# (Delete A). 3 Get the next B. (Delete B). action treats a string as a
|
||||
# single character. Wow! action recognizes a regular expression if it is
|
||||
# preceded by ( or , or =.
|
||||
def action(a)
|
||||
if(a==1)
|
||||
$stdout.write $theA
|
||||
end
|
||||
if(a==1 || a==2)
|
||||
$theA = $theB
|
||||
if ($theA == "\'" || $theA == "\"")
|
||||
while (true)
|
||||
$stdout.write $theA
|
||||
$theA = get
|
||||
break if ($theA == $theB)
|
||||
raise "Unterminated string literal" if ($theA <= "\n")
|
||||
if ($theA == "\\")
|
||||
$stdout.write $theA
|
||||
$theA = get
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if(a==1 || a==2 || a==3)
|
||||
$theB = mynext
|
||||
if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
|
||||
$theA == ":" || $theA == "[" || $theA == "!" ||
|
||||
$theA == "&" || $theA == "|" || $theA == "?" ||
|
||||
$theA == "{" || $theA == "}" || $theA == ";" ||
|
||||
$theA == "\n"))
|
||||
$stdout.write $theA
|
||||
$stdout.write $theB
|
||||
while (true)
|
||||
$theA = get
|
||||
if ($theA == "/")
|
||||
break
|
||||
elsif ($theA == "\\")
|
||||
$stdout.write $theA
|
||||
$theA = get
|
||||
elsif ($theA <= "\n")
|
||||
raise "Unterminated RegExp Literal"
|
||||
end
|
||||
$stdout.write $theA
|
||||
end
|
||||
$theB = mynext
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# jsmin -- Copy the input to the output, deleting the characters which are
|
||||
# insignificant to JavaScript. Comments will be removed. Tabs will be
|
||||
# replaced with spaces. Carriage returns will be replaced with linefeeds.
|
||||
# Most spaces and linefeeds will be removed.
|
||||
def jsmin
|
||||
$theA = "\n"
|
||||
action(3)
|
||||
while ($theA != EOF)
|
||||
case $theA
|
||||
when " "
|
||||
if (isAlphanum($theB))
|
||||
action(1)
|
||||
else
|
||||
action(2)
|
||||
end
|
||||
when "\n"
|
||||
case ($theB)
|
||||
when "{","[","(","+","-"
|
||||
action(1)
|
||||
when " "
|
||||
action(3)
|
||||
else
|
||||
if (isAlphanum($theB))
|
||||
action(1)
|
||||
else
|
||||
action(2)
|
||||
end
|
||||
end
|
||||
else
|
||||
case ($theB)
|
||||
when " "
|
||||
if (isAlphanum($theA))
|
||||
action(1)
|
||||
else
|
||||
action(3)
|
||||
end
|
||||
when "\n"
|
||||
case ($theA)
|
||||
when "}","]",")","+","-","\"","\\", "'", '"'
|
||||
action(1)
|
||||
else
|
||||
if (isAlphanum($theA))
|
||||
action(1)
|
||||
else
|
||||
action(3)
|
||||
end
|
||||
end
|
||||
else
|
||||
action(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ARGV.each do |anArg|
|
||||
$stdout.write "// #{anArg}\n"
|
||||
end
|
||||
|
||||
jsmin
|
||||
@@ -1,212 +0,0 @@
|
||||
module Synthesis
|
||||
class AssetPackage
|
||||
|
||||
# class variables
|
||||
@@asset_packages_yml = $asset_packages_yml ||
|
||||
(File.exists?("#{RAILS_ROOT}/config/asset_packages.yml") ? YAML.load_file("#{RAILS_ROOT}/config/asset_packages.yml") : nil)
|
||||
|
||||
# singleton methods
|
||||
class << self
|
||||
|
||||
def merge_environments=(environments)
|
||||
@@merge_environments = environments
|
||||
end
|
||||
|
||||
def merge_environments
|
||||
@@merge_environments ||= ["production"]
|
||||
end
|
||||
|
||||
def parse_path(path)
|
||||
/^(?:(.*)\/)?([^\/]+)$/.match(path).to_a
|
||||
end
|
||||
|
||||
def find_by_type(asset_type)
|
||||
@@asset_packages_yml[asset_type].map { |p| self.new(asset_type, p) }
|
||||
end
|
||||
|
||||
def find_by_target(asset_type, target)
|
||||
package_hash = @@asset_packages_yml[asset_type].find {|p| p.keys.first == target }
|
||||
package_hash ? self.new(asset_type, package_hash) : nil
|
||||
end
|
||||
|
||||
def find_by_source(asset_type, source)
|
||||
path_parts = parse_path(source)
|
||||
package_hash = @@asset_packages_yml[asset_type].find do |p|
|
||||
key = p.keys.first
|
||||
p[key].include?(path_parts[2]) && (parse_path(key)[1] == path_parts[1])
|
||||
end
|
||||
package_hash ? self.new(asset_type, package_hash) : nil
|
||||
end
|
||||
|
||||
def targets_from_sources(asset_type, sources)
|
||||
package_names = Array.new
|
||||
sources.each do |source|
|
||||
package = find_by_target(asset_type, source) || find_by_source(asset_type, source)
|
||||
package_names << (package ? package.current_file : source)
|
||||
end
|
||||
package_names.uniq
|
||||
end
|
||||
|
||||
def sources_from_targets(asset_type, targets)
|
||||
source_names = Array.new
|
||||
targets.each do |target|
|
||||
package = find_by_target(asset_type, target)
|
||||
source_names += (package ? package.sources.collect do |src|
|
||||
package.target_dir.gsub(/^(.+)$/, '\1/') + src
|
||||
end : target.to_a)
|
||||
end
|
||||
source_names.uniq
|
||||
end
|
||||
|
||||
def build_all
|
||||
@@asset_packages_yml.keys.each do |asset_type|
|
||||
@@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).build }
|
||||
end
|
||||
end
|
||||
|
||||
def delete_all
|
||||
@@asset_packages_yml.keys.each do |asset_type|
|
||||
@@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).delete_previous_build }
|
||||
end
|
||||
end
|
||||
|
||||
def create_yml
|
||||
unless File.exists?("#{RAILS_ROOT}/config/asset_packages.yml")
|
||||
asset_yml = Hash.new
|
||||
|
||||
asset_yml['javascripts'] = [{"base" => build_file_list("#{RAILS_ROOT}/public/javascripts", "js")}]
|
||||
asset_yml['stylesheets'] = [{"base" => build_file_list("#{RAILS_ROOT}/public/stylesheets", "css")}]
|
||||
|
||||
File.open("#{RAILS_ROOT}/config/asset_packages.yml", "w") do |out|
|
||||
YAML.dump(asset_yml, out)
|
||||
end
|
||||
|
||||
log "config/asset_packages.yml example file created!"
|
||||
log "Please reorder files under 'base' so dependencies are loaded in correct order."
|
||||
else
|
||||
log "config/asset_packages.yml already exists. Aborting task..."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# instance methods
|
||||
attr_accessor :asset_type, :target, :target_dir, :sources
|
||||
|
||||
def initialize(asset_type, package_hash)
|
||||
target_parts = self.class.parse_path(package_hash.keys.first)
|
||||
@target_dir = target_parts[1].to_s
|
||||
@target = target_parts[2].to_s
|
||||
@sources = package_hash[package_hash.keys.first]
|
||||
@asset_type = asset_type
|
||||
@asset_path = ($asset_base_path ? "#{$asset_base_path}/" : "#{RAILS_ROOT}/public/") +
|
||||
"#{@asset_type}#{@target_dir.gsub(/^(.+)$/, '/\1')}"
|
||||
@extension = get_extension
|
||||
@file_name = "#{@target}_packaged.#{@extension}"
|
||||
@full_path = File.join(@asset_path, @file_name)
|
||||
end
|
||||
|
||||
def package_exists?
|
||||
File.exists?(@full_path)
|
||||
end
|
||||
|
||||
def current_file
|
||||
build unless package_exists?
|
||||
|
||||
path = @target_dir.gsub(/^(.+)$/, '\1/')
|
||||
"#{path}#{@target}_packaged"
|
||||
end
|
||||
|
||||
def build
|
||||
delete_previous_build
|
||||
create_new_build
|
||||
end
|
||||
|
||||
def delete_previous_build
|
||||
File.delete(@full_path) if File.exists?(@full_path)
|
||||
end
|
||||
|
||||
private
|
||||
def create_new_build
|
||||
new_build_path = "#{@asset_path}/#{@target}_packaged.#{@extension}"
|
||||
if File.exists?(new_build_path)
|
||||
log "Latest version already exists: #{new_build_path}"
|
||||
else
|
||||
File.open(new_build_path, "w") {|f| f.write(compressed_file) }
|
||||
log "Created #{new_build_path}"
|
||||
end
|
||||
end
|
||||
|
||||
def merged_file
|
||||
merged_file = ""
|
||||
@sources.each {|s|
|
||||
File.open("#{@asset_path}/#{s}.#{@extension}", "r") { |f|
|
||||
merged_file += f.read + "\n"
|
||||
}
|
||||
}
|
||||
merged_file
|
||||
end
|
||||
|
||||
def compressed_file
|
||||
case @asset_type
|
||||
when "javascripts" then compress_js(merged_file)
|
||||
when "stylesheets" then compress_css(merged_file)
|
||||
end
|
||||
end
|
||||
|
||||
def compress_js(source)
|
||||
jsmin_path = "#{RAILS_ROOT}/vendor/plugins/asset_packager/lib"
|
||||
tmp_path = "#{RAILS_ROOT}/tmp/#{@target}_packaged"
|
||||
|
||||
# write out to a temp file
|
||||
File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) }
|
||||
|
||||
# compress file with JSMin library
|
||||
`ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n`
|
||||
|
||||
# read it back in and trim it
|
||||
result = ""
|
||||
File.open("#{tmp_path}_compressed.js", "r") { |f| result += f.read.strip }
|
||||
|
||||
# delete temp files if they exist
|
||||
File.delete("#{tmp_path}_uncompressed.js") if File.exists?("#{tmp_path}_uncompressed.js")
|
||||
File.delete("#{tmp_path}_compressed.js") if File.exists?("#{tmp_path}_compressed.js")
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def compress_css(source)
|
||||
source.gsub!(/\s+/, " ") # collapse space
|
||||
source.gsub!(/\/\*(.*?)\*\//, "") # remove comments - caution, might want to remove this if using css hacks
|
||||
source.gsub!(/\} /, "}\n") # add line breaks
|
||||
source.gsub!(/\n$/, "") # remove last break
|
||||
source.gsub!(/ \{ /, " {") # trim inside brackets
|
||||
source.gsub!(/; \}/, "}") # trim inside brackets
|
||||
source
|
||||
end
|
||||
|
||||
def get_extension
|
||||
case @asset_type
|
||||
when "javascripts" then "js"
|
||||
when "stylesheets" then "css"
|
||||
end
|
||||
end
|
||||
|
||||
def log(message)
|
||||
self.class.log(message)
|
||||
end
|
||||
|
||||
def self.log(message)
|
||||
puts message
|
||||
end
|
||||
|
||||
def self.build_file_list(path, extension)
|
||||
re = Regexp.new(".#{extension}\\z")
|
||||
file_list = Dir.new(path).entries.delete_if { |x| ! (x =~ re) }.map {|x| x.chomp(".#{extension}")}
|
||||
# reverse javascript entries so prototype comes first on a base rails app
|
||||
file_list.reverse! if extension == "js"
|
||||
file_list
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,39 +0,0 @@
|
||||
module Synthesis
|
||||
module AssetPackageHelper
|
||||
|
||||
def should_merge?
|
||||
AssetPackage.merge_environments.include?(RAILS_ENV)
|
||||
end
|
||||
|
||||
def javascript_include_merged(*sources)
|
||||
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
|
||||
|
||||
if sources.include?(:defaults)
|
||||
sources = sources[0..(sources.index(:defaults))] +
|
||||
['prototype', 'effects', 'dragdrop', 'controls'] +
|
||||
(File.exists?("#{RAILS_ROOT}/public/javascripts/application.js") ? ['application'] : []) +
|
||||
sources[(sources.index(:defaults) + 1)..sources.length]
|
||||
sources.delete(:defaults)
|
||||
end
|
||||
|
||||
sources.collect!{|s| s.to_s}
|
||||
sources = (should_merge? ?
|
||||
AssetPackage.targets_from_sources("javascripts", sources) :
|
||||
AssetPackage.sources_from_targets("javascripts", sources))
|
||||
|
||||
sources.collect {|source| javascript_include_tag(source, options) }.join("\n")
|
||||
end
|
||||
|
||||
def stylesheet_link_merged(*sources)
|
||||
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
|
||||
|
||||
sources.collect!{|s| s.to_s}
|
||||
sources = (should_merge? ?
|
||||
AssetPackage.targets_from_sources("stylesheets", sources) :
|
||||
AssetPackage.sources_from_targets("stylesheets", sources))
|
||||
|
||||
sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
23
vendor/plugins/asset_packager/lib/tasks/asset_packager_tasks.rake
vendored
Normal file
23
vendor/plugins/asset_packager/lib/tasks/asset_packager_tasks.rake
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'yaml'
|
||||
require File.dirname(__FILE__) + '/../../lib/synthesis/asset_package'
|
||||
|
||||
namespace :asset do
|
||||
namespace :packager do
|
||||
|
||||
desc "Merge and compress assets"
|
||||
task :build_all do
|
||||
Synthesis::AssetPackage.build_all
|
||||
end
|
||||
|
||||
desc "Delete all asset builds"
|
||||
task :delete_all do
|
||||
Synthesis::AssetPackage.delete_all
|
||||
end
|
||||
|
||||
desc "Generate asset_packages.yml from existing assets"
|
||||
task :create_yml do
|
||||
Synthesis::AssetPackage.create_yml
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user