Skip to content

Instantly share code, notes, and snippets.

@bradfordcp
Created November 23, 2011 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradfordcp/1388095 to your computer and use it in GitHub Desktop.
Save bradfordcp/1388095 to your computer and use it in GitHub Desktop.
ERB usage in a rake task
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