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();
}
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);
}
}
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());
}
}
}
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);
}
}
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;
}
Aggregations