Commit a2feca57 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(importWorkspace) migrate before exporting as .bos (#683)

* fix(importWorkspace) migrate before exporting as .bos

Closes BS-16775
parent 438bc282
......@@ -35,6 +35,7 @@ import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.edapt.migration.MigrationException;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.preference.IPreferenceStore;
......@@ -51,18 +52,17 @@ public class ImportWorkspaceApplication implements IApplication {
*/
@Override
public Object start(IApplicationContext context) throws Exception {
disableGroovyDSL();
repositoryAccessor.init();
final String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
final Optional<String> scan = Stream.of(args).filter("-scan"::equals).findFirst();
final Optional<String> export = Stream.of(args).filter(arg -> arg.startsWith("-export=")).findFirst();
final File targetFolder = new File(System.getProperty("java.io.tmpdir"), IMPORT_CACHE_FOLDER);
final File exportTargetFolder = new File(System.getProperty("java.io.tmpdir"), IMPORT_CACHE_FOLDER);
if (export.isPresent()) {
if (targetFolder.exists()) {
PlatformUtil.delete(targetFolder, Repository.NULL_PROGRESS_MONITOR);
if (exportTargetFolder.exists()) {
PlatformUtil.delete(exportTargetFolder, Repository.NULL_PROGRESS_MONITOR);
}
targetFolder.mkdirs();
exportTargetFolder.mkdirs();
}
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
......@@ -82,25 +82,35 @@ public class ImportWorkspaceApplication implements IApplication {
.orElse(Stream.empty())
.filter(repository.getName()::equals)
.findFirst()
.ifPresent(repoToExport -> {
System.out.println(
String.format("$EXPORT_PROGRESS_%s",
String.format(Messages.exportingWorkspace, repository.getName())));
final boolean closed = !repository.getProject().isOpen();
repositoryAccessor.setRepository(repository.getName());
repository
.exportToArchive(
new File(targetFolder, repository.getName() + ".bos").getAbsolutePath());
if (closed) {
repository.close();
}
});
.ifPresent(repoToExport -> migrateAndExportRepository(exportTargetFolder, repository));
});
}
return IApplication.EXIT_OK;
}
private void migrateAndExportRepository(final File targetFolder, Repository repository) {
System.out.println(
String.format("$EXPORT_PROGRESS_%s",
String.format(Messages.exportingWorkspace, repository.getName())));
final boolean closed = !repository.getProject().isOpen();
repositoryAccessor.setRepository(repository.getName());
try {
repository.migrate(Repository.NULL_PROGRESS_MONITOR);
repository
.exportToArchive(
new File(targetFolder, repository.getName() + ".bos")
.getAbsolutePath());
} catch (CoreException | MigrationException e) {
BonitaStudioLog.error(e);
} finally {
if (closed) {
repository.close();
}
}
}
private String findEdition(Repository repository) {
final DiagramRepositoryStore diagramStore = repository.getRepositoryStore(DiagramRepositoryStore.class);
final WebFragmentRepositoryStore fragmentStore = repository.getRepositoryStore(WebFragmentRepositoryStore.class);
......
......@@ -2,6 +2,7 @@ package org.bonitasoft.studio.importer.bos.operation;
import static java.util.Objects.requireNonNull;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.stream.Collectors;
......@@ -33,6 +34,7 @@ import org.eclipse.pde.launching.IPDELauncherConstants;
public class ImportWorkspaceOperation implements IRunnableWithProgress {
private static final String APPLICATION_ID = "org.bonitasoft.studio.importer.bos.ImportWorkspaceApplication";
private final ImportWorkspaceModel workspaceModel;
protected RepositoryAccessor repositoryAccessor;
private MultiStatus status = new MultiStatus(BosArchiveImporterPlugin.PLUGIN_ID, 0, "", null);
......@@ -65,7 +67,8 @@ public class ImportWorkspaceOperation implements IRunnableWithProgress {
workingCopy.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false);
workingCopy.setAttribute(IPDELauncherConstants.RUN_IN_UI_THREAD, true);
workingCopy.setAttribute(IPDELauncherConstants.DOCLEAR, false);
workingCopy.setAttribute(IPDELauncherConstants.LOCATION, workspaceModel.getWorksapceFolder());
workingCopy.setAttribute(IPDELauncherConstants.LOCATION,
new File(System.getProperty("java.io.tmpdir"), ScanWorkspaceOperation.TMP_WS_FOLDER).getAbsolutePath());
workingCopy.setAttribute(IPDELauncherConstants.USE_PRODUCT, false);
final ILaunch launch = workingCopy.launch("run", Repository.NULL_PROGRESS_MONITOR);
launch.getProcesses()[0].getStreamsProxy().getOutputStreamMonitor().addListener(new IStreamListener() {
......@@ -73,7 +76,7 @@ public class ImportWorkspaceOperation implements IRunnableWithProgress {
@Override
public void streamAppended(String text, IStreamMonitor streamMonitor) {
if (text.startsWith("$EXPORT_PROGRESS_")) {
monitor.subTask(text.replace("$EXPORT_PROGRESS_", ""));
monitor.subTask(text.replace("$EXPORT_PROGRESS_", "").split(System.lineSeparator())[0]);
}
}
});
......
......@@ -3,10 +3,18 @@ package org.bonitasoft.studio.importer.bos.operation;
import static java.util.Objects.requireNonNull;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
import org.bonitasoft.studio.common.ProductVersion;
import org.bonitasoft.studio.common.platform.tools.PlatformUtil;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.importer.bos.BosArchiveImporterPlugin;
import org.bonitasoft.studio.importer.bos.i18n.Messages;
......@@ -34,6 +42,7 @@ import org.eclipse.pde.launching.IPDELauncherConstants;
public class ScanWorkspaceOperation implements IRunnableWithProgress {
private static final String APPLICATION_ID = "org.bonitasoft.studio.importer.bos.ImportWorkspaceApplication";
public static final String TMP_WS_FOLDER = ".tmpWS";
private final ImportWorkspaceModel workspaceModel;
protected Repository repository;
......@@ -59,7 +68,7 @@ public class ScanWorkspaceOperation implements IRunnableWithProgress {
workingCopy.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false);
workingCopy.setAttribute(IPDELauncherConstants.RUN_IN_UI_THREAD, true);
workingCopy.setAttribute(IPDELauncherConstants.DOCLEAR, false);
workingCopy.setAttribute(IPDELauncherConstants.LOCATION, workspaceModel.getWorksapceFolder());
workingCopy.setAttribute(IPDELauncherConstants.LOCATION, tmpWorskpaceFolder());
workingCopy.setAttribute(IPDELauncherConstants.USE_PRODUCT, false);
final ILaunch launch = workingCopy.launch("run", Repository.NULL_PROGRESS_MONITOR);
launch.getProcesses()[0].getStreamsProxy().getOutputStreamMonitor().addListener(new IStreamListener() {
......@@ -80,11 +89,50 @@ public class ScanWorkspaceOperation implements IRunnableWithProgress {
if (launch.isTerminated()) {
monitor.done();
}
} catch (final CoreException e) {
} catch (final CoreException | IOException e) {
throw new InvocationTargetException(e);
}
}
private String tmpWorskpaceFolder() throws IOException {
File wsFolder = new File(workspaceModel.getWorksapceFolder());
File tmpFolder = new File(System.getProperty("java.io.tmpdir"), TMP_WS_FOLDER);
if (tmpFolder.exists()) {
PlatformUtil.delete(tmpFolder, Repository.NULL_PROGRESS_MONITOR);
}
Files.walkFileTree(wsFolder.toPath(), new SimpleFileVisitor<Path>() {
/*
* (non-Javadoc)
* @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.endsWith(Paths.get("workspace", "tomcat")) //Do not copy tomcat folder
|| dir.endsWith(Paths.get(".plugins", "org.eclipse.jdt.core"))) {
return FileVisitResult.SKIP_SUBTREE;
}
Files.createDirectories(tmpFolder.toPath().resolve(wsFolder.toPath()
.relativize(dir)));
return FileVisitResult.CONTINUE;
}
/*
* (non-Javadoc)
* @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.endsWith(Paths.get("workspace", ".metadata", ".log"))) {
return FileVisitResult.CONTINUE;
}
Files.copy(file, tmpFolder.toPath().resolve(wsFolder.toPath().relativize(file)));
return FileVisitResult.CONTINUE;
}
});
return tmpFolder.getAbsolutePath();
}
protected void scanStatus(String text) {
final String[] scannedRepo = text.replace("$SCAN_PROGRESS_", "").split(":");
final String repoName = scannedRepo[0];
......
......@@ -93,9 +93,7 @@ public class ImportWorkspaceControlSupplier implements ControlSupplier {
statusSection.setVisible(false);
textWidget.addTextListener(SWT.Modify, e -> {
statusSection.setVisible(textWidget.getText() != null && !textWidget.getText().isEmpty());
statusSection.layout();
});
return mainComposite;
}
......@@ -175,6 +173,8 @@ public class ImportWorkspaceControlSupplier implements ControlSupplier {
statusSection.setClient(createStatusControl(statusSection));
if (workspaceModel.getStatus().isOK()) {
statusSection.setExpanded(true);
statusSection.getShell().pack(true);
statusSection.getShell().layout(true, true);
}
textWidget.getParent().getParent().layout();
if (new File(filePath).exists()) {
......@@ -264,6 +264,7 @@ public class ImportWorkspaceControlSupplier implements ControlSupplier {
*/
@Override
public void expansionStateChanged(ExpansionEvent e) {
toLayout.getShell().pack(true);
toLayout.layout();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment