Search in sources :

Example 1 with Instructions

use of aQute.bnd.osgi.Instructions in project bnd by bndtools.

the class Profiles method _create.

public void _create(CreateOptions options) throws Exception {
    Builder b = new Builder();
    bnd.addClose(b);
    b.setBase(bnd.getBase());
    if (options.properties() != null) {
        for (String propertyFile : options.properties()) {
            File pf = bnd.getFile(propertyFile);
            b.addProperties(pf);
        }
    }
    if (options.bsn() != null)
        b.setProperty(Constants.BUNDLE_SYMBOLICNAME, options.bsn());
    if (options.version() != null)
        b.setProperty(Constants.BUNDLE_VERSION, options.version().toString());
    Instructions match = options.match();
    Parameters packages = new Parameters();
    Parameters capabilities = new Parameters();
    Collection<String> paths = new ArrayList<String>(new Parameters(b.getProperty("-paths"), bnd).keySet());
    if (paths.isEmpty())
        paths = options._arguments();
    logger.debug("input {}", paths);
    ResourceBuilder pb = new ResourceBuilder();
    for (String root : paths) {
        File f = bnd.getFile(root);
        if (!f.exists()) {
            error("could not find %s", f);
        } else {
            Glob g = options.extension();
            if (g == null)
                g = new Glob("*.jar");
            Collection<File> files = IO.tree(f, "*.jar");
            logger.debug("will profile {}", files);
            for (File file : files) {
                Domain domain = Domain.domain(file);
                if (domain == null) {
                    error("Not a bundle because no manifest %s", file);
                    continue;
                }
                String bsn = domain.getBundleSymbolicName().getKey();
                if (bsn == null) {
                    error("Not a bundle because no manifest %s", file);
                    continue;
                }
                if (match != null) {
                    Instruction instr = match.finder(bsn);
                    if (instr == null || instr.isNegated()) {
                        logger.debug("skipped {} because of non matching bsn {}", file, bsn);
                        continue;
                    }
                }
                Parameters eps = domain.getExportPackage();
                Parameters pcs = domain.getProvideCapability();
                logger.debug("parse {}:\ncaps: {}\npac: {}\n", file, pcs, eps);
                packages.mergeWith(eps, false);
                capabilities.mergeWith(pcs, false);
            }
        }
    }
    b.setProperty(Constants.PROVIDE_CAPABILITY, capabilities.toString());
    b.setProperty(Constants.EXPORT_PACKAGE, packages.toString());
    logger.debug("Found {} packages and {} capabilities", packages.size(), capabilities.size());
    Jar jar = b.build();
    File f = b.getOutputFile(options.output());
    logger.debug("Saving as {}", f);
    jar.write(f);
}
Also used : ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Parameters(aQute.bnd.header.Parameters) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Builder(aQute.bnd.osgi.Builder) ArrayList(java.util.ArrayList) Instructions(aQute.bnd.osgi.Instructions) Instruction(aQute.bnd.osgi.Instruction) Glob(aQute.libg.glob.Glob) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Example 2 with Instructions

use of aQute.bnd.osgi.Instructions in project bnd by bndtools.

the class DiffCommand method diff.

