Dynamicall generate (native) model-component

Porting over 10+ years of Makefiles to Gradle. Want to separate the metadata (lib/executable names, dependencies etc…) from the build script. In the following simplified example, I’m able to set the final executable name and sources dynamically.

model {
    components {
        hello (NativeExecutableSpec){
            baseName = mySources.executableName
            sources.c.source.srcDir mySources.srcDir
            sources.c.source.include mySources.include
        }
    } 
}   

How can I set ‘hello’ dynamically. It doesn’t seem to work with properties. I believe the answer lies in Chapter 66 of the Gradle 2.4 User Guide - Rule based model configuration.

If it’s too involved to give me the whole answer, could someone just please tell me that IT IS possible and maybe give me some pointers, and I will continue to plug along to figure it out.

Thanks in advance!

Yes. In this case you can simply use a groovy string.

model {
    components {
        "${mySources.componentName}"(NativeExecutableSpec)
    }
}

Quick tip, you can use a GString in this fashion in place of any method or property name in Groovy.

D’oh, I just needed the double-quotes. Good quick tip, too.

Thanks much!

Follow-up question if I may.

Now how do I dynamically set the Native component type (NativeExecutableSpec in the example) since it is neither a method nor a property.

Thanks!

Never mind I’m answering my own question below

def coreType = NativeExecutableSpec
components {
"${myBaseName}" (coreType){