package com.starlight.tools;
import com.starlight.IOKit;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Simple app to find and
*/
public class TCFileRegexCounter {
public static void main( String[] args ) throws IOException {
if ( args.length < 2 ) {
System.out.println(
"Usage: TCFileRegexCounter <regex> <output_dir> <search_dirs...>");
return;
}
Pattern pattern = Pattern.compile( args[ 0 ], Pattern.CASE_INSENSITIVE );
System.out.println( "Pattern: " + args[ 0 ] );
File output_dir = new File( args[ 1 ] );
File[] search_dirs = new File[ args.length - 2 ];
for( int i = 0; i < search_dirs.length; i++ ) {
search_dirs[ i ] = new File( args[ i + 2 ] );
}
List<MatchInfo> matches = new ArrayList<MatchInfo>();
for( File dir : search_dirs ) {
searchDir( pattern, dir, matches, "", dir );
}
System.out.println( "Matches: " + matches.size() );
for( MatchInfo match : matches ) {
System.out.println( match.file.getName() + " : " + match.line + " - " +
match.getText() );
}
writeInfoFile( output_dir, matches );
writeDetailFile( output_dir, matches );
}
private static void searchDir( Pattern pattern, File dir, List<MatchInfo> matches,
String indent, File base_dir ) throws IOException {
if ( dir.isHidden() ) return;
if ( dir.getName().startsWith( "." ) ) return;
// System.out.println( indent + dir.getPath() );
if ( !dir.exists() ) {
System.out.println( "WARNING: Search dir doesn't exist: " + dir );
return;
}
File[] files = dir.listFiles();
Arrays.sort( files, new Comparator<File>() {
@Override public int compare( File o1, File o2 ) {
if ( o1.isDirectory() ) {
if ( !o2.isDirectory() ) return -1;
}
else if ( o2.isDirectory() ) return 1;
return String.CASE_INSENSITIVE_ORDER.compare( o1.getName(),
o2.getName() );
}
} );
for( File file : files ) {
if ( file.isHidden() ) continue;
if ( file.isDirectory() ) {
searchDir( pattern, file, matches, indent + " ", base_dir );
}
else {
// System.out.println( indent + file.getPath() );
searchFile( pattern, file, matches, base_dir );
}
}
}
private static void searchFile( Pattern pattern, File file,
List<MatchInfo> matches, File base_dir ) throws IOException {
// For now, only handle java files
if ( !file.getName().endsWith( ".java" ) ) return;
BufferedReader in = null;
try {
in = new BufferedReader( new FileReader( file ) );
Matcher matcher = null;
String line;
int line_number = 0;
while( ( line = in.readLine() ) != null ) {
line_number++;
if ( matcher == null ) matcher = pattern.matcher( line );
else matcher.reset( line );
if ( matcher.find() ) {
int start_index = matcher.regionStart();
String text = line.substring( start_index + 4 );
if ( text.startsWith( ":" ) ) text = text.substring( 1 );
text = text.trim();
matches.add( new MatchInfo( file, line_number, text, base_dir ) );
}
}
}
finally {
IOKit.close( in );
}
}
private static void writeInfoFile( File output_dir, List<MatchInfo> matches )
throws IOException {
PrintWriter out = null;
try {
out = new PrintWriter(
new FileWriter( new File( output_dir, "teamcity-info.xml" ) ) );
out.println( "<build>" );
out.println( " <statisticValue key=\"todoCount\" value=\"" +
matches.size() + "\"/>" );
out.println( "</build>" );
}
finally {
IOKit.close( out );
}
}
private static void writeDetailFile( File output_dir, List<MatchInfo> matches )
throws IOException {
PrintWriter out = null;
try {
out = new PrintWriter(
new FileWriter( new File( output_dir, "todo_list.html" ) ) );
out.println( "<html>" );
out.println( "<head>" );
out.println( "<script type=\"text/javascript\" " +
"src=\"/js/activation.js\"></script>" );
// out.println( "<link rel=\"stylesheet\" type=\"text/css\" " +
// "href=\"/css/main.css\" />" );
out.println( "<link rel=\"stylesheet\" type=\"text/css\" " +
"href=\"/css/buildLog/buildLog.css\" />" );
out.println( "</head>" );
out.println( "<body>" );
out.println( " <div class=\"log\" id=\"buildLog\">" );
for( MatchInfo match : matches ) {
String path_only = match.getPathRelativeToBase();
path_only = path_only.substring( 0,
( path_only.length() - match.getFile().getName().length() ) - 1 );
out.print( " <div class=\"a1\">" );
out.print( "<u>[" );
out.print( path_only );
out.print( "]</u> <i style='color:#C47003'>" );
out.print( match.getFile().getName() );
out.print( ":" );
out.print( match.getLine() );
out.print( "</i>" );
out.print( "<a href=\"javascript://\" onclick=\"Activator.doOpen(" +
"'file?file=" + match.getPathRelativeToBase().replace( "\\", "/" ) +
"&project=')\"" );
out.print( " title=\"Click top open in active IDE\">" );
// out.println( " <img style=\"vertical-align:middle;\" " +
out.print( "<img src=\"/img/openInIde.gif\">" );
out.print( "</a>" );
out.println( "</div>" );
}
out.println( " </div>" );
out.println( "</body>" );
out.println( "</html>" );
}
finally {
IOKit.close( out );
}
}
private static class MatchInfo {
private final File file;
private final int line;
private final String text;
private final File base_dir;
MatchInfo( File file, int line, String text, File base_dir ) {
this.file = file;
this.line = line;
this.text = text;
this.base_dir = base_dir;
}
public File getFile() {
return file;
}
public int getLine() {
return line;
}
public String getText() {
return text;
}
public String getPathRelativeToBase() {
String path = file.getPath();
String base_path = base_dir.getPath();
if ( path.startsWith( base_path ) ) {
return base_dir.getName() + path.substring( base_path.length() );
}
else return path;
}
}
}