Search in sources :

Example 1 with Restrict

use of org.apache.tools.ant.types.resources.Restrict in project ant by apache.

the class Concat method getResources.

/**
 * Get the resources to concatenate.
 */
private ResourceCollection getResources() {
    if (rc == null) {
        return new StringResource(getProject(), textBuffer.toString());
    }
    if (dest != null) {
        Intersect checkDestNotInSources = new Intersect();
        checkDestNotInSources.setProject(getProject());
        checkDestNotInSources.add(rc);
        checkDestNotInSources.add(dest);
        if (checkDestNotInSources.size() > 0) {
            throw new BuildException("Destination resource %s was specified as an input resource.", dest);
        }
    }
    Restrict noexistRc = new Restrict();
    noexistRc.add(NOT_EXISTS);
    noexistRc.add(rc);
    for (Resource r : noexistRc) {
        log(r + " does not exist.", Project.MSG_ERR);
    }
    Restrict result = new Restrict();
    result.add(EXISTS);
    result.add(rc);
    return result;
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) Intersect(org.apache.tools.ant.types.resources.Intersect) Restrict(org.apache.tools.ant.types.resources.Restrict) FileResource(org.apache.tools.ant.types.resources.FileResource) LogOutputResource(org.apache.tools.ant.types.resources.LogOutputResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) BuildException(org.apache.tools.ant.BuildException)

Example 2 with Restrict

use of org.apache.tools.ant.types.resources.Restrict in project ant by apache.

the class ResourceUtils method selectSources.

/**
 * Tells which sources should be reprocessed because the given
 * selector selects at least one target.
 *
 * @param logTo where to send (more or less) interesting output.
 * @param source ResourceCollection.
 * @param mapper filename mapper indicating how to find the target Resources.
 * @param targets object able to map a relative path as a Resource.
 * @param selector returns a selector that is applied to target
 * files.  If it selects at least one target the source will be
 * added to the returned collection.
 * @return ResourceCollection.
 * @since Ant 1.8.0
 */
public static ResourceCollection selectSources(final ProjectComponent logTo, ResourceCollection source, final FileNameMapper mapper, final ResourceFactory targets, final ResourceSelectorProvider selector) {
    if (source.isEmpty()) {
        logTo.log("No sources found.", Project.MSG_VERBOSE);
        return Resources.NONE;
    }
    source = Union.getInstance(source);
    final Union result = new Union();
    for (final Resource sr : source) {
        String srName = sr.getName();
        srName = srName == null ? srName : srName.replace('/', File.separatorChar);
        String[] targetnames = null;
        try {
            targetnames = mapper.mapFileName(srName);
        } catch (final Exception e) {
            logTo.log("Caught " + e + " mapping resource " + sr, Project.MSG_VERBOSE);
        }
        if (targetnames == null || targetnames.length == 0) {
            logTo.log(sr + " skipped - don\'t know how to handle it", Project.MSG_VERBOSE);
            continue;
        }
        final Union targetColl = new Union();
        for (String targetname : targetnames) {
            if (targetname == null) {
                targetname = "(no name)";
            }
            targetColl.add(targets.getResource(targetname.replace(File.separatorChar, '/')));
        }
        // find the out-of-date targets:
        final Restrict r = new Restrict();
        r.add(selector.getTargetSelectorForSource(sr));
        r.add(targetColl);
        if (r.size() > 0) {
            result.add(sr);
            final Resource t = r.iterator().next();
            logTo.log(sr.getName() + " added as " + t.getName() + (t.isExists() ? " is outdated." : " doesn\'t exist."), Project.MSG_VERBOSE);
            continue;
        }
        // log uptodateness of all targets:
        logTo.log(sr.getName() + " omitted as " + targetColl.toString() + (targetColl.size() == 1 ? " is" : " are ") + " up to date.", Project.MSG_VERBOSE);
    }
    return result;
}
Also used : Restrict(org.apache.tools.ant.types.resources.Restrict) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) Union(org.apache.tools.ant.types.resources.Union) IOException(java.io.IOException)

Example 3 with Restrict

use of org.apache.tools.ant.types.resources.Restrict in project ant by apache.

the class ResourceUtils method logFuture.

/**
 * Log which Resources (if any) have been modified in the future.
 * @param logTo the ProjectComponent to do the logging.
 * @param rc the collection of Resources to check.
 * @param granularity the timestamp granularity to use.
 * @since Ant 1.7
 */
