private void linkFilesOutOfSandbox()

in plugins/sandbox/src/main/java/co/elastic/gradle/sandbox/SandboxExecBaseTask.java [315:362]


    private void linkFilesOutOfSandbox(Path outputRoot) {
        List<Path> incorrectFiles = outputFiles.stream()
                .map(this::getPathInSandbox)
                .filter(Files::isDirectory)
                .collect(Collectors.toList());
        List<Path> incorrectDirs = outputDirs.stream()
                .map(this::getPathInSandbox)
                .filter(Files::isRegularFile)
                .collect(Collectors.toList());
        if (!(incorrectFiles.isEmpty() && incorrectDirs.isEmpty())) {
            String errorMessage = "";
            if (!incorrectFiles.isEmpty()) {
                errorMessage = "The following outputs should be files, but are in fact directories:" +
                        GradleUtils.listPathsRelativeToProject(getProject(), incorrectFiles) + "\n";
            }
            if (!incorrectDirs.isEmpty()) {
                errorMessage = "The following outputs should be directories, but are in fact files:" +
                        GradleUtils.listPathsRelativeToProject(getProject(), incorrectDirs) + "\n";
            }
            throw new IllegalArgumentException(errorMessage);
        }

        Stream.concat(outputFiles.stream(), outputDirs.stream())
                .map(this::getPathInSandbox)
                // Recurse into directories to get all output files
                .map(each -> {
                    if (Files.isDirectory(each)) {
                        return getProject().fileTree(each);
                    } else {
                        return getProject().files(each);
                    }
                })
                .flatMap(each -> each.getFiles().stream())
                .map(File::toPath)
                // We already check for all outputs to exist in case the command is successful, but we need to skip
                // non existing ones here to account for missing files in case the command failed so we at least get
                // some of the outputs.
                .filter(Files::exists)
                .forEach(source -> {
                    // we converted the output paths to point to the sandbox, so we have to work back the destination outside of it
                    Path relativeDestination = sandbox.relativize(source);
                    Path destination = outputRoot.resolve(relativeDestination);
                    if (Files.isDirectory(destination)) {
                        throw new IllegalArgumentException("Unexpected directory: " + source);
                    }
                    createHardlink(source, destination);
                });
    }