Search in sources :

Example 11 with ModuleInfo

use of org.eclipse.ceylon.cmr.api.ModuleInfo in project ceylon by eclipse.

the class Java9Util method main.

public static void main(String[] args) throws IOException {
    System.err.println("Add Java9 Module info for " + args[0]);
    // FIXME: check arg
    String jarName = args[0];
    File jarFile = new File(jarName);
    ZipFile zipFile = new ZipFile(jarFile);
    File outFile = File.createTempFile(jarName, ".jar");
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFile));
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    Set<String> packages = new HashSet<String>();
    ModuleInfo info = null;
    String moduleName = null, version = null;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        String name = entry.getName();
        if (name.equals("module-info.class")) {
            System.err.println(" Already has a module info!");
            zipFile.close();
            zos.close();
            outFile.delete();
            return;
        } else if (name.endsWith(".class")) {
            int lastSlash = name.lastIndexOf('/');
            // ignore the default package (not sure if we can import or conceal it in Java 9
            if (lastSlash != -1) {
                String pkg = name.substring(0, lastSlash).replace('/', '.');
                packages.add(pkg);
            }
        } else if (name.startsWith(METAINF_JBOSSMODULES) && name.endsWith("module.xml")) {
            String path = name.substring(METAINF_JBOSSMODULES.length());
            path = path.substring(0, path.length() - MODULE_XML.length() - 1);
            int p = path.lastIndexOf('/');
            if (p > 0) {
                moduleName = path.substring(0, p).replace('/', '.');
                version = path.substring(p + 1);
            }
            try (InputStream is = zipFile.getInputStream(entry)) {
                // name/version only used when we have an override
                info = XmlDependencyResolver.INSTANCE.resolveFromInputStream(is, null, null, null);
                System.err.println(" Found module descriptor at " + name);
            }
        }
        zos.putNextEntry(entry);
        if (!entry.isDirectory())
            IOUtils.copyStream(zipFile.getInputStream(entry), zos, true, false);
        zos.closeEntry();
    }
    zipFile.close();
    if (info != null && moduleName != null && version != null) {
        writeModuleDescriptor(zos, new Java9ModuleDescriptor(moduleName, version, info, packages));
    }
    zos.flush();
    zos.close();
    // rename
    jarFile.delete();
    Files.copy(outFile.toPath(), jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
    outFile.delete();
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) HashSet(java.util.HashSet)

Example 12 with ModuleInfo

use of org.eclipse.ceylon.cmr.api.ModuleInfo in project ceylon by eclipse.

the class ModuleSourceMapper method overrideModuleImports.

