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.
Let’s say you want to add the following copyright header to all
.rb files in the lib directory…
And all the ruby files will look like this:
Typically I keep the copyright text in a separate file and write the following Rake task:
Or I write the following ant task if need be:
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.