Search in sources :

Example 1 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class bnd method _source.

/**
	 * Merge a bundle with its source.
	 * 
	 * @throws Exception
	 */
@Description("Merge a binary jar with its sources. It is possible to specify  source path")
public void _source(sourceOptions opts) throws Exception {
    List<String> arguments = opts._arguments();
    File jarFile = getFile(arguments.remove(0));
    if (!jarFile.exists()) {
        error("File %s does not exist ", jarFile);
        return;
    }
    File sourceFile = getFile(arguments.remove(0));
    if (!sourceFile.exists()) {
        error("Source file %s does not exist ", sourceFile);
        return;
    }
    File output = jarFile;
    if (opts.output() != null)
        output = getFile(opts.output());
    File tmp = File.createTempFile("tmp", ".jar", jarFile.getParentFile());
    tmp.deleteOnExit();
    try (Jar bin = new Jar(jarFile);
        Jar src = new Jar(sourceFile)) {
        bin.setDoNotTouchManifest();
        for (String path : src.getResources().keySet()) bin.putResource("OSGI-OPT/src/" + path, src.getResource(path));
        bin.write(tmp);
    }
    IO.rename(tmp, output);
}
Also used : Jar(aQute.bnd.osgi.Jar) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 2 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class bnd method _release.

/**
	 * Release the project
	 * 
	 * @throws Exception
	 */
