Skip to content

Instantly share code, notes, and snippets.

@justincase
Forked from kneath/_README.md
Created June 28, 2010 08:12

Revisions

  1. @kneath kneath revised this gist Nov 19, 2009. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions production.rb
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,6 @@
    end
    end

    GitHub::MasterSHA = File.read("#{RAILS_ROOT}/.git/refs/heads/master").chomp

    repo = Grit::Repo.new(RAILS_ROOT)
    js = repo.log('master', 'public/javascripts', :max_count => 1).first
    css = repo.log('master', 'public/stylesheets', :max_count => 1).first
  2. @kneath kneath revised this gist Nov 18, 2009. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    desc "Shrink and bundle js and css"
    task :bundle, :roles => :web, :except => { :no_release => true } do
    run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:all"
    namespace :deploy do
    desc "Shrink and bundle js and css"
    task :bundle, :roles => :web, :except => { :no_release => true } do
    run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:all"
    end
    end

    after "deploy:update_code", "deploy:bundle"
  3. @kneath kneath revised this gist Nov 18, 2009. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    desc "Shrink and bundle js and css"
    task :bundle, :roles => :web, :except => { :no_release => true } do
    run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:all"
    end

    after "deploy:update_code", "deploy:bundle"
  4. @kneath kneath revised this gist Nov 18, 2009. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions production.rb
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,6 @@
    end
    end

    require "#{RAILS_ROOT}/config/environments/constants/#{RAILS_ENV}"

    GitHub::MasterSHA = File.read("#{RAILS_ROOT}/.git/refs/heads/master").chomp

    repo = Grit::Repo.new(RAILS_ROOT)
  5. @kneath kneath revised this gist Nov 18, 2009. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions production.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    config.action_controller.asset_host = Proc.new do |source, request|
    non_ssl_host = "http://assets#{source.hash % 4}.github.com"
    ssl_host = "https://assets#{source.hash % 4}.github.com"

    if request.ssl?
    if source =~ /\.js$/
    ssl_host
    elsif request.headers["USER_AGENT"] =~ /(Safari)/
    non_ssl_host
    else
    ssl_host
    end
    else
    non_ssl_host
    end
    end

    require "#{RAILS_ROOT}/config/environments/constants/#{RAILS_ENV}"

    GitHub::MasterSHA = File.read("#{RAILS_ROOT}/.git/refs/heads/master").chomp

    repo = Grit::Repo.new(RAILS_ROOT)
    js = repo.log('master', 'public/javascripts', :max_count => 1).first
    css = repo.log('master', 'public/stylesheets', :max_count => 1).first

    ENV['RAILS_ASSET_ID'] = js.committed_date > css.committed_date ? js.id : css.id
  6. @kneath kneath revised this gist Nov 18, 2009. 2 changed files with 127 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions _README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # GitHub Javascript Strategy

    Unless otherwise necessary (such as mobile development), the GitHub javascript codebase is based off jQuery. You can safely assume it will be included on every page.

    ## File naming

    * All jquery plugins should be prefixed with jquery, such as `jquery.facebox`
    * All github-specific jquery plugins should be prefixed with jquery.github. Like `jquery.github.repo_list.js`
    * All page-specific files (that only run on ONE page) should be prefixed with page. `page.billing.js`

    ## File structure

    All javascript for the site should be put into a directory. Feel free to create new directories when it makes sense (gist, iphone, etc). There are a few directories that have special meanings:

    * `common` - Contains all files that are used and shared between the main site and sub-sites (like gist)
    * `rogue` - Files that are directly called by individual pages. Use for large libraries that are only used on one (or a very few) pages.
    * `dev` - Files that are hosted elsewhere (such as google) that you will need if you do not have an internet connection.

    Inside of each directory, you should note the difference between plugins and files where possible and split them into different directories (see `github`).

    ## Deployment

    On deploy, each top level directory is bundled into a compressed file. Any files that are inside the directory (and it's subfolders) will be bundled into one file and available at `bundle_[directory].js`

    ## Template inclusion

    If you would like to include a bundle in your Rails templates, you reference bundles or individual files using the `javascript_bundle` and `javascript_dev` helpers.

    ### Example

    javascript_dev ['jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js']
    javascript_bundle 'common', 'github', 'rogue/s3_upload'

    In production, this will include:

    * `http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js` in production.
    * `bundle_common.js`
    * `bundle_github.js`
    * `rogue/s3_upload.js`

    In development, this will include:

    * `dev/jquery.js`
    * All javascripts inside of the `common` directory
    * All javascripts inside the `github` directory
    * `rogue/s3_upload.js`
    81 changes: 81 additions & 0 deletions bundle_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    require 'find'
    module BundleHelper
    def bundle_files?
    Rails.production? || Rails.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.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 + "/" + 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
  7. @kneath kneath revised this gist Nov 18, 2009. 1 changed file with 67 additions and 33 deletions.
    100 changes: 67 additions & 33 deletions bundle.rake
    Original file line number Diff line number Diff line change
    @@ -1,53 +1,87 @@
    RAILS_ROOT ||= ENV["RAILS_ROOT"]

    namespace :bundle do
    js_bundle = Rails.root + "/public/javascripts/bundle.js"
    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js"].sort
    task :all => [ :js, :css ]

    task :all => [ js_bundle, :gist_js, :css ]
    task :js do
    compression_method = "closure"

    file js_bundle => js_files do |task|
    require 'lib/js_minimizer'
    require 'lib/js_minimizer' if compression_method != "closure"
    closure_path = RAILS_ROOT + '/lib/closure_compressor.jar'

    File.open(task.name, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(task.prerequisites)
    paths = get_top_level_directories('/public/javascripts')

    targets = []
    paths.each do |bundle_directory|
    bundle_name = bundle_directory.gsub(RAILS_ROOT + '/public/javascripts/', "")
    files = recursive_file_list(bundle_directory, ".js")
    next if files.empty? || bundle_name == 'dev'
    target = RAILS_ROOT + "/public/javascripts/bundle_#{bundle_name}.js"

    if compression_method == "closure"
    `java -jar #{closure_path} --js #{files.join(" --js ")} --js_output_file #{target} 2> /dev/null`
    else
    File.open(target, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(*files)
    end
    end
    targets << target
    end

    puts "=> bundled js at #{task.name}"
    targets.each do |target|
    puts "=> bundled js at #{target}"
    end
    end

    task :gist_js do
    require 'lib/js_minimizer'
    paths = []
    paths += Dir[RAILS_ROOT + '/public/javascripts/gist/*.js'].sort
    paths = paths.flatten
    task :css do
    yuipath = RAILS_ROOT + '/lib/yuicompressor-2.4.1.jar'

    target = RAILS_ROOT + '/public/javascripts/gist.js'
    paths = get_top_level_directories('/public/stylesheets')

    File.open(target, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(*paths)
    end
    targets = []
    paths.each do |bundle_directory|
    bundle_name = bundle_directory.gsub(RAILS_ROOT + '/public/stylesheets/', "")
    files = recursive_file_list(bundle_directory, ".css")
    next if files.empty? || bundle_name == 'dev'

    puts "=> bundled gist js at #{target}"
    end
    bundle = ''
    files.each do |file_path|
    bundle << File.read(file_path) << "\n"
    end

    task :css do
    paths = []
    paths += Dir[RAILS_ROOT + '/public/stylesheets/*.css'].reject { |path| path =~ /bundle|admin|staff|scaffold|iui/ }.sort
    target = RAILS_ROOT + "/public/stylesheets/bundle_#{bundle_name}.css"
    rawpath = "/tmp/bundle_raw.css"
    File.open(rawpath, 'w') { |f| f.write(bundle) }
    `java -jar #{yuipath} --line-break 0 #{rawpath} -o #{target}`

    bundle = ''
    paths.flatten.each do |path|
    bundle << File.read(path) << "\n"
    targets << target
    end

    rawpath = "/tmp/bundle_raw.css"
    minpath = "/tmp/bundle_min.css"
    bundlepath = RAILS_ROOT + "/public/stylesheets/bundle.css"
    yuipath = RAILS_ROOT + '/lib/yuicompressor-2.4.1.jar'
    targets.each do |target|
    puts "=> bundled css at #{target}"
    end
    end

    File.open(rawpath, 'w') { |f| f.write(bundle) }
    `java -jar #{yuipath} --line-break 0 #{rawpath} -o #{bundlepath}`
    require 'find'
    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

    puts "=> bundled css at #{bundlepath}"
    def get_top_level_directories(base_path)
    Dir.entries(RAILS_ROOT + base_path).collect do |path|
    path = RAILS_ROOT + "#{base_path}/#{path}"
    File.basename(path)[0] == ?. || !File.directory?(path) ? nil : path # not dot directories or files
    end - [nil]
    end
    end
    end
  8. @jbarnette jbarnette revised this gist Aug 26, 2009. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions bundle.rake
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,19 @@
    RAILS_ROOT ||= ENV["RAILS_ROOT"]

    namespace :bundle do
    task :all => [ :js, :gist_js, :css ]
    js_bundle = Rails.root + "/public/javascripts/bundle.js"
    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js"].sort

    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js"].sort
    task :all => [ js_bundle, :gist_js, :css ]

    task :js => js_files do |task|
    file js_bundle => js_files do |task|
    require 'lib/js_minimizer'

    target = RAILS_ROOT + '/public/javascripts/bundle.js'

    File.open(target, 'w+') do |f|
    File.open(task.name, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(task.prerequisites)
    end

    puts "=> bundled js at #{target}"
    puts "=> bundled js at #{task.name}"
    end

    task :gist_js do
  9. @jbarnette jbarnette revised this gist Aug 26, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bundle.rake
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ RAILS_ROOT ||= ENV["RAILS_ROOT"]
    namespace :bundle do
    task :all => [ :js, :gist_js, :css ]

    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js].sort
    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js"].sort

    task :js => js_files do |task|
    require 'lib/js_minimizer'
  10. @jbarnette jbarnette revised this gist Aug 26, 2009. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions bundle.rake
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,15 @@ RAILS_ROOT ||= ENV["RAILS_ROOT"]
    namespace :bundle do
    task :all => [ :js, :gist_js, :css ]

    task :js do
    js_files = Dir[Rails.root + "/public/javascripts/{github,jquery}.*.js].sort
    task :js => js_files do |task|
    require 'lib/js_minimizer'
    paths = []
    paths += Dir[RAILS_ROOT + '/public/javascripts/jquery.*.js'].sort
    paths += Dir[RAILS_ROOT + '/public/javascripts/github.*.js'].sort
    paths = paths.flatten
    target = RAILS_ROOT + '/public/javascripts/bundle.js'
    File.open(target, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(*paths)
    f.puts JSMinimizer.minimize_files(task.prerequisites)
    end
    puts "=> bundled js at #{target}"
  11. @defunkt defunkt created this gist Aug 26, 2009.
    56 changes: 56 additions & 0 deletions bundle.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    RAILS_ROOT ||= ENV["RAILS_ROOT"]

    namespace :bundle do
    task :all => [ :js, :gist_js, :css ]

    task :js do
    require 'lib/js_minimizer'
    paths = []
    paths += Dir[RAILS_ROOT + '/public/javascripts/jquery.*.js'].sort
    paths += Dir[RAILS_ROOT + '/public/javascripts/github.*.js'].sort
    paths = paths.flatten

    target = RAILS_ROOT + '/public/javascripts/bundle.js'

    File.open(target, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(*paths)
    end

    puts "=> bundled js at #{target}"
    end

    task :gist_js do
    require 'lib/js_minimizer'
    paths = []
    paths += Dir[RAILS_ROOT + '/public/javascripts/gist/*.js'].sort
    paths = paths.flatten

    target = RAILS_ROOT + '/public/javascripts/gist.js'

    File.open(target, 'w+') do |f|
    f.puts JSMinimizer.minimize_files(*paths)
    end

    puts "=> bundled gist js at #{target}"
    end

    task :css do
    paths = []
    paths += Dir[RAILS_ROOT + '/public/stylesheets/*.css'].reject { |path| path =~ /bundle|admin|staff|scaffold|iui/ }.sort

    bundle = ''
    paths.flatten.each do |path|
    bundle << File.read(path) << "\n"
    end

    rawpath = "/tmp/bundle_raw.css"
    minpath = "/tmp/bundle_min.css"
    bundlepath = RAILS_ROOT + "/public/stylesheets/bundle.css"
    yuipath = RAILS_ROOT + '/lib/yuicompressor-2.4.1.jar'

    File.open(rawpath, 'w') { |f| f.write(bundle) }
    `java -jar #{yuipath} --line-break 0 #{rawpath} -o #{bundlepath}`

    puts "=> bundled css at #{bundlepath}"
    end
    end