![]() |
Articles Feed |
Categories
Archives
- July 2010 (5)
- June 2010 (4)
- April 2010 (3)
- March 2010 (2)
- February 2010 (2)
- January 2010 (1)
- December 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (1)
- July 2009 (5)
- June 2009 (2)
- May 2009 (2)
- April 2009 (8)
- March 2009 (7)
- January 2009 (2)
- December 2008 (3)
- November 2008 (5)
- October 2008 (4)
- September 2008 (6)
- August 2008 (4)
- July 2008 (5)
- June 2008 (5)
- May 2008 (4)
- April 2008 (2)
- February 2008 (4)
- January 2008 (2)
- December 2007 (2)
- November 2007 (2)
- October 2007 (2)
- September 2007 (1)
- August 2007 (3)
- July 2007 (1)
- June 2007 (4)
- May 2007 (7)
- April 2007 (2)
- February 2007 (3)
- January 2007 (3)
- November 2006 (3)
- October 2006 (3)
- September 2006 (17)
- November 2004 (1)
Copyrights Conundrum
by: micah | March 12th, 2009 |
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 files. 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.
Installing
It’s just a matter of installing the gem hosted on Github.
- gem sources -a http://gems.github.com
- sudo gem install slagyr-mmcopyrights
Update: The github shenanigans is not longer needed. To install, simply:
- sudo gem install mmcopyrights
Usage
Let’s say you want to add the following copyright header to all .rb files in the lib directory…
- require 'mmcopyrights'
- MM::Copyrights.process("lib", "rb", "#-", "©2009 Micah Martin\nAll rights reserved")
And all the ruby files will look like this:
- #- ©2009 Micah Martin
- #- All rights reserved
- ... ruby code ...
Typically I keep the copyright text in a separate file and write the following Rake task…
- task :copyrights do
- require 'mmcopyrights'
- MM::Copyrights.process('lib', "rb", "#-", IO.read('copyrights.txt'))
- end
or the following ant task if need be…
- <target name="copyrights">
- <exec command="ruby">
- <arg value="-rrubygems" />
- <arg value="-e" />
- <arg value="require 'mmcopyrights'; MM::Copyrights.process('src', 'java', '//-', IO.read('copyright.txt'))" />
- </exec>
- </target>
Details
If you haven’t guessed, MM::Copyrights::process takes 4 parameters.
- The path of the directory containing source code.
- The extension of source file to be processed.
- The comment prefix. Note that there’s an extra -. This is because we want the copyright comments to be unique from all other comments. The tool will search for this unique prefix to find existing copyright headers.
- The copyright text, without the comment prefix.
That’s about it. Feel free to run MM::Copyrights::process 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.
