Skip to content

Commit

Permalink
Merge pull request #10 from briacp/feature/9-export-offline-omt
Browse files Browse the repository at this point in the history
Add an option to skip repositories to create an "offline" package
  • Loading branch information
briacp authored May 3, 2023
2 parents ebfda6f + f300cdd commit ea3488b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.omegat.util.gui.UIThreadsUtil;
import org.openide.awt.Mnemonics;

@SuppressWarnings({"java:S106", "java:S1192"})
public class ManageOMTPackage
{
public static final String PLUGIN_VERSION = ManageOMTPackage.class.getPackage().getImplementationVersion();
Expand All @@ -105,6 +107,10 @@ public class ManageOMTPackage
public static final String PROPERTY_OPEN_DIR = "open-directory-after-export";
public static final String PROPERTY_GENERATE_TARGET = "generate-target-files";
public static final String PROPERTY_PROMPT_DELETE_IMPORT = "prompt-remove-omt-after-import";
public static final String PROPERTY_PACK_OFFLINE="pack-as-offline-project";

private static final String FALSE = Boolean.FALSE.toString();

protected static final Logger LOGGER = Logger.getLogger(ManageOMTPackage.class.getName());
protected static final String OMT_PACKER_LOGNAME = "omt-packer.log";

Expand Down Expand Up @@ -321,7 +327,7 @@ protected Void doInBackground() throws Exception {
protected void done() {
try {

if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_PROMPT_DELETE_IMPORT, "false"))) {
if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_PROMPT_DELETE_IMPORT, FALSE))) {
//@formatter:off
int deletePackage = JOptionPane.showConfirmDialog(
getMainWindow().getApplicationFrame(),
Expand Down Expand Up @@ -430,7 +436,7 @@ protected Void doInBackground() throws Exception
mainWindow.showStatusMessageRB("MW_STATUS_SAVING");
ManageOMTPackage.executeExclusively(true, () -> Core.getProject().saveProject(true) );

if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_GENERATE_TARGET, "false"))) {
if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_GENERATE_TARGET, FALSE))) {
ManageOMTPackage.executeExclusively(true, () -> {
try {
Core.getProject().compileProject(".*");
Expand All @@ -454,7 +460,7 @@ protected Void doInBackground() throws Exception
}

// Display the containing folder on the desktop
if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_OPEN_DIR, "false"))) {
if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_OPEN_DIR, FALSE))) {
Desktop.getDesktop().open(omtFile.getParentFile());
}

Expand All @@ -474,8 +480,9 @@ protected void done()
Log.log("Deleting project directory...");
Path pathToBeDeleted = projectDir.toPath();

Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile)
.forEach(File::delete);
try (Stream<Path> s = Files.walk(pathToBeDeleted)) {
s.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}

if (Files.exists(pathToBeDeleted)) {
Log.log("Couldn't delete project directory...");
Expand Down Expand Up @@ -737,11 +744,15 @@ private static final int addZipDir(final ZipOutputStream out, final Path root, f
// package.
File tmpProjectFile = File.createTempFile("omt", OConsts.BACKUP_EXTENSION,
props.getProjectRootDir());

boolean skipRepositories = Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_PACK_OFFLINE, FALSE));

try {
ProjectFileStorage.writeProjectFile(props, tmpProjectFile);
ProjectFileStorage.writeProjectFile(props, tmpProjectFile, skipRepositories);
} catch (Exception e) {
throw new IOException(e);
}

omtPackLog(String.format("addZipDir\tproject\t[%s]", OConsts.FILE_PROJECT));
out.putNextEntry(new ZipEntry(OConsts.FILE_PROJECT));
Files.copy(Paths.get(tmpProjectFile.getAbsolutePath()), out);
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/net/briac/omegat/plugin/omt/ProjectFileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.omegat.util.OConsts;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import java.io.File;
Expand Down Expand Up @@ -59,9 +60,17 @@ public class ProjectFileStorage {
throw new ExceptionInInitializerError(ex);
}
}

private ProjectFileStorage() {
/* empty */
}

// Should be in org.omegat.util.ProjectFileStorage.writeProjectFile
public static void writeProjectFile(ProjectProperties props, File outFile) throws Exception {
public static void writeProjectFile(ProjectProperties props, File outFile) throws IOException, JAXBException {
writeProjectFile(props, outFile, false);
}

public static void writeProjectFile(ProjectProperties props, File outFile, boolean skipRepositories) throws IOException, JAXBException {
String root = outFile.getAbsoluteFile().getParent();

Omegat om = new Omegat();
Expand Down Expand Up @@ -96,7 +105,7 @@ && new File(glossaryFile).getName().equals(OConsts.DEFAULT_W_GLOSSARY)) {
om.getProject().setRemoveTags(props.isRemoveTags());
om.getProject().setExternalCommand(props.getExternalCommand());

if (props.getRepositories() != null && !props.getRepositories().isEmpty()) {
if (!skipRepositories && props.getRepositories() != null && !props.getRepositories().isEmpty()) {
om.getProject().setRepositories(new Project.Repositories());
om.getProject().getRepositories().getRepository().addAll(props.getRepositories());
}
Expand All @@ -122,10 +131,10 @@ && new File(absolutePath).getCanonicalPath().equals(new File(root, defaultName).
// Use the relativized path as it is "near" enough.
result = rel;
} else {
//
result = absPath.toString();
}
} catch (IllegalArgumentException e) {
/* empty, not sure if it still can be thrown */
}
return normalizeSlashes(result);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/omt-package-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ prompt-remove-omt-after-import=false
# binding "omtPackageFile". The console output is done in OmegaT
# logfile, not in the scripting window.
#post-package-script=processOmtPackage.groovy

# If set to true and the project is a team project, the repositories
# information are stripped of the exported omegat.project
pack-as-offline-project=false

0 comments on commit ea3488b

Please sign in to comment.