Search in sources :

Example 6 with SortedList

use of aQute.lib.collections.SortedList in project bnd by bndtools.

the class BaselineCommands method _schema.

/**
	 * Create a schema of a set of jars outling the packages and their versions.
	 * This will create a list of packages with multiple versions, link to their
	 * specifications, and the deltas between versions.
	 * 
	 * <pre>
	 *  bnd package schema
	 * <file.jar>*
	 * </pre>
	 * 
	 * @param opts
	 * @throws Exception
	 */
public void _schema(schemaOptions opts) throws Exception {
    MultiMap<String, PSpec> map = new MultiMap<String, PSpec>();
    Tag top = new Tag("jschema");
    int n = 1000;
    for (String spec : opts._arguments()) {
        File f = bnd.getFile(spec);
        if (!f.isFile()) {
            bnd.messages.NoSuchFile_(f);
        } else {
            // For each specification jar we found
            logger.debug("spec {}", f);
            // spec
            Jar jar = new Jar(f);
            Manifest m = jar.getManifest();
            Attributes main = m.getMainAttributes();
            Tag specTag = new Tag(top, "specification");
            specTag.addAttribute("jar", spec);
            specTag.addAttribute("name", main.getValue("Specification-Name"));
            specTag.addAttribute("title", main.getValue("Specification-Title"));
            specTag.addAttribute("jsr", main.getValue("Specification-JSR"));
            specTag.addAttribute("url", main.getValue("Specification-URL"));
            specTag.addAttribute("version", main.getValue("Specification-Version"));
            specTag.addAttribute("vendor", main.getValue("Specification-Vendor"));
            specTag.addAttribute("id", n);
            specTag.addContent(main.getValue(Constants.BUNDLE_DESCRIPTION));
            Parameters exports = OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE));
            // Create a map with versions. Ensure import ranges overwrite
            // the
            // exported versions
            Parameters versions = new Parameters();
            versions.putAll(exports);
            versions.putAll(OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE)));
            Analyzer analyzer = new Analyzer();
            analyzer.setJar(jar);
            analyzer.analyze();
            Tree tree = differ.tree(analyzer);
            for (Entry<String, Attrs> entry : exports.entrySet()) {
                // For each exported package in the specification JAR
                Attrs attrs = entry.getValue();
                String packageName = entry.getKey();
                PackageRef packageRef = analyzer.getPackageRef(packageName);
                String version = attrs.get(Constants.VERSION_ATTRIBUTE);
                PSpec pspec = new PSpec();
                pspec.packageName = packageName;
                pspec.version = new Version(version);
                pspec.id = n;
                pspec.attrs = attrs;
                pspec.tree = tree;
                Collection<PackageRef> uses = analyzer.getUses().get(packageRef);
                if (uses != null) {
                    for (PackageRef x : uses) {
                        if (x.isJava())
                            continue;
                        String imp = x.getFQN();
                        if (imp.equals(packageName))
                            continue;
                        String v = null;
                        if (versions.containsKey(imp))
                            v = versions.get(imp).get(Constants.VERSION_ATTRIBUTE);
                        pspec.uses.put(imp, v);
                    }
                }
                map.add(packageName, pspec);
            }
            jar.close();
            n++;
        }
    }
    // We now gather all the information about all packages in the map.
    // Next phase is generating the XML. Sorting the packages is
    // important because XSLT is brain dead.
    SortedList<String> names = new SortedList<String>(map.keySet());
    Tag packagesTag = new Tag(top, "packages");
    Tag baselineTag = new Tag(top, "baseline");
    for (String pname : names) {
        // For each distinct package name
        SortedList<PSpec> specs = new SortedList<PSpec>(map.get(pname));
        PSpec older = null;
        Parameters olderExport = null;
        for (PSpec newer : specs) {
            // For each package in the total set
            Tag pack = new Tag(packagesTag, "package");
            pack.addAttribute("name", newer.packageName);
            pack.addAttribute("version", newer.version);
            pack.addAttribute("spec", newer.id);
            Parameters newerExport = new Parameters();
            newerExport.put(pname, newer.attrs);
            if (older != null) {
                String compareId = newer.packageName + "-" + newer.id + "-" + older.id;
                pack.addAttribute("delta", compareId);
                logger.debug(" newer={} older={}", newerExport, olderExport);
                Set<Info> infos = baseline.baseline(newer.tree, newerExport, older.tree, olderExport, new Instructions(pname));
                for (Info info : infos) {
                    Tag tag = getTag(info);
                    tag.addAttribute("id", compareId);
                    tag.addAttribute("newerSpec", newer.id);
                    tag.addAttribute("olderSpec", older.id);
                    baselineTag.addContent(tag);
                }
                older.tree = null;
                older.attrs = null;
                older = newer;
            }
            for (Entry<String, String> uses : newer.uses.entrySet()) {
                Tag reference = new Tag(pack, "import");
                reference.addAttribute("name", uses.getKey());
                reference.addAttribute("version", uses.getValue());
            }
            older = newer;
            olderExport = newerExport;
        }
    }
    String o = opts.output("schema.xml");
    File of = bnd.getFile(o);
    File pof = of.getParentFile();
    IO.mkdirs(pof);
    try (PrintWriter pw = IO.writer(of, UTF_8)) {
        pw.print("<?xml version='1.0' encoding='UTF-8'?>\n");
        top.print(0, pw);
    }
    if (opts.xsl() != null) {
        URL home = bnd.getBase().toURI().toURL();
        URL xslt = new URL(home, opts.xsl());
        String path = of.getAbsolutePath();
        if (path.endsWith(".xml"))
            path = path.substring(0, path.length() - 4);
        path = path + ".html";
        File html = new File(path);
        logger.debug("xslt {} {} {} {}", xslt, of, html, html.exists());
        try (OutputStream out = IO.outputStream(html);
            InputStream in = xslt.openStream()) {
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
            transformer.transform(new StreamSource(of), new StreamResult(out));
        }
    }
}
Also used : Transformer(javax.xml.transform.Transformer) OutputStream(java.io.OutputStream) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) SortedList(aQute.lib.collections.SortedList) Analyzer(aQute.bnd.osgi.Analyzer) URL(java.net.URL) MultiMap(aQute.lib.collections.MultiMap) Version(aQute.bnd.version.Version) Tree(aQute.bnd.service.diff.Tree) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) PrintWriter(java.io.PrintWriter) Parameters(aQute.bnd.header.Parameters) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Instructions(aQute.bnd.osgi.Instructions) Manifest(java.util.jar.Manifest) Info(aQute.bnd.differ.Baseline.Info) BundleInfo(aQute.bnd.differ.Baseline.BundleInfo) Jar(aQute.bnd.osgi.Jar) Tag(aQute.lib.tag.Tag) File(java.io.File)

Example 7 with SortedList

use of aQute.lib.collections.SortedList in project bnd by bndtools.

the class RepoCommand method _list.

@Description("List all artifacts from the current repositories with their versions")
public void _list(listOptions opts) throws Exception {
    logger.debug("list");
    Set<String> bsns = new HashSet<String>();
    Instruction from = opts.from();
    if (from == null)
        from = new Instruction("*");
    for (RepositoryPlugin repo : repos) {
        if (from.matches(repo.getName()))
            bsns.addAll(repo.list(opts.query()));
    }
    logger.debug("list {}", bsns);
    for (String bsn : new SortedList<String>(bsns)) {
        if (!opts.noversions()) {
            Set<Version> versions = new TreeSet<Version>();
            for (RepositoryPlugin repo : repos) {
                logger.debug("get {} from {}", bsn, repo);
                if (from.matches(repo.getName())) {
                    SortedSet<Version> result = repo.versions(bsn);
                    if (result != null)
                        versions.addAll(result);
                }
            }
            bnd.out.printf("%-40s %s%n", bsn, versions);
        } else {
            bnd.out.printf("%s%n", bsn);
        }
    }
}
Also used : Version(aQute.bnd.version.Version) TreeSet(java.util.TreeSet) SortedList(aQute.lib.collections.SortedList) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Instruction(aQute.bnd.osgi.Instruction) HashSet(java.util.HashSet) Description(aQute.lib.getopt.Description)

Example 8 with SortedList

use of aQute.lib.collections.SortedList in project bnd by bndtools.

the class RepoCommand method _get.

/**
	 * get a file from the repo
	 * 
	 * @param opts
	 */
