Skip to content

Commit

Permalink
[m2e] Add import wizard for Maven-based SARL projects.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 26, 2018
1 parent 9f6af51 commit 997ca04
Show file tree
Hide file tree
Showing 13 changed files with 637 additions and 154 deletions.
Expand Up @@ -43,7 +43,7 @@
import org.eclipse.ui.wizards.datatransfer.ImportOperation;

import io.sarl.eclipse.natures.SARLProjectConfigurator;
import io.sarl.m2e.utils.M2EUtilities;
import io.sarl.m2e.wizards.importproject.MavenImportUtils;

/** Wizard for importing SARL samples.
*
Expand Down Expand Up @@ -98,7 +98,7 @@ protected void installProject(ProjectDescriptor projectDescriptor, ImportOperati
projectDescriptionFile.delete();
}
// Import
M2EUtilities.importMavenProject(
MavenImportUtils.importMavenProject(
project.getWorkspace().getRoot(),
projectDescriptor.getName(),
true,
Expand Down
6 changes: 6 additions & 0 deletions main/coreplugins/io.sarl.eclipse/plugin.xml
@@ -1,6 +1,12 @@
<?eclipse version="3.0"?>

<plugin>
<extension point="org.eclipse.ui.importWizards">
<category
id="io.sarl.eclipse.category.wizards"
name="%category.name">
</category>
</extension>
<extension point="org.eclipse.ui.newWizards">
<category
id="io.sarl.eclipse.category.wizards"
Expand Down
12 changes: 7 additions & 5 deletions main/coreplugins/io.sarl.m2e/META-INF/MANIFEST.MF
Expand Up @@ -18,12 +18,14 @@ Require-Bundle: io.sarl.eclipse;bundle-version="0.8.0",
org.eclipse.ui;bundle-version="3.109.100",
org.eclipse.jdt.core;bundle-version="3.14.0",
org.eclipse.ui.ide;bundle-version="3.14.0",
org.eclipse.m2e.jdt;bundle-version="1.9.0",
org.eclipse.m2e.core;bundle-version="1.9.0",
org.eclipse.m2e.core.ui;bundle-version="1.9.0",
org.eclipse.m2e.maven.runtime;bundle-version="1.9.0",
org.eclipse.m2e.core.ui;bundle-version="1.9.0"
Export-Package: io.sarl.m2e;x-friends:="io.sarl.m2e.tests",
org.eclipse.m2e.core;bundle-version="1.9.0",
org.eclipse.m2e.jdt;bundle-version="1.9.0"
Export-Package: io.sarl.m2e,
io.sarl.m2e.config,
io.sarl.m2e.sre,
io.sarl.m2e.utils
io.sarl.m2e.utils,
io.sarl.m2e.wizards.importproject,
io.sarl.m2e.wizards.newproject

4 changes: 3 additions & 1 deletion main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties
Expand Up @@ -3,4 +3,6 @@ Bundle-Name = SARL Maven Integration for Eclipse
m2e.sarlconfigurator.name = SARL Project Configurator
wizard.name.newSarlMavenProject = SARL Maven Project
wizard.description.newSarlMavenProject.short = Create a new SARL Maven project
wizard.description.newSarlMavenProject.long = Create a new SARL Maven project in the SARL ide.
wizard.description.newSarlMavenProject.long = Create a new SARL Maven project in the SARL ide.
wizard.name.importSarlMavenProject = Existing SARL Maven Project
wizard.description.importSarlMavenProject.short = Import an existing SARL Maven project
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 29 additions & 2 deletions main/coreplugins/io.sarl.m2e/plugin.xml
Expand Up @@ -21,15 +21,42 @@
<extension point="org.eclipse.ui.newWizards">
<wizard
category="io.sarl.eclipse.category.wizards"
class="io.sarl.m2e.wizards.MavenSarlProjectWizard"
class="io.sarl.m2e.wizards.newproject.NewMavenSarlProjectWizard"
icon="icons/new_m2_sarl_project.png"
id="io.sarl.m2e.wizard.newSarlMavenProject"
id="io.sarl.m2e.wizard.newSarlMavenProject.1"
name="%wizard.name.newSarlMavenProject"
finalPerspective="io.sarl.eclipse.perspective.devel"
preferredPerspectives="io.sarl.eclipse.perspective.devel,org.eclipse.jdt.ui.JavaPerspective,org.eclipse.jdt.ui.JavaBrowsingPerspective"
project="true">
<description>%wizard.description.newSarlMavenProject.short"</description>
</wizard>
<wizard
category="org.eclipse.m2e"
class="io.sarl.m2e.wizards.newproject.NewMavenSarlProjectWizard"
icon="icons/new_m2_sarl_project.png"
id="io.sarl.m2e.wizard.newSarlMavenProject.2"
name="%wizard.name.newSarlMavenProject"
finalPerspective="io.sarl.eclipse.perspective.devel"
preferredPerspectives="io.sarl.eclipse.perspective.devel,org.eclipse.jdt.ui.JavaPerspective,org.eclipse.jdt.ui.JavaBrowsingPerspective"
project="true">
<description>%wizard.description.newSarlMavenProject.short"</description>
</wizard>
</extension>
<extension point="org.eclipse.ui.importWizards">
<wizard id="io.sarl.m2e.wizard.importSarlMavenProject.1"
class="io.sarl.m2e.wizards.importproject.ImportMavenSarlProjectWizard"
icon="icons/import_m2_sarl_project.png"
category="io.sarl.eclipse.category.wizards"
name="%wizard.name.importSarlMavenProject">
<description>%wizard.description.importwSarlMavenProject.short</description>
</wizard>
<wizard id="io.sarl.m2e.wizard.importSarlMavenProject.2"
class="io.sarl.m2e.wizards.importproject.ImportMavenSarlProjectWizard"
icon="icons/import_m2_sarl_project.png"
category="org.eclipse.m2e"
name="%wizard.name.importSarlMavenProject">
<description>%wizard.description.importwSarlMavenProject.short</description>
</wizard>
</extension>
<extension point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
Expand Down
136 changes: 0 additions & 136 deletions main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/utils/M2EUtilities.java
Expand Up @@ -21,38 +21,13 @@

package io.sarl.m2e.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Strings;
import com.google.common.io.Files;
import org.apache.maven.artifact.Artifact;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.project.AbstractProjectScanner;
import org.eclipse.m2e.core.project.IMavenProjectImportResult;
import org.eclipse.m2e.core.project.LocalProjectScanner;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.osgi.framework.Version;

import io.sarl.eclipse.natures.SARLProjectConfigurator;
import io.sarl.m2e.SARLMavenEclipsePlugin;


/**
* M2E utilities.
Expand Down Expand Up @@ -147,115 +122,4 @@ public static Version parseMavenVersion(String version) {
return new Version(numbers[0], numbers[1], numbers[2]);
}

/** Create a fresh Maven project from existing files, assuming a pom file exists.
*
* <p>If the Maven project is associated to an existing {@code IProject}, then the source folders
* must be configured before the call to this function.
*
* @param workspaceRoot the workspace root.
* @param projectName the name of the project that is also the name of the project's folder.
* @param addSarlSpecificSourceFolders indicates if the source folders that are specific to SARL should be added.
* @param monitor the progress monitor.
* @since 0.8
*/
@SuppressWarnings("synthetic-access")
public static void importMavenProject(IWorkspaceRoot workspaceRoot, String projectName,
boolean addSarlSpecificSourceFolders, IProgressMonitor monitor) {
// TODO: The m2e plugin seems to have an issue for creating a fresh project with the SARL plugin as an extension.
// Solution: Create a simple project, and switch to a real SARL project.

final WorkspaceJob bugFixJob = new WorkspaceJob("Creating Simple Maven project") { //$NON-NLS-1$
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
forceSimplePom(workspaceRoot, projectName);
final AbstractProjectScanner<MavenProjectInfo> scanner = getProjectScanner(workspaceRoot, projectName);
scanner.run(monitor);
final ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration();
final List<IMavenProjectImportResult> importResults = MavenPlugin.getProjectConfigurationManager()
.importProjects(scanner.getProjects(), importConfiguration, null, monitor);
if (addSarlSpecificSourceFolders) {
for (final IMavenProjectImportResult importResult : importResults) {
SARLProjectConfigurator.configureSARLSourceFolders(
// Project to configure
importResult.getProject(),
// Create folders
true,
// Monitor
monitor);

final WorkspaceJob job = new WorkspaceJob("Creating Maven project") { //$NON-NLS-1$
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
restorePom(importResult.getProject());
MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(importResult.getProject(), monitor);
} catch (Exception exception) {
return SARLMavenEclipsePlugin.getDefault().createStatus(IStatus.ERROR, exception);
}
return Status.OK_STATUS;
}
};
job.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
job.schedule();
}
}
} catch (Exception exception) {
return SARLMavenEclipsePlugin.getDefault().createStatus(IStatus.ERROR, exception);
}
return Status.OK_STATUS;
}
};
bugFixJob.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
bugFixJob.schedule();
}

