Search in sources :

Example 1 with ModuleDescriptorReader

use of org.eclipse.ceylon.common.ModuleDescriptorReader in project ceylon by eclipse.

the class ModuleVersionReader method getModuleVersionDetailsFromSource.

/**
 * Reads a module descriptor and returns its information
 * @param moduleName The name of the module
 * @param srcDir The source directory where to find the descriptor
 * @return A <code>ModuleVersionsDetails</code> with the encountered information
 * @throws NoSuchModuleException if the module could not be found
 */
public static ModuleVersionDetails getModuleVersionDetailsFromSource(String moduleName, File srcDir) throws NoSuchModuleException {
    ModuleDescriptorReader mdr = new ModuleDescriptorReader(moduleName, srcDir);
    String module = mdr.getModuleName();
    String version = mdr.getModuleVersion();
    // PS In case the module descriptor was found but could not be parsed
    // we'll create an invalid details object
    ModuleVersionDetails mvd = new ModuleVersionDetails(module != null ? module : "", version != null ? version : "", mdr.getModuleGroupId(), mdr.getModuleArtifactId());
    mvd.setLabel(mdr.getModuleLabel());
    mvd.setLicense(mdr.getModuleLicense());
    List<String> by = mdr.getModuleAuthors();
    if (by != null) {
        mvd.getAuthors().addAll(by);
    }
    SortedSet<ModuleDependencyInfo> dependencies = new TreeSet<>();
    for (Object[] dep : mdr.getModuleImports()) {
        dependencies.add(new ModuleDependencyInfo((String) dep[0], (String) dep[1], (String) dep[2], (Boolean) dep[3], (Boolean) dep[4], (Backends) dep[5]));
    }
    mvd.setDependencies(dependencies);
    mvd.setRemote(false);
    mvd.setOrigin("Local source folder");
    return mvd;
}
Also used : ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader) Backends(org.eclipse.ceylon.common.Backends) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) TreeSet(java.util.TreeSet) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)

Example 2 with ModuleDescriptorReader

use of org.eclipse.ceylon.common.ModuleDescriptorReader in project ceylon by eclipse.

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(org.eclipse.ceylon.launcher.CeylonClassLoader) ClassLoaderSetupException(org.eclipse.ceylon.launcher.ClassLoaderSetupException) URI(java.net.URI) ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) NoSuchModuleException(org.eclipse.ceylon.common.ModuleDescriptorReader.NoSuchModuleException) File(java.io.File)

Example 3 with ModuleDescriptorReader

use of org.eclipse.ceylon.common.ModuleDescriptorReader in project ceylon by eclipse.

the class LazyHelper method inferModule.

private Module inferModule(File file) {
    if (file.exists()) {
        for (File src : task.getSrc()) {
            if (Util.isChildOfOrEquals(src, file)) {
                while (!file.equals(src)) {
                    File moduleDescriptor = file.isDirectory() ? new File(file, Constants.MODULE_DESCRIPTOR) : file;
                    if (moduleDescriptor.exists() && moduleDescriptor.getName().equals(Constants.MODULE_DESCRIPTOR)) {
                        String moduleName = moduleDescriptor.getParentFile().getAbsolutePath().substring(src.getAbsolutePath().length() + 1).replace(File.separator, ".");
                        try {
                            ModuleDescriptorReader mdr = new ModuleDescriptorReader(task.getClassLoader(), moduleName, src);
                            return new Module(mdr.getModuleName(), mdr.getModuleVersion());
                        } catch (NoSuchModuleException e) {
                            continue;
                        } catch (ClassLoaderSetupException e) {
                            // not like we can try again
                            return null;
                        }
                    }
                    file = file.getParentFile();
                }
            }
        }
    }
    return null;
}
Also used : ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader) NoSuchModuleException(org.eclipse.ceylon.common.ModuleDescriptorReader.NoSuchModuleException) ClassLoaderSetupException(org.eclipse.ceylon.launcher.ClassLoaderSetupException) File(java.io.File)

Example 4 with ModuleDescriptorReader

use of org.eclipse.ceylon.common.ModuleDescriptorReader in project ceylon by eclipse.

the class ModuleDescriptorReaderTest method testReadDescriptorA.

@Test
public void testReadDescriptorA() throws NoSuchModuleException {
    // We need this property because the ModuleDescriptorReader uses the Ceylon
    // Launcher+CeylonClassLoader that dynamically find and load the necessary
    // jar and car files. But when running from within Eclipse it doesn't know
    // where to look, so we need to give it a hint
    System.setProperty("ceylon.home", "../dist/dist");
    ModuleDescriptorReader r = new ModuleDescriptorReader("org.eclipse.ceylon.ant.modules.a", new File("test/src"));
    Assert.assertEquals("1.0", r.getModuleVersion());
    Assert.assertEquals("org.eclipse.ceylon.ant.modules.a", r.getModuleName());
    Assert.assertEquals("http://example.com/license", r.getModuleLicense());
    List<String> authors = r.getModuleAuthors();
    Assert.assertEquals(2, authors.size());
    Assert.assertEquals("Tom", authors.get(0));
    Assert.assertEquals("and Tom", authors.get(1));
}
Also used : ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader) File(java.io.File) Test(org.junit.Test)

Example 5 with ModuleDescriptorReader

use of org.eclipse.ceylon.common.ModuleDescriptorReader in project ceylon by eclipse.

the class ModuleWildcardsHelper method includeModule.

private static boolean includeModule(String name, File sourceRoot, String prefix, Backend forBackend) {
    if (prefix != null) {
        int p = name.lastIndexOf('.');
        String lastPart = (p >= 0) ? name.substring(p + 1) : name;
        if (!lastPart.startsWith(prefix)) {
            return false;
        }
    }
    if (forBackend != null) {
        try {
            ModuleDescriptorReader mdr = new ModuleDescriptorReader(name, sourceRoot);
            List<String> backends = mdr.getModuleBackends();
            return backends.isEmpty() || backends.contains(forBackend.nativeAnnotation);
        } catch (ModuleDescriptorReader.NoSuchModuleException x) {
            x.printStackTrace();
        }
    }
    return true;
}
Also used : ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader)

Aggregations

ModuleDescriptorReader (org.eclipse.ceylon.common.ModuleDescriptorReader)8 File (java.io.File)4 NoSuchModuleException (org.eclipse.ceylon.common.ModuleDescriptorReader.NoSuchModuleException)3 ClassLoaderSetupException (org.eclipse.ceylon.launcher.ClassLoaderSetupException)3 CeylonClassLoader (org.eclipse.ceylon.launcher.CeylonClassLoader)2 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeSet (java.util.TreeSet)1 BuildException (org.apache.tools.ant.BuildException)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 FileSet (org.apache.tools.ant.types.FileSet)1 ModuleDependencyInfo (org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)1 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)1 Backends (org.eclipse.ceylon.common.Backends)1 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)1 Test (org.junit.Test)1