Skip to content

Commit

Permalink
Fix for #890: add jars for shell/console and fix bootstrap classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 13, 2019
1 parent 93bf81f commit f0ef8f8
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 105 deletions.
9 changes: 9 additions & 0 deletions base/org.codehaus.groovy24/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,14 @@ <h4>ivy-2.4.0.jar</h4>
<li>License text: <a href="about_files/asl2-license.txt">asl2-license.txt</a></li>
</ul>

<h4>jline-2.12.jar</h4>

<ul>
<li>Obtained from: <a href="https://dl.bintray.com/groovy/maven/apache-groovy-sdk-2.4.17.zip">https://dl.bintray.com/groovy/maven/apache-groovy-sdk-2.4.17.zip</a></li>
<li>License kind: ASL</li>
<li>License URL: <a href="https://www.opensource.org/licenses/bsd-license.php">https://www.opensource.org/licenses/bsd-license.php</a></li>
<li>License text: <a href="about_files/jline2-license.txt">jline2-license.txt</a></li>
</ul>

</body>
</html>
Binary file modified base/org.codehaus.groovy24/lib/groovy-all-2.4.17-javadoc.jar
Binary file not shown.
Binary file modified base/org.codehaus.groovy24/lib/groovy-all-2.4.17-sources.jar
Binary file not shown.
Binary file modified base/org.codehaus.groovy24/lib/groovy-all-2.4.17.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@
package org.codehaus.groovy.eclipse.launchers;

import static org.codehaus.groovy.eclipse.core.compiler.CompilerUtils.getActiveGroovyBundle;
import static org.codehaus.groovy.eclipse.core.compiler.CompilerUtils.getExportedGroovyAllJar;
import static org.codehaus.groovy.eclipse.core.compiler.CompilerUtils.getExtraJarsForClasspath;
import static org.eclipse.jdt.launching.JavaRuntime.newArchiveRuntimeClasspathEntry;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SortedSet;
import java.util.Set;
import java.util.TreeSet;

