Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eclipse support #590

Merged
merged 9 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
needs: build
strategy:
matrix:
tests: [ProtobufJavaPluginTest, ProtobufKotlinDslCopySpecTest, ProtobufKotlinDslPluginTest, ProtobufAndroidPluginTest, ProtobufAndroidPluginKotlinTest, AndroidProjectDetectionTest]
tests: [ProtobufJavaPluginTest, ProtobufKotlinDslCopySpecTest, ProtobufKotlinDslPluginTest, ProtobufAndroidPluginTest, ProtobufAndroidPluginKotlinTest, AndroidProjectDetectionTest, IDESupportTest]
fail-fast: false
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class ProtobufPlugin implements Plugin<Project> {
project.tasks.withType(GenerateProtoTask).each { GenerateProtoTask generateProtoTask ->
generateProtoTask.getOutputSourceDirectories().each { File outputDir ->
Utils.addToIdeSources(project, generateProtoTask.isTest, outputDir, true)
Utils.addToEclipseSources(project, generateProtoTask.isTest, generateProtoTask.sourceSet.name, outputDir)
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import groovy.transform.CompileStatic
import org.apache.commons.lang.StringUtils
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import org.gradle.plugins.ide.eclipse.model.Classpath
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry
import org.gradle.plugins.ide.eclipse.model.EclipseModel
import org.gradle.plugins.ide.eclipse.model.SourceFolder
import org.gradle.plugins.ide.idea.GenerateIdeaModule
import org.gradle.plugins.ide.idea.model.IdeaModel
import org.gradle.util.GUtil
Expand Down Expand Up @@ -129,4 +133,78 @@ class Utils {
}
}
}

/**
* Add the the folder of generated source to Eclipse .classpath file.
*/
static void addToEclipseSources(Project project, boolean isTest, String sourceSetName, File f) {
project.plugins.withId("eclipse") {
File projectDir = project.getProjectDir()

EclipseModel model = project.getExtensions().findByType(EclipseModel)
model.classpath.file.whenMerged { Classpath cp ->
// buildship requires the folder exists on disk, otherwise
// it will be ignored when updating classpath file, see:
// https://github.com/eclipse/buildship/issues/1196
f.mkdirs()

String relativePath = projectDir.toURI().relativize(f.toURI()).getPath()
// remove trailing slash
if (relativePath.endsWith("/")) {
relativePath = relativePath[0..(relativePath.length() - 2)]
}
SourceFolder entry = new SourceFolder(relativePath, getOutputPath(cp, sourceSetName))
entry.entryAttributes.put("optional", "true")
entry.entryAttributes.put("ignore_optional_problems", "true")

// this attribute is optional, but it could be useful for IDEs to recognize that it is
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
// generated source folder from protobuf, thus providing some support for that.
// e.g. Hint user to run generate tasks if the folder is empty or does not exist.
entry.entryAttributes.put("protobuf_generated_source", "true")

// check if output is not null here because test source folder
// must have a separate output folder in Eclipse
if (entry.output != null && isTest) {
entry.entryAttributes.put("test", "true")
}
cp.entries.add(entry)
}
}
}

/**
* Get the output path according to the source set name, if no valid path can be
* found, <code>null</code> will return.
*/
private static String getOutputPath(Classpath classpath, String sourceSetName) {
String path = "bin/" + sourceSetName
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
if (isValidOutput(classpath, path)) {
return path
}
// fallback to default output
return null
}

/**
* Check if the output path is valid or not.
* See: org.eclipse.jdt.internal.core.ClasspathEntry#validateClasspath()
*/
private static boolean isValidOutput(Classpath classpath, String path) {
Set<String> outputs = []
for (ClasspathEntry cpe : classpath.getEntries()) {
if (cpe instanceof SourceFolder) {
outputs.add(((SourceFolder) cpe).getOutput())
}
}
for (String output : outputs) {
if (Objects.equals(output, path)) {
continue
}
// Eclipse does not allow nested output path
if (output.startsWith(path) || path.startsWith(output)) {
return false
}
}
return true
}
}
142 changes: 142 additions & 0 deletions src/test/groovy/com/google/protobuf/gradle/IDESupportTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.google.protobuf.gradle
rougsig marked this conversation as resolved.
Show resolved Hide resolved

import com.google.common.collect.ImmutableSet
import groovy.transform.CompileDynamic
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import spock.lang.Specification
import spock.lang.Unroll

