Search in sources :

Example 1 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class bnd method _bsn2url.

public void _bsn2url(Bsn2UrlOptions opts) throws Exception {
    Project p = getProject(opts.project());
    if (p == null) {
        error("You need to be in a project or specify the project with -p/--project");
        return;
    }
    MultiMap<String, Version> revisions = new MultiMap<String, Version>();
    for (RepositoryPlugin repo : p.getPlugins(RepositoryPlugin.class)) {
        if (!(repo instanceof InfoRepository))
            continue;
        for (String bsn : repo.list(null)) {
            revisions.addAll(bsn, repo.versions(bsn));
        }
    }
    for (List<Version> versions : revisions.values()) {
        Collections.sort(versions, Collections.reverseOrder());
    }
    List<String> files = opts._arguments();
    for (String f : files) {
        try (BufferedReader r = IO.reader(getFile(f))) {
            String line;
            nextLine: while ((line = r.readLine()) != null) {
                Matcher matcher = LINE_P.matcher(line);
                if (!matcher.matches())
                    continue nextLine;
                line = matcher.group(1);
                Parameters bundles = new Parameters(line, this);
                for (Map.Entry<String, Attrs> entry : bundles.entrySet()) {
                    String bsn = entry.getKey();
                    VersionRange range = new VersionRange(entry.getValue().getVersion());
                    List<Version> versions = revisions.get(bsn);
                    if (versions == null) {
                        error("No for versions for %s", bsn);
                        break nextLine;
                    }
                    for (Version version : versions) {
                        if (range.includes(version)) {
                            for (RepositoryPlugin repo : p.getPlugins(RepositoryPlugin.class)) {
                                if (!(repo instanceof InfoRepository))
                                    continue;
                                InfoRepository rp = (InfoRepository) repo;
                                ResourceDescriptor descriptor = rp.getDescriptor(bsn, version);
                                if (descriptor == null) {
                                    error("Found bundle, but no descriptor %s;version=%s", bsn, version);
                                    return;
                                }
                                out.println(descriptor.url + " #" + descriptor.bsn + ";version=" + descriptor.version);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            error("failed to create url list from file %s : %s", f, e);
        }
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Matcher(java.util.regex.Matcher) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) VersionRange(aQute.bnd.version.VersionRange) InfoRepository(aQute.bnd.service.repository.InfoRepository) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ZipException(java.util.zip.ZipException) Project(aQute.bnd.build.Project) MultiMap(aQute.lib.collections.MultiMap) Entry(java.util.Map.Entry) Version(aQute.bnd.version.Version) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) ExtList(aQute.lib.collections.ExtList) List(java.util.List) SortedList(aQute.lib.collections.SortedList) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 2 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class CapReqBuilder method addFilter.

public void addFilter(String ns, String name, String version, Attrs attrs) {
    List<String> parts = new ArrayList<String>();
    parts.add("(" + ns + "=" + name + ")");
    if (version != null && VersionRange.isOSGiVersionRange(version)) {
        VersionRange range = VersionRange.parseOSGiVersionRange(version);
        parts.add(range.toFilter());
    }
    String mandatory = attrs.get(Constants.MANDATORY_DIRECTIVE + ":");
    if (mandatory != null) {
        String[] mandatoryAttrs = mandatory.split("\\s*,\\s*");
        Arrays.sort(mandatoryAttrs);
        for (String mandatoryAttr : mandatoryAttrs) {
            String value = attrs.get(mandatoryAttr);
            if (value != null) {
                parts.add("(" + mandatoryAttr + "=" + escapeFilterValue(value) + ")");
            }
        }
    }
    StringBuilder sb = new StringBuilder();
    if (parts.size() > 0)
        sb.append("(&");
    for (String s : parts) {
        sb.append(s);
    }
    if (parts.size() > 0)
        sb.append(")");
    addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, sb.toString());
}
Also used : ArrayList(java.util.ArrayList) VersionRange(aQute.bnd.version.VersionRange)

Example 3 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class Processor method _frange.

/**
	 * Return a range expression for a filter from a version. By default this is
	 * based on consumer compatibility. You can specify a third argument (true)
	 * to get provider compatibility.
	 *
	 * <pre>
	 *  ${frange;1.2.3} ->
	 * (&(version>=1.2.3)(!(version>=2.0.0)) ${frange;1.2.3, true} ->
	 * (&(version>=1.2.3)(!(version>=1.3.0)) ${frange;[1.2.3,2.3.4)} ->
	 * (&(version>=1.2.3)(!(version>=2.3.4))
	 * </pre>
	 */
public String _frange(String[] args) {
    if (args.length < 2 || args.length > 3) {
        error("Invalid filter range, 2 or 3 args ${frange;<version>[;true|false]}");
        return null;
    }
    String v = args[1];
    boolean isProvider = args.length == 3 && isTrue(args[2]);
    VersionRange vr;
    if (Verifier.isVersion(v)) {
        Version l = new Version(v);
        Version h = isProvider ? new Version(l.getMajor(), l.getMinor() + 1, 0) : new Version(l.getMajor() + 1, 0, 0);
        vr = new VersionRange(true, l, h, false);
    } else if (Verifier.isVersionRange(v)) {
        vr = new VersionRange(v);
    } else {
        error("The _frange parameter %s is neither a version nor a version range", v);
        return null;
    }
    return vr.toFilter();
}
Also used : Version(aQute.bnd.version.Version) VersionRange(aQute.bnd.version.VersionRange)

Example 4 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class Verifier method verifyImports.

/**
	 * Verify that the imports properly use version ranges.
	 */
private void verifyImports() {
    if (isStrict()) {
        Parameters map = parseHeader(manifest.getMainAttributes().getValue(Constants.IMPORT_PACKAGE));
        Set<String> noimports = new HashSet<String>();
        Set<String> toobroadimports = new HashSet<String>();
        for (Entry<String, Attrs> e : map.entrySet()) {
            String version = e.getValue().get(Constants.VERSION_ATTRIBUTE);
            if (version == null) {
                if (!e.getKey().startsWith("javax.")) {
                    noimports.add(e.getKey());
                }
            } else {
                if (!VERSIONRANGE.matcher(version).matches()) {
                    Location location = error("Import Package %s has an invalid version range syntax %s", e.getKey(), version).location();
                    location.header = Constants.IMPORT_PACKAGE;
                    location.context = e.getKey();
                } else {
                    try {
                        VersionRange range = new VersionRange(version);
                        if (!range.isRange()) {
                            toobroadimports.add(e.getKey());
                        }
                        if (range.includeHigh() == false && range.includeLow() == false && range.getLow().equals(range.getHigh())) {
                            Location location = error("Import Package %s has an empty version range syntax %s, likely want to use [%s,%s]", e.getKey(), version, range.getLow(), range.getHigh()).location();
                            location.header = Constants.IMPORT_PACKAGE;
                            location.context = e.getKey();
                        }
                    // TODO check for exclude low, include high?
                    } catch (Exception ee) {
                        Location location = exception(ee, "Import Package %s has an invalid version range syntax %s: %s", e.getKey(), version, ee).location();
                        location.header = Constants.IMPORT_PACKAGE;
                        location.context = e.getKey();
                    }
                }
            }
        }
        if (!noimports.isEmpty()) {
            Location location = error("Import Package clauses without version range (excluding javax.*): %s", noimports).location();
            location.header = Constants.IMPORT_PACKAGE;
        }
        if (!toobroadimports.isEmpty()) {
            Location location = error("Import Package clauses which use a version instead of a version range. This imports EVERY later package and not as many expect until the next major number: %s", toobroadimports).location();
            location.header = Constants.IMPORT_PACKAGE;
        }
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) VersionRange(aQute.bnd.version.VersionRange) HashSet(java.util.HashSet)

Example 5 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class LocalIndexedRepo method actions.

public Map<String, Runnable> actions(Object... target) throws Exception {
    Map<String, Runnable> map = new HashMap<String, Runnable>();
    map.put("Refresh", new Runnable() {

        public void run() {
            regenerateAllIndexes();
        }
    });
    if (target.length == 3) {
        String bsn = (String) target[1];
        String version = (String) target[2];
        @SuppressWarnings("deprecation") aQute.bnd.filerepo.FileRepo storageRepo = new aQute.bnd.filerepo.FileRepo(storageDir);
        @SuppressWarnings("deprecation") final File f = storageRepo.get(bsn, new VersionRange(version, version), 0);
        if (f != null) {
            map.put("Delete", new Runnable() {

                public void run() {
                    deleteEntry(f);
                    regenerateAllIndexes();
                }

                private void deleteEntry(final File f) {
                    File parent = f.getParentFile();
                    IO.delete(f);
                    File[] listFiles = parent.listFiles();
                    if (listFiles.length == 1 && listFiles[0].getName().endsWith("-latest.jar"))
                        IO.delete(listFiles[0]);
                    listFiles = parent.listFiles();
                    if (listFiles.length == 0)
                        IO.delete(parent);
                }
            });
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) VersionRange(aQute.bnd.version.VersionRange) File(java.io.File)

Aggregations

VersionRange (aQute.bnd.version.VersionRange)30 Version (aQute.bnd.version.Version)11 File (java.io.File)6 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)5 Attrs (aQute.bnd.header.Attrs)4 Parameters (aQute.bnd.header.Parameters)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Version (org.osgi.framework.Version)3 Resource (org.osgi.resource.Resource)3 Project (aQute.bnd.build.Project)2 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)2 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)2 SortedList (aQute.lib.collections.SortedList)2 AndFilter (aQute.libg.filters.AndFilter)2 Filter (aQute.libg.filters.Filter)2 NotFilter (aQute.libg.filters.NotFilter)2 SimpleFilter (aQute.libg.filters.SimpleFilter)2