How would I go about making a task using kotlin ds...
# gradle
z
How would I go about making a task using kotlin dsl that reads some remote files, and generates kotlin code that can be used by the main module?
e
mostly the same way you'd do it outside of Gradle
if the remote files can be downloaded by Gradle (through a custom repository) then they can take place in build caching, otherwise you'll have to manage caching yourself (or don't cache)
a
for a task that generates code I’d prefer creating a standalone task in buildSrc. It would be possible with an adhoc task in a gradle.kts file, or to create a class, but a separate file helps with organisation and prevents accidentally using properties from the script within the task (which Gradle will let you do, but it can cause weird issues) Like @ephemient said, downloading a file using a custom repository is best for performance https://stackoverflow.com/a/34327202/4161471. It’s also fine to download it using a Java HTTP client, or to use another Gradle plugin (gradle-download-task for example). I’d recommend splitting up the downloading&unpacking task from the generation task. It helps separate concerns, and Gradle can better cache and/or re-use task outputs. As for being able to use the generated code in the main module - that’s pretty easy! You can map the code generation task to a
Provider<File>
, and add that as source directory. Gradle will then understand that it needs to run the generation task in order to build the source set, so the generation task will be triggered automatically Take a look at this answer for a more basic code generation example (it’s only missing the downloading step) https://stackoverflow.com/questions/74743976/kotlin-multiplatform-accessing-build-variables-in-code/74771876#74771876
e
I'd also use onlyForConfigurations on the repository but yes, that's the approach