Search in sources :

Example 1 with FileSystemLocation

use of org.gradle.api.file.FileSystemLocation in project gradle by gradle.

the class FileOrUriNotationConverter method convert.

public void convert(Object notation, NotationConvertResult<? super Object> result) throws TypeConversionException {
    if (notation instanceof File) {
        result.converted(notation);
        return;
    }
    if (notation instanceof Path) {
        result.converted(((Path) notation).toFile());
        return;
    }
    if (notation instanceof FileSystemLocation) {
        result.converted(((FileSystemLocation) notation).getAsFile());
        return;
    }
    if (notation instanceof URL) {
        try {
            notation = ((URL) notation).toURI();
        } catch (URISyntaxException e) {
            throw new UncheckedIOException(e);
        }
    }
    if (notation instanceof URI) {
        URI uri = (URI) notation;
        if ("file".equals(uri.getScheme()) && uri.getPath() != null) {
            result.converted(new File(uri.getPath()));
        } else {
            result.converted(uri);
        }
        return;
    }
    if (notation instanceof CharSequence) {
        String notationString = notation.toString();
        if (notationString.startsWith("file:")) {
            result.converted(new File(uriDecode(notationString.substring(5))));
            return;
        }
        for (File file : File.listRoots()) {
            String rootPath = file.getAbsolutePath();
            String normalisedStr = notationString;
            if (!fileSystem.isCaseSensitive()) {
                rootPath = rootPath.toLowerCase();
                normalisedStr = normalisedStr.toLowerCase();
            }
            if (normalisedStr.startsWith(rootPath) || normalisedStr.startsWith(rootPath.replace(File.separatorChar, '/'))) {
                result.converted(new File(notationString));
                return;
            }
        }
        // Check if string starts with a URI scheme
        if (URI_SCHEME.matcher(notationString).matches()) {
            try {
                result.converted(new URI(notationString));
                return;
            } catch (URISyntaxException e) {
                throw new UncheckedIOException(e);
            }
        }
        result.converted(new File(notationString));
    }
}
Also used : Path(java.nio.file.Path) FileSystemLocation(org.gradle.api.file.FileSystemLocation) UncheckedIOException(org.gradle.api.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 2 with FileSystemLocation

use of org.gradle.api.file.FileSystemLocation in project gradle by gradle.

the class LazyPublishArtifact method getFile.

@Override
public File getFile() {
    if (file == null) {
        Object value = provider.get();
        if (value instanceof FileSystemLocation) {
            FileSystemLocation location = (FileSystemLocation) value;
            file = location.getAsFile();
        } else if (value instanceof File) {
            file = (File) value;
        } else {
            throw new InvalidUserDataException(String.format("Cannot convert provided value (%s) to a file.", value));
        }
    }
    return file;
}
Also used : FileSystemLocation(org.gradle.api.file.FileSystemLocation) InvalidUserDataException(org.gradle.api.InvalidUserDataException) File(java.io.File)

Aggregations

File (java.io.File)2 FileSystemLocation (org.gradle.api.file.FileSystemLocation)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1