Here’s the situation. You’ve spent days working on an open source project. Your sweat, blood, and tears have been poured into thousands of lines of code spread through countless file in a sprawling tree of directories. The final step is to release your masterpiece into the wild.
So you choose your open source license and then it hits you; you realize that need to add a copyright header to each and every source code file.
How tedious! You google for a tool that will automatically add the headers for you, and you find nothing satisfactory. In the end, you write your own script to add the headers for you.
I’ve been in this exact situation and written such a script too many times. My most recent experience and script implementation will be the last.
I’ve dubbed this last imeplementation MM Copyrights and you’re welcome to use it.
MM Copyrights is a simple Ruby gem that will search a directory for source code files, inserting or updating a header comment in each file.
It’s just a matter of installing the gem hosted on Github.
1 sudo gem install mmcopyrights
Let’s say you want to add the following copyright header to all
.rb files in the lib directory…
1 require 'mmcopyrights' 2 MM::Copyrights.process("lib", "rb", "#-", "©2009 Micah Martin\nAll rights reserved")
And all the ruby files will look like this:
1 #- ©2009 Micah Martin 2 #- All rights reserved 3 4 ... ruby code ...
Typically I keep the copyright text in a separate file and write the following Rake task:
1 task :copyrights do 2 require 'mmcopyrights' 3 MM::Copyrights.process('lib', "rb", "#-", IO.read('copyrights.txt')) 4 end
Or I write the following ant task if need be:
1 <target name="copyrights"> 2 <exec command="ruby"> 3 <arg value="-rrubygems" /> 4 <arg value="-e" /> 5 <arg value="require 'mmcopyrights'; MM::Copyrights.process('src', 'java', 6 '//-', IO.read('copyright.txt'))" /> 7 </exec> 8 </target>
If you haven’t guessed,
MM::Copyrights::process takes 4
- The path of the directory containing source code.
- The extension of source file to be processed.
- The comment prefix. Note that there’s a unique prefix the tool will use to find existing copyright headers.
- The copyright text, without the comment prefix.
That’s about it. Feel free to run
multiple times on the same code base. It’s harmless. And when the new year
rolls around, just change the copyright text and rerun the tool. It will
update existing headers.