/* * [UndupVersion.java] * * Summary: ONE-SHOT DO NOT USE remove duplicate at-version. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-04-19 initial version */ package com.mindprod.repair; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * ONE-SHOT DO NOT USE remove duplicate at-version. *

* Removes first instance of (@)version duplicate, But done NOT delete related following lines. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-04-19 initial version * @since 2009-04-19 */ public class UndupVersion { /** * take the version line apart */ private static final Pattern AT_VERSION_FINDER = Pattern.compile( "@[Vv]ersion[ \\-]*(\\d+\\.\\d)" + "[ \\-,]*" + "([12][09]\\d\\d\\-\\d\\d\\-\\d\\d)" + "[\\.,\\-: ]*" + "(.*?)$", Pattern.MULTILINE ); /** * true if proofing, false if actually tidying. */ private static boolean proof; /** * remove dup @versions in file the comments in one file * * @param file Java source file to tidy * * @throws java.io.IOException if can't write logs or update the sourse */ private static void tidyOneFile( final File file ) throws IOException { String big = HunkIO.readEntireFile( file ); Matcher m = AT_VERSION_FINDER.matcher( big ); if ( m.find() ) { int start = m.start(); int end = m.end(); final String ver1 = m.group( 1 ); final String yyyymmdd1 = m.group( 2 ); String changeDescription1 = tidyVersionChangeDescription( m.group( 3 ) ); if ( m.find() ) { final String ver2 = m.group( 1 ); final String yyyymmdd2 = m.group( 2 ); String changeDescription2 = tidyVersionChangeDescription( m.group( 3 ) ); if ( ver1.equals( ver2 ) && yyyymmdd1.equals( yyyymmdd2 ) && changeDescription1.equals( changeDescription2 ) ) { out.println( "dup @version in " + EIO.getCanOrAbsPath( file ) ); // was a dup. Remove the first. big = big.substring( 0, start ) + big.substring( end ); if ( !proof ) { HunkIO.writeEntireFile( file, big ); } } else { out.println( ">>> dup @versions don't match precisely in " + EIO.getCanOrAbsPath( file ) ); out.println( "[" + ver1 + "]" ); out.println( "[" + ver2 + "]" ); out.println( "[" + yyyymmdd1 + "]" ); out.println( "[" + yyyymmdd2 + "]" ); out.println( "[" + changeDescription1 + "]" ); out.println( "[" + changeDescription2 + "]" ); } } } } /** * Tidy a changeDesscription. Remove lead space, dots, stars, dashes, colons, commas * * @param changeDescription untidied version change description line. * * @return tidied change description. */ private static String tidyVersionChangeDescription( String changeDescription ) { String temp = ST.trimLeading( changeDescription, " -.*:," ); temp = ST.trimTrailing( temp, " *" ); return ST.condense( temp ); } /** * prepare java source files by replacing the first header comment with a standard. * When proofing, logs to old.txt and new.txt in the current directory. * * @param args word proof or tidy followed by a list of files and directories to process. * -s mean means process subdirs of subsquently mentioned dirs. * * @throws java.io.IOException if cannot read or write source files. */ public static void main( final String[] args ) throws IOException { final String flavour = args[ 0 ]; if ( flavour.equals( "proof" ) ) { proof = true; } else if ( flavour.equals( "tidy" ) ) { proof = false; } else { throw new IllegalArgumentException( "Usage: UnDupVersion [proof/tidy] [list of dirs/files]" ); } args[ 0 ] = null; final CommandLine files = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "java" ) ); for ( File file : files ) { tidyOneFile( file ); } // end for } }