Skip to content

Instantly share code, notes, and snippets.

@robballou
Created September 7, 2012 15:13
Show Gist options
  • Save robballou/3667048 to your computer and use it in GitHub Desktop.
Save robballou/3667048 to your computer and use it in GitHub Desktop.
Compress rakefile
# =====================================================================
# File definitions
# =====================================================================
js = [
{
:target => "target.js",
:minify => true,
:files => [
"source1.js",
"source2.js",
]
}
]
css = [
{
:target => "target.css",
:minify => true,
:files => [
"source1.css",
"source2.css",
]
}
]
# =====================================================================
# Tasks
# =====================================================================
task :compress do |t|
Rake::Task['compress_css'].execute
Rake::Task['compress_js'].execute
end
task :compress_css do |t|
css.each do |target|
if File.exists? target[:target]
File.unlink target[:target]
end
# open the target file
File.open(target[:target], 'w') do |file|
target[:files].each do |source_file|
file.write "\n/* #{source_file} */\n"
if target[:minify]
file.write `yuicompressor #{source_file}`
else
file.write `cat #{source_file}`
end
end
end
end
end
task :compress_js do |t|
js.each do |target|
if File.exists? target[:target]
File.unlink target[:target]
end
# open the target file
File.open(target[:target], 'w') do |file|
target[:files].each do |source_file|
file.write "\n/* #{source_file} */\n"
if target[:minify] and not source_file.end_with? ".min.js"
file.write `yuicompressor #{source_file}`
else
file.write `cat #{source_file}`
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment