How to retrieve 'current' platform of a native platform

software-model
plugins

(Janito Vaqueiro Ferreira Filho) #1

I’m writing a custom (software model) plugin that generates distributions for every combination of platform, build type and flavor using all available components in the project. The problem is that the PlatformContainer instance is empty. In the Gradle User Guide, it says that

If no platforms are defined in a project, then a single, default platform ‘current’ is added.

I couldn’t find where a ‘current’ platform is added, the closest I got to was NativeComponentRules.resolvePlatforms which resolves the platforms for a given binary, and if none are found, a default one is returned.

If the empty platform container is the expected behavior, I think the user guide should probably be updated (maybe just change added to used?). Either way, how can I obtain the Platform representing the current platform? Should I do the same as in NativeComponentRules (using internal API, such as NativePlatforms, and DefaultPlatformRequirement)? Or is there an easier or more future-proof way?


(Janito Vaqueiro Ferreira Filho) #2

I managed to add the ‘current’ platform using a @Finalize rule and the org.gradle.DefaultNativePlatform class:

@Finalize
public void addCurrentPlatformIfNoPlatformsWereDefined(PlatformContainer platforms) {
    if (platforms.isEmpty())
        platforms.add(new DefaultNativePlatform("current"));
}

So some new questions arise:

  • Is there a reason this wasn’t done in NativeComponentModelPlugin?
  • Is it a bad practice to finalize something that wasn’t created by another plugin?
  • Can a model object be finalized more than once? (I could see a problem if a “mixed” platform project existed, like Java with JNI, and both plugins wanted to add a default JVM platform and a native platform if the user doesn’t specify any)
  • Would it be a bad practice to have this rule in a custom plugin?