Search in sources :

Example 1 with AbstractFileSet

use of org.apache.tools.ant.types.AbstractFileSet in project ant by apache.

the class ExecuteOn method runExec.

/**
 * Run the specified Execute object.
 * @param exe the Execute instance representing the external process.
 * @throws BuildException on error
 */
@Override
protected void runExec(Execute exe) throws BuildException {
    int totalFiles = 0;
    int totalDirs = 0;
    boolean haveExecuted = false;
    try {
        Vector<String> fileNames = new Vector<>();
        Vector<File> baseDirs = new Vector<>();
        for (AbstractFileSet fs : filesets) {
            String currentType = type;
            if (fs instanceof DirSet) {
                if (!FileDirBoth.DIR.equals(type)) {
                    log("Found a nested dirset but type is " + type + ". " + "Temporarily switching to type=\"dir\" on the assumption that you really did mean <dirset> not <fileset>.", Project.MSG_DEBUG);
                    currentType = FileDirBoth.DIR;
                }
            }
            File base = fs.getDir(getProject());
            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            if (!FileDirBoth.DIR.equals(currentType)) {
                for (String value : getFiles(base, ds)) {
                    totalFiles++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (!FileDirBoth.FILE.equals(currentType)) {
                for (String value : getDirs(base, ds)) {
                    totalDirs++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (fileNames.isEmpty() && skipEmpty) {
                logSkippingFileset(currentType, ds, base);
                continue;
            }
            if (!parallel) {
                for (String srcFile : fileNames) {
                    String[] command = getCommandline(srcFile, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, srcFile);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                }
                fileNames.clear();
                baseDirs.clear();
            }
        }
        if (resources != null) {
            for (Resource res : resources) {
                if (!res.isExists() && ignoreMissing) {
                    continue;
                }
                File base = null;
                String name = res.getName();
                FileProvider fp = res.as(FileProvider.class);
                if (fp != null) {
                    FileResource fr = ResourceUtils.asFileResource(fp);
                    base = fr.getBaseDir();
                    if (base == null) {
                        name = fr.getFile().getAbsolutePath();
                    }
                }
                if (restrict(new String[] { name }, base).length == 0) {
                    continue;
                }
                if ((!res.isDirectory() || !res.isExists()) && !FileDirBoth.DIR.equals(type)) {
                    totalFiles++;
                } else if (res.isDirectory() && !FileDirBoth.FILE.equals(type)) {
                    totalDirs++;
                } else {
                    continue;
                }
                baseDirs.add(base);
                fileNames.add(name);
                if (!parallel) {
                    String[] command = getCommandline(name, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, name);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                    fileNames.clear();
                    baseDirs.clear();
                }
            }
        }
        if (parallel && (!fileNames.isEmpty() || !skipEmpty)) {
            runParallel(exe, fileNames, baseDirs);
            haveExecuted = true;
        }
        if (haveExecuted) {
            log("Applied " + cmdl.getExecutable() + " to " + totalFiles + " file" + (totalFiles != 1 ? "s" : "") + " and " + totalDirs + " director" + (totalDirs != 1 ? "ies" : "y") + ".", verbose ? Project.MSG_INFO : Project.MSG_VERBOSE);
        }
    } catch (IOException e) {
        throw new BuildException("Execute failed: " + e, e, getLocation());
    } finally {
        // close the output file if required
        logFlush();
        redirector.setAppendProperties(false);
        redirector.setProperties();
    }
}
Also used : AbstractFileSet(org.apache.tools.ant.types.AbstractFileSet) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) IOException(java.io.IOException) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) Vector(java.util.Vector) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Vector (java.util.Vector)1 BuildException (org.apache.tools.ant.BuildException)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 AbstractFileSet (org.apache.tools.ant.types.AbstractFileSet)1 DirSet (org.apache.tools.ant.types.DirSet)1 Resource (org.apache.tools.ant.types.Resource)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1