001    package org.hackystat.utilities.ivy;
002    
003    import java.io.File;
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    
008    /**
009     * Removes all but most recently published files from the Hackystat local repository.
010     * Can be invoked locally from the hackystat-utilities build system using 
011     * "ant cleanlocalrepository". 
012     * The repository is structured as follows:
013     * <pre>
014     * ~/.ivy2/local-repository/org.hackystat/
015     *                                        hackystat-utilities/
016     *                                                            2009.07.13.12.49.00/
017     *                                                                                f1
018     *                                                                                f2
019     *                                                            2009.07.08.16.26.27/
020     *                                                                                f1
021     *                                                                                f2
022     *                                                            :
023     *                                        hackystat-sensorbase-uh/
024     *                                                                2009.07.13.12.49.00/
025     *                                                                :
026     *                                        :
027     * </pre>  
028     * @author Philip Johnson
029     *
030     */
031    public class CleanLocalRepository {  //NOPMD (Avoid singleton warning)
032      
033      /** The location of the Hackystat Ivy local repository. */
034      private static File repo =
035        new File(System.getProperty("user.home") + "/.ivy2/local-repository/org.hackystat");
036    
037      /**
038       * Processes the entire local Hackystat repository.
039       * @param repo The local repo.
040       */
041      private static void processRepository(File repo) {
042        System.out.println("Processing repository: " + repo);
043        for (File fileOrDir : repo.listFiles()) {
044          if (fileOrDir.isDirectory()) {
045            processModule(fileOrDir);
046          }
047        }
048      }
049      
050      /**
051       * Processes an individual Hackystat module publication directory.
052       * @param moduleDir The module directory.
053       */
054      private static void processModule(File moduleDir) {
055        System.out.println("Processing module: " + moduleDir);
056        // Create sorted list of publication date directory names.
057        List<String> pubDirNames = new ArrayList<String>();
058        for (String pubDirName : moduleDir.list()) {
059          if ((new File(moduleDir, pubDirName)).isDirectory()) {
060            pubDirNames.add(pubDirName);
061          }
062        }
063        Collections.sort(pubDirNames);
064        // Determine whether there are any directories to delete.
065        int size = pubDirNames.size();
066        if (size <= 1) {
067          System.out.println("  No old publications to delete.");
068        }
069        else {
070          // Delete all old publications.
071          for (String pubDirName : pubDirNames.subList(0, (size - 1))) {
072            deletePublicationDir(new File(moduleDir, pubDirName));
073          }
074        }
075      }
076      
077      /**
078       * Deletes the passed publication directory.
079       * @param pubDir The publication directory to delete.
080       */
081      private static void deletePublicationDir(File pubDir) {
082        System.out.println("  Deleting contents of directory: " + pubDir);
083        for (File pubFile : pubDir.listFiles()) {
084          if (pubFile.isFile()) {
085            String deleteSuccess = pubFile.delete() ? "OK" : "not OK";
086            System.out.println("    Deleting file: " + pubFile + " was " + deleteSuccess);
087          }
088        }
089        String deleteSuccess = pubDir.delete() ? "OK" : "not OK";
090        System.out.println("   Deleting directory: " + pubDir + " was " + deleteSuccess);
091      }
092      
093      /**
094       * Runs the program.
095       * @param args Ignored. 
096       */
097      public static void main(String[] args) {
098        if (repo.exists()) {
099          processRepository(repo);
100          System.out.println("Finished processing the local hackystat repository.");
101        }
102        else {
103          System.out.println(repo + " does not exist. Doing nothing.");
104        }
105      }
106    
107    }