Skip to content

Commit

Permalink
Mark generated source dirs in IDEA (#477)
Browse files Browse the repository at this point in the history
Adds generated code to IDEA module as generated source so that it is correctly marked as generated in IDEA and users are warned if they try to edit those files.
  • Loading branch information
oehme authored Mar 2, 2021
1 parent 568b373 commit 0500ff3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
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

0 comments on commit 0500ff3

Please sign in to comment.