Skip to content

Commit

Permalink
[examples] The user's JDK version is used into the pom files of the e…
Browse files Browse the repository at this point in the history
…xamples.

close #1019

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 17, 2020
1 parent 91d72c4 commit 1a40f90
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<properties>
<sarl.version>@SARL_VERSION@</sarl.version>
<afc.version>@AFC_VERSION@</afc.version>
<compiler.level>@JAVA_VERSION@</compiler.level>
<compiler.level>@USER_JAVA_VERSION@</compiler.level>
<project.build.sourceEncoding>@FILE_ENCODING@</project.build.sourceEncoding>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<properties>
<sarl.version>@SARL_VERSION@</sarl.version>
<target.jdk.version>@JAVA_VERSION@</target.jdk.version>
<target.jdk.version>@USER_JAVA_VERSION@</target.jdk.version>
<project.build.sourceEncoding>@FILE_ENCODING@</project.build.sourceEncoding>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<properties>
<sarl.version>@SARL_VERSION@</sarl.version>
<afc.version>@AFC_VERSION@</afc.version>
<target.jdk.version>@JAVA_VERSION@</target.jdk.version>
<target.jdk.version>@USER_JAVA_VERSION@</target.jdk.version>
<project.build.sourceEncoding>@FILE_ENCODING@</project.build.sourceEncoding>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<properties>
<sarl.version>@SARL_VERSION@</sarl.version>
<target.jdk.version>@JAVA_VERSION@</target.jdk.version>
<target.jdk.version>@USER_JAVA_VERSION@</target.jdk.version>
<project.build.sourceEncoding>@FILE_ENCODING@</project.build.sourceEncoding>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<properties>
<sarl.version>@SARL_VERSION@</sarl.version>
<target.jdk.version>@JAVA_VERSION@</target.jdk.version>
<target.jdk.version>@USER_JAVA_VERSION@</target.jdk.version>
<project.build.sourceEncoding>@FILE_ENCODING@</project.build.sourceEncoding>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,13 +58,17 @@
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;
import io.sarl.eclipse.launching.shortcuts.SarlStandardClasspathProvider;
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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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}.
*
Expand Down
4 changes: 4 additions & 0 deletions contribs/io.sarl.examples/io.sarl.examples.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<token>(MAVEN_COMMAND *= *")([^"]+)(";)</token>
<value>$1${mvn.command}$3</value>
</replacement>
<replacement>
<token>(CURRENT_JAVA_VERSION *= *")([^"]+)(";)</token>
<value>$1${sarl-dsl.min.jdk.version}$3</value>
</replacement>
<replacement>
<token>(DEFAULT_JAVAFX_PATH *= *")([^"]+)(";)</token>
<value>$1${openjfx.fxml.linux.path}$3</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +90,7 @@ private static List<File> installFiles(ExampleDescription example, File projectR
} else {
installedFiles = unpackFiles(projectRoot, example.archive);
}
preparePomFileForTest(projectRoot);
return installedFiles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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$

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1a40f90

Please sign in to comment.