I’m trying to develop a Gradle plugin for a language I use (SystemVerilog). I’m still experimenting and figuring things out. Before I write the entire thing as a plugin, I thought it would be best to try out the different parts I need inside a build script, to get a feel of how things should work.
I already asked this question on Stack Overflow, but I’d like to add it here as well, as I see that this forum is pretty active and has the attention of the developers.
I’m trying to define a container of source sets, similar to how the Java plugin does it. I’d like to be able to use a closure when configuring a source set. Concretely, I’d like to be able to do the following:
sourceSets {
main {
sv {
include '*.sv'
}
}
}
I defined my own sourceSet
class:
class SourceSet implements Named {
final String name
final ObjectFactory objectFactory
@Inject
SourceSet(String name, ObjectFactory objectFactory) {
this.name = name
this.objectFactory = objectFactory
}
SourceDirectorySet getSv() {
SourceDirectorySet sv = objectFactory.sourceDirectorySet('sv',
'SystemVerilog source')
sv.srcDir("src/${name}/sv")
return sv
}
SourceDirectorySet sv(@Nullable Closure configureClosure) {
configure(configureClosure, getSv());
return this;
}
}
I’m using org.gradle.api.file.SourceDirectorySet
because that already implements PatternFilterable
, so it should give me access to include
, exclude
, etc.
If I understand the concept correctly, the sv(@Nullable Closure configureClosure)
method is the one that gives me the ability to write sv { ... }
to configure via a closure.
To add the sourceSets
property to the project, I did the following:
project.extensions.add("sourceSets",
project.objects.domainObjectContainer(SourceSet.class))
As per the Gradle docs, this should give me the possibility to configure sourceSets
using a closure. This site, which details using custom types, states that by using NamedDomainObjectContainer
, Gradle will provide a DSL that build scripts can use to define and configure elements . This would be the sourceSets { ... }
part. This should also be the sourceSets { main { ... } }
part.
If I create a sourceSet
for main
and use it in a task, then everything works fine:
project.sourceSets.create('main')
task compile(type: Task) {
println 'Compiling source files'
println project.sourceSets.main.sv.files
}
If I try to configure the main
source set to only include files with the .sv
extension, then I get an error:
sourceSets {
main {
sv {
include '*.sv'
}
}
}
I get the following error:
No signature of method: build_47mnuak4y5k86udjcp7v5dkwm.sourceSets() is applicable for argument types: (build_47mnuak4y5k86udjcp7v5dkwm$_run_closure1) values: [build_47mnuak4y5k86udjcp7v5dkwm$_run_closure1@effb286]
I don’t know what I’m doing wrong. I’m sure it’s just a simple thing that I’m forgetting. Does anyone have an idea of what that might be?