private void diff(diffOptions options, DiffPluginImpl di, Jar newer, Jar older) throws Exception {
    if (newer == null) {
        bnd.error("No newer file specified");
        return;
    }
    if (older == null) {
        bnd.error("No older file specified");
        return;
    }
    PrintWriter pw = null;
    try {
        File fout = options.output();
        if (fout == null)
            pw = IO.writer(bnd.out);
        else
            pw = IO.writer(fout, UTF_8);
        Instructions packageFilters = new Instructions(options.pack());
        if (options.ignore() != null)
            di.setIgnore(Processor.join(options.ignore()));
        Tree n = di.tree(newer);
        Tree o = di.tree(older);
        Diff diff = n.diff(o);
        boolean all = options.api() == false && options.resources() == false && options.manifest() == false;
        if (!options.xml()) {
            if (all || options.api())
                for (Diff packageDiff : diff.get("<api>").getChildren()) {
                    if (packageFilters.matches(packageDiff.getName()))
                        show(pw, packageDiff, 0, !options.full());
                }
            if (all || options.manifest())
                show(pw, diff.get("<manifest>"), 0, !options.full());
            if (all || options.resources())
                show(pw, diff.get("<resources>"), 0, !options.full());
        } else {
            Tag tag = new Tag("diff");
            tag.addAttribute("date", new Date());
            tag.addContent(getTagFrom("newer", newer));
            tag.addContent(getTagFrom("older", older));
            if (all || options.api())
                tag.addContent(getTagFrom(diff.get("<api>"), !options.full()));
            if (all || options.manifest())
                tag.addContent(getTagFrom(diff.get("<manifest>"), !options.full()));
            if (all || options.resources())
                tag.addContent(getTagFrom(diff.get("<resources>"), !options.full()));
            pw.print("<?xml version='1.0' encoding='UTF-8'?>\n");
            tag.print(0, pw);
        }
    } finally {
        if (older != null) {
            older.close();
        }
        if (newer != null) {
            newer.close();
        }
        if (pw != null) {
            pw.close();
        }
    }
}
Also used : Diff(aQute.bnd.service.diff.Diff) Tree(aQute.bnd.service.diff.Tree) Instructions(aQute.bnd.osgi.Instructions) Tag(aQute.lib.tag.Tag) File(java.io.File) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 3 with Instructions

use of aQute.bnd.osgi.Instructions in project bnd by bndtools.

the class DiffCommand method showTree.

/**
	 * Just show the single tree
	 * 
	 * @param bnd
	 * @param options
	 * @throws Exception
	 */
private static void showTree(bnd bnd, diffOptions options) throws Exception {
    File fout = options.output();
    PrintWriter pw;
    if (fout == null)
        pw = IO.writer(bnd.out);
    else
        pw = IO.writer(fout, UTF_8);
    Instructions packageFilters = new Instructions(options.pack());
    try (Jar newer = new Jar(bnd.getFile(options._arguments().get(0)))) {
        Differ di = new DiffPluginImpl();
        Tree n = di.tree(newer);
        boolean all = options.api() == false && options.resources() == false && options.manifest() == false;
        if (all || options.api())
            for (Tree packageDiff : n.get("<api>").getChildren()) {
                if (packageFilters.matches(packageDiff.getName()))
                    show(pw, packageDiff, 0);
            }
        if (all || options.manifest())
            show(pw, n.get("<manifest>"), 0);
        if (all || options.resources())
            show(pw, n.get("<resources>"), 0);
    } finally {
        pw.close();
    }
}
Also used : DiffPluginImpl(aQute.bnd.differ.DiffPluginImpl) Differ(aQute.bnd.service.diff.Differ) Tree(aQute.bnd.service.diff.Tree) Instructions(aQute.bnd.osgi.Instructions) Jar(aQute.bnd.osgi.Jar) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with Instructions

use of aQute.bnd.osgi.Instructions in project bnd by bndtools.

the class ProjectBuilder method getExportedRuns.

/**
	 * Return the bndrun files that need to be exported
	 * 
	 * @throws Exception
	 */
public List<Run> getExportedRuns() throws Exception {
    Instructions runspec = new Instructions(getProperty(EXPORT));
    List<Run> runs = new ArrayList<Run>();
    Map<File, Attrs> files = runspec.select(getBase());
    for (Entry<File, Attrs> e : files.entrySet()) {
        Run run = new Run(project.getWorkspace(), getBase(), e.getKey());
        for (Entry<String, String> ee : e.getValue().entrySet()) {
            run.setProperty(ee.getKey(), ee.getValue());
        }
        runs.add(run);
    }
    return runs;
}
Also used : ArrayList(java.util.ArrayList) Attrs(aQute.bnd.header.Attrs) Instructions(aQute.bnd.osgi.Instructions) File(java.io.File)

Example 5 with Instructions

use of aQute.bnd.osgi.Instructions in project bnd by bndtools.

the class ProjectBuilder method getBaselineJar.

