Generic handling for Provider<FileSystemLocation> as output

I have a task that can operate on files or directories. The input and outputs are defined as Provider<FileSystemLocation>. I’m not sure how to mark the output for this task between @OutputFile, @OutputFiles, @OutputDirectory and @OutputDirectories.

Thanks

Hi Steve,

currently you can’t have a task which produces a file or a directory in a single output property. OutputFile and OutputFiles verify that the output is a file and OutputDirectory and OutputDirectories verify that the output is a directory.

IIUC, your task will output a directory, if the input is a directory, and it will output a file if the input is a file. There could be a workaround by doing something along these lines:
@Nested properties can also define outputs. So you could add an @Nested property of your own type, say MyOutput, with two subclasses:

class MyOutput {
    private final Provider<FileSystemLocation> output;

    public MyOutput(Provider<FileSystemLocation> output) {
        this.output = output;
    }

    Provider<FileSystemLocation> getOutput() {
        return output;
    }
}
class MyFileOutput implements MyOutput {
    public MyFileOutput(Provider<FileSystemLocation> output) {
        super(output);
    }

    @OutputFile
    @Override
    public Provider<FileSystemLocation> getOutput() {
        return super.getOutput();
    }
}
class MyDirectoryOutput implements MyOutput {
    public MyDirectoryOutput(Provider<FileSystemLocation> output) {
        super(output);
    }

    @OutputDirectory
    @Override
    public Provider<FileSystemLocation> getOutput() {
        return super.getOutput();
    }
}

And then the nested property on your task would return an instance of MyFileOutput or MyDirectoryOutput depending on the type of the input property.

Not the nicest workaround, though that should work now.

In the long run, we should have a way to declare an output where we don’t know whether it is a file or a directory.

Cheers,
Stefan.

Wow, thanks for the detailed reply :slight_smile:

It would be a useful feature for sure.