private static void logFuture(final ProjectComponent logTo, final ResourceCollection rc, final long granularity) {
    final long now = System.currentTimeMillis() + granularity;
    final Date sel = new Date();
    sel.setMillis(now);
    sel.setWhen(TimeComparison.AFTER);
    final Restrict future = new Restrict();
    future.add(sel);
    future.add(rc);
    for (final Resource r : future) {
        logTo.log("Warning: " + r.getName() + " modified in the future.", Project.MSG_WARN);
    }
}
Also used : Restrict(org.apache.tools.ant.types.resources.Restrict) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) Date(org.apache.tools.ant.types.resources.selectors.Date)

Example 4 with Restrict

use of org.apache.tools.ant.types.resources.Restrict in project ant by apache.

the class DependSet method logFuture.

private void logFuture(ResourceCollection rc, ResourceSelector rsel) {
    Restrict r = new Restrict();
    r.add(rsel);
    r.add(rc);
    for (Resource res : r) {
        log("Warning: " + res + " modified in the future.", Project.MSG_WARN);
    }
}
Also used : Restrict(org.apache.tools.ant.types.resources.Restrict) Resource(org.apache.tools.ant.types.Resource)

Example 5 with Restrict

use of org.apache.tools.ant.types.resources.Restrict in project ant by apache.

the class Delete method execute.

/**
 * Delete the file(s).
 *
 * @exception BuildException if an error occurs
 */
@Override
public void execute() throws BuildException {
    if (usedMatchingTask) {
        log("DEPRECATED - Use of the implicit FileSet is deprecated.  Use a nested fileset element instead.", quiet ? Project.MSG_VERBOSE : verbosity);
    }
    if (file == null && dir == null && filesets.isEmpty() && rcs == null) {
        throw new BuildException("At least one of the file or dir attributes, or a nested resource collection, must be set.");
    }
    if (quiet && failonerror) {
        throw new BuildException("quiet and failonerror cannot both be set to true", getLocation());
    }
    // delete the single file
    if (file != null) {
        if (file.exists()) {
            if (file.isDirectory()) {
                log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute.  Use dir instead.", quiet ? Project.MSG_VERBOSE : verbosity);
            } else {
                log("Deleting: " + file.getAbsolutePath());
                if (!delete(file)) {
                    handle("Unable to delete file " + file.getAbsolutePath());
                }
            }
        } else if (isDanglingSymlink(file)) {
            log("Trying to delete file " + file.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(file)) {
                handle("Unable to delete file " + file.getAbsolutePath());
            }
        } else {
            log("Could not find file " + file.getAbsolutePath() + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);
        }
    }
    // delete the directory
    if (dir != null && !usedMatchingTask) {
        if (dir.exists() && dir.isDirectory()) {
            /*
                  If verbosity is MSG_VERBOSE, that mean we are doing
                  regular logging (backwards as that sounds).  In that
                  case, we want to print one message about deleting the
                  top of the directory tree.  Otherwise, the removeDir
                  method will handle messages for _all_ directories.
                */
            if (verbosity == Project.MSG_VERBOSE) {
                log("Deleting directory " + dir.getAbsolutePath());
            }
            removeDir(dir);
        } else if (isDanglingSymlink(dir)) {
            log("Trying to delete directory " + dir.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(dir)) {
                handle("Unable to delete directory " + dir.getAbsolutePath());
            }
        }
    }
    Resources resourcesToDelete = new Resources();
    resourcesToDelete.setProject(getProject());
    resourcesToDelete.setCache(true);
    Resources filesetDirs = new Resources();
    filesetDirs.setProject(getProject());
    filesetDirs.setCache(true);
    FileSet implicit = null;
    if (usedMatchingTask && dir != null && dir.isDirectory()) {
        // add the files from the default fileset:
        implicit = getImplicitFileSet();
        implicit.setProject(getProject());
        filesets.add(implicit);
    }
    final int size = filesets.size();
    for (FileSet fs : filesets) {
        if (fs.getProject() == null) {
            log("Deleting fileset with no project specified; assuming executing project", Project.MSG_VERBOSE);
            fs = (FileSet) fs.clone();
            fs.setProject(getProject());
        }
        final File fsDir = fs.getDir();
        if (!fs.getErrorOnMissingDir() && (fsDir == null || !fsDir.exists())) {
            continue;
        }
        if (fsDir == null) {
            throw new BuildException("File or Resource without directory or file specified");
        } else if (!fsDir.isDirectory()) {
            handle("Directory does not exist: " + fsDir);
        } else {
            DirectoryScanner ds = fs.getDirectoryScanner();
            // the previous line has already scanned the
            // filesystem, in order to avoid a rescan when later
            // iterating, capture the results now and store them
            final String[] files = ds.getIncludedFiles();
            resourcesToDelete.add(new ResourceCollection() {

                @Override
                public boolean isFilesystemOnly() {
                    return true;
                }

                @Override
                public int size() {
                    return files.length;
                }

                @Override
                public Iterator<Resource> iterator() {
                    return new FileResourceIterator(getProject(), fsDir, files);
                }
            });
            if (includeEmpty) {
                filesetDirs.add(new ReverseDirs(getProject(), fsDir, ds.getIncludedDirectories()));
            }
            if (removeNotFollowedSymlinks) {
                String[] n = ds.getNotFollowedSymlinks();
                if (n.length > 0) {
                    String[] links = new String[n.length];
                    System.arraycopy(n, 0, links, 0, n.length);
                    Arrays.sort(links, Comparator.reverseOrder());
                    for (String link : links) {
                        final Path filePath = Paths.get(link);
                        if (!Files.isSymbolicLink(filePath)) {
                            // it's not a symbolic link, so move on
                            continue;
                        }
                        // it's a symbolic link, so delete it
                        final boolean deleted = filePath.toFile().delete();
                        if (!deleted) {
                            handle("Could not delete symbolic link at " + filePath);
                        }
                    }
                }
            }
        }
    }
    resourcesToDelete.add(filesetDirs);
    if (rcs != null) {
        // sort first to files, then dirs
        Restrict exists = new Restrict();
        exists.add(EXISTS);
        exists.add(rcs);
        Sort s = new Sort();
        s.add(REVERSE_FILESYSTEM);
        s.add(exists);
        resourcesToDelete.add(s);
    }
    try {
        if (resourcesToDelete.isFilesystemOnly()) {
            for (Resource r : resourcesToDelete) {
                // nonexistent resources could only occur if we already
                // deleted something from a fileset:
                File f = r.as(FileProvider.class).getFile();
                if (!f.exists()) {
                    continue;
                }
                if (!f.isDirectory() || f.list().length == 0) {
                    log("Deleting " + f, verbosity);
                    if (!delete(f) && failonerror) {
                        handle("Unable to delete " + (f.isDirectory() ? "directory " : "file ") + f);
                    }
                }
            }
        } else {
            handle(getTaskName() + " handles only filesystem resources");
        }
    } catch (Exception e) {
        handle(e);
    } finally {
        if (implicit != null) {
            filesets.remove(implicit);
        }
    }
}
Also used : Path(java.nio.file.Path) FileSet(org.apache.tools.ant.types.FileSet) Resource(org.apache.tools.ant.types.Resource) FileResourceIterator(org.apache.tools.ant.types.resources.FileResourceIterator) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Restrict(org.apache.tools.ant.types.resources.Restrict) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Sort(org.apache.tools.ant.types.resources.Sort) BuildException(org.apache.tools.ant.BuildException) Resources(org.apache.tools.ant.types.resources.Resources) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Aggregations

Restrict (org.apache.tools.ant.types.resources.Restrict)6 Resource (org.apache.tools.ant.types.Resource)5 FileResource (org.apache.tools.ant.types.resources.FileResource)3 StringResource (org.apache.tools.ant.types.resources.StringResource)3 BuildException (org.apache.tools.ant.BuildException)2 FileSet (org.apache.tools.ant.types.FileSet)2 Resources (org.apache.tools.ant.types.resources.Resources)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 AbstractFileSet (org.apache.tools.ant.types.AbstractFileSet)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResourceIterator (org.apache.tools.ant.types.resources.FileResourceIterator)1 Intersect (org.apache.tools.ant.types.resources.Intersect)1 LogOutputResource (org.apache.tools.ant.types.resources.LogOutputResource)1 Sort (org.apache.tools.ant.types.resources.Sort)1 Union (org.apache.tools.ant.types.resources.Union)1 Date (org.apache.tools.ant.types.resources.selectors.Date)1