private static AbstractProjectScanner<MavenProjectInfo> getProjectScanner(IWorkspaceRoot workspaceRoot, String projectName) {
final File root = workspaceRoot.getLocation().toFile();
final String projectPath = new File(root, projectName).getAbsolutePath();
final MavenModelManager modelManager = MavenPlugin.getMavenModelManager();
return new LocalProjectScanner(
root,
projectPath,
false,
modelManager);
}

private static void restorePom(IProject project) throws Exception {
final NullProgressMonitor monitor = new NullProgressMonitor();
final IFile pomFile = project.getFile("pom.xml"); //$NON-NLS-1$
final IFile savedPomFile = project.getFile("pom.xml.bak"); //$NON-NLS-1$
pomFile.refreshLocal(IResource.DEPTH_ZERO, monitor);
savedPomFile.refreshLocal(IResource.DEPTH_ZERO, monitor);
if (savedPomFile.exists()) {
if (pomFile.exists()) {
pomFile.delete(true, false, monitor);
}
savedPomFile.copy(pomFile.getFullPath(), true, monitor);
savedPomFile.delete(true, false, monitor);
}
}

private static void forceSimplePom(IWorkspaceRoot workspaceRoot, String projectName) throws Exception {
final File projectDir = new File(workspaceRoot.getLocation().toFile(), projectName);
final File pomFile = new File(projectDir, "pom.xml"); //$NON-NLS-1$
if (pomFile.exists()) {
final File savedPomFile = new File(projectDir, "pom.xml.bak"); //$NON-NLS-1$
if (savedPomFile.exists()) {
savedPomFile.delete();
}
Files.copy(pomFile, savedPomFile);
final StringBuilder content = new StringBuilder();
try (BufferedReader stream = new BufferedReader(new FileReader(pomFile))) {
String line = stream.readLine();
while (line != null) {
line = line.replaceAll("<extensions>\\s*true\\s*</extensions>", ""); //$NON-NLS-1$ //$NON-NLS-2$
content.append(line).append("\n"); //$NON-NLS-1$
line = stream.readLine();
}
}
Files.write(content.toString().getBytes(), pomFile);
}
}

}

0 comments on commit 997ca04

Please sign in to comment.