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 bc27100c4d8766eda425feb8e763181952059d40 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 27 11:17:06 2015 +0100 fixes #6542: [TECH] Disparition du .bat au démarrage + amélioration du code --- .../tutti/ui/swing/updater/DeleteHelper.java | 4 +- .../fr/ifremer/tutti/ui/swing/updater/Updater.java | 225 +++++---------------- .../ui/swing/updater/UpdaterFileSystemPathes.java | 200 ++++++++++++++++++ 3 files changed, 257 insertions(+), 172 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 index 4b8555f..bddf87a 100644 --- 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 @@ -81,6 +81,7 @@ public class DeleteHelper { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + System.out.println("Delete directory: " + dir); Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } @@ -103,6 +104,7 @@ public class DeleteHelper { // If the file name matches the glob or if no matcher, delete the file if (matcher.matches(file.getFileName())) { + System.out.println("Delete file: " + file); Files.deleteIfExists(file); } return FileVisitResult.CONTINUE; @@ -128,7 +130,7 @@ public class DeleteHelper { if (deleteDir == null) { if (matcher.matches(dir.getFileName())) { - System.out.println("Match directory name to delete: " + dir); + System.out.println("Delete directory: " + dir); deleteDir = dir; } } 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 10e1664..ecafb14 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 @@ -25,22 +25,14 @@ 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; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.PosixFilePermission; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; /** * @author Ludovic Pecquot (ludovic.pecquot@e-is.pro) @@ -51,39 +43,18 @@ public class Updater { public static final String APPLICATION_UPDATER_TITLE = "Allegro Campaign UI Updater"; - public static final String NEW_DIR = "NEW"; - - public static final String OLD_DIR = "OLD"; - - public static final String LAUNCHER_DIR = "launcher"; - - public static final String UPDATE_RUNTIME_CMD = "update_runtime"; - - public static final String BATCH_WINDOWS_EXT = ".bat"; - - public static final String BATCH_UNIX_EXT = ".sh"; - - public static final String VERSION_FILE = "version.appup"; - public static final int NORMAL_EXIT_CODE = 0; public static final int ERROR_EXIT_CODE = 1; public static final int RUNTIME_UPDATE_EXIT_CODE = 90; - private final Path baseDir; - - private final String backupDate; - - private final boolean windowsOS; + private final UpdaterFileSystemPathes pathHelper; public static void main(String... args) { - // Instantiate the launcher Updater updater = new Updater(); int exitCode = updater.execute(); - - // exit System.exit(exitCode); } @@ -91,29 +62,12 @@ public class Updater { public Updater() { // Get the current directory where application has been launched - baseDir = Paths.get(System.getProperty("user.dir")); - - // Compute the date to create backup directories - Date now = new Date(); - DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); - backupDate = df.format(now); - - windowsOS = System.getProperty("os.name").startsWith("Windows"); - - } - - private Path getBackupDirectory() throws IOException { - - Path backupDirectory = baseDir.resolve(OLD_DIR); - if (!Files.isDirectory(backupDirectory)) { - Files.createDirectory(backupDirectory); - } + Path baseDir = Paths.get(System.getProperty("user.dir")); - return backupDirectory; + pathHelper = new UpdaterFileSystemPathes(baseDir); } - public int execute() { System.out.println("updater started at " + new Date().toString()); @@ -122,17 +76,26 @@ public class Updater { try { - // Check runtime update (jre or launcher) - boolean runtimeUpdate = checkRuntimeUpdate(); + // before trying to update runtime modules + beforeUpdateRuntimeModules(); + + // update runtime modules + boolean runtimeUpdate = updateRuntimeModules(); if (runtimeUpdate) { + // there is some runtime updates + afterUpdateRuntimeModules(); + exitCode = RUNTIME_UPDATE_EXIT_CODE; } else { - // Launch update - launchUpdate(); + // update application modules + updateApplicationModules(); + + // clean files + cleanFiles(); exitCode = NORMAL_EXIT_CODE; @@ -145,19 +108,20 @@ public class Updater { } - System.out.println("updater ended at " + new Date().toString()); + System.out.println("updater ended at " + new Date().toString() + " with exit code: " + exitCode); return exitCode; } - protected boolean checkRuntimeUpdate() throws Exception { + protected void beforeUpdateRuntimeModules() throws IOException { - String scriptFilename = UPDATE_RUNTIME_CMD + (windowsOS ? BATCH_WINDOWS_EXT : BATCH_UNIX_EXT); + Path runtimeUpdater = pathHelper.getUpdaterScriptPath(); + Files.deleteIfExists(runtimeUpdater); - Path runtimeUpdater = baseDir.resolve(scriptFilename); + } - Files.deleteIfExists(runtimeUpdater); + protected boolean updateRuntimeModules() throws Exception { boolean mustUpdateRuntime = false; @@ -175,71 +139,68 @@ public class Updater { } - if (mustUpdateRuntime) { + return mustUpdateRuntime; - // A runtime update is available, so generate the script - URL resource = getClass().getResource("/" + scriptFilename); + } - try (InputStream stream = resource.openStream()) { + protected void afterUpdateRuntimeModules() throws IOException { - Files.copy(stream, runtimeUpdater, StandardCopyOption.REPLACE_EXISTING); + Path runtimeUpdater = pathHelper.getUpdaterScriptPath(); - } + URL resource = getClass().getResource("/" + runtimeUpdater.getFileName()); - makeExecutable(runtimeUpdater); + try (InputStream stream = resource.openStream()) { - String message = String.format("Runtime updates available.\nYou must execute '%s' manually to apply new runtime.", runtimeUpdater.getFileName()); - System.out.println(message); - JOptionPane.showMessageDialog(null, message); + Files.copy(stream, runtimeUpdater, StandardCopyOption.REPLACE_EXISTING); } - return mustUpdateRuntime; + pathHelper.makeExecutable(runtimeUpdater); + + String message = String.format("Runtime updates available.\nYou must execute '%s' manually to apply new runtime.", runtimeUpdater.getFileName()); + System.out.println(message); + JOptionPane.showMessageDialog(null, message); } - protected void launchUpdate() throws IOException { + protected void updateApplicationModules() throws IOException { for (Module module : Module.values()) { if (!module.isRuntimeModule()) { - updateModule(module); + updateApplicationModule(module); } } - // Cleaning process - cleanObsoleteFiles(); - DeleteHelper.deleteDirectory(baseDir.resolve(NEW_DIR)); + } + + protected void cleanFiles() throws IOException { + + pathHelper.cleanObsoleteFiles(); + DeleteHelper.deleteDirectory(pathHelper.getUpdateDirectory()); } protected boolean updateRuntimeModule(Module module) throws IOException { boolean updateFound; - String moduleName = module.name(); - Path modulePath = baseDir.resolve(moduleName); - String oldVersion = getVersion(modulePath); + String oldVersion = pathHelper.getModuleVersion(module); 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)) { + if (pathHelper.isUpdateModuleExists(module)) { - String newVersion = getVersion(moduleNewPath); + String newVersion = pathHelper.getUpdateModuleVersion(module); System.out.println(String.format("%s New version detected %s", moduleNameStr, newVersion)); - Path backupDirectory = getBackupDirectory(); - // Remove older backup - System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory + File.separator + moduleName + "-*")); - DeleteHelper.deleteDirectories(backupDirectory, moduleName + "-*"); + pathHelper.removeOlderBackup(module); updateFound = true; @@ -254,43 +215,29 @@ public class Updater { } - private void updateModule(Module module) throws IOException { - - String moduleName = module.name(); - - Path modulePath = baseDir.resolve(moduleName); - String oldVersion = getVersion(modulePath); + protected void updateApplicationModule(Module module) throws IOException { String moduleNameStr = module.getModuleLoggerName(); + String oldVersion = pathHelper.getModuleVersion(module); 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); + if (pathHelper.isUpdateModuleExists(module)) { - if (Files.isDirectory(moduleNewPath)) { - - String newVersion = getVersion(moduleNewPath); + String newVersion = pathHelper.getUpdateModuleVersion(module); System.out.println(String.format("%s New version detected %s", moduleNameStr, newVersion)); - Path backupDirectory = getBackupDirectory(); + pathHelper.removeOlderBackup(module); - // Remove older backup - System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory + File.separator + moduleName + "-*")); - DeleteHelper.deleteDirectories(backupDirectory, moduleName + "-*"); + Path modulePath = pathHelper.getModulePath(module); // Backup existing module - if (Files.isDirectory(modulePath)) { - - 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); - - } + pathHelper.backupModule(module, oldVersion); // Installing new module System.out.println(String.format("%s Install new version %s", moduleNameStr, newVersion)); + Path moduleNewPath = pathHelper.getUpdateModulePath(module); Files.move(moduleNewPath, modulePath, StandardCopyOption.REPLACE_EXISTING); } else { @@ -301,68 +248,4 @@ public class Updater { } - private String getVersion(Path path) throws IOException { - - // Return the version of a module from version.appup file - Path versionFile = path.resolve(VERSION_FILE); - List<String> lines = Files.readAllLines(versionFile, StandardCharsets.UTF_8); - if (lines == null || lines.isEmpty()) { - throw new IOException(versionFile.toString() + " is empty"); - } - return lines.get(0); - } - - protected void makeExecutable(Path path) throws IOException { - - if (!windowsOS) { - - Set<PosixFilePermission> perms = new HashSet<>(); - //add owners permission - perms.add(PosixFilePermission.OWNER_READ); - perms.add(PosixFilePermission.OWNER_WRITE); - perms.add(PosixFilePermission.OWNER_EXECUTE); - //add group permissions - perms.add(PosixFilePermission.GROUP_READ); - perms.add(PosixFilePermission.GROUP_WRITE); - perms.add(PosixFilePermission.GROUP_EXECUTE); - //add others permissions - perms.add(PosixFilePermission.OTHERS_READ); - perms.add(PosixFilePermission.OTHERS_EXECUTE); - - Files.setPosixFilePermissions(path, perms); - - } - - } - - private void cleanObsoleteFiles() throws IOException { - - Path applicationDirectoryPath = baseDir.resolve(Module.tutti.name()); - - if (windowsOS) { - - // Delete obsolete batch files - Files.deleteIfExists(baseDir.resolve("tutti.bat")); - Files.deleteIfExists(applicationDirectoryPath.resolve("launch.bat")); - - // Delete non Windows files - DeleteHelper.deleteFiles(baseDir, "*" + BATCH_UNIX_EXT); - } else { - - // Delete obsolete script files - Files.deleteIfExists(applicationDirectoryPath.resolve("launch.sh")); - - // Delete Windows files - 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")); - DeleteHelper.deleteDirectory(applicationDirectoryPath.resolve("launcher")); - - } - - } diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java new file mode 100644 index 0000000..e31d01c --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java @@ -0,0 +1,200 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.PosixFilePermission; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Created on 1/27/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.13 + */ +public class UpdaterFileSystemPathes { + + private static final String BACKUP_DIRECTORY_NAME = "OLD"; + + private static final String UPDATE_DIRECTORY_NAME = "NEW"; + + private static final String LAUNCHER_DIRECTORY_NAME = "launcher"; + + private static final String EMBEDDED_DIRECTORY_NAME = "embedded"; + + private static final String UPDATE_RUNTIME_CMD = "update_runtime"; + + private static final String BATCH_WINDOWS_EXTENSION = ".bat"; + + private static final String EXE_WINDOWS_EXTENSION = ".exe"; + + private static final String BATCH_UNIX_EXTENSION = ".sh"; + + private static final String VERSION_FILENAME = "version.appup"; + + private final Path baseDir; + + private final boolean windowsOS; + + private final String backupDate; + + public UpdaterFileSystemPathes(Path baseDir) { + this.baseDir = baseDir; + this.windowsOS = System.getProperty("os.name").startsWith("Windows"); + this.backupDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); + } + + public Path getUpdateDirectory() { + Path updateDirectory = baseDir.resolve(UPDATE_DIRECTORY_NAME); + return updateDirectory; + } + + public Path getUpdateModulePath(Module module) { + Path modulePath = getUpdateDirectory().resolve(module.name()); + return modulePath; + } + + public boolean isUpdateModuleExists(Module module) { + + Path updateModulePath = getUpdateModulePath(module); + boolean isDirectory = Files.isDirectory(updateModulePath); + return isDirectory; + + } + + public String getUpdateModuleVersion(Module module) throws IOException { + + Path updateModulePath = getUpdateModulePath(module); + String version = getVersion(updateModulePath); + return version; + + } + + public Path getModulePath(Module module) { + Path modulePath = baseDir.resolve(module.name()); + return modulePath; + } + + public String getModuleVersion(Module module) throws IOException { + + Path modulePath = getModulePath(module); + String version = getVersion(modulePath); + return version; + + } + + public Path getUpdaterScriptPath() { + + String scriptFilename = UPDATE_RUNTIME_CMD + (windowsOS ? BATCH_WINDOWS_EXTENSION : BATCH_UNIX_EXTENSION); + Path runtimeUpdater = baseDir.resolve(scriptFilename); + return runtimeUpdater; + + } + + public void cleanObsoleteFiles() throws IOException { + + Path applicationDirectoryPath = getModulePath(Module.tutti); + + if (windowsOS) { + + // Delete linux files + DeleteHelper.deleteFiles(baseDir, "*" + BATCH_UNIX_EXTENSION); + + } else { + + // Delete Windows files + DeleteHelper.deleteFiles(baseDir, "*" + BATCH_WINDOWS_EXTENSION); + DeleteHelper.deleteFiles(baseDir, "*" + EXE_WINDOWS_EXTENSION); + + } + + // Delete embedded files + DeleteHelper.deleteFiles(applicationDirectoryPath, "*" + BATCH_UNIX_EXTENSION); + DeleteHelper.deleteFiles(applicationDirectoryPath, "*" + BATCH_WINDOWS_EXTENSION); + DeleteHelper.deleteFiles(applicationDirectoryPath, "*" + EXE_WINDOWS_EXTENSION); + DeleteHelper.deleteDirectory(applicationDirectoryPath.resolve(LAUNCHER_DIRECTORY_NAME)); + DeleteHelper.deleteDirectory(applicationDirectoryPath.resolve(EMBEDDED_DIRECTORY_NAME)); + + } + + public void makeExecutable(Path path) throws IOException { + + if (!windowsOS) { + + Set<PosixFilePermission> perms = new HashSet<>(); + //add owners permission + perms.add(PosixFilePermission.OWNER_READ); + perms.add(PosixFilePermission.OWNER_WRITE); + perms.add(PosixFilePermission.OWNER_EXECUTE); + //add group permissions + perms.add(PosixFilePermission.GROUP_READ); + perms.add(PosixFilePermission.GROUP_WRITE); + perms.add(PosixFilePermission.GROUP_EXECUTE); + //add others permissions + perms.add(PosixFilePermission.OTHERS_READ); + perms.add(PosixFilePermission.OTHERS_EXECUTE); + + Files.setPosixFilePermissions(path, perms); + + } + + } + + public void removeOlderBackup(Module module) throws IOException { + + String moduleName = module.name(); + + Path backupDirectory = getBackupDirectory(); + + // Remove older backup + System.out.println(String.format("%s Clean backup directory %s", module.getModuleLoggerName(), backupDirectory + File.separator + moduleName + "-*")); + DeleteHelper.deleteDirectories(backupDirectory, moduleName + "-*"); + + } + + public void backupModule(Module module, String version) throws IOException { + + Path modulePath = getModulePath(module); + if (Files.isDirectory(modulePath)) { + + String moduleName = module.name(); + + Path backupDirectory = getBackupDirectory(); + + Path backupModulePath = backupDirectory.resolve(String.format("%s-%s-%s", moduleName, version, backupDate)); + + System.out.println(String.format("%s Backup old version %s to %s", module.getModuleLoggerName(), version, backupModulePath)); + Files.move(modulePath, backupModulePath); + + } + + } + + private Path getBackupDirectory() throws IOException { + + Path backupDirectory = baseDir.resolve(BACKUP_DIRECTORY_NAME); + if (!Files.isDirectory(backupDirectory)) { + Files.createDirectory(backupDirectory); + } + + return backupDirectory; + + } + + private String getVersion(Path path) throws IOException { + + // Return the version of a module from version.appup file + Path versionFile = path.resolve(VERSION_FILENAME); + List<String> lines = Files.readAllLines(versionFile, StandardCharsets.UTF_8); + if (lines == null || lines.isEmpty()) { + throw new IOException(versionFile.toString() + " is empty"); + } + return lines.get(0); + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.