Skip to content
Browse files

Initial commit after extraction from rails app

  • Loading branch information...
0 parents commit afb002a88fcf6054ca76adfac2c4f73231a8d390 Matthew Savage committed Oct 1, 2009
Showing with 199 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +2 −0 init.rb
  5. +1 −0 install.rb
  6. +13 −0 lib/asset_packager_overrides.rb
  7. +62 −0 lib/heroku_asset_packager.rb
  8. +4 −0 tasks/heroku_asset_packager_tasks.rake
  9. +57 −0 test/heroku_asset_packager_test.rb
  10. +3 −0 test/test_helper.rb
  11. +1 −0 uninstall.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+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.
13 README
@@ -0,0 +1,13 @@
+HerokuAssetPackager
+===================
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the heroku_asset_packager plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the heroku_asset_packager plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HerokuAssetPackager'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
2 init.rb
@@ -0,0 +1,2 @@
+require 'asset_packager_overrides'
+ActionController::Dispatcher.middleware.use HerokuAssetPackager
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
13 lib/asset_packager_overrides.rb
@@ -0,0 +1,13 @@
+Synthesis::AssetPackage.class_eval do
+ private
+ def create_new_build
+ new_path = $mega_asset_base_path ? $mega_asset_base_path : @asset_path
+ new_build_path = "#{new_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
+end
62 lib/heroku_asset_packager.rb
@@ -0,0 +1,62 @@
+class HerokuAssetPackager
+ @@regex_pattern = /\/\w+\/(.*)_packaged.*/i
+
+ def initialize_asset_packager
+ # Make dirs
+ unless File.directory? heroku_file_location
+ Dir.mkdir(heroku_file_location)
+ end
+
+ # Set asset base path
+ # TODO: Check to see if this is a globally used thing?
+ $mega_asset_base_path = heroku_file_location
+ Synthesis::AssetPackage.build_all
+
+ end
+
+ def initialize(app)
+ @app = app
+ initialize_asset_packager
+ end
+
+ def call(env)
+ @env = env
+ return render_css if env['REQUEST_PATH'] =~ /\/stylesheets\/.*_packaged.css/i
+ return render_js if env['REQUEST_PATH'] =~ /\/javascripts\/.*_packaged.js/i
+
+ @app.call(env)
+ end
+
+ def render_js
+ file_name = @@regex_pattern.match(@env['REQUEST_PATH'])[1]
+
+ file = "#{heroku_file_location}/#{file_name}_packaged.js"
+ [
+ 200,
+ {
+ 'Cache-Control' => 'public, max-age=86400',
+ 'Content-Length' => File.size(file).to_s,
+ 'Content-Type' => 'text/javascript'
+ },
+ File.read(file)
+ ]
+ end
+
+ def render_css
+ file_name = @@regex_pattern.match(@env['REQUEST_PATH'])[1]
+ file = "#{heroku_file_location}/#{file_name}_packaged.css"
+ [
+ 200,
+ {
+ 'Cache-Control' => 'public, max-age=86400',
+ 'Content-Length' => File.size(file).to_s,
+ 'Content-Type' => 'text/css'
+ },
+ File.read(file)
+ ]
+ end
+
+ def heroku_file_location
+ "#{RAILS_ROOT}/tmp/asset_packager"
+ end
+end
4 tasks/heroku_asset_packager_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :heroku_asset_packager do
+# # Task goes here
+# end
57 test/heroku_asset_packager_test.rb
@@ -0,0 +1,57 @@
+require 'test_helper'
+# require 'mocha'
+
+class HerokuAssetPackagerTest < ActiveSupport::TestCase
+ def test_css_call
+ raise "Test"
+ env = {"REQUEST_PATH" => "/stylesheets/test_packaged.css"}
+
+ app = object.new
+
+ Synthesis::AssetPackager.expects(:build_all)
+ hap = HerokuAssetPackager.new(app)
+
+ assert !$mega_asset_base_path.nil?
+ hap.expects(:render_css)
+
+ hap.call(env)
+ assert_equal true, false
+ end
+
+ def test_js_call
+ raise "Test"
+ env = {"REQUEST_PATH" => "/javascripts/test_packaged.js"}
+
+ app = object.new
+
+ Synthesis::AssetPackager.expects(:build_all)
+ hap = HerokuAssetPackager.new(app)
+
+ assert_equal false, $mega_asset_base_path.nil?
+ hap.expects(:render_js)
+
+ hap.call(env)
+ end
+
+ def test_regular_js_call
+ raise "Test"
+ env = {"REQUEST_PATH" => "/javascripts/test.js"}
+
+ app = object.new
+ app.expects(:call).with(env)
+
+ Synthesis::AssetPackager.expects(:build_all)
+ hap = HerokuAssetPackager.new(app)
+
+ hap.call(env)
+ end
+end
+
+
+
+
+module Synthesis
+ class AssetPackage
+
+ end
+end
3 test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
1 uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit afb002a

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