/**
	 * This method attempts to find the baseline jar for the current project. It
	 * reads the -baseline property and treats it as instructions. These
	 * instructions are matched against the bsns of the jars (think sub
	 * builders!). If they match, the sub builder is selected.
	 * <p>
	 * The instruction can then specify the following options:
	 * 
	 * <pre>
	 *  version :
	 * baseline version from repository file : a file path
	 * </pre>
	 * 
	 * If neither is specified, the current version is used to find the highest
	 * version (without qualifier) that is below the current version. If a
	 * version is specified, we take the highest version with the same base
	 * version.
	 * <p>
	 * Since baselining is expensive and easily generates errors you must enable
	 * it. The easiest solution is to {@code -baseline: *}. This will match all
	 * sub builders and will calculate the version.
	 * 
	 * @return a Jar or null
	 */
public Jar getBaselineJar() throws Exception {
    String bl = getProperty(Constants.BASELINE);
    if (bl == null || Constants.NONE.equals(bl))
        return null;
    Instructions baselines = new Instructions(getProperty(Constants.BASELINE));
    if (baselines.isEmpty())
        // no baselining
        return null;
    RepositoryPlugin repo = getBaselineRepo();
    if (repo == null)
        // errors reported already
        return null;
    String bsn = getBsn();
    Version version = new Version(getVersion());
    SortedSet<Version> versions = removeStagedAndFilter(repo.versions(bsn), repo, bsn);
    if (versions.isEmpty()) {
        // We have a repo
        Version v = Version.parseVersion(getVersion()).getWithoutQualifier();
        if (v.compareTo(Version.ONE) > 0) {
            warning("There is no baseline for %s in the baseline repo %s. The build is for version %s, which is higher than 1.0.0 which suggests that there should be a prior version.", getBsn(), repo, v);
        }
        return null;
    }
    for (Entry<Instruction, Attrs> e : baselines.entrySet()) {
        if (e.getKey().matches(bsn)) {
            Attrs attrs = e.getValue();
            Version target;
            if (attrs.containsKey("version")) {
                // Specified version!
                String v = attrs.get("version");
                if (!Verifier.isVersion(v)) {
                    error("Not a valid version in %s %s", Constants.BASELINE, v);
                    return null;
                }
                Version base = new Version(v);
                SortedSet<Version> later = versions.tailSet(base);
                if (later.isEmpty()) {
                    error("For baselineing %s-%s, specified version %s not found", bsn, version, base);
                    return null;
                }
                // First element is equal or next to the base we desire
                target = later.first();
            // Now, we could end up with a higher version than our
            // current
            // project
            } else if (attrs.containsKey("file")) {
                // Can be useful to specify a file
                // for example when copying a bundle with a public api
                File f = getProject().getFile(attrs.get("file"));
                if (f != null && f.isFile()) {
                    Jar jar = new Jar(f);
                    addClose(jar);
                    return jar;
                }
                error("Specified file for baseline but could not find it %s", f);
                return null;
            } else {
                target = versions.last();
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) > 0) {
                error("The baseline version %s is higher than the current version %s for %s in %s", target, version, bsn, repo);
                return null;
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) == 0) {
                if (isPedantic()) {
                    warning("Baselining against jar");
                }
            }
            File file = repo.get(bsn, target, attrs);
            if (file == null || !file.isFile()) {
                error("Decided on version %s-%s but cannot get file from repo %s", bsn, version, repo);
                return null;
            }
            Jar jar = new Jar(file);
            addClose(jar);
            return jar;
        }
    }
    // Ignore, nothing matched
    return null;
}
Also used : Version(aQute.bnd.version.Version) Attrs(aQute.bnd.header.Attrs) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Instructions(aQute.bnd.osgi.Instructions) Jar(aQute.bnd.osgi.Jar) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File)

Aggregations

Instructions (aQute.bnd.osgi.Instructions)27 File (java.io.File)15 Jar (aQute.bnd.osgi.Jar)11 Description (aQute.lib.getopt.Description)10 Parameters (aQute.bnd.header.Parameters)9 Attrs (aQute.bnd.header.Attrs)6 Instruction (aQute.bnd.osgi.Instruction)6 FileResource (aQute.bnd.osgi.FileResource)5 ArrayList (java.util.ArrayList)5 Clazz (aQute.bnd.osgi.Clazz)4 Resource (aQute.bnd.osgi.Resource)4 Tree (aQute.bnd.service.diff.Tree)4 HashSet (java.util.HashSet)4 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)3 Version (aQute.bnd.version.Version)3 MultiMap (aQute.lib.collections.MultiMap)3 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Date (java.util.Date)3