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

Mark generated source dirs in IDEA #477

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,17 @@ class ProtobufPlugin implements Plugin<Project> {
getSourceSets().each { sourceSet ->
SourceDirectorySet protoSrcDirSet = sourceSet.proto
protoSrcDirSet.srcDirs.each { File protoDir ->
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir)
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir, false)
}
}
// Make the extracted proto dirs known to IDEs
project.tasks.withType(ProtobufExtract).each { ProtobufExtract extractProtoTask ->
Utils.addToIdeSources(project, extractProtoTask.isTest, extractProtoTask.destDir)
Utils.addToIdeSources(project, extractProtoTask.isTest, extractProtoTask.destDir, true)
}
// Make the generated code dirs known to IDEs
project.tasks.withType(GenerateProtoTask).each { GenerateProtoTask generateProtoTask ->
generateProtoTask.getOutputSourceDirectorySet().srcDirs.each { File outputDir ->
Utils.addToIdeSources(project, generateProtoTask.isTest, outputDir)
Utils.addToIdeSources(project, generateProtoTask.isTest, outputDir, true)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ class Utils {
* Adds the file to the IDE plugin's set of sources / resources. If the directory does
* not exist, it will be created before the IDE task is run.
*/
static void addToIdeSources(Project project, boolean isTest, File f) {
static void addToIdeSources(Project project, boolean isTest, File f, boolean isGenerated) {
IdeaModel model = project.getExtensions().findByType(IdeaModel)
if (model != null) {
// TODO(zpencer): switch to model.module.generatedSourceDirs when that API becomes stable
// For now, just hint to the IDE that it's a source dir or a test source dir.
if (isTest) {
model.module.testSourceDirs += f
} else {
model.module.sourceDirs += f
}
if (isGenerated) {
model.module.generatedSourceDirs += f
}
project.tasks.withType(GenerateIdeaModule).each {
it.doFirst {
// This is required because the intellij plugin does not allow adding source directories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,16 @@ class ProtobufJavaPluginTest extends Specification {

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()
Expand All @@ -390,8 +394,21 @@ class ProtobufJavaPluginTest extends Specification {
.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
Expand Down