Created
November 23, 2011 07:24
-
-
Save bradfordcp/1388095 to your computer and use it in GitHub Desktop.
ERB usage in a rake task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
require 'erb' | |
desc 'Builds manifest.js files for models, and collections.' | |
task :build_class_manifests do | |
STDOUT.sync = true | |
# Build Class Manifests First | |
classes = ['initializers', 'config', 'models', 'collections'] | |
classes.each do |aclass| | |
puts "Building #{aclass.capitalize} Manifest" | |
File.open(File.join('www', 'scripts', aclass, 'manifest.js'), 'w') { |build_file| | |
# Place to store all of the models | |
files = [] | |
# Grab all of the .js files | |
Dir.glob(File.join('www', 'scripts', aclass, '**', '*.js')) { |path| | |
parts = File.split(path) | |
files << File.join(parts[0].split(File::SEPARATOR) - ['www', 'scripts'] + [File.basename(parts[1], '.js')]) unless parts[1] == 'manifest.js' | |
} | |
template = <<-EOF | |
// Automatically Generated File | |
define( | |
// require shovel | |
[ | |
'common/shovel', | |
<% files.each do |file| %>'<%= file %>'<%= ',\n ' unless file == files.last %><% end %> | |
], | |
function (shovel){ | |
shovel.debug('Loaded <%= aclass.capitalize %>'); | |
$(document).trigger('shovel_<%= aclass %>_loaded'); | |
} | |
); | |
EOF | |
buffer = ERB.new(template).result(binding) | |
build_file.puts buffer | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment