Search in sources :

Example 21 with DirectoryScanner

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

the class ApexCli method expandFileName.

private static String expandFileName(String fileName, boolean expandWildCard) throws IOException {
    if (fileName.matches("^[a-zA-Z]+:.*")) {
        // it's a URL
        return fileName;
    }
    // TODO: need to work with other users' home directory
    if (fileName.startsWith("~" + File.separator)) {
        fileName = System.getProperty("user.home") + fileName.substring(1);
    }
    fileName = new File(fileName).getCanonicalPath();
    //LOG.debug("Canonical path: {}", fileName);
    if (expandWildCard) {
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[] { fileName });
        scanner.scan();
        String[] files = scanner.getIncludedFiles();
        if (files.length == 0) {
            throw new CliException(fileName + " does not match any file");
        } else if (files.length > 1) {
            throw new CliException(fileName + " matches more than one file");
        }
        return files[0];
    } else {
        return fileName;
    }
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File)

Example 22 with DirectoryScanner

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

the class StramAppLauncher method processLibJars.

private void processLibJars(String libjars, Set<URL> clUrls) throws Exception {
    for (String libjar : libjars.split(",")) {
        // if hadoop file system, copy from hadoop file system to local
        URI uri = new URI(libjar);
        String scheme = uri.getScheme();
        if (scheme == null) {
            // expand wildcards
            DirectoryScanner scanner = new DirectoryScanner();
            scanner.setIncludes(new String[] { libjar });
            scanner.scan();
            String[] files = scanner.getIncludedFiles();
            for (String file : files) {
                clUrls.add(new URL("file:" + file));
            }
        } else if (scheme.equals("file")) {
            clUrls.add(new URL(libjar));
        } else {
            if (fs != null) {
                Path path = new Path(libjar);
                File dependencyJarsDir = new File(StramClientUtils.getUserDTDirectory(), "dependencyJars");
                dependencyJarsDir.mkdirs();
                File localJarFile = new File(dependencyJarsDir, path.getName());
                fs.copyToLocalFile(path, new Path(localJarFile.getAbsolutePath()));
                clUrls.add(new URL("file:" + localJarFile.getAbsolutePath()));
            } else {
                throw new NotImplementedException("Jar file needs to be from Hadoop File System also in order for the dependency jars to be in Hadoop File System");
            }
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) NotImplementedException(org.apache.commons.lang.NotImplementedException) URI(java.net.URI) File(java.io.File) URL(java.net.URL)

Example 23 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project gcontracts by andresteingress.

the class ContractGroovyDoc 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 24 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project bnd by bndtools.

the class RepoIndexTask method execute.

@Override
public void execute() throws BuildException {
    printCopyright(System.err);
    if (repositoryFile == null)
        throw new BuildException("Output file not specified");
    try {
        // Configure PojoSR
        Map<String, Object> pojoSrConfig = new HashMap<String, Object>();
        pojoSrConfig.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, new ClasspathScanner());
        // Start PojoSR 'framework'
        Framework framework = new PojoServiceRegistryFactoryImpl().newFramework(pojoSrConfig);
        framework.init();
        framework.start();
        if (knownBundles) {
            registerKnownBundles(framework.getBundleContext());
        }
        // Look for indexer and run index generation
        ServiceTracker<ResourceIndexer, ResourceIndexer> tracker = new ServiceTracker<ResourceIndexer, ResourceIndexer>(framework.getBundleContext(), ResourceIndexer.class, null);
        tracker.open();
        ResourceIndexer index = tracker.waitForService(1000);
        if (index == null)
            throw new IllegalStateException("Timed out waiting for ResourceIndexer service.");
        // Flatten the file sets into a single list
        Set<File> fileList = new LinkedHashSet<File>();
        for (FileSet fileSet : fileSets) {
            DirectoryScanner ds = fileSet.getDirectoryScanner(getProject());
            File basedir = ds.getBasedir();
            String[] files = ds.getIncludedFiles();
            for (int i = 0; i < files.length; i++) fileList.add(new File(basedir, files[i]));
        }
        // Run
        try (OutputStream fos = Files.newOutputStream(repositoryFile.toPath())) {
            index.index(fileList, fos, config);
        }
    } catch (Exception e) {
        throw new BuildException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileSet(org.apache.tools.ant.types.FileSet) HashMap(java.util.HashMap) ServiceTracker(org.osgi.util.tracker.ServiceTracker) OutputStream(java.io.OutputStream) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ClasspathScanner(de.kalpatec.pojosr.framework.launch.ClasspathScanner) PojoServiceRegistryFactoryImpl(de.kalpatec.pojosr.framework.PojoServiceRegistryFactoryImpl) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Framework(org.osgi.framework.launch.Framework)

Example 25 with DirectoryScanner

use of org.apache.tools.ant.DirectoryScanner in project ceylon-compiler by ceylon.

the class SourceModules method getModules.

// TODO filters by module name, supported backends (transitive)
public Set<Module> getModules() {
    if (this.dir == null) {
        this.dir = getProject().resolveFile(Constants.DEFAULT_SOURCE_DIR);
    }
    FileSet fs = new FileSet();
    fs.setDir(this.dir);
    // TODO Handle default module
    fs.setIncludes("**/" + Constants.MODULE_DESCRIPTOR);
    DirectoryScanner ds = fs.getDirectoryScanner(getProject());
    String[] files = ds.getIncludedFiles();
    log("<sourcemodules> found files " + Arrays.toString(files), Project.MSG_VERBOSE);
    URI base = dir.toURI();
    LinkedHashSet<Module> result = new LinkedHashSet<Module>();
    try {
        CeylonClassLoader loader = Util.getCeylonClassLoaderCachedInProject(getProject());
        for (String file : files) {
            URI uri = new File(this.dir, file).getParentFile().toURI();
            log("<sourcemodules> file " + file + "=> uri " + uri, Project.MSG_VERBOSE);
            String moduleName = base.relativize(uri).getPath().replace('/', '.');
            if (moduleName.endsWith(".")) {
                moduleName = moduleName.substring(0, moduleName.length() - 1);
            }
            log("<sourcemodules> file " + file + "=> moduleName " + moduleName, Project.MSG_VERBOSE);
            Module mav = new Module();
            mav.setName(moduleName);
            String version;
            try {
                version = new ModuleDescriptorReader(loader, mav.getName(), dir).getModuleVersion();
            } catch (NoSuchModuleException e) {
                log("<sourcemodules> file " + file + "=> module cannot be read: " + moduleName, Project.MSG_VERBOSE);
                // skip it
                continue;
            }
            log("<sourcemodules> file " + file + "=> module " + moduleName + "/" + version, Project.MSG_VERBOSE);
            mav.setVersion(version);
            result.add(mav);
        }
    } catch (ClassLoaderSetupException x) {
        log("failed to set up Ceylon classloader: could not load module set", Project.MSG_VERBOSE);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileSet(org.apache.tools.ant.types.FileSet) CeylonClassLoader(com.redhat.ceylon.launcher.CeylonClassLoader) ClassLoaderSetupException(com.redhat.ceylon.launcher.ClassLoaderSetupException) URI(java.net.URI) ModuleDescriptorReader(com.redhat.ceylon.common.ModuleDescriptorReader) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) NoSuchModuleException(com.redhat.ceylon.common.ModuleDescriptorReader.NoSuchModuleException) File(java.io.File)

Aggregations

DirectoryScanner (org.apache.tools.ant.DirectoryScanner)57 File (java.io.File)52 BuildException (org.apache.tools.ant.BuildException)26 FileSet (org.apache.tools.ant.types.FileSet)26 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)12 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 FileInputStream (java.io.FileInputStream)4 FilenameFilter (java.io.FilenameFilter)4 Project (org.apache.tools.ant.Project)4 DirSet (org.apache.tools.ant.types.DirSet)4 PatternSet (org.apache.tools.ant.types.PatternSet)4 LinkedList (java.util.LinkedList)3 Path (org.apache.tools.ant.types.Path)3 AbstractProject (hudson.model.AbstractProject)2 VirtualChannel (hudson.remoting.VirtualChannel)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2