Skip to content

Instantly share code, notes, and snippets.

@archfear
Created August 4, 2010 23:20
Show Gist options
  • Save archfear/508967 to your computer and use it in GitHub Desktop.
Save archfear/508967 to your computer and use it in GitHub Desktop.
namespace :assets do
namespace :js do
desc "Package JavaScript"
task :package do
puts "Packaging path.js..."
repackage_javascript
end
namespace :package do
task "pre-commit" do
repackage_js = false
modified_files = `git status -s`
modified_files.split("\n").each do |file_status|
if file_status =~ %r{^\w. app/javascripts/.*\.js\s*$}
repackage_js = true
break
end
end
if repackage_js
puts "JavaScript has been modified. Repackaging path.js..."
repackage_javascript
`git add #{Rails.root}/public/javascripts/path.js`
end
end
end
end
task "git:pre-commit:append" do
begin
puts "Appending asset packaging logic to .git/hooks/pre-commit ..."
if File.exists?(".git/hooks/pre-commit")
precommit = File.read(".git/hooks/pre-commit")
precommit.sub!(/exit 0\s*$/, '')
else
precommit = ""
end
File.open(".git/hooks/pre-commit", "w") do |file|
file.puts precommit
file.puts <<-TEXT
exec rake assets:pre-commit
exit 0
TEXT
end
File.chmod(0755, ".git/hooks/pre-commit")
rescue Errno::ENOENT
abort "You have to run git init first!"
end
end
namespace :css do
namespace :package do
task "pre-commit", :needs => [:environment] do
puts "Regenerating style.css..."
Sass::Plugin.options[:style] = :compressed
Rake::Task['sass:sass2css'].execute
`cd #{Rails.root} && git add -f #{Rails.root}/public/stylesheets/style.css`
end
end
end
task 'pre-commit' => ['assets:js:package:pre-commit', 'assets:css:package:pre-commit']
def repackage_javascript
closure_path = File.join(Rails.root, '/lib/closure_compiler.jar')
uncompressed_js_file = File.join(Rails.root, '/public/sprockets.js')
compressed_js_file = File.join(Rails.root, '/public/javascripts/path.js')
`rake sprockets:install_script`
`java -jar #{closure_path} --js #{uncompressed_js_file} --js_output_file #{compressed_js_file}`
FileUtils.rm_f uncompressed_js_file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment