From 4cac76c5161a2d286b194f4ba79846f7fd53dd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Briac=20Pilpr=C3=A9?= Date: Mon, 11 Mar 2019 21:34:29 +0100 Subject: [PATCH] Configuration file added --- CHANGELOG.md | 8 ++ README.md | 30 +++++- gradle.properties | 2 +- .../omegat/plugin/omt/DirectoryFilter.java | 33 ++++++ .../omegat/plugin/omt/ManageOMTPackage.java | 100 +++++++++++++----- 5 files changed, 145 insertions(+), 28 deletions(-) create mode 100644 src/main/java/net/briac/omegat/plugin/omt/DirectoryFilter.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6602220..ec7ba41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Change Log +## [v1.0.2] + +### Add +- Configuration file omt-package-config.properties +- Option `exclude-pattern` to specify regular expressions (separated by ";") of files to exclude. +- Option `open-directory-after-export` (true/false) to open the folder containing the created package. +- Option `generate-target-files` (true/false) to generate the target documents before creating the package. + ## [v1.0.1] ### Changed diff --git a/README.md b/README.md index 04eb925..aeb40d4 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This plugin introduce an option to import and export OmegaT packages. These packages are made so that it can be easier to share OmegaT projects between people without using team projects. -At the moment, it is simply a zipped copy of the project, excluding backup files. +At the moment, it is simply a zipped copy of the project, excluding some files. ## Installation @@ -11,6 +11,34 @@ You can get a plugin jar file from zip/tgz distribution file. OmegaT plugin should be placed in `$HOME/.omegat/plugin` or `C:\Program Files\OmegaT\plugin` depending on your operating system. +## Configuration + +This plugin uses a property file, either located in `$HOME/.omegat/omt-package-config.properties` or +at the root of the project, with the same name `omt-package-config.properties`. All the properties +redefined in the project folder will override those in the `.omegat` folder. + +The default configuration looks like the following: + +``` +# +# OmegaT Plugin - OMT Package Manager +# +# omt-package-config.properties +# + +# List of regex pattern (separated by ";") of files to exclude when +# generating an OMT package. (default: "\\.(zip|bak|omt)$") +exclude-pattern=\\.(zip|bak|omt)$ + +# If set to true, the folder containing the exported OMT will be +# displayed. (default: false) +open-directory-after-export=false + +# If set to true, the translated documents will be created before +# creating the package. (default: false) +generate-target-files=false +``` + ## Sponsor Thanks a lot to [cApStAn](http://www.capstan.be/) for sponsoring the development of this plugin. diff --git a/gradle.properties b/gradle.properties index 7044575..66a831e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ pluginMainClass=net.briac.omegat.plugin.omt.ManageOMTPackage pluginName=OMT Package Plugin -version=1.0.1 +version=1.0.2 omegatPluginDir=C:/Users/briac/AppData/Roaming/OmegaT/plugins/ diff --git a/src/main/java/net/briac/omegat/plugin/omt/DirectoryFilter.java b/src/main/java/net/briac/omegat/plugin/omt/DirectoryFilter.java new file mode 100644 index 0000000..12b3569 --- /dev/null +++ b/src/main/java/net/briac/omegat/plugin/omt/DirectoryFilter.java @@ -0,0 +1,33 @@ +package net.briac.omegat.plugin.omt; + +import org.omegat.util.Log; + +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class DirectoryFilter implements DirectoryStream.Filter { + + private List excludePatterns = new ArrayList<>(); + + public DirectoryFilter(List excludePatterns) { + for (String e : excludePatterns) { + this.excludePatterns.add(Pattern.compile(e)); + } + } + + @Override + public boolean accept(Path entry) throws IOException { + //Log.log(String.format("filter\tentry:[%s]", entry)); + for (Pattern excludePattern : excludePatterns) { + if (excludePattern.matcher(entry.toString()).find()) { + Log.log(String.format("Excluded\t%s", entry)); + return false; + } + } + return true; + } +} diff --git a/src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java b/src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java index 182c862..a65edc0 100644 --- a/src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java +++ b/src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java @@ -30,6 +30,7 @@ import org.omegat.util.FileUtil; import org.omegat.util.Log; import org.omegat.util.OConsts; +import org.omegat.util.StaticUtils; import org.omegat.util.gui.OmegaTFileChooser; import org.omegat.util.gui.UIThreadsUtil; import org.openide.awt.Mnemonics; @@ -41,9 +42,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Enumeration; -import java.util.Locale; -import java.util.ResourceBundle; +import java.util.List; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; @@ -54,10 +54,20 @@ public class ManageOMTPackage { public static final String OMT_EXTENSION = ".omt"; public static final String IGNORE_FILE = ".empty"; - static final ResourceBundle res = ResourceBundle.getBundle("omt-package", Locale.getDefault()); + public static final String CONFIG_FILE = "omt-package-config.properties"; + + public static final String PROPERTY_EXCLUDE = "exclude-pattern"; + public static final String DEFAULT_EXCLUDE = "\\.(zip|bak|omt)$"; + public static final String PROPERTY_OPEN_DIR = "open-directory-after-export"; + public static final String PROPERTY_GENERATE_TARGET = "generate-target-files"; + + protected static final ResourceBundle res = ResourceBundle.getBundle("omt-package", Locale.getDefault()); + private static JMenuItem importOMT; private static JMenuItem exportOMT; + private static Properties pluginProps = new Properties(); + public static void loadPlugins() { CoreEvents.registerProjectChangeListener(e -> onProjectStatusChanged(Core.getProject().isProjectLoaded())); @@ -108,6 +118,8 @@ private static void onProjectStatusChanged(boolean isProjectLoaded) { public static void projectImportOMT() { UIThreadsUtil.mustBeSwingThread(); + loadPluginProps(); + if (Core.getProject().isProjectLoaded()) { return; } @@ -145,6 +157,8 @@ protected void done() { public static void projectExportOMT() { UIThreadsUtil.mustBeSwingThread(); + loadPluginProps(); + if (!Core.getProject().isProjectLoaded()) { return; } @@ -201,17 +215,24 @@ protected Void doInBackground() throws Exception { mainWindow.showStatusMessageRB("MW_STATUS_SAVING"); - Core.executeExclusively(true, () -> { - Core.getProject().saveProject(true); - try { - Core.getProject().compileProject(".*"); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - }); + if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_GENERATE_TARGET, "false"))) { + Core.executeExclusively(true, () -> { + Core.getProject().saveProject(true); + try { + Core.getProject().compileProject(".*"); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + }); + } createOmt(omtFile, Core.getProject().getProjectProperties()); + // Display the containing folder on the desktop + if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_OPEN_DIR, "false"))) { + Desktop.getDesktop().open(omtFile.getParentFile()); + } + mainWindow.showStatusMessageRB("MW_STATUS_SAVED"); mainWindow.setCursor(oldCursor); return null; @@ -229,6 +250,39 @@ protected void done() { }.execute(); } + private static void loadPluginProps() { + pluginProps = new Properties(); + File propFile = new File(StaticUtils.getConfigDir(), CONFIG_FILE); + if (propFile.exists()) { + try (FileInputStream inStream = new FileInputStream(propFile)) { + pluginProps.load(inStream); + System.out.println("OMT App Plugin Properties"); + pluginProps.list(System.out); + } catch (IOException e) { + Log.log(String.format("Could not load plugin property file \"%s\"", propFile.getAbsolutePath())); + } + } else { + System.out.println(String.format("No app plugin properties \"%s\"", propFile.getAbsolutePath())); + } + + if (!Core.getProject().isProjectLoaded()) { + // No project specific properties! + return; + } + + propFile = new File(Core.getProject().getProjectProperties().getProjectRootDir(), CONFIG_FILE); + if (propFile.exists()) { + try (FileInputStream inStream = new FileInputStream(propFile)) { + pluginProps.load(inStream); + System.out.println("OMT Project Plugin Properties"); + pluginProps.list(System.out); + } catch (IOException e) { + Log.log(String.format("Could not load project plugin property file \"%s\"", propFile.getAbsolutePath())); + } + } else { + System.out.println(String.format("No project plugin propeties \"%s\"", propFile.getAbsolutePath())); + } + } /** * It creates project internals from OMT zip file. @@ -313,10 +367,14 @@ public static void createOmt(final File omtZip, final ProjectProperties props) t throw new IllegalArgumentException("Path must be a directory."); } + List listExcludes = Arrays.asList(pluginProps.getProperty(PROPERTY_EXCLUDE, DEFAULT_EXCLUDE).split(";")); + + DirectoryStream.Filter filter = new DirectoryFilter(listExcludes); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(omtZip)); Log.log(String.format("Zipping to file [%s]", omtZip.getAbsolutePath())); try (ZipOutputStream out = new ZipOutputStream(bos)) { - addZipDir(out, null, path, props); + addZipDir(out, null, path, props, filter); } JOptionPane.showMessageDialog( @@ -328,21 +386,11 @@ public static void createOmt(final File omtZip, final ProjectProperties props) t } private static final void addZipDir(final ZipOutputStream out, final Path root, final Path dir, - final ProjectProperties props) throws IOException { - try (DirectoryStream stream = Files.newDirectoryStream(dir)) { + final ProjectProperties props, DirectoryStream.Filter filter) throws IOException { + try (DirectoryStream stream = Files.newDirectoryStream(dir, filter)) { for (Path child : stream) { final Path childPath = child.getFileName(); - final String name = childPath.toFile().getName(); - - // TODO - The list of excluded/included files should be read from a - // properties file in OmT config folder. - if (name.endsWith(".zip") || name.endsWith(OConsts.BACKUP_EXTENSION) || name.endsWith(OMT_EXTENSION) - ) { - // Skip .bak and .omt files - continue; - } - // Skip projects inside projects if (Files.isDirectory(child) && new File(child.toFile(), OConsts.FILE_PROJECT).exists()) { Log.log(String.format("The directory \"%s\" appears to be an OmegaT project, we'll skip it.", child.toFile().getAbsolutePath())); @@ -380,7 +428,7 @@ private static final void addZipDir(final ZipOutputStream out, final Path root, out.putNextEntry(new ZipEntry(emptyDirFile)); out.closeEntry(); } - addZipDir(out, entry, child, props); + addZipDir(out, entry, child, props, filter); } else { Log.log(String.format("addZipDir\tfile\t[%s]", entry)); out.putNextEntry(new ZipEntry(entry.toString()));