Skip to content

Instantly share code, notes, and snippets.

@joeyAghion
Created February 23, 2011 18:09
Show Gist options
  • Save joeyAghion/840848 to your computer and use it in GitHub Desktop.
Save joeyAghion/840848 to your computer and use it in GitHub Desktop.
Processes a directory of files and ERB templates into a destination directory (within the Rails app's environment).
# DirectoryGenerator.new.generate(Rails.root.join('src'), Rails.root.join('public', 'dest'))
class DirectoryGenerator
include Rails.application.routes.url_helpers
include ActionView::Helpers::TagHelper
default_url_options[:host] = 'www.example.com'
def generate(source, destination)
FileUtils.rmtree(destination)
FileUtils.mkdir_p(destination)
Dir.glob(File.join(source, '*')).each do |path|
pathname = Pathname.new(path)
if pathname.extname == '.erb'
File.open(destination.join(pathname.basename.sub(/\.erb$/, '')), 'w') do |file|
file.puts(ERB.new(File.read(path)).result(binding))
end
else
FileUtils.cp(pathname, File.join(destination, pathname.basename))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment