From 1a40f907d1f28fc1ce713c3c49526b628ae24318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Thu, 16 Jul 2020 21:11:45 +0200 Subject: [PATCH] [examples] The user's JDK version is used into the pom files of the examples. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #1019 Signed-off-by: Stéphane Galland --- .../META-INF/MANIFEST.MF | 3 +- .../projects/io-sarl-demos-boids-awt/pom.xml | 2 +- .../projects/io-sarl-demos-fireworks/pom.xml | 2 +- .../projects/io-sarl-demos-gameoflife/pom.xml | 2 +- .../io-sarl-demos-sierpinskifractals/pom.xml | 2 +- .../projects/io-sarl-templates-javafx/pom.xml | 2 +- .../wizard/SarlExampleInstallerWizard.java | 53 +++++++++++++++++++ .../io.sarl.examples.tests/pom.xml | 4 ++ .../io/sarl/examples/tests/ExamplesTest.java | 3 +- .../examples/tests/ExamplesTestUtils.java | 38 +++++++++++++ 10 files changed, 104 insertions(+), 7 deletions(-) diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/META-INF/MANIFEST.MF b/contribs/io.sarl.examples/io.sarl.examples.plugin/META-INF/MANIFEST.MF index 4a40a23460..9dc04ac6cb 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/META-INF/MANIFEST.MF +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/META-INF/MANIFEST.MF @@ -19,7 +19,8 @@ Require-Bundle: io.sarl.eclipse;bundle-version="0.12.0", org.eclipse.core.resources;bundle-version="3.13.700", org.eclipse.jdt.launching;bundle-version="3.17.100", javax.inject;bundle-version="1.0.0", - com.google.inject;bundle-version="4.2.3" + com.google.inject;bundle-version="4.2.3", + org.eclipse.jdt.core;bundle-version="3.22.0" Export-Package: io.sarl.examples, io.sarl.examples.wizard Import-Package: io.sarl.m2e.wizards.importproject diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-boids-awt/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-boids-awt/pom.xml index 3f0a933b54..4e1908b21b 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-boids-awt/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-boids-awt/pom.xml @@ -10,7 +10,7 @@ @SARL_VERSION@ @AFC_VERSION@ - @JAVA_VERSION@ + @USER_JAVA_VERSION@ @FILE_ENCODING@ diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-fireworks/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-fireworks/pom.xml index 6dd18725e5..112a65c91f 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-fireworks/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-fireworks/pom.xml @@ -8,7 +8,7 @@ @SARL_VERSION@ - @JAVA_VERSION@ + @USER_JAVA_VERSION@ @FILE_ENCODING@ diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-gameoflife/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-gameoflife/pom.xml index 2f96767ea8..f36b51f72d 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-gameoflife/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-gameoflife/pom.xml @@ -9,7 +9,7 @@ @SARL_VERSION@ @AFC_VERSION@ - @JAVA_VERSION@ + @USER_JAVA_VERSION@ @FILE_ENCODING@ diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-sierpinskifractals/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-sierpinskifractals/pom.xml index 6dd18725e5..112a65c91f 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-sierpinskifractals/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-demos-sierpinskifractals/pom.xml @@ -8,7 +8,7 @@ @SARL_VERSION@ - @JAVA_VERSION@ + @USER_JAVA_VERSION@ @FILE_ENCODING@ diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-templates-javafx/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-templates-javafx/pom.xml index 6dd18725e5..112a65c91f 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-templates-javafx/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/projects/io-sarl-templates-javafx/pom.xml @@ -8,7 +8,7 @@ @SARL_VERSION@ - @JAVA_VERSION@ + @USER_JAVA_VERSION@ @FILE_ENCODING@ diff --git a/contribs/io.sarl.examples/io.sarl.examples.plugin/src/io/sarl/examples/wizard/SarlExampleInstallerWizard.java b/contribs/io.sarl.examples/io.sarl.examples.plugin/src/io/sarl/examples/wizard/SarlExampleInstallerWizard.java index 96668f18c1..b50bd19a69 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.plugin/src/io/sarl/examples/wizard/SarlExampleInstallerWizard.java +++ b/contribs/io.sarl.examples/io.sarl.examples.plugin/src/io/sarl/examples/wizard/SarlExampleInstallerWizard.java @@ -25,10 +25,14 @@ import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readLaunchConfigurationFromXml; import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readXmlContent; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; import javax.inject.Inject; import org.eclipse.core.resources.IFile; @@ -38,10 +42,14 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.debug.internal.ui.SWTFactory; import org.eclipse.emf.common.ui.wizard.ExampleInstallerWizard; +import org.eclipse.jdt.launching.AbstractVMInstall; +import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.JavaRuntime; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; @@ -50,6 +58,9 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.intro.IIntroManager; import org.eclipse.ui.intro.IIntroPart; +import org.eclipse.xtext.util.RuntimeIOException; +import org.eclipse.xtext.util.StringInputStream; +import org.osgi.framework.Version; import org.w3c.dom.Document; import io.sarl.eclipse.launching.config.ILaunchConfigurationConfigurator; @@ -57,6 +68,7 @@ import io.sarl.eclipse.natures.SARLProjectConfigurator; import io.sarl.examples.SARLExamplePlugin; import io.sarl.lang.SARLConfig; +import io.sarl.lang.SARLVersion; import io.sarl.m2e.wizards.importproject.MavenImportUtils; /** Wizard for importing SARL samples. @@ -150,6 +162,18 @@ protected void postProjectInstallation(ProjectDescriptor projectDescriptor, IPro final IProject project = projectDescriptor.getProject(); final IFile pomFile = project.getFile(Path.fromOSString("pom.xml")); //$NON-NLS-1$ + final boolean hasPomFile = pomFile.exists(); + if (hasPomFile) { + // Search for specific keywords into the pom file, and replace them by the user configuration. + String compliance = SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT; + final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall(); + if (vmInstall instanceof AbstractVMInstall) { + final AbstractVMInstall jvmInstall = (AbstractVMInstall) vmInstall; + final Version vers = Version.parseVersion(jvmInstall.getJavaVersion()); + compliance = vers.getMajor() + "." + vers.getMinor(); + } + updatePomContent(pomFile, compliance); + } if (this.configurationPage.isMavenNatureEnabled() && pomFile.exists()) { // The project should be a Maven project. final IPath descriptionFilename = project.getFile(new Path(IProjectDescription.DESCRIPTION_FILE_NAME)).getLocation(); @@ -216,6 +240,35 @@ protected void postProjectInstallation(ProjectDescriptor projectDescriptor, IPro mon.done(); } + private void updatePomContent(IFile pomFile, String jdkCompliance) { + // Read the pom + final StringBuilder content = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(pomFile.getContents()))) { + String line = reader.readLine(); + while (line != null) { + line = line.replaceAll(Pattern.quote("@USER_JAVA_VERSION@"), jdkCompliance); //$NON-NLS-1$ + content.append(line).append("\n"); //$NON-NLS-1$ + line = reader.readLine(); + } + } catch (Exception exception) { + throw new RuntimeIOException(exception); + } + // Delete the pom + try { + pomFile.delete(true, false, new NullProgressMonitor()); + } catch (CoreException exception) { + throw new RuntimeException(exception); + } + // Write the pom + try (StringInputStream is = new StringInputStream(content.toString())) { + pomFile.create(is, true, new NullProgressMonitor()); + } catch (CoreException exception) { + throw new RuntimeException(exception); + } catch (IOException exception) { + throw new RuntimeIOException(exception); + } + } + /** Replies the configurations to launch. * These configurations are inside an xml file with the name {@link #LAUNCH_PROPERTY_FILE}. * diff --git a/contribs/io.sarl.examples/io.sarl.examples.tests/pom.xml b/contribs/io.sarl.examples/io.sarl.examples.tests/pom.xml index b1eae41887..9227f66312 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.tests/pom.xml +++ b/contribs/io.sarl.examples/io.sarl.examples.tests/pom.xml @@ -65,6 +65,10 @@ (MAVEN_COMMAND *= *")([^"]+)(";) $1${mvn.command}$3 + + (CURRENT_JAVA_VERSION *= *")([^"]+)(";) + $1${sarl-dsl.min.jdk.version}$3 + (DEFAULT_JAVAFX_PATH *= *")([^"]+)(";) $1${openjfx.fxml.linux.path}$3 diff --git a/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTest.java b/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTest.java index 154886ae35..09dd4850ac 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTest.java +++ b/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTest.java @@ -35,7 +35,7 @@ import static io.sarl.examples.tests.ExamplesTestUtils.readFileToOpenFromXml; import static io.sarl.examples.tests.ExamplesTestUtils.readWizardClassesFromXml; import static io.sarl.examples.tests.ExamplesTestUtils.readXmlNode; -import static io.sarl.examples.tests.ExamplesTestUtils.unpackFiles; +import static io.sarl.examples.tests.ExamplesTestUtils.*; import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.LAUNCH_PROPERTY_FILE; import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readLaunchConfigurationFromXml; import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readXmlAttribute; @@ -90,6 +90,7 @@ private static List installFiles(ExampleDescription example, File projectR } else { installedFiles = unpackFiles(projectRoot, example.archive); } + preparePomFileForTest(projectRoot); return installedFiles; } diff --git a/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTestUtils.java b/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTestUtils.java index f9c0543fdd..77009d57fd 100644 --- a/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTestUtils.java +++ b/contribs/io.sarl.examples/io.sarl.examples.tests/src/test/java/io/sarl/examples/tests/ExamplesTestUtils.java @@ -27,6 +27,9 @@ import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; @@ -43,12 +46,14 @@ import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Pattern; import java.util.stream.Stream; import com.google.inject.Injector; import org.arakhne.afc.vmutil.ClasspathUtil; import org.arakhne.afc.vmutil.FileSystem; import org.eclipse.xtext.diagnostics.Severity; +import org.eclipse.xtext.util.RuntimeIOException; import org.eclipse.xtext.util.Strings; import org.eclipse.xtext.xbase.lib.Pair; import org.eclipse.xtext.xbase.validation.IssueCodes; @@ -96,6 +101,10 @@ private ExamplesTestUtils() { */ public static final String MAVEN_COMMAND = "mvn"; //$NON-NLS-1$ + /** Current Java version. + */ + public static final String CURRENT_JAVA_VERSION = "1.8"; //$NON-NLS-1$ + // TODO Remove this definition when moving to Java 9 or higher (because JavaFX is mavenized) public static final String DEFAULT_JAVAFX_PATH = "/home/sgalland/git/sarl.dsl/contribs/io.sarl.examples/io.sarl.examples.tests/../../../build-tools/libs/jfxrt.jar"; //$NON-NLS-1$ @@ -289,6 +298,35 @@ public static void assertFile(File file) { } } + /** Prepare the pom file for a test. + * + * @param root the root directory where the pom file is located. + * @since 0.12 + */ + public static void preparePomFileForTest(File root) { + final File pomFile = new File(root, "pom.xml"); //$NON-NLS-1$ + if (pomFile.exists() && pomFile.canWrite()) { + // Read the pom + final StringBuilder content = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new FileReader(pomFile))) { + String line = reader.readLine(); + while (line != null) { + line = line.replaceAll(Pattern.quote("@USER_JAVA_VERSION@"), CURRENT_JAVA_VERSION); //$NON-NLS-1$ + content.append(line).append("\n"); //$NON-NLS-1$ + line = reader.readLine(); + } + } catch (Exception exception) { + throw new RuntimeIOException(exception); + } + // Write the pom + try (FileWriter os = new FileWriter(pomFile)) { + os.write(content.toString()); + } catch (IOException exception) { + throw new RuntimeIOException(exception); + } + } + } + /** Compile the given project with the standard maven tool. * * @param compiler the SARL compiler to use.