Skip to content

Commit

Permalink
initial jammit commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Nov 8, 2009
0 parents commit 8f30dbd
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.gem
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2009 Jeremy Ashkenas, DocumentCloud

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 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.
Empty file added README
Empty file.
26 changes: 26 additions & 0 deletions Rakefile
@@ -0,0 +1,26 @@
require 'rake/testtask'

# desc 'Run all tests'
# task :test do
# $LOAD_PATH.unshift(File.expand_path('test'))
# require 'redgreen' if Gem.available?('redgreen')
# require 'test/unit'
# Dir['test/**/test_*.rb'].each {|test| require test }
# end

namespace :gem do

desc 'Build and install the jammit gem'
task :install do
sh "gem build jammit.gemspec"
sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc"
end

desc 'Uninstall the jammit gem'
task :uninstall do
sh "sudo gem uninstall -x jammit"
end

end

task :default => :test
Empty file added TODO
Empty file.
22 changes: 22 additions & 0 deletions assets.example.yml
@@ -0,0 +1,22 @@
javascripts:
workspace:
- public/javascripts/vendor/*.js
- public/javascripts/application.js
- public/javascripts/lib/bindable.js
- public/javascripts/lib/set.js
- public/javascripts/lib/*.js
- public/javascripts/**/*.js

stylesheets:
app:
- public/stylesheets/reset.css
- public/stylesheets/aristo.css
- public/stylesheets/*.css
workspace:
- public/stylesheets/pages/workspace.css
empty:
- public/stylesheets/pages/empty.css

jst:
complete:
- app/views/jst/**/*.jst
41 changes: 41 additions & 0 deletions jammit.gemspec
@@ -0,0 +1,41 @@
Gem::Specification.new do |s|
s.name = 'jammit'
s.version = '0.1.0' # Keep version in sync with jammit.rb
s.date = '2009-11-06'

s.homepage = "http://wiki.github.com/documentcloud/jammit"
s.summary = "Asset Packaging, Javascript Templates, YUI Compressed"
s.description = <<-EOS
Lorem Ipsum.
EOS

s.authors = ['Jeremy Ashkenas']
s.email = 'jeremy@documentcloud.org'
s.rubyforge_project = 'jammit'

s.require_paths = ['lib']

s.has_rdoc = true
s.extra_rdoc_files = ['README']
s.rdoc_options << '--title' << 'Jammit' <<
'--exclude' << 'test' <<
'--main' << 'README' <<
'--all'

s.add_dependency 'rails'
s.add_dependency 'yui-compressor', ['>= 0.9.2']

s.files = %w(
jammit.gemspec
lib/jammit.rb
lib/jammit/controller.rb
lib/jammit/compressor.rb
lib/jammit/helper.rb
lib/jammit/jst.js
lib/jammit/packager.rb
lib/jammit/routes.rb
LICENSE
Rakefile
README
)
end
33 changes: 33 additions & 0 deletions lib/jammit.rb
@@ -0,0 +1,33 @@
module Jammit

VERSION = "0.1.0"

ROOT = File.expand_path(File.dirname(__FILE__) + '/..')

DEV = RAILS_ENV == 'development'

JST_SCRIPT = File.read(ROOT + '/lib/jammit/jst.js')

def self.load_configuration
YAML.load_file("#{RAILS_ROOT}/config/assets.yml")
end

def self.packager
@packager ||= Packager.new
end

ASSET_CONFIG = load_configuration

class PackageNotFound < NameError
end

end

require 'yui/compressor'
require 'jammit/controller'
require 'jammit/compressor'
require 'jammit/helper'
require 'jammit/packager'
require 'jammit/routes'

ActionView::Base.send(:include, Jammit::Helper)
38 changes: 38 additions & 0 deletions lib/jammit/compressor.rb
@@ -0,0 +1,38 @@
module Jammit

class Compressor

JST_NAMER = /\/(\w+)\.jst\Z/

def initialize
@yui_js = YUI::JavaScriptCompressor.new(:munge => true)
@yui_css = YUI::CssCompressor.new
end

def compress_js(*paths)
@yui_js.compress(concatenate(paths))
end

def compress_css(*paths)
@yui_css.compress(concatenate(paths))
end

def compile_jst(*paths)
compiled = paths.map { |path|
template_name = path.match(JST_NAMER)[1]
contents = File.read(path).gsub(/\n/, '').gsub("'", '\\\\\'')
"window.JST.#{template_name} = JST.compile('#{contents}');"
}.join("\n")
JST_SCRIPT + "\n" + compiled
end


private

def concatenate(paths)
[paths].flatten.map {|p| File.read(p) }.join("\n")
end