@Description("Release this project")
public void _release(releaseOptions options) throws Exception {
    Set<Project> projects = new LinkedHashSet<Project>();
    Workspace ws = Workspace.findWorkspace(getBase());
    if (ws == null) {
        error("Workspace option was specified but cannot find a workspace from %s", getBase());
        return;
    }
    if (options.workspace()) {
        projects.addAll(ws.getAllProjects());
    }
    Project project = getProject(options.project());
    if (project != null) {
        projects.add(project);
    }
    if (projects.isEmpty()) {
        error("Cannot find any projects");
        return;
    }
    String repo = options.repo();
    if (repo != null) {
        RepositoryPlugin repository = ws.getRepository(repo);
        if (repository == null) {
            error("No such release repo %s%nFound:%n%s", repository, Strings.join("\n", ws.getRepositories()));
        }
    }
    for (Project p : projects) {
        if (repo != null) {
            p.setProperty(Constants.RELEASEREPO, repo);
        }
        p.release(options.test());
    }
    getInfo(project);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Project(aQute.bnd.build.Project) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Workspace(aQute.bnd.build.Workspace) Description(aQute.lib.getopt.Description)

Example 3 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class bnd method _xref.

/**
	 * Cross reference every class in the jar file to the files it references
	 */
@Description("Show a cross references for all classes in a set of jars.")
public void _xref(xrefOptions options) throws IOException, Exception {
    Analyzer analyzer = new Analyzer();
    final MultiMap<TypeRef, TypeRef> table = new MultiMap<TypeRef, TypeRef>();
    final MultiMap<PackageRef, PackageRef> packages = new MultiMap<PackageRef, PackageRef>();
    Set<TypeRef> set = Create.set();
    Instructions filter = new Instructions(options.match());
    for (String arg : options._arguments()) {
        try {
            File file = new File(arg);
            try (Jar jar = new Jar(file.getName(), file)) {
                for (Map.Entry<String, Resource> entry : jar.getResources().entrySet()) {
                    String key = entry.getKey();
                    Resource r = entry.getValue();
                    if (key.endsWith(".class")) {
                        TypeRef ref = analyzer.getTypeRefFromPath(key);
                        if (filter.matches(ref.toString())) {
                            set.add(ref);
                            try (InputStream in = r.openInputStream()) {
                                Clazz clazz = new Clazz(analyzer, key, r);
                                // TODO use the proper bcp instead
                                // of using the default layout
                                Set<TypeRef> s = clazz.parseClassFile();
                                for (Iterator<TypeRef> t = s.iterator(); t.hasNext(); ) {
                                    TypeRef tr = t.next();
                                    if (tr.isJava() || tr.isPrimitive())
                                        t.remove();
                                    else
                                        packages.add(ref.getPackageRef(), tr.getPackageRef());
                                }
                                table.addAll(ref, s);
                                set.addAll(s);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    boolean to = options.to();
    boolean from = options.from();
    if (to == false && from == false)
        to = from = true;
    if (options.classes()) {
        if (to)
            printxref(table, ">");
        if (from)
            printxref(table.transpose(), "<");
    } else {
        if (to)
            printxref(packages, ">");
        if (from)
            printxref(packages.transpose(), "<");
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Instructions(aQute.bnd.osgi.Instructions) Analyzer(aQute.bnd.osgi.Analyzer) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ZipException(java.util.zip.ZipException) MultiMap(aQute.lib.collections.MultiMap) Jar(aQute.bnd.osgi.Jar) Clazz(aQute.bnd.osgi.Clazz) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) File(java.io.File) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Description(aQute.lib.getopt.Description)

Example 4 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class bnd method __merge.

@Description("Merge a number of jar files into a new jar file. The used manifest is that of the first" + "given JAR file. The order of the JAR file is the class path order. I.e. earlier resources" + "are preferred over later resources with the same name.")
public void __merge(MergeOptions options) throws Exception {
    String name = options.output() == null ? "output.jar" : options.output();
    File out = getFile(name);
    if (!out.getParentFile().isDirectory()) {
        error("Output file is not in a valid directory: %s", out.getParentFile());
    }
    List<String> list = options._arguments();
    Collections.reverse(list);
    try (Jar jar = new Jar(name)) {
        Jar last = null;
        for (String member : list) {
            File m = getFile(member);
            if (!m.isFile()) {
                error("%s is not a file", m.getAbsolutePath());
            } else {
                Jar jm = new Jar(m);
                last = jm;
                addClose(jm);
                jar.addAll(jm);
            }
        }
        if (last != null) {
            jar.setManifest(last.getManifest());
        }
        jar.write(out);
    }
}
Also used : Jar(aQute.bnd.osgi.Jar) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 5 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class bnd method _select.

@Description("Helps finding information in a set of JARs by filtering on manifest data and printing out selected information.")
public void _select(selectOptions opts) throws Exception {
    PrintStream out = this.out;
    Filter filter = null;
    if (opts.where() != null) {
        String w = opts.where();
        if (!w.startsWith("("))
            w = "(" + w + ")";
        filter = new Filter(w);
    }
    Instructions instructions = new Instructions(opts.header());
    for (String s : opts._arguments()) {
        Jar jar = getJar(s);
        if (jar == null) {
            err.println("no file " + s);
            continue;
        }
        Domain domain = Domain.domain(jar.getManifest());
        Hashtable<String, Object> ht = new Hashtable<String, Object>();
        Iterator<String> i = domain.iterator();
        Set<String> realNames = new HashSet<String>();
        while (i.hasNext()) {
            String key = i.next();
            String value = domain.get(key).trim();
            ht.put(key.trim().toLowerCase(), value);
            realNames.add(key);
        }
        ht.put("resources", jar.getResources().keySet());
        realNames.add("resources");
        if (filter != null) {
            if (!filter.match(ht))
                continue;
        }
        Set<Instruction> unused = new HashSet<Instruction>();
        Collection<String> select = instructions.select(realNames, unused, true);
        for (String h : select) {
            if (opts.path()) {
                out.print(jar.getSource().getAbsolutePath() + ":");
            }
            if (opts.name()) {
                out.print(jar.getSource().getName() + ":");
            }
            if (opts.key()) {
                out.print(h + ":");
            }
            out.println(ht.get(h.toLowerCase()));
        }
        for (Instruction ins : unused) {
            String literal = ins.getLiteral();
            if (literal.equals("name"))
                out.println(jar.getSource().getName());
            else if (literal.equals("path"))
                out.println(jar.getSource().getAbsolutePath());
            else if (literal.equals("size") || literal.equals("length"))
                out.println(jar.getSource().length());
            else if (literal.equals("modified"))
                out.println(new Date(jar.getSource().lastModified()));
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Hashtable(java.util.Hashtable) Instructions(aQute.bnd.osgi.Instructions) Instruction(aQute.bnd.osgi.Instruction) Date(java.util.Date) FilenameFilter(java.io.FilenameFilter) Filter(aQute.lib.filter.Filter) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Description(aQute.lib.getopt.Description)

Aggregations

Description (aQute.lib.getopt.Description)63 File (java.io.File)31 Jar (aQute.bnd.osgi.Jar)14 Project (aQute.bnd.build.Project)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 Instructions (aQute.bnd.osgi.Instructions)10 CommandLine (aQute.lib.getopt.CommandLine)10 Justif (aQute.lib.justif.Justif)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 Workspace (aQute.bnd.build.Workspace)7 PomFromManifest (aQute.bnd.maven.PomFromManifest)7 Service (aQute.jpm.lib.Service)7 Manifest (java.util.jar.Manifest)7 FileResource (aQute.bnd.osgi.FileResource)6 Resource (aQute.bnd.osgi.Resource)6 Analyzer (aQute.bnd.osgi.Analyzer)5 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)5 CommandData (aQute.jpm.lib.CommandData)5 ServiceData (aQute.jpm.lib.ServiceData)5