Skip to content

Commit 0500ff3

Browse files
authored
Mark generated source dirs in IDEA (#477)
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.
1 parent 568b373 commit 0500ff3

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,17 @@ class ProtobufPlugin implements Plugin<Project> {
527527
getSourceSets().each { sourceSet ->
528528
SourceDirectorySet protoSrcDirSet = sourceSet.proto
529529
protoSrcDirSet.srcDirs.each { File protoDir ->
530-
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir)
530+
Utils.addToIdeSources(project, Utils.isTest(sourceSet.name), protoDir, false)
531531
}
532532
}
533533
// Make the extracted proto dirs known to IDEs
534534
project.tasks.withType(ProtobufExtract).each { ProtobufExtract extractProtoTask ->
535-
Utils.addToIdeSources(project, extractProtoTask.isTest, extractProtoTask.destDir)
535+
Utils.addToIdeSources(project, extractProtoTask.isTest, extractProtoTask.destDir, true)
536536
}
537537
// Make the generated code dirs known to IDEs
538538
project.tasks.withType(GenerateProtoTask).each { GenerateProtoTask generateProtoTask ->
539539
generateProtoTask.getOutputSourceDirectorySet().srcDirs.each { File outputDir ->
540-
Utils.addToIdeSources(project, generateProtoTask.isTest, outputDir)
540+
Utils.addToIdeSources(project, generateProtoTask.isTest, outputDir, true)
541541
}
542542
}
543543
}

src/main/groovy/com/google/protobuf/gradle/Utils.groovy

+4-3
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,17 @@ class Utils {
125125
* Adds the file to the IDE plugin's set of sources / resources. If the directory does
126126
* not exist, it will be created before the IDE task is run.
127127
*/
128-
static void addToIdeSources(Project project, boolean isTest, File f) {
128+
static void addToIdeSources(Project project, boolean isTest, File f, boolean isGenerated) {
129129
IdeaModel model = project.getExtensions().findByType(IdeaModel)
130130
if (model != null) {
131-
// TODO(zpencer): switch to model.module.generatedSourceDirs when that API becomes stable
132-
// For now, just hint to the IDE that it's a source dir or a test source dir.
133131
if (isTest) {
134132
model.module.testSourceDirs += f
135133
} else {
136134
model.module.sourceDirs += f
137135
}
136+
if (isGenerated) {
137+
model.module.generatedSourceDirs += f
138+
}
138139
project.tasks.withType(GenerateIdeaModule).each {
139140
it.doFirst {
140141
// This is required because the intellij plugin does not allow adding source directories

src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy

+17
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,16 @@ class ProtobufJavaPluginTest extends Specification {
363363
364364
Set<String> sourceDir = [] as Set
365365
Set<String> testSourceDir = [] as Set
366+
Set<String> generatedDirs = [] as Set
366367
rootMgr.content.sourceFolder[0].each {
367368
if (Boolean.parseBoolean(it.@isTestSource)) {
368369
testSourceDir.add(it.@url)
369370
} else {
370371
sourceDir.add(it.@url)
371372
}
373+
if (Boolean.parseBoolean(it.@generated)) {
374+
generatedDirs.add(it.@url)
375+
}
372376
}
373377
374378
Set<String> expectedSourceDir = ImmutableSet.builder()
@@ -390,8 +394,21 @@ class ProtobufJavaPluginTest extends Specification {
390394
.add('file://$MODULE_DIR$/build/extracted-include-protos/test')
391395
.add('file://$MODULE_DIR$/build/generated/source/proto/test/java')
392396
.build()
397+
Set<String> expectedGeneratedDirs = [
398+
'file://$MODULE_DIR$/build/extracted-include-protos/grpc',
399+
'file://$MODULE_DIR$/build/extracted-protos/main',
400+
'file://$MODULE_DIR$/build/extracted-include-protos/main',
401+
'file://$MODULE_DIR$/build/extracted-protos/grpc',
402+
'file://$MODULE_DIR$/build/generated/source/proto/grpc/java',
403+
'file://$MODULE_DIR$/build/generated/source/proto/grpc/grpc_output',
404+
'file://$MODULE_DIR$/build/generated/source/proto/main/java',
405+
'file://$MODULE_DIR$/build/extracted-protos/test',
406+
'file://$MODULE_DIR$/build/extracted-include-protos/test',
407+
'file://$MODULE_DIR$/build/generated/source/proto/test/java',
408+
]
393409
assert Objects.equals(expectedSourceDir, sourceDir)
394410
assert Objects.equals(expectedTestSourceDir, testSourceDir)
411+
Objects.equals(expectedGeneratedDirs, generatedDirs)
395412
396413
where:
397414
gradleVersion << GRADLE_VERSIONS

0 commit comments

Comments
 (0)