Hi
I’d like to know if it is planned to add a construct to use project properties as rule inputs, e.g.
@Mutate
Void foo(rule subject, Map projectProps){
If(projectProps.contains(‘aProp’)) {
//do some stuff on the subject
}
}
Not at the moment. An alternative would be to define a @Model
and use the model DSL to assign it’s value to that of a project property then declare the model as an input to your rule.
Hey Mark
Don’t you think it would be a valid thing to add in a future release?
(having project properties as rule inputs and maybe subjects)
@mark_vieira As of today, I don’t think that anything has changed regarding this behavior. Can you confirm ?
This is quite OK, but from inside a plugin, how can I do it ?
No, nothing new on this front although this is a known limitation. In general we want a lot more types of things to be available as rule inputs. Right now its mostly limited to other stuff that is part of the model.
This cannot be done from within a rule. As stated, right now you are limited to using other model elements as rule inputs. If a user wants the convenience of specifying part of the model via a project property they’ll have to leverage the model DSL for that purpose (which has its own limitations).
model {
myModelElement {
myElementProperty = project.property('foo')
}
}
What about builtin properties, such as the project directory? How on earth is a RuleSource-based plugin expected to provide default values for File
model elements if it doesn’t have access to the project directory? I know that you can add a File
rule input with the @Path("buildDir")
annotation, so it seems strange that other properties aren’t available.
How does the Java plugin do this for the default main and test source sets? The conventions for those are relative to the project directory.
What about builtin properties, such as the project directory? How on earth is a RuleSource-based plugin expected to provide default values for File model elements if it doesn’t have access to the project directory? I know that you can add a File rule input with the @Path(“buildDir”) annotation, so it seems strange that other properties aren’t available.
As of now you can access the project directory by using ProjectIdentifier
as a rule input and calling getProjectDir()
. This is how the core plugins access it as well.
That’s great news, thanks. Do you know which version of Gradle that was added in? I don’t remember seeing it in the docs or release notes.
I found a workaround to use project’s properties in a rule but it relies on internal APIs.
As of gradle 2.12 here is what it looks like:
// in the Plugin.apply(ProjectInternal) method implementation
project.getModelRegistry().configure(ModelActionRole.Defaults,
new AbstractModelActionWithView<YourType>(
ModelReference.of("your model element name", YourType.class),
new SimpleModelRuleDescriptor("a big workaround ;)"),
Collections.<ModelReference<?>> emptyList()) {
@Override
protected void execute(final MutableModelNode modelNode,
final YourType view,
final List<ModelView<?>> inputs) {
// do what you need with the project's properties here.
}
});