Generate Code in and for Plugin

I am developing a plugin.
This plugin, makes use of a parser generated with protoc.
The protoc is executed with the com.google.protobuf plugin.

import com.google.protobuf.gradle.builtins
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.protoc

plugins {
    id ("com.google.protobuf") version "0.8.19"
    id ("java")
}

sourceSets {
    this.named("main") {
        proto {
            setSrcDirs(listOf("Foo"))
        }
    }
}

dependencies {
    implementation("com.google.protobuf:protobuf-javalite:3.21.4")
}

protobuf {
    var configurator = this.protobuf
    configurator.protoc {
        artifact = "com.google.protobuf:protoc:3.21.4"
    }
    configurator.generatedFilesBaseDir = project.layout.
        projectDirectory.dir("build/generated").toString()

    configurator.generateProtoTasks {
        this.all().forEach { task ->
            task.builtins {
                java { }
            }
        }
    }
}

This works generates the Foo.java source for the parser in my main project but I want to use the parser in a custom plugin.
Is there a recommended way to do this?

I suspect this would be a fairly common thing e.g. antlr, but I have not found a good example yet.