/**
* Unit tests to check ide metadata generation
*/
@CompileDynamic
class IDESupportTest extends Specification {
// Current supported version is Gradle 5+.
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0", "6.7.1", "7.4.2"]

@Unroll
void "testProject proto and generated output directories should be added to intellij [gradle #gradleVersion]"() {
given: "project from testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testIdea')
.copyDirs('testProjectBase', 'testProject')
.build()

when: "idea is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('idea')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.build()

then: "it succeed"
result.task(":idea").outcome == TaskOutcome.SUCCESS
Node imlRoot = new XmlParser().parse(projectDir.toPath().resolve("testProject.iml").toFile())
Collection rootMgr = imlRoot.component.findAll { it.'@name' == 'NewModuleRootManager' }
assert rootMgr.size() == 1
assert rootMgr.content.sourceFolder.size() == 1

Set<String> sourceDir = [] as Set
Set<String> testSourceDir = [] as Set
Set<String> generatedDirs = [] as Set
rootMgr.content.sourceFolder[0].each {
if (Boolean.parseBoolean(it.@isTestSource)) {
testSourceDir.add(it.@url)
} else {
sourceDir.add(it.@url)
}
if (Boolean.parseBoolean(it.@generated)) {
generatedDirs.add(it.@url)
}
}

Set<String> expectedSourceDir = ImmutableSet.builder()
.add('file://$MODULE_DIR$/src/main/java')
.add('file://$MODULE_DIR$/src/grpc/proto')
.add('file://$MODULE_DIR$/src/main/proto')
.add('file://$MODULE_DIR$/build/extracted-include-protos/grpc')
.add('file://$MODULE_DIR$/build/extracted-protos/main')
.add('file://$MODULE_DIR$/build/extracted-include-protos/main')
.add('file://$MODULE_DIR$/build/extracted-protos/grpc')
.add('file://$MODULE_DIR$/build/generated/source/proto/grpc/java')
.add('file://$MODULE_DIR$/build/generated/source/proto/grpc/grpc_output')
.add('file://$MODULE_DIR$/build/generated/source/proto/main/java')
.build()
Set<String> expectedTestSourceDir = ImmutableSet.builder()
.add('file://$MODULE_DIR$/src/test/java')
.add('file://$MODULE_DIR$/src/test/proto')
.add('file://$MODULE_DIR$/build/extracted-protos/test')
.add('file://$MODULE_DIR$/build/extracted-include-protos/test')
.add('file://$MODULE_DIR$/build/generated/source/proto/test/java')
.build()
Set<String> expectedGeneratedDirs = [
'file://$MODULE_DIR$/build/extracted-include-protos/grpc',
'file://$MODULE_DIR$/build/extracted-protos/main',
'file://$MODULE_DIR$/build/extracted-include-protos/main',
'file://$MODULE_DIR$/build/extracted-protos/grpc',
'file://$MODULE_DIR$/build/generated/source/proto/grpc/java',
'file://$MODULE_DIR$/build/generated/source/proto/grpc/grpc_output',
'file://$MODULE_DIR$/build/generated/source/proto/main/java',
'file://$MODULE_DIR$/build/extracted-protos/test',
'file://$MODULE_DIR$/build/extracted-include-protos/test',
'file://$MODULE_DIR$/build/generated/source/proto/test/java',
]
assert Objects.equals(expectedSourceDir, sourceDir)
assert Objects.equals(expectedTestSourceDir, testSourceDir)
Objects.equals(expectedGeneratedDirs, generatedDirs)

where:
gradleVersion << GRADLE_VERSIONS
}

@Unroll
void "testProject proto and generated output directories should be added to Eclipse [gradle #gradleVersion]"() {
given: "project from testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testEclipse')
.copyDirs('testProjectBase', 'testProject')
.build()

when: "eclipse is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('eclipse')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.build()

then: "it succeed"
result.task(":eclipse").outcome == TaskOutcome.SUCCESS
Node classpathFile = new XmlParser().parse(projectDir.toPath().resolve(".classpath").toFile())
Collection srcEntries = classpathFile.classpathentry.findAll { it.'@kind' == 'src' }
assert srcEntries.size() == 6

Set<String> sourceDir = [] as Set
srcEntries.each {
String path = it.@path
sourceDir.add(path)
if (path.startsWith("build/generated/source/proto")) {
if (path.contains("test")) {
// test source path has one more attribute: ["test"="true"]
assert it.attributes.attribute.size() == 4
} else {
assert it.attributes.attribute.size() == 3
}
}
}

Set<String> expectedSourceDir = ImmutableSet.builder()
.add('src/main/java')
.add('src/test/java')
.add('build/generated/source/proto/grpc/java')
.add('build/generated/source/proto/grpc/grpc_output')
.add('build/generated/source/proto/main/java')
.add('build/generated/source/proto/test/java')
.build()
assert Objects.equals(expectedSourceDir, sourceDir)

where:
gradleVersion << GRADLE_VERSIONS
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.google.protobuf.gradle

import com.google.common.collect.ImmutableSet
import groovy.transform.CompileDynamic
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
Expand Down Expand Up @@ -339,81 +338,6 @@ class ProtobufJavaPluginTest extends Specification {
gradleVersion << GRADLE_VERSIONS
}
@Unroll
void "testProject proto and generated output directories should be added to intellij [gradle #gradleVersion]"() {
given: "project from testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testIdea')
.copyDirs('testProjectBase', 'testProject')
.build()
when: "idea is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments('idea')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.build()
then: "it succeed"
result.task(":idea").outcome == TaskOutcome.SUCCESS
Node imlRoot = new XmlParser().parse(projectDir.toPath().resolve("testProject.iml").toFile())
Collection rootMgr = imlRoot.component.findAll { it.'@name' == 'NewModuleRootManager' }
assert rootMgr.size() == 1
assert rootMgr.content.sourceFolder.size() == 1
Set<String> sourceDir = [] as Set
Set<String> testSourceDir = [] as Set
Set<String> generatedDirs = [] as Set
rootMgr.content.sourceFolder[0].each {
if (Boolean.parseBoolean(it.@isTestSource)) {
testSourceDir.add(it.@url)
} else {
sourceDir.add(it.@url)
}
if (Boolean.parseBoolean(it.@generated)) {
generatedDirs.add(it.@url)
}
}
Set<String> expectedSourceDir = ImmutableSet.builder()
.add('file://$MODULE_DIR$/src/main/java')
.add('file://$MODULE_DIR$/src/grpc/proto')
.add('file://$MODULE_DIR$/src/main/proto')
.add('file://$MODULE_DIR$/build/extracted-include-protos/grpc')
.add('file://$MODULE_DIR$/build/extracted-protos/main')
.add('file://$MODULE_DIR$/build/extracted-include-protos/main')
.add('file://$MODULE_DIR$/build/extracted-protos/grpc')
.add('file://$MODULE_DIR$/build/generated/source/proto/grpc/java')
.add('file://$MODULE_DIR$/build/generated/source/proto/grpc/grpc_output')
.add('file://$MODULE_DIR$/build/generated/source/proto/main/java')
.build()
Set<String> expectedTestSourceDir = ImmutableSet.builder()
.add('file://$MODULE_DIR$/src/test/java')
.add('file://$MODULE_DIR$/src/test/proto')
.add('file://$MODULE_DIR$/build/extracted-protos/test')
.add('file://$MODULE_DIR$/build/extracted-include-protos/test')
.add('file://$MODULE_DIR$/build/generated/source/proto/test/java')
.build()
Set<String> expectedGeneratedDirs = [
'file://$MODULE_DIR$/build/extracted-include-protos/grpc',
'file://$MODULE_DIR$/build/extracted-protos/main',
'file://$MODULE_DIR$/build/extracted-include-protos/main',
'file://$MODULE_DIR$/build/extracted-protos/grpc',
'file://$MODULE_DIR$/build/generated/source/proto/grpc/java',
'file://$MODULE_DIR$/build/generated/source/proto/grpc/grpc_output',
'file://$MODULE_DIR$/build/generated/source/proto/main/java',
'file://$MODULE_DIR$/build/extracted-protos/test',
'file://$MODULE_DIR$/build/extracted-include-protos/test',
'file://$MODULE_DIR$/build/generated/source/proto/test/java',
]
assert Objects.equals(expectedSourceDir, sourceDir)
assert Objects.equals(expectedTestSourceDir, testSourceDir)
Objects.equals(expectedGeneratedDirs, generatedDirs)
where:
gradleVersion << GRADLE_VERSIONS
}
@Unroll
void "test proto generation is not up-to-date on dependency changes [gradle #gradleVersion]"() {
given: "project from testProject"
Expand Down
1 change: 1 addition & 0 deletions testProject/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
plugins {
id 'java'
id 'idea'
id 'eclipse'
id 'com.google.protobuf'
}
apply from: 'build_base.gradle'