Monday, August 25, 2008

Adding a header-comment to all java-files in a repository

I had to add a header comment to all java files in a given repository.
Not knowing any better solution, I wrote a groovy script:
 1 def delClos
 2 delClos = { println "Checking directory: ${it.canonicalPath}";
 3     it.eachDir( delClos );
 4     it.eachFile {
 5         if(it.name =~ ~/.*\.java$/){
 6             def original_text = it.text;
 7             if (!original_text.startsWith('/*')){
 8                 println "Adding header to file: ${it.canonicalPath}";
 9                 def className = it.name.substring(0, it.name.length() - 5);
10                 it.delete()
11                 def new_file = new File (it.canonicalPath)
12                 new_file.createNewFile();
13                 new_file << '/*'                            <<'\n'
14                 new_file << ' * ' << className              <<'\n'
15                 new_file << ' *'                            <<'\n' 
16                 new_file << ' * Version $Revision$, $Date$' <<'\n'
17                 new_file << ' *'                            <<'\n'
18                 new_file << ' * Project:  Mayhem'           <<'\n'
19                 new_file << ' *'                            <<'\n' 
20                 new_file << ' * (c) 2008 by Tyler Durden'   <<'\n'
21                 new_file << ' */'                           <<'\n' 
22                 new_file << original_text;
23             }
24         }               
25     }
26 }
27 delClos( new File("."))
This is certainly not the most elegant, clever, performant ... whatsoever solution, but it did the job. If you have a better solution, please leave a comment.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...