Search in sources :

Example 11 with Description

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

the class bnd method _ees.

/**
	 * Show the class versions used in a JAR
	 * 
	 * @throws Exception
	 */
@Description("Show the Execution Environments of a JAR")
public void _ees(EEOptions options) throws Exception {
    for (String path : options._arguments()) {
        File f = getFile(path);
        if (!f.isFile()) {
            error("Not a file");
        } else {
            try (Analyzer a = new Analyzer(this)) {
                a.setJar(f);
                a.analyze();
                out.printf("%s %s%n", a.getJar().getName(), a.getEEs());
            }
        }
    }
}
Also used : Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 12 with Description

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

the class bnd method _bnd.

/**
	 * Main command. This has options the bnd base options and will then run
	 * another command.
	 * 
	 * @param options
	 * @throws Exception
	 */
@Description("The swiss army tool for OSGi")
public void _bnd(bndOptions options) throws Exception {
    try {
        set(FAIL_OK, options.failok() + "");
        setExceptions(options.exceptions());
        setTrace(options.trace());
        if (options.trace()) {
            System.setProperty(DEFAULT_LOG_LEVEL_KEY, "trace");
        } else {
            System.setProperty(DEFAULT_LOG_LEVEL_KEY, "warn");
        }
        setPedantic(options.pedantic());
        if (options.base() != null)
            setBase(getFile(getBase(), options.base()));
        // And the properties
        for (Entry<String, String> entry : options._properties().entrySet()) {
            setProperty(entry.getKey(), entry.getValue());
        }
        CommandLine handler = options._command();
        List<String> arguments = options._arguments();
        // Rewrite command line to match jar commands and
        // handle commands that provide file names
        rewrite(arguments);
        logger.debug("rewritten {}", arguments);
        if (arguments.isEmpty()) {
            Justif f = new Justif(80, 20, 22, 72);
            handler.help(f.formatter(), this);
            err.append(f.wrap());
        } else {
            String cmd = arguments.remove(0);
            String help = handler.execute(this, cmd, arguments);
            if (help != null) {
                err.println(help);
            }
        }
        if (options.secret() != null) {
            password = options.secret();
            settings.load(password);
        }
    } catch (Throwable t) {
        while (t instanceof InvocationTargetException) t = t.getCause();
        exception(t, "%s", t);
    }
    out.flush();
    err.flush();
    if (ws != null)
        getInfo(ws);
    if (!check(options.ignore())) {
        System.err.flush();
        System.err.flush();
        Thread.sleep(1000);
        exitWithCode(getErrors().size());
    }
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Justif(aQute.lib.justif.Justif) InvocationTargetException(java.lang.reflect.InvocationTargetException) Description(aQute.lib.getopt.Description)

Example 13 with Description

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

the class bnd method _grep.

@Description("Grep the manifest of bundles/jar files. ")
public void _grep(grepOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String s = args.remove(0);
    Pattern pattern = Glob.toPattern(s);
    if (pattern == null) {
        messages.InvalidGlobPattern_(s);
        return;
    }
    if (args.isEmpty()) {
        args = new ExtList<String>(getBase().list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        }));
    }
    Set<String> headers = opts.headers();
    if (headers == null)
        headers = new TreeSet<String>();
    if (opts.exports())
        headers.add(Constants.EXPORT_PACKAGE);
    if (opts.bsn())
        headers.add(Constants.BUNDLE_SYMBOLICNAME);
    if (opts.imports())
        headers.add(Constants.IMPORT_PACKAGE);
    Instructions instructions = new Instructions(headers);
    for (String fileName : args) {
        File file = getFile(fileName);
        if (!file.isFile()) {
            messages.NoSuchFile_(file);
            continue;
        }
        try (JarInputStream in = new JarInputStream(IO.stream(file))) {
            Manifest m = in.getManifest();
            for (Object header : m.getMainAttributes().keySet()) {
                Attributes.Name name = (Name) header;
                if (instructions.isEmpty() || instructions.matches(name.toString())) {
                    String h = m.getMainAttributes().getValue(name);
                    QuotedTokenizer qt = new QuotedTokenizer(h, ",;=");
                    for (String value : qt.getTokenSet()) {
                        Matcher matcher = pattern.matcher(value);
                        while (matcher.find()) {
                            int start = matcher.start() - 8;
                            if (start < 0)
                                start = 0;
                            int end = matcher.end() + 8;
                            if (end > value.length())
                                end = value.length();
                            out.printf("%40s : %20s ...%s[%s]%s...\n", fileName, name, value.substring(start, matcher.start()), value.substring(matcher.start(), matcher.end()), value.substring(matcher.end(), end));
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) JarInputStream(java.util.jar.JarInputStream) Matcher(java.util.regex.Matcher) Attributes(java.util.jar.Attributes) Instructions(aQute.bnd.osgi.Instructions) Name(java.util.jar.Attributes.Name) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Name(java.util.jar.Attributes.Name) FilenameFilter(java.io.FilenameFilter) TreeSet(java.util.TreeSet) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 14 with Description

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

the class bnd method _view.

/**
	 * View files from JARs We parse the commandline and print each file on it.
	 * 
	 * @throws Exception
	 */
@Description("View a resource from a JAR file.")
public void _view(viewOptions options) throws Exception {
    Charset charset = UTF_8;
    if (options.charset() != null)
        charset = Charset.forName(options.charset());
    if (options._arguments().isEmpty()) {
        error("Need a jarfile as source");
        return;
    }
    List<String> args = options._arguments();
    File file = getFile(args.remove(0));
    if (!file.isFile()) {
        error("File does not exist %s", file);
        return;
    }
    try (Jar jar = new Jar(file)) {
        if (args.isEmpty())
            args.add("*");
        Instructions instructions = new Instructions(args);
        Collection<String> selected = instructions.select(jar.getResources().keySet(), true);
        for (String selection : selected) {
            Resource r = jar.getResource(selection);
            if (selection.endsWith(".MF")) {
                Manifest m = new Manifest(r.openInputStream());
                printManifest(m);
            } else if (selection.endsWith(".class")) {
                ClassDumper clsd = new ClassDumper(selection, r.openInputStream());
                clsd.dump(out);
            } else {
                InputStreamReader isr = new InputStreamReader(r.openInputStream(), charset);
                IO.copy(isr, out);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Charset(java.nio.charset.Charset) Jar(aQute.bnd.osgi.Jar) Instructions(aQute.bnd.osgi.Instructions) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) ClassDumper(aQute.libg.classdump.ClassDumper) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 15 with Description

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

the class bnd method _info.

@Description("Show key project variables")
public void _info(infoOptions options) throws Exception {
    Project p = getProject(options.project());
    if (p == null) {
        messages.NoProject();
        return;
    }
    boolean any = options.runbundles() || options.buildpath() || options.classpath() || options.dependsOn() || options.vmpath();
    MultiMap<String, Object> table = new MultiMap<String, Object>();
    if (any || options.runbundles()) {
        table.addAll("Run", p.getRunbundles());
    }
    if (any || options.buildpath()) {
        table.addAll("Build", p.getBuildpath());
    }
    if (any || options.buildpath()) {
        table.addAll("Depends on", p.getDependson());
    }
    if (any || options.sourcepath()) {
        table.addAll("Source", p.getSourcePath());
    }
    if (any || options.classpath()) {
        table.addAll("Class path", p.getClasspath());
    }
    if (any || options.vmpath()) {
        table.addAll("Run path", p.getRunpath());
    }
    printMultiMap(table);
}
Also used : Project(aQute.bnd.build.Project) MultiMap(aQute.lib.collections.MultiMap) 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