Slackbot
05/02/2023, 11:35 AMStylianos Gakis
05/02/2023, 11:35 AMproject
inside of it for various cases, and it seems like the error message tells me that I can not use that at all.
At first, I was using project.file it to get a hold of some temporary file to download something into, which it gets deleted at the end of this task. For this, a replacement seems to be public File getTemporaryDir()
which exists inside DefaultTask
so using that it seems like it’s all okay now
But then, we’re also using the project
object in order to both: 1) Get access to the copy action in order to take that downloaded zip and paste it into a provided directory. 2) Get the zipTree
function which helps us deal with that downloaded zip file to pass into the from
function of that copy task.
For the copy task, maybe the Worker API is what I am looking for, as I see a nice example there of getting FileSystemOperations injected into it, so that we can get the copy
function and use it using this worked, so I will look into that.
But I couldn’t find some documentation about the zipTree fuction, and trying to also inject FileOperations
seems to fail, it only knows how to inject FileSystemOperators
, with error “Unable to determine constructor argument #2: missing parameter of type FileOperations, or no service of type FileOperations.”
Any ideas if I am going down the right path, and if there’s some documentation I am missing for this?Adam
05/02/2023, 11:42 AMcopy {}
you can inject FileSystemOperations
, and for zipTree {}
there’s ArchiveOperations
https://docs.gradle.org/current/userguide/custom_gradle_types.html#service_injectionThomas Broyer
05/02/2023, 11:42 AMArchiveOperations
?
https://docs.gradle.org/current/userguide/custom_gradle_types.html#services_for_injectionAdam
05/02/2023, 11:44 AMabstract class DownloadStringsTask : DefaultTask() {
with
import javax.inject.Inject
abstract class DownloadStringsTask @Inject constructor(
private val fs: FileSystemOperations,
private val archives: ArchiveOperations,
): DefaultTask() {
Adam
05/02/2023, 11:47 AMtempraryDir
as you have, but it might be a good idea to register it as a @LocalState
@get:LocalState
val cacheDir: File get() = temporaryDir
https://docs.gradle.org/current/userguide/custom_tasks.html#sec:storing_incremental_task_stateStylianos Gakis
05/02/2023, 11:47 AMAdam
05/02/2023, 11:48 AMgradle inject services
to find the servicesStylianos Gakis
05/02/2023, 11:49 AMAdam
05/02/2023, 11:51 AMStylianos Gakis
05/02/2023, 11:51 AMStylianos Gakis
05/02/2023, 11:52 AMAdam
05/02/2023, 11:53 AMStylianos Gakis
05/02/2023, 12:13 PMVampire
05/02/2023, 1:07 PM@LocalState
should not be necessary though for two reasons.
• the task does not reuse the state in that file, it just overwrites the file if the task is rerun
• the task is not cacheableVampire
05/02/2023, 1:08 PMVampire
05/02/2023, 1:08 PMStylianos Gakis
05/02/2023, 1:30 PMtemporaryCacheDir.delete()
now then? Since I’ll just use temporaryDir
directly. I am assuming this temporaryDir is something that gets auto-deleted at the the of the task run right? I tried locating where it’s created while the task is still running to ensure that this is the case but didn’t find a way to do so this far.Vampire
05/02/2023, 2:03 PMStylianos Gakis
05/02/2023, 2:13 PMVampire
05/02/2023, 2:37 PMVampire
05/02/2023, 2:38 PMAdam
05/02/2023, 2:38 PMtemporaryDir
for each task is ./build/tmp/nameOfTask
. So it’s not shared with other tasks. I wouldn’t worry about overlapping - Gradle will warn if there’s two tasks are clashingVampire
05/02/2023, 2:38 PMVampire
05/02/2023, 2:38 PMVampire
05/02/2023, 2:38 PMVampire
05/02/2023, 2:39 PMStylianos Gakis
05/02/2023, 2:52 PMFile.createTempFile("lang-file", ".zip")
in my case. Just tested this too and it also just works it seems. Might as well let it be like this then.
Vampire
05/02/2023, 2:59 PMVampire
05/02/2023, 2:59 PMVampire
05/02/2023, 3:00 PMStylianos Gakis
05/02/2023, 3:01 PMAdam
05/02/2023, 3:01 PMFile.deleteOnExit()
https://stackoverflow.com/questions/19012557/use-of-deleteonexit-method-in-java, but I agree that you shouldn’t overthink it :) Using createTempFile()
will be fine, unless the file is sensitive, but then nothing about this is sensitive, so there are bigger problemsVampire
05/02/2023, 3:03 PMdeleteOnExit
. It is fine for things that currently cannot be deleted because somehow the file is still in use or similar but can maybe be deleted later and if not, then not. But those actions are not guaranteed to be run just like JVM shutdown hooks iirc.Vampire
05/02/2023, 3:04 PMephemient
05/03/2023, 5:28 PMdeleteOnExit()
isn't very timely in the long-lived Gradle daemon anywayVampire
05/03/2023, 5:33 PM