end

end
23 changes: 23 additions & 0 deletions lib/jammit/controller.rb
@@ -0,0 +1,23 @@
module Jammit

class Controller < ActionController::Base

caches_page :jst, :javascripts, :stylesheets

def javascripts
render :js => Jammit.packager.pack_javascript(params[:package].to_sym)
end

def stylesheets
render :text => Jammit.packager.pack_stylesheet(params[:package].to_sym), :content_type => 'text/css'
end

def jst
render :js => Jammit.packager.pack_jst(params[:package].to_sym)
end

end

end

::JammitController = Jammit::Controller
21 changes: 21 additions & 0 deletions lib/jammit/helper.rb
@@ -0,0 +1,21 @@
module Jammit

module Helper

def include_stylesheets(*packages)
tags = packages.map {|p| DEV ? Jammit.packager.stylesheet_urls(p.to_sym) : "/assets/#{p}.css" }
stylesheet_link_tag(tags.flatten)
end

def include_javascripts(*packages)
tags = packages.map {|p| DEV ? Jammit.packager.javascript_urls(p.to_sym) : "/assets/#{p}.js" }
javascript_include_tag(tags.flatten)
end

def include_jst(*packages)
javascript_include_tag(packages.map {|p| "/assets/#{p}.jst" })
end

end

end
1 change: 1 addition & 0 deletions lib/jammit/jst.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions lib/jammit/packager.rb
@@ -0,0 +1,81 @@
module Jammit

class Packager

def initialize
@asset_config = DEV ? Jammit.load_configuration : ASSET_CONFIG
@root = @asset_config['asset_root'] || ''
@css_config = @asset_config['stylesheets'].symbolize_keys
@js_config = @asset_config['javascripts'].symbolize_keys
@jst_config = @asset_config['jst'].symbolize_keys
@compressor = Compressor.new
@css, @js, @jst = nil, nil, nil
end

def stylesheet_urls(package)
stylesheet_packages[package][:urls]
end

def javascript_urls(package)
javascript_packages[package][:urls]
end

def jst_urls(package)
jst_packages[package][:urls]
end

def pack_stylesheet(package)
pack = stylesheet_packages[package]
raise PackageNotFound, "assets.yml does not contain a '#{package}' stylesheet package" if !pack
@compressor.compress_css(*pack[:paths])
end

def pack_javascript(package)
pack = javascript_packages[package]
raise PackageNotFound, "assets.yml does not contain a '#{package}' javascript package" if !pack
@compressor.compress_js(*pack[:paths])
end

def pack_jst(package)
pack = jst_packages[package]
raise PackageNotFound, "assets.yml does not contain a '#{package}' jst package" if !pack
@compressor.compile_jst(*pack[:paths])
end

def stylesheet_packages
@css ||= create_packages(@css_config)
end

def javascript_packages
@js ||= create_packages(@js_config)
end

def jst_packages
@jst ||= create_packages(@jst_config)
end


private

def create_packages(config)
packages = {}
return packages if !config
config.each do |name, globs|
packages[name] = {}
paths = packages[name][:paths] = unique_paths(globs)
packages[name][:urls] = paths_to_urls(paths)
end
packages
end

def unique_paths(globs)
globs.map {|glob| Dir[glob] }.flatten.uniq
end

def paths_to_urls(paths)
paths.map {|path| path.sub(/\Apublic/, @root) }
end

end

end
15 changes: 15 additions & 0 deletions lib/jammit/routes.rb
@@ -0,0 +1,15 @@
module Jammit

module Routes

def self.draw(map)
map.with_options :controller => 'jammit' do |jammit|
jammit.connect '/assets/:package.js', :action => 'javascripts'
jammit.connect '/assets/:package.css', :action => 'stylesheets'
jammit.connect '/assets/:package.jst', :action => 'jst'
end
end

end

end
1 change: 1 addition & 0 deletions test/fixtures/jammed/hello_world.js
@@ -0,0 +1 @@
var myself={sayHi:function(a){console.log("hello, "+a)}};
8 changes: 8 additions & 0 deletions test/fixtures/src/hello_world.js
@@ -0,0 +1,8 @@
var myself = {

// An Introduction:
sayHi : function(name) {
console.log("hello, " + name);
}

};
5 changes: 5 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,5 @@
ENV['RACK_ENV'] = 'test'

class Test::Unit::TestCase
include Jammit
end
7 changes: 7 additions & 0 deletions test/unit/test_stylesheet_jammer.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class StylesheetJammerTest < Test::Unit::TestCase

def test_css

end

0 comments on commit 8f30dbd

Please sign in to comment.