namespace "check" do desc "Checks license headers." task("headers") do # Define license headers based on the filename extension. licenses = {} licenses[".java"] = < EOF licenses[".bpel"] = licenses[".wsdl"] = licenses[".xml"] # This also tells us which files to look at. extensions = licenses.keys.join(",") count = FileList["**/*{#{extensions}}"].inject(0) do |count, filename| if File.readlines(filename)[0..3].join !~ /Licensed to the Apache Software Foundation/ when_writing "Missing header in #{filename}" do # Figure the license from the file, inject it into the file and rewrite it. license = licenses[filename.pathmap("%x")] modified = File.read(filename).sub(/(<\?xml .*\?>\n?)(.*)/m) { "#{$1}#{license}#{$2}" } File.open(filename, "w") { |file| file.write modified } count + 1 end else count end end if count > 0 warn "#{count} files found to have missing headers." else puts "All #{extensions} files checked and have the license in them." end end end