Skip to content

Commit

Permalink
Remove eclipse plugin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rougsig committed Sep 26, 2022
1 parent 2e4f5fe commit 38a667a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 86 deletions.
15 changes: 9 additions & 6 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,16 @@ class ProtobufPlugin implements Plugin<Project> {
}

postConfigure.add {
boolean isTest = Utils.isTest(sourceSet.name)
project.plugins.withId("eclipse") {
// This is required because the intellij/eclipse plugin does not allow adding source directories
// that do not exist. The intellij/eclipse config files should be valid from the start.
generateProtoTask.get().getOutputSourceDirectories().each { File outputDir ->
outputDir.mkdirs()
}
}

project.plugins.withId("idea") {
boolean isTest = Utils.isTest(sourceSet.name)
protoSrcDirSet.srcDirs.each { File protoDir ->
Utils.addToIdeSources(project, isTest, protoDir, false)
}
Expand All @@ -274,11 +282,6 @@ class ProtobufPlugin implements Plugin<Project> {
Utils.addToIdeSources(project, isTest, outputDir, true)
}
}
project.plugins.withId("eclipse") {
generateProtoTask.get().getOutputSourceDirectories().each { File outputDir ->
Utils.addToEclipseSources(project, isTest, sourceSet.name, outputDir)
}
}
}
}

Expand Down
78 changes: 0 additions & 78 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ 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 @@ -133,78 +129,4 @@ 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
// 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
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ class IDESupportTest extends Specification {
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
} else {
assert it.attributes.attribute.size() == 2
}
}
}
Expand Down

0 comments on commit 38a667a

Please sign in to comment.