protected void overrideModuleImports(Module module, ArtifactResult artifact) {
    Overrides overrides = getContext().getRepositoryManager().getOverrides();
    if (overrides != null) {
        Set<ModuleDependencyInfo> existingModuleDependencies = new HashSet<>();
        for (ModuleImport i : module.getImports()) {
            Module m = i.getModule();
            if (m != null) {
                existingModuleDependencies.add(new ModuleDependencyInfo(i.getNamespace(), m.getNameAsString(), m.getVersion(), i.isOptional(), i.isExport(), i.getNativeBackends()));
            }
        }
        ModuleInfo sourceModuleInfo = new ModuleInfo(artifact.namespace(), artifact.name(), artifact.version(), artifact.groupId(), artifact.artifactId(), artifact.classifier(), null, existingModuleDependencies);
        ModuleInfo newModuleInfo = overrides.applyOverrides(artifact.name(), artifact.version(), sourceModuleInfo);
        List<ModuleImport> newModuleImports = new ArrayList<>();
        for (ModuleDependencyInfo dep : newModuleInfo.getDependencies()) {
            Module dependency = getModuleManager().getOrCreateModule(ModuleManager.splitModuleName(dep.getName()), dep.getVersion());
            Backends backends = dependency.getNativeBackends();
            ModuleImport newImport = new ModuleImport(dep.getNamespace(), dependency, dep.isOptional(), dep.isExport(), backends);
            newModuleImports.add(newImport);
        }
        module.overrideImports(newModuleImports);
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) ArrayList(java.util.ArrayList) Overrides(org.eclipse.ceylon.cmr.api.Overrides) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashSet(java.util.HashSet)

Example 13 with ModuleInfo

use of org.eclipse.ceylon.cmr.api.ModuleInfo in project ceylon by eclipse.

the class CMRTests method testMavenFileResolver.

@Test
public void testMavenFileResolver() throws ZipException, IOException {
    CeylonRepoManagerBuilder builder = CeylonUtils.repoManager();
    RepositoryManager repository = builder.buildManager();
    String groupId = "javax.el";
    String artifactId = "javax.el-api";
    String version = "3.0.0";
    String coord = groupId + ":" + artifactId;
    File artifact = repository.getArtifact(MavenArtifactContext.NAMESPACE, coord, version);
    Assert.assertNotNull(artifact);
    try (ZipFile zf = new ZipFile(artifact)) {
        String descriptorPath = String.format("META-INF/maven/%s/%s/pom.xml", groupId, artifactId);
        ZipEntry entry = zf.getEntry(descriptorPath);
        Assert.assertNotNull(entry);
        try (InputStream is = zf.getInputStream(entry)) {
            DependencyResolver resolver = new MavenDependencyResolver();
            ModuleInfo info = resolver.resolveFromInputStream(is, coord, version, null);
            Assert.assertNotNull(info);
            // FIXME: find one with dependencies
            System.err.println(info.getDependencies());
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) CeylonRepoManagerBuilder(org.eclipse.ceylon.cmr.ceylon.CeylonUtils.CeylonRepoManagerBuilder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) MavenDependencyResolver(org.eclipse.ceylon.cmr.maven.MavenDependencyResolver) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) MavenDependencyResolver(org.eclipse.ceylon.cmr.maven.MavenDependencyResolver) DependencyResolver(org.eclipse.ceylon.cmr.api.DependencyResolver) Test(org.junit.Test)

Example 14 with ModuleInfo

use of org.eclipse.ceylon.cmr.api.ModuleInfo in project ceylon by eclipse.

the class XmlDependencyResolver method resolveFromInputStream.

@Override
public ModuleInfo resolveFromInputStream(InputStream stream, String name, String version, Overrides overrides) {
    try {
        final Module module = parse(stream);
        final Set<ModuleDependencyInfo> infos = new LinkedHashSet<>();
        for (ModuleIdentifier mi : module.getDependencies()) {
            infos.add(new ModuleDependencyInfo(null, mi.getName(), mi.getSlot(), mi.isOptional(), mi.isExport(), Backends.JAVA));
        }
        ModuleInfo ret = new ModuleInfo(null, name, version, module.getGroupId(), module.getArtifactId(), null, module.getFilter(), infos);
        if (overrides != null)
            ret = overrides.applyOverrides(name, version, ret);
        return ret;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 15 with ModuleInfo

use of org.eclipse.ceylon.cmr.api.ModuleInfo in project ceylon by eclipse.

the class ModulesDependencyResolver method augment.

private ModuleInfo augment(ArtifactResult result, ModuleInfo ret) {
    if (ret == null)
        return null;
    if (ret.getGroupId() != null)
        return ret;
    // see if we have a Maven descriptor in there
    if (result.artifact() != null) {
        try (ZipFile zf = new ZipFile(result.artifact())) {
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                String path = entry.getName();
                if (path.startsWith("META-INF/maven/") && path.endsWith("/pom.xml")) {
                    String part = path.substring(15, path.length() - 8);
                    int sep = part.indexOf('/');
                    if (sep != -1) {
                        String groupId = part.substring(0, sep);
                        String artifactId = part.substring(sep + 1);
                        return new ModuleInfo(ret.getNamespace(), ret.getName(), ret.getVersion(), groupId, artifactId, null, ret.getFilter(), ret.getDependencies());
                    }
                }
            }
        } catch (IOException e) {
        // not a zip file
        }
    }
    return ret;
}
Also used : ZipFile(java.util.zip.ZipFile) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Aggregations

ModuleInfo (org.eclipse.ceylon.cmr.api.ModuleInfo)20 ModuleDependencyInfo (org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)12 HashSet (java.util.HashSet)9 File (java.io.File)4 IOException (java.io.IOException)3 Map (java.util.Map)3 ZipEntry (java.util.zip.ZipEntry)3 ZipFile (java.util.zip.ZipFile)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 JarFile (java.util.jar.JarFile)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ModuleVersionArtifact (org.eclipse.ceylon.cmr.api.ModuleVersionArtifact)2 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)2 MavenDependencyResolver (org.eclipse.ceylon.cmr.maven.MavenDependencyResolver)2 Backends (org.eclipse.ceylon.common.Backends)2 Annotation (org.eclipse.ceylon.langtools.classfile.Annotation)2 ClassFile (org.eclipse.ceylon.langtools.classfile.ClassFile)2 Test (org.junit.Test)2