Does anyone have a good idea how to model a task i...
# plugin-development
v
Does anyone have a good idea how to model a task input property that can either be a file or directory? In pre-
Property
times I had an
@Internal
property that was set to either, and then a derived
@Optional @InputFile
read-only property and a derived
@Optional @InputDirectory
read-only property which checked whether the internal property is a file or directory and at execution time checked that exactly one of them is set. I'm thinking about cleaner ways to rewrite this, without splitting the task into multiple tasks.
e
I haven't tried but does this possibly work?
Copy code
@get:Nested var input: Either

interface Either {
    abstract class File : Either {
        @get:InputFile abstract val file: RegularFileProperty
    }
    abstract class Directory : Either {
        @get:InputDirectory abstract val dir: DirectoryProperty
    }
}
m
Probably not the answer you're looking for but your `@InputDirectory` should probably be `@InputFiles` in the first place
e
for the record, you can
Copy code
when (val either = input.get()) {
    is Either.File -> "Input file: ${either.file.get().asFile.absolutePath}"
    is Either.Directory -> "Input directory: ${either.dir.get().asFile.absolutePath}"
}
instead of casting a second
get
👌 1
and yeah, Martin has a good point about
FileCollection