Search in sources :

Example 66 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project groovy-core by groovy.

the class GenerateStubsTask method compile.

@Override
protected void compile() {
    GroovyClassLoader gcl = createClassLoader();
    JavaStubCompilationUnit cu = new JavaStubCompilationUnit(config, gcl, destdir);
    int count = 0;
    String[] list = src.list();
    for (int i = 0; i < list.length; i++) {
        File basedir = getProject().resolveFile(list[i]);
        if (!basedir.exists()) {
            throw new BuildException("Source directory does not exist: " + basedir, getLocation());
        }
        DirectoryScanner scanner = getDirectoryScanner(basedir);
        String[] includes = scanner.getIncludedFiles();
        log.debug("Including files from: " + basedir);
        for (int j = 0; j < includes.length; j++) {
            log.debug("    " + includes[j]);
            File file = new File(basedir, includes[j]);
            cu.addSource(file);
            // Increment the count for each non/java src we found
            if (!includes[j].endsWith(".java")) {
                count++;
            }
        }
    }
    if (count > 0) {
        log.info("Generating " + count + " Java stub" + (count > 1 ? "s" : "") + " to " + destdir);
        cu.compile();
        log.info("Generated " + cu.getStubCount() + " Java stub(s)");
    } else {
        log.info("No sources found for stub generation");
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) JavaStubCompilationUnit(org.codehaus.groovy.tools.javac.JavaStubCompilationUnit) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 67 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project groovy-core by groovy.

the class Groovydoc method parsePackages.

/**
 * Add the directories matched by the nested dirsets to the resulting
 * packages list and the base directories of the dirsets to the Path.
 * It also handles the packages and excludepackages attributes and
 * elements.
 *
 * @param resultantPackages a list to which we add the packages found
 * @param sourcePath a path to which we add each basedir found
 * @since 1.5
 */
private void parsePackages(List<String> resultantPackages, Path sourcePath) {
    List<String> addedPackages = new ArrayList<String>();
    List<DirSet> dirSets = new ArrayList<DirSet>(packageSets);
    // and nested excludepackage elements
    if (this.sourcePath != null) {
        PatternSet ps = new PatternSet();
        if (packageNames.size() > 0) {
            for (String pn : packageNames) {
                String pkg = pn.replace('.', '/');
                if (pkg.endsWith("*")) {
                    pkg += "*";
                }
                ps.createInclude().setName(pkg);
            }
        } else {
            ps.createInclude().setName("**");
        }
        for (String epn : excludePackageNames) {
            String pkg = epn.replace('.', '/');
            if (pkg.endsWith("*")) {
                pkg += "*";
            }
            ps.createExclude().setName(pkg);
        }
        String[] pathElements = this.sourcePath.list();
        for (String pathElement : pathElements) {
            File dir = new File(pathElement);
            if (dir.isDirectory()) {
                DirSet ds = new DirSet();
                ds.setDefaultexcludes(useDefaultExcludes);
                ds.setDir(dir);
                ds.createPatternSet().addConfiguredPatternset(ps);
                dirSets.add(ds);
            } else {
                log.warn("Skipping " + pathElement + " since it is no directory.");
            }
        }
    }
    for (DirSet ds : dirSets) {
        File baseDir = ds.getDir(getProject());
        log.debug("scanning " + baseDir + " for packages.");
        DirectoryScanner dsc = ds.getDirectoryScanner(getProject());
        String[] dirs = dsc.getIncludedDirectories();
        boolean containsPackages = false;
        for (String dir : dirs) {
            // are there any groovy or java files in this directory?
            File pd = new File(baseDir, dir);
            String[] files = pd.list(new FilenameFilter() {

                public boolean accept(File dir1, String name) {
                    if (!includeNoSourcePackages && name.equals("package.html"))
                        return true;
                    final StringTokenizer tokenizer = new StringTokenizer(extensions, ":");
                    while (tokenizer.hasMoreTokens()) {
                        String ext = tokenizer.nextToken();
                        if (name.endsWith(ext))
                            return true;
                    }
                    return false;
                }
            });
            for (String filename : Arrays.asList(files)) {
                sourceFilesToDoc.add(dir + File.separator + filename);
            }
            if (files.length > 0) {
                if ("".equals(dir)) {
                    log.warn(baseDir + " contains source files in the default package," + " you must specify them as source files not packages.");
                } else {
                    containsPackages = true;
                    String pn = dir.replace(File.separatorChar, '.');
                    if (!addedPackages.contains(pn)) {
                        addedPackages.add(pn);
                        resultantPackages.add(pn);
                    }
                }
            }
        }
        if (containsPackages) {
            // We don't need to care for duplicates here,
            // Path.list does it for us.
            sourcePath.createPathElement().setLocation(baseDir);
        } else {
            log.verbose(baseDir + " doesn't contain any packages, dropping it.");
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) PatternSet(org.apache.tools.ant.types.PatternSet) File(java.io.File)

Example 68 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project groovy-core by groovy.

the class Groovyc method execute.

/**
 * Executes the task.
 *
 * @throws BuildException if an error occurs
 */
public void execute() throws BuildException {
    checkParameters();
    resetFileLists();
    loadRegisteredScriptExtensions();
    if (javac != null)
        jointCompilation = true;
    // scan source directories and dest directory to build up
    // compile lists
    String[] list = src.list();
    for (String filename : list) {
        File file = getProject().resolveFile(filename);
        if (!file.exists()) {
            throw new BuildException("srcdir \"" + file.getPath() + "\" does not exist!", getLocation());
        }
        DirectoryScanner ds = this.getDirectoryScanner(file);
        String[] files = ds.getIncludedFiles();
        scanDir(file, destDir != null ? destDir : file, files);
    }
    compile();
    if (updatedProperty != null && taskSuccess && compileList.length != 0) {
        getProject().setNewProperty(updatedProperty, "true");
    }
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 69 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project groovy by apache.

the class Groovydoc method parsePackages.

/**
 * Add the directories matched by the nested dirsets to the resulting
 * packages list and the base directories of the dirsets to the Path.
 * It also handles the packages and excludepackages attributes and
 * elements.
 *
 * @param resultantPackages a list to which we add the packages found
 * @param sourcePath        a path to which we add each basedir found
 * @since 1.5
 */
private void parsePackages(List<String> resultantPackages, Path sourcePath) {
    List<String> addedPackages = new ArrayList<>();
    List<DirSet> dirSets = new ArrayList<DirSet>(packageSets);
    // and nested excludepackage elements
    if (this.sourcePath != null) {
        PatternSet ps = new PatternSet();
        if (!packageNames.isEmpty()) {
            for (String pn : packageNames) {
                String pkg = pn.replace('.', '/');
                if (pkg.endsWith("*")) {
                    pkg += "*";
                }
                ps.createInclude().setName(pkg);
            }
        } else {
            ps.createInclude().setName("**");
        }
        for (String epn : excludePackageNames) {
            String pkg = epn.replace('.', '/');
            if (pkg.endsWith("*")) {
                pkg += "*";
            }
            ps.createExclude().setName(pkg);
        }
        String[] pathElements = this.sourcePath.list();
        for (String pathElement : pathElements) {
            File dir = new File(pathElement);
            if (dir.isDirectory()) {
                DirSet ds = new DirSet();
                ds.setDefaultexcludes(useDefaultExcludes);
                ds.setDir(dir);
                ds.createPatternSet().addConfiguredPatternset(ps);
                dirSets.add(ds);
            } else {
                log.warn("Skipping " + pathElement + " since it is no directory.");
            }
        }
    }
    for (DirSet ds : dirSets) {
        File baseDir = ds.getDir(getProject());
        log.debug("scanning " + baseDir + " for packages.");
        DirectoryScanner dsc = ds.getDirectoryScanner(getProject());
        String[] dirs = dsc.getIncludedDirectories();
        boolean containsPackages = false;
        for (String dir : dirs) {
            // are there any groovy or java files in this directory?
            File pd = new File(baseDir, dir);
            String[] files = pd.list((dir1, name) -> {
                if (!includeNoSourcePackages && name.equals("package.html"))
                    return true;
                final StringTokenizer tokenizer = new StringTokenizer(extensions, ":");
                while (tokenizer.hasMoreTokens()) {
                    String ext = tokenizer.nextToken();
                    if (name.endsWith(ext))
                        return true;
                }
                return false;
            });
            if (files != null) {
                for (String filename : files) {
                    sourceFilesToDoc.add(dir + File.separator + filename);
                }
                if (files.length > 0) {
                    if (dir.isEmpty()) {
                        log.warn(baseDir + " contains source files in the default package," + " you must specify them as source files not packages.");
                    } else {
                        containsPackages = true;
                        String pn = dir.replace(File.separatorChar, '.');
                        if (!addedPackages.contains(pn)) {
                            addedPackages.add(pn);
                            resultantPackages.add(pn);
                        }
                    }
                }
            }
        }
        if (containsPackages) {
            // We don't need to care for duplicates here,
            // Path.list does it for us.
            sourcePath.createPathElement().setLocation(baseDir);
        } else {
            log.verbose(baseDir + " doesn't contain any packages, dropping it.");
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ArrayList(java.util.ArrayList) PatternSet(org.apache.tools.ant.types.PatternSet) File(java.io.File)

Example 70 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project groovy by apache.

the class GenerateStubsTask method compile.

@Override
protected void compile() {
    GroovyClassLoader gcl = createClassLoader();
    JavaStubCompilationUnit cu = new JavaStubCompilationUnit(config, gcl, destdir);
    int count = 0;
    for (String srcPath : src.list()) {
        File srcDir = getProject().resolveFile(srcPath);
        if (!srcDir.exists()) {
            throw new BuildException("Source directory does not exist: " + srcDir, getLocation());
        }
        DirectoryScanner scanner = getDirectoryScanner(srcDir);
        log.debug("Including files from: " + srcDir);
        for (String includeName : scanner.getIncludedFiles()) {
            log.debug("    " + includeName);
            File file = new File(srcDir, includeName);
            if (isSource(includeName)) {
                cu.addSource(file);
            }
            // TODO support config.getScriptExtensions()?
            if (includeName.endsWith(config.getDefaultScriptExtension())) {
                count++;
            }
        }
    }
    if (count > 0) {
        log.info("Generating " + count + " Java stub" + (count > 1 ? "s" : "") + " to " + destdir);
        // Generate the stubs
        cu.compile(Phases.CONVERSION);
        log.info("Generated " + cu.getStubCount() + " Java stub(s)");
    } else {
        log.info("No sources found for stub generation");
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) JavaStubCompilationUnit(org.codehaus.groovy.tools.javac.JavaStubCompilationUnit) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Aggregations

DirectoryScanner (org.apache.tools.ant.DirectoryScanner)150 File (java.io.File)122 FileSet (org.apache.tools.ant.types.FileSet)84 BuildException (org.apache.tools.ant.BuildException)73 IOException (java.io.IOException)38 ArrayList (java.util.ArrayList)32 Project (org.apache.tools.ant.Project)14 Resource (org.apache.tools.ant.types.Resource)11 Test (org.junit.Test)11 FileResource (org.apache.tools.ant.types.resources.FileResource)8 HashMap (java.util.HashMap)7 Path (org.apache.tools.ant.types.Path)7 Hashtable (java.util.Hashtable)6 PatternSet (org.apache.tools.ant.types.PatternSet)6 FileWriter (java.io.FileWriter)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 StringTokenizer (java.util.StringTokenizer)5 Vector (java.util.Vector)5 DirSet (org.apache.tools.ant.types.DirSet)5