@Description("Get an artifact from a repository.")
public void _get(getOptions opts) throws Exception {
    Instruction from = opts.from();
    if (from == null)
        from = new Instruction("*");
    List<String> args = opts._arguments();
    if (args.isEmpty()) {
        bnd.error("Get needs at least a bsn");
        return;
    }
    String bsn = args.remove(0);
    String range = null;
    if (!args.isEmpty()) {
        range = args.remove(0);
        if (!args.isEmpty()) {
            bnd.error("Extra args %s", args);
        }
    }
    VersionRange r = new VersionRange(range == null ? "0" : range);
    Map<Version, RepositoryPlugin> index = new HashMap<Version, RepositoryPlugin>();
    for (RepositoryPlugin repo : repos) {
        if (from.matches(repo.getName())) {
            SortedSet<Version> versions = repo.versions(bsn);
            if (versions != null)
                for (Version v : versions) {
                    if (r.includes(v))
                        index.put(v, repo);
                }
        }
    }
    SortedList<Version> l = new SortedList<Version>(index.keySet());
    if (l.isEmpty()) {
        bnd.out.printf("No versions found for %s%n", bsn);
        return;
    }
    Version v;
    if (opts.lowest())
        v = l.first();
    else
        v = l.last();
    RepositoryPlugin repo = index.get(v);
    File file = repo.get(bsn, v, null);
    File dir = bnd.getBase();
    String name = file.getName();
    if (opts.output() != null) {
        File f = bnd.getFile(opts.output());
        if (f.isDirectory())
            dir = f;
        else {
            dir = f.getParentFile();
            name = f.getName();
        }
    }
    IO.mkdirs(dir);
    IO.copy(file, new File(dir, name));
}
Also used : HashMap(java.util.HashMap) Version(aQute.bnd.version.Version) SortedList(aQute.lib.collections.SortedList) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) VersionRange(aQute.bnd.version.VersionRange) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 9 with SortedList

use of aQute.lib.collections.SortedList in project bnd by bndtools.

the class BuilderTest method testSources.

public static void testSources() throws Exception {
    Builder bmaker = new Builder();
    try {
        bmaker.addClasspath(new File("bin"));
        bmaker.setSourcepath(new File[] { new File("src") });
        bmaker.setProperty("-sources", "true");
        bmaker.setProperty("Export-Package", "test.activator");
        Jar jar = bmaker.build();
        assertTrue(bmaker.check());
        assertEquals("[test/activator/AbstractActivator.class, test/activator/Activator.class, test/activator/Activator11.class, test/activator/Activator2.class, test/activator/Activator3.class, test/activator/ActivatorPackage.class, test/activator/ActivatorPrivate.class, test/activator/DefaultVisibilityActivator.class, test/activator/IActivator.class, test/activator/MissingNoArgsConstructorActivator.class, test/activator/NotAnActivator.class]", new SortedList<String>(jar.getDirectories().get("test/activator").keySet()).toString());
    } finally {
        bmaker.close();
    }
}
Also used : Builder(aQute.bnd.osgi.Builder) SortedList(aQute.lib.collections.SortedList) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Example 10 with SortedList

use of aQute.lib.collections.SortedList in project bnd by bndtools.

the class ProjectBuilder method removeStagedAndFilter.

/**
	 * Remove any staging versions that have a variant with a higher qualifier.
	 * 
	 * @param versions
	 * @param repo
	 * @throws Exception
	 */
private SortedSet<Version> removeStagedAndFilter(SortedSet<Version> versions, RepositoryPlugin repo, String bsn) throws Exception {
    List<Version> filtered = new ArrayList<Version>(versions);
    Collections.reverse(filtered);
    InfoRepository ir = (repo instanceof InfoRepository) ? (InfoRepository) repo : null;
    //
    // Filter any versions that only differ in qualifier
    // The last variable is the last one added. Since we are
    // sorted from high to low, we skip any earlier base versions
    //
    Version last = null;
    for (Iterator<Version> i = filtered.iterator(); i.hasNext(); ) {
        Version v = i.next();
        // Check if same base version as last
        Version current = v.getWithoutQualifier();
        if (last != null && current.equals(last)) {
            i.remove();
            continue;
        }
        // /
        if (ir != null && !isMaster(ir, bsn, v))
            i.remove();
        last = current;
    }
    SortedList<Version> set = new SortedList<Version>(filtered);
    logger.debug("filtered for only latest staged: {} from {} in range ", set, versions);
    return set;
}
Also used : Version(aQute.bnd.version.Version) ArrayList(java.util.ArrayList) SortedList(aQute.lib.collections.SortedList) InfoRepository(aQute.bnd.service.repository.InfoRepository)

Aggregations

SortedList (aQute.lib.collections.SortedList)10 Version (aQute.bnd.version.Version)8 File (java.io.File)6 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Attrs (aQute.bnd.header.Attrs)2 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)2 Instruction (aQute.bnd.osgi.Instruction)2 Jar (aQute.bnd.osgi.Jar)2 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)2 MultiMap (aQute.lib.collections.MultiMap)2 Description (aQute.lib.getopt.Description)2 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 BundleInfo (aQute.bnd.differ.Baseline.BundleInfo)1 Info (aQute.bnd.differ.Baseline.Info)1 Parameters (aQute.bnd.header.Parameters)1 Analyzer (aQute.bnd.osgi.Analyzer)1 Builder (aQute.bnd.osgi.Builder)1 Domain (aQute.bnd.osgi.Domain)1