import org.codehaus.groovy.eclipse.GroovyPlugin;
Expand All @@ -39,8 +45,10 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
Expand All @@ -58,7 +66,6 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.groovy.core.util.ScriptFolderSelector;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand Down Expand Up @@ -124,9 +131,6 @@ protected String mainArgs(IType runType, IJavaProject javaProject) {

@Override
public void launch(IEditorPart editor, String mode) {
// make sure we are saved as we run groovy from the file
editor.getEditorSite().getPage().saveEditor(editor, false);

ICompilationUnit unit = Adapters.adapt(editor, GroovyCompilationUnit.class);
if (unit != null) {
launchGroovy(unit, unit.getJavaProject(), mode);
Expand Down Expand Up @@ -174,12 +178,10 @@ private IJavaProject extractJavaProject(ISelection selection) {
}

/**
* Finds or creates a launch configuration for the given unit then launches it.
* Finds or creates a launch configuration for the given unit and then launches it.
*/
protected void launchGroovy(ICompilationUnit unit, IJavaProject javaProject, String mode) {
IType runType = null;

// if unit is null, then we are not looking for a run type
if (unit != null) {
IType[] types = null;
try {
Expand All @@ -194,16 +196,30 @@ protected void launchGroovy(ICompilationUnit unit, IJavaProject javaProject, Str
return;
}
}
Map<String, String> launchConfigProperties = createLaunchProperties(runType, javaProject);
Map<String, String> launchProperties = createLaunchProperties(runType, javaProject);

try {
ILaunchConfigurationWorkingCopy workingConfig = findOrCreateLaunchConfig(launchConfigProperties,
runType != null ? runType.getElementName() : javaProject.getElementName());
workingConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH,
Arrays.asList(JavaRuntime.computeDefaultRuntimeClassPath(javaProject)));
ILaunchConfiguration config = workingConfig.doSave();
String launchName = (runType != null ? runType.getElementName() : javaProject.getElementName());
ILaunchConfigurationWorkingCopy workingCopy = findOrCreateLaunchConfig(launchProperties, launchName);

List<String> classpath = new ArrayList<>();
classpath.add(newArchiveRuntimeClasspathEntry(getExportedGroovyAllJar()).getMemento());
for (IPath jarPath : getExtraJarsForClasspath()) {
classpath.add(newArchiveRuntimeClasspathEntry(jarPath).getMemento());
}
Enumeration<URL> jarUrls = getActiveGroovyBundle().findEntries("lib/" + applicationOrConsole().toLowerCase(), "*.jar", false);
if (jarUrls != null) {
for (URL jarUrl : Collections.list(jarUrls)) {
classpath.add(newArchiveRuntimeClasspathEntry(new Path(FileLocator.toFileURL(jarUrl).getPath())).getMemento());
}
}

workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath);
workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);

ILaunchConfiguration config = workingCopy.doSave();
DebugUITools.launch(config, mode);
} catch (CoreException e) {
} catch (CoreException | IOException e) {
GroovyCore.errorRunningGroovyFile((IFile) unit.getResource(), e);
}
}
Expand Down Expand Up @@ -263,8 +279,8 @@ private String getWorkingDirectory(IType runType, IJavaProject javaProject) {
}

private String generateClasspath(IJavaProject javaProject) {
SortedSet<String> sourceEntries = new TreeSet<>();
SortedSet<String> binEntries = new TreeSet<>();
Set<String> sourceEntries = new TreeSet<>();
Set<String> binEntries = new TreeSet<>();
addClasspathEntriesForProject(javaProject, sourceEntries, binEntries);
StringBuilder sb = new StringBuilder();
sb.append("\"");
Expand All @@ -287,7 +303,7 @@ private String generateClasspath(IJavaProject javaProject) {
* Need to recursively walk the classpath and visit all dependent projects
* Not looking at classpath containers yet.
*/
private void addClasspathEntriesForProject(IJavaProject javaProject, SortedSet<String> sourceEntries, SortedSet<String> binEntries) {
private void addClasspathEntriesForProject(IJavaProject javaProject, Set<String> sourceEntries, Set<String> binEntries) {
List<IJavaProject> dependingProjects = new ArrayList<>();
try {
IClasspathEntry[] entries = javaProject.getRawClasspath();
Expand Down Expand Up @@ -379,51 +395,31 @@ private IType findClassToRun(IType[] types) {
}

/**
* This method will find a Launch configration that matches the passed
* properties of if one is not found will create one.
*
* @param configProperties A <String, String> Map of launch configuration properties.
* @param classUnderTest The name of the class (without package) that is being tested.
* @return Returns a launch configuration for the class under test with the passed properties.
* Finds a launch configration that matches the given properties or creates one.
*/
protected ILaunchConfigurationWorkingCopy findOrCreateLaunchConfig(Map<String, String> configProperties, String simpleMainTypeName) throws CoreException {
String projectName = configProperties.get(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME);
ILaunchConfiguration config = findConfiguration(projectName, configProperties.get(GROOVY_TYPE_TO_RUN));
return (config != null ? config.getWorkingCopy() : createLaunchConfig(configProperties, simpleMainTypeName));
protected ILaunchConfigurationWorkingCopy findOrCreateLaunchConfig(Map<String, String> launchProperties, String launchName) throws CoreException {
String projectName = launchProperties.get(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME);
ILaunchConfiguration config = findConfiguration(projectName, launchProperties.get(GROOVY_TYPE_TO_RUN));
return (config != null ? config.getWorkingCopy() : createLaunchConfig(launchProperties, launchName));
}

/**
* This method creates a new launch configuration working copy for the
* classUnderTest with the properties defined in configProperites.
*
* @param configProperties A <String, String> Map of launch configuration properties.
* @param classUnderTest The name of the class (without package) that is being tested.
* @return Returns a new launch configuration.
* Creates a launch configuration for the given name and properties.
*/
private ILaunchConfigurationWorkingCopy createLaunchConfig(Map<String, String> configProperties, String classUnderTest)
throws CoreException {
String launchName = getLaunchManager().generateLaunchConfigurationName(classUnderTest);
ILaunchConfigurationWorkingCopy returnConfig = getGroovyLaunchConfigType().newInstance(null, launchName);
for (Map.Entry<String, String> entry : configProperties.entrySet()) {
returnConfig.setAttribute(entry.getKey(), entry.getValue());
private ILaunchConfigurationWorkingCopy createLaunchConfig(Map<String, String> launchProperties, String launchName) throws CoreException {
ILaunchConfigurationWorkingCopy workingCopy = getGroovyLaunchConfigType().newInstance(
null, getLaunchManager().generateLaunchConfigurationName(launchName));

for (Map.Entry<String, String> entry : launchProperties.entrySet()) {
workingCopy.setAttribute(entry.getKey(), entry.getValue());
}
return returnConfig;

return workingCopy;
}

/**
* This class finds any launch configrations that match the defined
* properties. If more that one match is found the user is prompted
* to select one.
*
* Semantics now matches {@link JavaLaunchShortcut}. If the main type name
* and the
* project name match, then this is considered a match.
*
* @param configProperties A <String, String> Map of properties to check
* when searching for a matching launch configuration.
* @return Returns a launch configuration that matches the given properties
* if a match is found, otherwise returns null.
* @throws CoreException
* Finds any launch configrations that match the project and type names.
* If more than one match is found the user is prompted to select one.
*/
private ILaunchConfiguration findConfiguration(String projectName, String mainTypeName) throws CoreException {
ILaunchConfiguration returnValue = null;
Expand All @@ -450,7 +446,7 @@ private ILaunchConfiguration findConfiguration(String projectName, String mainTy
* Prompts the user to select a launch configuration from {@code configList}.
*
* @param configList A List of ILaunchConfigrations for the user to pick from.
* @return Returns the ILaunchConfiguration that the user selected.
* @return The ILaunchConfiguration that the user selected.
*/
private ILaunchConfiguration chooseConfiguration(List<ILaunchConfiguration> configList) {
IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,42 @@
*/
package org.codehaus.groovy.eclipse.launchers;

import java.util.Arrays;

import org.codehaus.groovy.eclipse.GroovyPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.util.CompilerUtils;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

public class GroovyShellLaunchShortcut implements ILaunchShortcut {
public class GroovyShellLaunchShortcut extends AbstractGroovyLaunchShortcut {

@Override
public void launch(IEditorPart editor, String mode) {
// make sure we are saved as we run groovy from the file
editor.getEditorSite().getPage().saveEditor(editor, false);
IFile file = Adapters.adapt(editor.getEditorInput(), IFile.class);
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
if (unit.getJavaProject() != null) {
launchGroovy(unit.getJavaProject(), mode);
}
protected ILaunchConfigurationType getGroovyLaunchConfigType() {
return getLaunchManager().getLaunchConfigurationType("org.codehaus.groovy.eclipse.groovyShellLaunchConfiguration");
}

@Override
public void launch(ISelection selection, String mode) {
if (selection instanceof IStructuredSelection && ((IStructuredSelection) selection).getFirstElement() instanceof IJavaElement) {
IStructuredSelection structredSelection = (IStructuredSelection) selection;
Object elem = structredSelection.getFirstElement();
if (elem instanceof IJavaElement) {
launchGroovy(((IJavaElement) elem).getJavaProject(), mode);
}
}
protected String applicationOrConsole() {
return "Shell";
}

private void launchGroovy(IJavaProject project, String mode) {
try {
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
String launchName = launchManager.generateLaunchConfigurationName(project.getProject().getName());
ILaunchConfigurationType launchType = launchManager.getLaunchConfigurationType("org.codehaus.groovy.eclipse.groovyShellLaunchConfiguration");

ILaunchConfigurationWorkingCopy launchConfig = launchType.newInstance(null, launchName);
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getElementName());
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "org.codehaus.groovy.tools.shell.Main");
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "-Djline.terminal=jline.UnsupportedTerminal");
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, Arrays.asList(JavaRuntime.computeDefaultRuntimeClassPath(project)));
@Override
protected boolean canLaunchWithNoType() {
return true;
}

DebugUITools.launch(launchConfig, mode);
} catch (Exception e) {
GroovyPlugin.getDefault().logError("Exception launching Groovy Shell", e);
@Override
protected String mainArgs(IType runType, IJavaProject javaProject) {
StringBuilder mainArgs = new StringBuilder("org.codehaus.groovy.tools.shell.Main");
mainArgs.append(" --define jline.terminal=jline.UnsupportedTerminal");

if (isAtLeastGroovy(2, 5, 0)) {
CompilerOptions compilerOptions = new CompilerOptions(javaProject.getOptions(true));
CompilerUtils.configureOptionsBasedOnNature(compilerOptions, javaProject);
if (compilerOptions.produceMethodParameters) {
mainArgs.append(" --parameters");
}
}

return mainArgs.toString();
}
}

0 comments on commit f0ef8f8

Please sign in to comment.