Skip to content
Browse files

Initial commit; good to go with Rails 3!

  • Loading branch information...
0 parents commit 22a93a9d30b3e58786a83bf4dbc83a42a17ec9fb Josh Delsman committed Feb 12, 2010
Showing with 325 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +82 −0 README
  3. +10 −0 Rakefile
  4. BIN bin/closure-compiler.jar
  5. BIN bin/yui-compressor.jar
  6. +3 −0 init.rb
  7. +99 −0 lib/assemblage.rb
  8. +100 −0 lib/tasks/assemble.rake
  9. +8 −0 test/assemblage_test.rb
  10. +3 −0 test/test_helper.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Josh Delsman
+
+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.
82 README
@@ -0,0 +1,82 @@
+Assemblage
+==========
+
+Based on the concept {sported by GitHub}[http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails], this plugin serves the purpose of making it much easier to package your JavaScript and CSS files together into "bundles", so that they are easy for your clients to download once and cache in their browsers. This is a recommended practice to increase performance of your Rails application dramatically.
+
+Getting Started
+===============
+
+There is a simple methodology that needs to be adopted by your application when organising your JavaScript and CSS files:
+
+ public/javascripts
+ |-- admin
+ | |-- date.js
+ | `-- datePicker.js
+ |-- common
+ | |-- application.js
+ | |-- jquery.tablesorter.js
+ | `-- jquery.editable.js
+ |-- dev
+ |-- jquery-1.4.1.js
+ `-- jquery-ui-1.7.2.js
+
+The directory structure is similar in public/stylesheets, as well. This essentially namespaces your directories so they are easier to organise. You need to only place files that are used in that specific namespace in the directory. If you use the library in more than one namespace, it should be placed in a "common" folder.
+
+The "dev" folder is used locally, and is replaced with a cached version somewhere else on the Internet in production. More on that later...
+
+View Helper Methods
+===================
+
+You will need to include a couple of helpers in your layout files:
+
+ <%= javascript_dev ['jquery-1.4.1', "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"] %>
+ <%= javascript_bundle 'common', 'clients' %>
+ <%= stylesheet_bundle 'common', 'clients' %>
+
+In this instance, there are three helpers: `javascript_dev`, `javascript_bundle` and `stylesheet_bundle`.
+
+* <tt>javascript_dev</tt> - In development, the files included in the "dev" directory are included. Otherwise, it sets the cached version using the URL.
+* <tt>javascript_bundle</tt> - In development, this method does a recursive search for all files in the namespaces listed and includes the raw, uncompressed files. Otherwise, it will look for "bundle_namespace.js" and include this file, if it exists.
+* <tt>stylesheet_bundle</tt> - Exactly the same as above, but for CSS files.
+
+Compression
+===========
+
+JavaScript compression is handled using the Closure Compiler by Google, which is included in the bin/ directory. CSS compression is handled by YUI Compressor, also included.
+
+Rake Tasks
+==========
+
+Included Rake tasks are:
+
+* <tt>rake assemble:all</tt> - Compresses and bundles both CSS & JavaScript
+* <tt>rake assemble:js</tt> - Only assembles namespaced JavaScript
+* <tt>rake assemble:css</tt> - Only assembles namespaced CSS
+
+Deployment
+==========
+
+You can handle these tasks on deployment by doing the following:
+
+ namespace :deploy do
+ desc "Assemble JavaScript and CSS files"
+ task :assemble, :roles => :web, :except => { :no_release => true } do
+ run "cd #{current_path}; rake assemble:all"
+ end
+ end
+
+ after "deploy:update_code", "deploy:assemble"
+
+Issues & Contributions
+======================
+
+For all issues and bug/feature requests, please use the GitHub issue tracker:
+
+http://github.com/voxxit/assemblage
+
+Props
+=====
+
+Thanks to {Kyle Neath}[http://github.com/kneath] for the inspiration to turn his idea into a useful plugin for all!
+
+Copyright (c) 2010 Josh Delsman, released under the MIT license
10 Rakefile
@@ -0,0 +1,10 @@
+require 'rake/testtask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the assemblage plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+end
BIN bin/closure-compiler.jar
Binary file not shown.
BIN bin/yui-compressor.jar
Binary file not shown.
3 init.rb
@@ -0,0 +1,3 @@
+require 'assemblage'
+
+ActionView::Base.send(:include, Assemblage::ViewHelpers)
99 lib/assemblage.rb
@@ -0,0 +1,99 @@
+require 'find'
+
+module Assemblage
+ module ViewHelpers
+ def bundle_files?
+ Rails.env.production? || Rails.env.staging? || params[:bundle] || cookies[:bundle] == "yes"
+ end
+
+ def javascript_bundle(*sources)
+ sources = sources.to_a
+ bundle_files? ? javascript_include_bundles(sources) : javascript_include_files(sources)
+ end
+
+ # This method assumes you have manually bundled js using a rake command
+ # or similar. So, there better be bundle_* files!
+ def javascript_include_bundles(bundles)
+ output = ""
+
+ bundles.each do |bundle|
+ output << javascript_src_tag("bundle_#{bundle}", {}) + "\n"
+ end
+
+ output
+ end
+
+ def javascript_include_files(bundles)
+ output = ""
+
+ bundles.each do |bundle|
+ files = recursive_file_list("public/javascripts/#{bundle}", ".js")
+
+ files.each do |file|
+ file = file.gsub('public/javascripts/', '')
+
+ output << javascript_src_tag(file, {}) + "\n"
+ end
+ end
+
+ output
+ end
+
+ def javascript_dev(*sources)
+ output = ""
+ sources = sources.to_a
+
+ sources.each do |pair|
+ output << javascript_src_tag(Rails.env.development? ? "dev/#{pair[0]}" : pair[1], {})
+ end
+
+ output
+ end
+
+ def stylesheet_bundle(*sources)
+ sources = sources.to_a
+ bundle_files? ? stylesheet_include_bundles(sources) : stylesheet_include_files(sources)
+ end
+
+ # This method assumes you have manually bundled css using a rake command
+ # or similar. So, there better be bundle_* files!
+ def stylesheet_include_bundles(bundles)
+ stylesheet_link_tag(bundles.collect{ |b| "bundle_#{b}"})
+ end
+
+ def stylesheet_include_files(bundles)
+ output = ""
+
+ bundles.each do |bundle|
+ files = recursive_file_list("public/stylesheets/#{bundle}", ".css")
+
+ files.each do |file|
+ file = file.gsub('public/stylesheets/', '')
+
+ output << stylesheet_link_tag(file)
+ end
+ end
+
+ output
+ end
+
+ def recursive_file_list(basedir, extname)
+ files = []
+ basedir = Rails.root.join(basedir)
+
+ Find.find(basedir) do |path|
+ if FileTest.directory?(path)
+ if File.basename(path)[0] == ?.
+ Find.prune
+ else
+ next
+ end
+ end
+
+ files << path.gsub(Rails.root, '') if File.extname(path) == extname
+ end
+
+ files.sort
+ end
+ end
+end
100 lib/tasks/assemble.rake
@@ -0,0 +1,100 @@
+require 'find'
+
+namespace :assemble do
+ task :all => [ :js, :css ]
+
+ task :js do
+ paths = get_top_level_directories("public/javascripts")
+ targets = []
+
+ paths.each do |bundle_directory|
+ bundle_name = bundle_directory.basename
+ files = recursive_file_list(bundle_directory, ".js")
+
+ next if files.empty? || bundle_name == "dev"
+
+ target = execute_closure(files, bundle_name)
+
+ targets << target
+ end
+
+ targets.each do |target|
+ puts "=> Assembled JavaScript at: #{target}"
+ end
+ end
+
+ task :css do
+ paths = get_top_level_directories("public/stylesheets")
+ targets = []
+
+ paths.each do |bundle_directory|
+ bundle_name = bundle_directory.basename
+ files = recursive_file_list(bundle_directory, ".css")
+
+ next if files.empty? || bundle_name == 'dev'
+
+ bundle = ""
+
+ files.each do |file_path|
+ bundle << File.read(file_path) << "\n"
+ end
+
+ target = execute_yui_compressor(bundle, bundle_name)
+
+ targets << target
+ end
+
+ targets.each do |target|
+ puts "=> Assembled CSS at: #{target}"
+ end
+ end
+
+ private
+
+ def execute_closure(files, bundle_name)
+ jar = File.join(File.dirname(__FILE__), "..", "..", "bin", "closure-compiler.jar")
+ target = Rails.root.join("public/javascripts/bundle_#{bundle_name}.js")
+
+ `java -jar #{jar} #{files.join(" --js ")} --js_output_file #{target}`
+
+ return target
+ end
+
+ def execute_yui_compressor(bundle, bundle_name)
+ jar = File.join(File.dirname(__FILE__), "..", "..", "bin", "yui-compressor.jar")
+ target = Rails.root.join("public/stylesheets/bundle_#{bundle_name}.css")
+ temp_file = "/tmp/bundle_raw.css"
+
+ File.open(temp_file, 'w') { |f| f.write(bundle) }
+
+ `java -jar #{jar} --line-break 0 #{temp_file} -o #{target}`
+
+ return target
+ end
+
+ def recursive_file_list(basedir, ext)
+ files = []
+
+ Find.find(basedir) do |path|
+ if FileTest.directory?(path)
+ if File.basename(path)[0] == ?. # Skip dot directories
+ Find.prune
+ else
+ next
+ end
+ end
+
+ files << path if File.extname(path) == ext
+ end
+
+ files.sort
+ end
+
+ def get_top_level_directories(base_path)
+ Dir.entries(Rails.root.join(base_path)).collect do |path|
+ path = Rails.root.join("#{base_path}/#{path}")
+
+ File.basename(path)[0] == ?. || !File.directory?(path) ? nil : Pathname.new(path) # not dot directories or files
+ end - [nil]
+ end
+end
8 test/assemblage_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AssemblageTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
3 test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_support'

0 comments on commit 22a93a9

Please sign in to comment.
Something went wrong with that request. Please try again.