This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit e2a97c583e1b0d778deca578fc84b63596dc1b24 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 26 17:11:06 2015 +0100 improve updater code --- .../tutti/ui/swing/updater/DeleteHelper.java | 143 +++++++++++++++++++++ .../fr/ifremer/tutti/ui/swing/updater/Updater.java | 61 ++++----- 2 files changed, 169 insertions(+), 35 deletions(-) diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/DeleteHelper.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/DeleteHelper.java new file mode 100644 index 0000000..8d7bc16 --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/DeleteHelper.java @@ -0,0 +1,143 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * Created on 1/26/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.12 + */ +public class DeleteHelper { + + public static void deleteDirectories(Path path, String glob) throws IOException { + + PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob); + if (Files.isDirectory(path)) { + + DeleteDirectories deleteDirectories = new DeleteDirectories(matcher); + Files.walkFileTree(path, deleteDirectories); + } + + } + + public static void deleteFiles(Path path, String glob) throws IOException { + if (Files.isDirectory(path)) { + + PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob); + DeleteFiles deleteFiles = new DeleteFiles(matcher); + Files.walkFileTree(path, deleteFiles); + } + + } + + public static void deleteDirectory(Path path) throws IOException { + if (Files.isDirectory(path)) { + DeleteDirectory deleteDirectory = new DeleteDirectory(); + Files.walkFileTree(path, deleteDirectory); + } + } + + /** + * To delete the given directory. + */ + public static class DeleteDirectory extends SimpleFileVisitor<Path> { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.deleteIfExists(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.deleteIfExists(dir); + return FileVisitResult.CONTINUE; + } + + } + + /** + * To delete all files that are matching the given matcher. + */ + public static class DeleteFiles extends SimpleFileVisitor<Path> { + + private final PathMatcher matcher; + + protected DeleteFiles(PathMatcher matcher) { + this.matcher = matcher; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + + // If the file name matches the glob or if no matcher, delete the file + if (matcher.matches(file.getFileName())) { + Files.deleteIfExists(file); + } + return FileVisitResult.CONTINUE; + } + + } + + /** + * To delete all directories which names are matching the given matcher. + */ + public static class DeleteDirectories extends SimpleFileVisitor<Path> { + + private final PathMatcher matcher; + + protected DeleteDirectories(PathMatcher matcher) { + this.matcher = matcher; + } + + private Path deleteDir; + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + + if (deleteDir == null) { + if (matcher.matches(dir.getFileName())) { + System.out.println("Match directory name to delete: " + dir); + deleteDir = dir; + } + } + return FileVisitResult.CONTINUE; + + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + + if (deleteDir != null) { + Files.deleteIfExists(file); + } + return FileVisitResult.CONTINUE; + + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + + if (deleteDir != null) { + + Files.deleteIfExists(dir); + + if (dir.equals(deleteDir)) { + deleteDir = null; + } + + } + + return FileVisitResult.CONTINUE; + + } + + } +} diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java index ba95d2f..f6d9089 100644 --- a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java @@ -1,6 +1,7 @@ package fr.ifremer.tutti.ui.swing.updater; import javax.swing.JOptionPane; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -186,7 +187,7 @@ public class Updater { // Cleaning process cleanObsoleteFiles(); - cleanPath(baseDir.resolve(NEW_DIR)); + DeleteHelper.deleteDirectory(baseDir.resolve(NEW_DIR)); } @@ -195,8 +196,13 @@ public class Updater { boolean updateFound; String moduleName = module.name(); + Path modulePath = baseDir.resolve(moduleName); + String oldVersion = getVersion(modulePath); + String moduleNameStr = module.getModuleLoggerName(); + System.out.println(String.format("%s Current version: %s", moduleNameStr, oldVersion)); + Path moduleNewPath = baseDir.resolve(NEW_DIR).resolve(moduleName); if (Files.isDirectory(moduleNewPath)) { @@ -208,20 +214,16 @@ public class Updater { Path backupDirectory = getBackupDirectory(); // Remove older backup - System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory)); - cleanPath(backupDirectory, moduleName + "-*"); + System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory + File.separator + moduleName + "-*")); + DeleteHelper.deleteDirectories(backupDirectory, moduleName + "-*"); updateFound = true; } else { - Path modulePath = baseDir.resolve(moduleName); - - String newVersion = getVersion(modulePath); - - System.out.println(String.format("%s No update (current version: %s)", moduleNameStr, newVersion)); - + System.out.println(String.format("%s No update found", moduleNameStr)); updateFound = false; + } return updateFound; @@ -232,8 +234,13 @@ public class Updater { String moduleName = module.name(); + Path modulePath = baseDir.resolve(moduleName); + String oldVersion = getVersion(modulePath); + String moduleNameStr = module.getModuleLoggerName(); + System.out.println(String.format("%s Current version: %s", moduleNameStr, oldVersion)); + // Update a single module. moduleName corresponds to the name of the directory inside the baseDir Path moduleNewPath = baseDir.resolve(NEW_DIR).resolve(moduleName); @@ -246,17 +253,16 @@ public class Updater { Path backupDirectory = getBackupDirectory(); // Remove older backup - System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory)); - cleanPath(backupDirectory, moduleName + "-*"); + System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory + File.separator + moduleName + "-*")); + DeleteHelper.deleteDirectories(backupDirectory, moduleName + "-*"); // Backup existing module - Path modulePath = baseDir.resolve(moduleName); if (Files.isDirectory(modulePath)) { - String oldVersion = getVersion(modulePath); - Path moduleOldPath = backupDirectory.resolve(String.format("%s-%s-%s", moduleNameStr, oldVersion, backupDate)); - System.out.println(String.format("%s Backup old version %s to %s", moduleNameStr, oldVersion, moduleOldPath.toString())); + Path moduleOldPath = backupDirectory.resolve(String.format("%s-%s-%s", moduleName, oldVersion, backupDate)); + System.out.println(String.format("%s Backup old version %s to %s", moduleNameStr, oldVersion, moduleOldPath.toString())); Files.move(modulePath, moduleOldPath); + } // Installing new module @@ -265,10 +271,7 @@ public class Updater { } else { - Path modulePath = baseDir.resolve(moduleName); - String newVersion = getVersion(modulePath); - - System.out.println(String.format("%s No update (current version: %s).", moduleNameStr, newVersion)); + System.out.println(String.format("%s No update found", moduleNameStr)); } @@ -285,18 +288,6 @@ public class Updater { return lines.get(0); } - private void cleanPath(Path path) throws IOException { - if (Files.isDirectory(path)) { - Files.walkFileTree(path, RecursiveDeleteFileVisitor.newVisitor()); - } - } - - private void cleanPath(Path path, String glob) throws IOException { - if (Files.isDirectory(path)) { - Files.walkFileTree(path, RecursiveDeleteFileVisitor.newVisitor(baseDir, glob)); - } - } - protected void makeExecutable(Path path) throws IOException { if (!windowsOS) { @@ -331,21 +322,21 @@ public class Updater { Files.deleteIfExists(applicationDirectoryPath.resolve("launch.bat")); // Delete non Windows files - cleanPath(baseDir, "*" + BATCH_UNIX_EXT); + DeleteHelper.deleteFiles(baseDir, "*" + BATCH_UNIX_EXT); } else { // Delete obsolete script files Files.deleteIfExists(applicationDirectoryPath.resolve("launch.sh")); // Delete Windows files - cleanPath(baseDir, "*" + BATCH_WINDOWS_EXT); - cleanPath(baseDir, "*.exe"); + DeleteHelper.deleteFiles(baseDir, "*" + BATCH_WINDOWS_EXT); + DeleteHelper.deleteFiles(baseDir, "*.exe"); } // Delete embedded files Files.deleteIfExists(applicationDirectoryPath.resolve("tutti.exe")); Files.deleteIfExists(applicationDirectoryPath.resolve("tutti.sh")); - cleanPath(applicationDirectoryPath.resolve("launcher")); + DeleteHelper.deleteDirectory(applicationDirectoryPath.resolve("launcher")); } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.