Search in sources :

Example 26 with VersionRange

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

the class RepoResourceUtils method narrowVersionsByVersionRange.

public static List<Resource> narrowVersionsByVersionRange(SortedMap<Version, Resource> versionMap, String rangeStr) {
    List<Resource> result;
    if (aQute.bnd.osgi.Constants.VERSION_ATTR_LATEST.equals(rangeStr)) {
        Version highest = versionMap.lastKey();
        result = Create.list(versionMap.get(highest));
    } else {
        VersionRange range = rangeStr != null ? new VersionRange(rangeStr) : null;
        // optimisation: skip versions definitely less than the range
        if (range != null && range.getLow() != null)
            versionMap = versionMap.tailMap(range.getLow());
        result = new ArrayList<Resource>(versionMap.size());
        for (Entry<Version, Resource> entry : versionMap.entrySet()) {
            Version version = entry.getKey();
            if (range == null || range.includes(version))
                result.add(entry.getValue());
            // optimisation: skip versions definitely higher than the range
            if (range != null && range.isRange() && version.compareTo(range.getHigh()) >= 0)
                break;
        }
    }
    return result;
}
Also used : Version(aQute.bnd.version.Version) Resource(org.osgi.resource.Resource) VersionRange(aQute.bnd.version.VersionRange)

Example 27 with VersionRange

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

the class PackageSearchPanel method validate.

private void validate() {
    try {
        String filter = null;
        if (packageName == null || packageName.trim().isEmpty()) {
            setError(null);
            setRequirement(null);
            return;
        }
        VersionRange versionRange = null;
        if (versionRangeStr != null && versionRangeStr.trim().length() > 0) {
            try {
                versionRange = new VersionRange(versionRangeStr);
            } catch (Exception e) {
                throw new IllegalArgumentException("Invalid version range: " + e.getMessage());
            }
        }
        filter = formatPackageRequirement(packageName, versionRange);
        if (filter != null)
            setRequirement(new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE).addDirective(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE, filter).buildSyntheticRequirement());
        setError(null);
    } catch (Exception e) {
        setError(e.getMessage());
        setRequirement(null);
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) VersionRange(aQute.bnd.version.VersionRange)

Example 28 with VersionRange

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

the class RunconfigToDistributionTask method execute.

@Override
public void execute() throws BuildException {
    try {
        createReleaseDir();
        BndEditModel model = new BndEditModel();
        model.loadFrom(bndFile);
        Project bndProject = new Project(new Workspace(rootDir), buildProject, bndFile);
        List<RepositoryPlugin> repositories = bndProject.getPlugins(RepositoryPlugin.class);
        if (allowSnapshots) {
            snapshots = indexBundleSnapshots();
        }
        for (VersionedClause runBundle : model.getRunBundles()) {
            String bsn = runBundle.getName();
            if (bsn.endsWith(".jar")) {
                bsn = bsn.substring(0, bsn.indexOf(".jar"));
            }
            if (allowSnapshots && snapshots.containsKey(bsn)) {
                Jar jar = snapshots.get(bsn);
                jar.write(new File(outputDir, jar.getName() + "-" + jar.getVersion() + ".jar"));
            } else {
                Version version = null;
                File foundJar = null;
                for (RepositoryPlugin repo : repositories) {
                    SortedSet<Version> versions = repo.versions(bsn);
                    for (Version availableVersion : versions) {
                        VersionRange range = null;
                        if (runBundle.getVersionRange() != null && !runBundle.getVersionRange().equals(Constants.VERSION_ATTR_LATEST)) {
                            range = new VersionRange(runBundle.getVersionRange());
                        }
                        boolean rangeMatches = range == null || range.includes(availableVersion);
                        boolean availableMatches = version == null || availableVersion.compareTo(version) > 0;
                        if (rangeMatches && availableMatches) {
                            version = availableVersion;
                            foundJar = repo.get(bsn, version, null);
                        }
                    }
                }
                if (foundJar != null) {
                    File outputFile = new File(outputDir, foundJar.getName());
                    Files.copy(foundJar.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                } else {
                    log(bsn + " could not be found in any repository");
                }
            }
        }
        bndProject.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new BuildException(e);
    }
}
Also used : VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) VersionRange(aQute.bnd.version.VersionRange) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Project(aQute.bnd.build.Project) Version(aQute.bnd.version.Version) Jar(aQute.bnd.osgi.Jar) BuildException(org.apache.tools.ant.BuildException) BndEditModel(aQute.bnd.build.model.BndEditModel) File(java.io.File) Workspace(aQute.bnd.build.Workspace)

Example 29 with VersionRange

use of aQute.bnd.version.VersionRange 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 30 with VersionRange

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

the class WorkspaceRepository method matchVersion.

private boolean matchVersion(String range, Version version, boolean exact) {
    if (range == null || range.trim().length() == 0)
        return true;
    VersionRange vr = new VersionRange(range);
    boolean result;
    if (exact) {
        if (vr.isRange())
            result = false;
        else
            result = vr.getHigh().equals(version);
    } else {
        result = vr.includes(version);
    }
    return result;
}
Also used : VersionRange(aQute.bnd.version.VersionRange)

Aggregations

VersionRange (aQute.bnd.version.VersionRange)31 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 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 TreeMap (java.util.TreeMap)2