Copying assets intelligently
If you don't want to use nanoc's asset-compiling functionality because you only need to copy them and not filter them, you can use rsync to copy assets instead.
Using rsync
rsync is useful because it makes sure assets are only copied when they’re not copied already, for instance. It can also prevent subversion directories from being copied. To use rsync, create a Rake task which does something along these lines:
task :compile do system "rsync -rpgvP --exclude='.svn' assets/ output" system "nanoc co" end
You may want to play with the options, but the default set of options (the one used above) is probably good enough.
If you want, you can make the compile task from above be the default task, so you can simply type rake, which is even shorter than nanoc co (or nanoc compile), like this:
task :default => :compile
Using Ruby
Another way to do it is the following quick 'n dirty hack in Ruby code:
%w{yaml}.each{|lib| require lib}
# a rake task to copy any css and javascript
# files over to the webroot output directory
config = YAML.load(File.open("config.yaml"))
def path_tree(path,to_copy=[])
tree = []
Dir.glob("#{path}/*").each do |path|
if File.directory?(path)
tree << path_tree(path)
else
tree << path
path_tree(path)
end
end
tree.flatten
end
task :default do
config["asset_dirs"].each do |search_dir|
path_tree(search_dir).each do |asset|
# Some vars
from = asset
to = [config["output_dir"],asset].join("/")
# Create dir for file, if necessary;
# e.g., for /path/to/file.txt
# create /path/to, if need be
if !File.exist?(File.dirname(to))
puts "mkdir -p #{File.dirname(to)}"
File.makedirs(File.dirname(to))
end
# Copy the file over, but only if it's changed or doesn't already exist
if !File.exist?(to) || !File.compare(from,to)
puts "cp #{from} #{to}"
File.copy(from,to)
end
end
end
end
Put something like this in your config.yaml:
asset_dirs: ["css", "js"]
Where each member of the array is a subdir of the nanoc root directory that you want copied over.
