Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Fix eclipse plugin to handle groovy projects. Slight modification of patch supplied by Shaun Mangelsdorf.
  • Loading branch information
adammurdoch committed Dec 8, 2009
1 parent 9f1f89c commit becfafc
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -71,6 +71,7 @@ def FIRST_LEVEL_JMOCK = ['org.hamcrest:hamcrest-core:1.1@jar', 'org.hamcrest:ham
configure(groovyProjects()) {
usePlugin 'groovy'
usePlugin 'code-quality'
usePlugin 'eclipse'

archivesBaseName = "gradle-${name.replaceAll("\\p{Upper}") { "-${it.toLowerCase()}" } }"
dependencies {
Expand Down
Expand Up @@ -100,15 +100,23 @@ public boolean hasMethod(String method, Object... args) {
}

public <T> T getPlugin(Class<T> type) {
T value = findPlugin(type);
if (value == null) {
throw new IllegalStateException(String.format("Could not find any convention object of type %s.",
type.getSimpleName()));
}
return value;
}

public <T> T findPlugin(Class<T> type) throws IllegalStateException {
List<T> values = new ArrayList<T>();
for (Object object : plugins.values()) {
if (type.isInstance(object)) {
values.add(type.cast(object));
}
}
if (values.isEmpty()) {
throw new IllegalStateException(String.format("Could not find any convention object of type %s.",
type.getSimpleName()));
return null;
}
if (values.size() > 1) {
throw new IllegalStateException(String.format("Found multiple convention objects of type %s.",
Expand Down
Expand Up @@ -45,4 +45,13 @@ public interface Convention extends DynamicObject {
* multiple such objects.
*/
<T> T getPlugin(Class<T> type) throws IllegalStateException;

/**
* Locates the plugin convention object with the given type.
*
* @param type The convention object type.
* @return The object. Returns null if there is no such object.
* @throws IllegalStateException When there there are multiple matching objects.
*/
<T> T findPlugin(Class<T> type) throws IllegalStateException;
}
Expand Up @@ -30,6 +30,14 @@ public List<String> natureNames() {
return WrapUtil.toList("org.eclipse.jdt.core.javanature");
}
};
public static final ProjectType GROOVY = new ProjectType() {
public List<String> buildCommandNames() {
return WrapUtil.toList("org.eclipse.jdt.core.javabuilder");
}
public List<String> natureNames() {
return WrapUtil.toList("org.eclipse.jdt.groovy.core.groovyNature", "org.eclipse.jdt.core.javanature");
}
};
public static final ProjectType SIMPLE = new ProjectType() {
public List<String> buildCommandNames() {
return new ArrayList<String>();
Expand Down
Expand Up @@ -94,9 +94,11 @@ class DefaultConventionTest {
@Test public void testCanLocateConventionObjectByType() {
assertSame(convention1, convention.getPlugin(TestPluginConvention1))
assertSame(convention2, convention.getPlugin(TestPluginConvention2))
assertSame(convention1, convention.findPlugin(TestPluginConvention1))
assertSame(convention2, convention.findPlugin(TestPluginConvention2))
}

@Test public void testFailsWhenMultipleConventionObjectsWithCompatibleType() {
@Test public void testGetPluginFailsWhenMultipleConventionObjectsWithCompatibleType() {
try {
convention.getPlugin(Object)
fail()
Expand All @@ -105,12 +107,25 @@ class DefaultConventionTest {
}
}

@Test public void testFailsWhenNoConventionObjectsWithCompatibleType() {
@Test public void testFindPluginFailsWhenMultipleConventionObjectsWithCompatibleType() {
try {
convention.getPlugin(Object)
fail()
} catch (java.lang.IllegalStateException e) {
assertThat(e.message, equalTo('Found multiple convention objects of type Object.'))
}
}

@Test public void testGetPluginFailsWhenNoConventionObjectsWithCompatibleType() {
try {
convention.getPlugin(String)
fail()
} catch (java.lang.IllegalStateException e) {
assertThat(e.message, equalTo('Could not find any convention object of type String.'))
}
}

@Test public void testFindPluginReturnsNullWhenNoConventionObjectsWithCompatibleType() {
assertNull(convention.findPlugin(String))
}
}
Expand Up @@ -44,6 +44,13 @@ public void setUp() {
eclipseProject.setProjectName("myProject");
}

@Test
public void generateGroovyProject() throws IOException {
eclipseProject.setProjectType(ProjectType.GROOVY);
eclipseProject.execute();
checkProjectFile("expectedGroovyProjectFile.txt");
}

@Test
public void generateJavaProject() throws IOException {
eclipseProject.setProjectType(ProjectType.JAVA);
Expand Down
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<projectDescription>
<name>myProject</name>
<comment/>
<projects/>
<natures>
<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
</projectDescription>
Expand Up @@ -24,11 +24,13 @@
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.artifacts.specs.DependencySpecs;
import org.gradle.api.artifacts.specs.Type;
import org.gradle.api.internal.DynamicObjectAware;
import org.gradle.api.internal.IConventionAware;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.gradle.api.tasks.ConventionValue;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.GroovySourceSet;
import org.gradle.api.tasks.bundling.War;
import org.gradle.api.tasks.ide.eclipse.*;
import org.gradle.util.GUtil;
Expand All @@ -39,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
* <p>A {@link org.gradle.api.Plugin} which generates Eclipse project files for projects that use the {@link
Expand Down Expand Up @@ -80,24 +83,30 @@ private void configureEclipseProjectAndClasspath(Project project) {
private EclipseProject configureEclipseProject(Project project) {
EclipseProject eclipseProject = project.getTasks().add(ECLIPSE_PROJECT_TASK_NAME, EclipseProject.class);
eclipseProject.setProjectName(project.getName());
eclipseProject.setProjectType(ProjectType.JAVA);
eclipseProject.setProjectType(selectEclipseProjectType(project));
eclipseProject.setDescription("Generates an Eclipse .project file.");
return eclipseProject;
}

private ProjectType selectEclipseProjectType(Project project) {
if (project.getPlugins().hasPlugin(GroovyPlugin.class)) {
return ProjectType.GROOVY;
}

return ProjectType.JAVA;
}

private EclipseClasspath configureEclipseClasspath(final Project project) {
EclipseClasspath eclipseClasspath = project.getTasks().add(ECLIPSE_CP_TASK_NAME, EclipseClasspath.class);
eclipseClasspath.getConventionMapping().map(GUtil.map(
"srcDirs", new ConventionValue() {
public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
SourceSet sourceSet = java(convention).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
return GUtil.addLists(sourceSet.getJava().getSrcDirs(), sourceSet.getResources().getSrcDirs());
return allLanguageSrcDirs(convention, SourceSet.MAIN_SOURCE_SET_NAME);
}
},
"testSrcDirs", new ConventionValue() {
public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
SourceSet sourceSet = java(convention).getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
return GUtil.addLists(sourceSet.getJava().getSrcDirs(), sourceSet.getResources().getSrcDirs());
return allLanguageSrcDirs(convention, SourceSet.TEST_SOURCE_SET_NAME);
}
},
"outputDirectory", new ConventionValue() {
Expand Down Expand Up @@ -219,4 +228,27 @@ protected JavaPluginConvention java(Convention convention) {
private WarPluginConvention war(Convention convention) {
return convention.getPlugin(WarPluginConvention.class);
}
}

private SourceSet sourceSet(Convention convention, String name) {
return java(convention).getSourceSets().getByName(name);
}

private GroovySourceSet groovySourceSet(Convention convention, String name) {
return ((DynamicObjectAware) sourceSet(convention, name)).getConvention().findPlugin(GroovySourceSet.class);
}

private Object allLanguageSrcDirs(Convention convention, String name) {
SourceSet sourceSet = sourceSet(convention, name);

Set<Object> extraDirs = new TreeSet<Object>();
GroovySourceSet groovySourceSet = groovySourceSet(convention, name);
if (groovySourceSet != null) {
extraDirs.addAll(groovySourceSet.getGroovy().getSrcDirs());
}

return GUtil.addLists(
sourceSet.getJava().getSrcDirs(),
sourceSet.getResources().getSrcDirs(),
extraDirs);
}
}

0 comments on commit becfafc

Please sign in to comment.