I have a task that uses a RegularFileProperty as i...
# plugin-development
p
I have a task that uses a RegularFileProperty as input that should consume 1 file from a resolvable configuration (that only produces 1 file). What’s the best way to support lazy configurations? As a workaround I could use ConfigurableFileCollection as task input but the task only supports 1 file so I would like to keep a RegularFile.
myFile.set(layout.file(configuration.map { it.singleFile }))
Would this support lazy configurations?
> myFile.set(layout.file(configuration.map { it.singleFile })) Looks like that would work 👍
Wait no, probably have to go through
.elements
Copy code
layout.file(configuration.elements.map { it.single().asFile })
v
Exactly, that's the snippet I posted yesterday. 😄
🚀 1
The last one from Martin I mean
configuration.map
iirc would loose eventually present task dependencies
😭 1
That's what you have the
elements
for, as bridge from
FileCollection
to
Provider
p
Would love to keep the task dependencies…
v
Yeah, use the last snippet and it should be fine
p
But what about this:
myFile.set(configuration.flatMap { it.elements.map { it.single() as RegularFile } })
Then I don’t need a ProjectLayout
v
Actually, you need a
flatMap
yes, as
configuration
is a provider in your case, so the snippet was not fully working for you.
Casting to
RegularFile
, well, if it works it works, but that is not too type-safe I'd say 😄
And I'm not sure whether it will work. The
elements
view probably does not know whether something is a file or directory, so maybe it is not a
RegularFile
p
Well, it is a published file so it can’t be a directory 🤔 and elements returns a FileSystemLocation
v
I gave it a quick test and it is exactly like I said.
c.map { it.singleFile }
misses task dependency
layout.file(c.flatMap { it.elements.map { it.single().asFile } })
and
c.flatMap { it.elements.map { it.single() as RegularFile } }
preserve the task dependency But
c.flatMap { it.elements.map { it.single() as RegularFile } }
fails when you try to query the files with
Copy code
> class org.gradle.api.internal.file.DefaultFileSystemLocation cannot be cast to class org.gradle.api.file.RegularFile (org.gradle.api.internal.file.DefaultFileSystemLocation and org.gradle.api.file.RegularFile are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @66133adc)
p
Thank you!
👌 1