require 'tempfile' require 'fileutils' COPYRIGHT=%Q( ## # Author: AUTHOR NAME # Copyright: Copy right message # ) root_dir=ARGV[0] || '.' Dir.glob("#{root_dir}/{**/}*.rb") do |path| puts ">>> Prepending header for #{path} ..." tempfile = Tempfile.new('with_copyright') lines =File.open(path).readlines line='' while((line=lines.shift) && (line.strip[0]=='#')) do tempfile.write(line) end tempfile.write(COPYRIGHT) tempfile.write(line) tempfile.write(lines.join) tempfile.flush tempfile.close FileUtils.cp(tempfile.path,path) end