Search in sources :

Example 16 with RelativePath

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

the class RelativePathTest method testConstructors.

@Test
public void testConstructors() {
    RelativePath path;
    path = new RelativePath(true, "one");
    assertPathContains(path, true, "one");
    path = new RelativePath(false, "one", "two");
    assertPathContains(path, false, "one", "two");
}
Also used : RelativePath(org.gradle.api.file.RelativePath) Test(org.junit.Test)

Example 17 with RelativePath

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

the class RelativePathTest method appendNames.

@Test
public void appendNames() {
    RelativePath childPath = new RelativePath(false, "one", "two").append(true, "three", "four");
    assertPathContains(childPath, true, "one", "two", "three", "four");
    childPath = new RelativePath(false, "one", "two").append(true);
    assertPathContains(childPath, true, "one", "two");
}
Also used : RelativePath(org.gradle.api.file.RelativePath) Test(org.junit.Test)

Example 18 with RelativePath

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

the class CoffeeScriptCompileDestinationCalculator method transform.

public File transform(RelativePath relativePath) {
    String sourceFileName = relativePath.getLastName();
    String destinationFileNameBase = sourceFileName;
    if (sourceFileName.endsWith(".coffee")) {
        destinationFileNameBase = sourceFileName.substring(0, sourceFileName.length() - 7);
    }
    String destinationFileName = destinationFileNameBase + ".js";
    RelativePath destinationRelativePath = relativePath.replaceLastName(destinationFileName);
    return new File(destination, destinationRelativePath.getPathString());
}
Also used : RelativePath(org.gradle.api.file.RelativePath) File(java.io.File)

Example 19 with RelativePath

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

the class TarTaskOutputPacker method unpackPropertyEntry.

private void unpackPropertyEntry(ResolvedTaskOutputFilePropertySpec propertySpec, InputStream input, TarArchiveEntry entry, String childPath, boolean missing, ImmutableMultimap.Builder<String, FileSnapshot> fileSnapshots) throws IOException {
    File propertyRoot = propertySpec.getOutputFile();
    String propertyName = propertySpec.getPropertyName();
    if (propertyRoot == null) {
        throw new IllegalStateException("Optional property should have a value: " + propertyName);
    }
    File outputFile;
    boolean isDirEntry = entry.isDirectory();
    boolean root = Strings.isNullOrEmpty(childPath);
    if (root) {
        // We are handling the root of the property here
        if (missing) {
            if (!makeDirectory(propertyRoot.getParentFile())) {
                // Make sure output is removed if it exists already
                if (propertyRoot.exists()) {
                    FileUtils.forceDelete(propertyRoot);
                }
            }
            return;
        }
        OutputType outputType = propertySpec.getOutputType();
        if (isDirEntry) {
            if (outputType != OutputType.DIRECTORY) {
                throw new IllegalStateException("Property should be an output directory property: " + propertyName);
            }
        } else {
            if (outputType == OutputType.DIRECTORY) {
                throw new IllegalStateException("Property should be an output file property: " + propertyName);
            }
        }
        ensureDirectoryForProperty(outputType, propertyRoot);
        outputFile = propertyRoot;
    } else {
        outputFile = new File(propertyRoot, childPath);
    }
    String internedPath = stringInterner.intern(outputFile.getAbsolutePath());
    RelativePath relativePath = root ? RelativePath.parse(!isDirEntry, outputFile.getName()) : RelativePath.parse(!isDirEntry, childPath);
    if (isDirEntry) {
        FileUtils.forceMkdir(outputFile);
        fileSnapshots.put(propertyName, new DirectoryFileSnapshot(internedPath, relativePath, root));
    } else {
        OutputStream output = new FileOutputStream(outputFile);
        HashCode hash;
        try {
            hash = streamHasher.hashCopy(input, output);
        } finally {
            IOUtils.closeQuietly(output);
        }
        FileHashSnapshot contentSnapshot = new FileHashSnapshot(hash, outputFile.lastModified());
        fileSnapshots.put(propertyName, new RegularFileSnapshot(internedPath, relativePath, root, contentSnapshot));
    }
    fileSystem.chmod(outputFile, entry.getMode() & FILE_PERMISSION_MASK);
}
Also used : DirectoryFileSnapshot(org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot) RelativePath(org.gradle.api.file.RelativePath) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.api.internal.changedetection.state.RegularFileSnapshot) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileHashSnapshot(org.gradle.api.internal.changedetection.state.FileHashSnapshot) File(java.io.File) OutputType(org.gradle.api.internal.tasks.OutputType)

Example 20 with RelativePath

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

the class TarTaskOutputPacker method storeDirectoryProperty.

private long storeDirectoryProperty(String propertyPath, File directory, Map<String, FileContentSnapshot> outputSnapshots, final TarArchiveOutputStream tarOutput) throws IOException {
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException(String.format("Expected '%s' to be a directory", directory));
    }
    long entries = 0;
    final String propertyRoot = propertyPath + "/";
    createTarEntry(propertyRoot, 0, UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM, tarOutput);
    tarOutput.closeArchiveEntry();
    entries++;
    String rootAbsolutePath = directory.getAbsolutePath();
    Path rootPath = directory.toPath();
    for (Map.Entry<String, FileContentSnapshot> entry : outputSnapshots.entrySet()) {
        String absolutePath = entry.getKey();
        // We've already created the directory for the property
        if (absolutePath.equals(rootAbsolutePath)) {
            continue;
        }
        File file = new File(absolutePath);
        String relativePath = rootPath.relativize(file.toPath()).toString();
        String targetPath = propertyRoot + relativePath;
        int mode = fileSystem.getUnixMode(file);
        switch(entry.getValue().getType()) {
            case RegularFile:
                storeFileEntry(file, targetPath, file.length(), mode, tarOutput);
                break;
            case Directory:
                storeDirectoryEntry(targetPath, mode, tarOutput);
                break;
            case Missing:
                throw new IllegalStateException("File should not be missing: " + file);
            default:
                throw new AssertionError();
        }
        entries++;
    }
    return entries;
}
Also used : Path(java.nio.file.Path) RelativePath(org.gradle.api.file.RelativePath) FileContentSnapshot(org.gradle.api.internal.changedetection.state.FileContentSnapshot) Map(java.util.Map) File(java.io.File)

Aggregations

RelativePath (org.gradle.api.file.RelativePath)25 File (java.io.File)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 FileVisitDetails (org.gradle.api.file.FileVisitDetails)5 DefaultFileVisitDetails (org.gradle.api.internal.file.DefaultFileVisitDetails)4 GradleException (org.gradle.api.GradleException)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Spec (org.gradle.api.specs.Spec)2 PatternSet (org.gradle.api.tasks.util.PatternSet)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1