Get output file of BinarySpec

Is there a good general way to get the output file of a BinarySpec? I’ve written a few simple plugins to post-process build artifacts, and I find myself using this helper often:

private File getBinaryOutputFile(BinarySpec binary) throws IllegalArgumentException {
    if (binary in SharedLibraryBinarySpec) {
        return binary.sharedLibraryFile
    } else if (binary in StaticLibraryBinarySpec) {
        return binary.staticLibraryFile
    } else if (binary in NativeExecutableBinarySpec) {
        return binary.executable.file
    } else if (binary in JarBinarySpec) {
        return binary.jarFile
    }

    throw new IllegalArgumentException("Unsupported binary type: ${binary.class.name}")
}

This seems a little hacky. Is there a better way?

1 Like